Browse Source

refactor: move common testing code to helper

Sam Jaffe 3 months ago
parent
commit
729029efbb
2 changed files with 63 additions and 57 deletions
  1. 1 57
      tests/annotation_test.cxx
  2. 62 0
      tests/matchers.h

+ 1 - 57
tests/annotation_test.cxx

@@ -5,7 +5,6 @@
 
 #include <jvalidate/adapter.h>
 #include <jvalidate/adapters/jsoncpp.h>
-#include <jvalidate/detail/pointer.h>
 #include <jvalidate/enum.h>
 #include <jvalidate/schema.h>
 #include <jvalidate/status.h>
@@ -13,30 +12,11 @@
 #include <jvalidate/validation_result.h>
 #include <jvalidate/validator.h>
 
-#include <json/reader.h>
-#include <json/value.h>
+#include "matchers.h"
 
 using enum jvalidate::schema::Version;
 using testing::Not;
 
-auto operator""_jptr(char const * data, size_t len) {
-  return jvalidate::detail::Pointer(std::string_view{data, len});
-}
-
-Json::Value operator""_json(char const * data, size_t len) {
-  Json::Value value;
-
-  Json::CharReaderBuilder builder;
-  std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
-
-  std::string error;
-  if (not reader->parse(data, data + len, &value, &error)) {
-    throw std::runtime_error(error);
-  }
-
-  return value;
-}
-
 auto validate(Json::Value const & schema_doc, Json::Value const & instance_doc,
               jvalidate::schema::Version version = Draft2020_12) {
   jvalidate::Schema const schema(schema_doc, version);
@@ -47,42 +27,6 @@ auto validate(Json::Value const & schema_doc, Json::Value const & instance_doc,
   return result;
 }
 
-MATCHER_P(HasAnnotationsFor, doc_path, "") { return arg.has(doc_path); }
-
-MATCHER_P2(HasAnnotationAt, doc_path, schema_path, "") { return arg.has(doc_path, schema_path); }
-
-MATCHER_P2(AnnotationAt, key, matcher, "") {
-  auto const * anno = arg.annotation({}, {}, key);
-  if (not anno) {
-    return false;
-  }
-  return testing::ExplainMatchResult(matcher, *anno, result_listener);
-}
-
-MATCHER_P4(AnnotationAt, doc_path, schema_path, key, matcher, "") {
-  auto const * anno = arg.annotation(doc_path, schema_path, key);
-  if (not anno) {
-    return false;
-  }
-  return testing::ExplainMatchResult(matcher, *anno, result_listener);
-}
-
-MATCHER_P2(ErrorAt, key, matcher, "") {
-  auto const * anno = arg.error({}, {}, key);
-  if (not anno) {
-    return false;
-  }
-  return testing::ExplainMatchResult(matcher, *anno, result_listener);
-}
-
-MATCHER_P4(ErrorAt, doc_path, schema_path, key, matcher, "") {
-  auto const * anno = arg.error(doc_path, schema_path, key);
-  if (not anno) {
-    return false;
-  }
-  return testing::ExplainMatchResult(matcher, *anno, result_listener);
-}
-
 TEST(Annotation, AttachesFormattingAnnotation) {
   auto const schema = R"({
     "format": "uri"

+ 62 - 0
tests/matchers.h

@@ -0,0 +1,62 @@
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+#include <jvalidate/detail/pointer.h>
+#include <jvalidate/validation_result.h>
+
+#include <json/reader.h>
+#include <json/value.h>
+
+inline auto operator""_jptr(char const * data, size_t len) {
+  return jvalidate::detail::Pointer(std::string_view{data, len});
+}
+
+inline Json::Value operator""_json(char const * data, size_t len) {
+  Json::Value value;
+
+  Json::CharReaderBuilder builder;
+  std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
+
+  std::string error;
+  if (not reader->parse(data, data + len, &value, &error)) {
+    throw std::runtime_error(error);
+  }
+
+  return value;
+}
+
+MATCHER_P(HasAnnotationsFor, doc_path, "") { return arg.has(doc_path); }
+
+MATCHER_P2(HasAnnotationAt, doc_path, schema_path, "") { return arg.has(doc_path, schema_path); }
+
+MATCHER_P2(AnnotationAt, key, matcher, "") {
+  auto const * anno = arg.annotation({}, {}, key);
+  if (not anno) {
+    return false;
+  }
+  return testing::ExplainMatchResult(matcher, *anno, result_listener);
+}
+
+MATCHER_P4(AnnotationAt, doc_path, schema_path, key, matcher, "") {
+  auto const * anno = arg.annotation(doc_path, schema_path, key);
+  if (not anno) {
+    return false;
+  }
+  return testing::ExplainMatchResult(matcher, *anno, result_listener);
+}
+
+MATCHER_P2(ErrorAt, key, matcher, "") {
+  auto const * anno = arg.error({}, {}, key);
+  if (not anno) {
+    return false;
+  }
+  return testing::ExplainMatchResult(matcher, *anno, result_listener);
+}
+
+MATCHER_P4(ErrorAt, doc_path, schema_path, key, matcher, "") {
+  auto const * anno = arg.error(doc_path, schema_path, key);
+  if (not anno) {
+    return false;
+  }
+  return testing::ExplainMatchResult(matcher, *anno, result_listener);
+}