Browse Source

refactor: better test documentation

Sam Jaffe 1 năm trước cách đây
mục cha
commit
7de9550010

+ 8 - 6
include/jvalidate/validator.h

@@ -31,19 +31,21 @@ public:
       : schema_(schema), cfg_(cfg) {}
 
   template <Adapter A>
-  requires(not MutableAdapter<A>) Status
-      validate(A const & json, ValidationResult * result = nullptr) {
+  requires(not MutableAdapter<A>) bool validate(A const & json,
+                                                ValidationResult * result = nullptr) {
     EXPECT_M(not cfg_.construct_default_values,
              "Cannot perform mutations on an immutable JSON Adapter");
-    return ValidationVisitor<A, RE>(json, schema_, cfg_, regex_cache_, result).validate();
+    return static_cast<bool>(
+        ValidationVisitor<A, RE>(json, schema_, cfg_, regex_cache_, result).validate());
   }
 
-  template <MutableAdapter A> Status validate(A const & json, ValidationResult * result = nullptr) {
-    return ValidationVisitor<A, RE>(json, schema_, cfg_, regex_cache_, result).validate();
+  template <MutableAdapter A> bool validate(A const & json, ValidationResult * result = nullptr) {
+    return static_cast<bool>(
+        ValidationVisitor<A, RE>(json, schema_, cfg_, regex_cache_, result).validate());
   }
 
   template <typename JSON>
-  requires(not Adapter<JSON>) Status validate(JSON & json, ValidationResult * result = nullptr) {
+  requires(not Adapter<JSON>) bool validate(JSON & json, ValidationResult * result = nullptr) {
     return validate(adapter::AdapterFor<JSON>(json), result);
   }
 };

+ 12 - 0
tests/json_schema_test_suite.h

@@ -2,6 +2,8 @@
 
 #include <gmock/gmock.h>
 
+#include <jvalidate/validator.h>
+
 inline std::filesystem::path const & JSONSchemaTestSuiteDir() {
   static auto const g_root =
       std::filesystem::path(__FILE__).parent_path().parent_path().parent_path();
@@ -36,3 +38,13 @@ static auto SchemaTestName = [](auto const & info) {
                  [](char c) { return std::isalnum(c) ? c : '_'; });
   return name;
 };
+
+MATCHER_P(ValidatesAgainst, schema, "") { return jvalidate::Validator(schema).validate(arg); }
+
+template <typename T>
+testing::Matcher<T> ValidatesAgainst(jvalidate::Schema const & schema, T const & test) {
+  if (jvalidate::adapter::AdapterFor<T const>(test)["valid"].as_boolean()) {
+    return testing::Not(ValidatesAgainst(std::cref(schema)));
+  }
+  return ValidatesAgainst(std::cref(schema));
+}

+ 1 - 2
tests/selfvalidate_test.cxx

@@ -59,8 +59,7 @@ TEST_P(JsonSchema, TestSuite) {
       try {
         std::cout << "\033[0;32m[ CASE     ] \033[0;0m    " << test["description"].asString()
                   << std::endl;
-        auto status = jvalidate::Validator(schema).validate(test["data"]);
-        EXPECT_THAT(status != jvalidate::Status::Reject, test["valid"].asBool()) << test["data"];
+        EXPECT_THAT(test["data"], ValidatesAgainst(schema, test));
       } catch (std::exception const & ex) { FAIL() << ex.what() << "\n" << test; }
     }
   }