Quellcode durchsuchen

test-refactor: improve matcher(s)

Sam Jaffe vor 2 Wochen
Ursprung
Commit
ae19793864
2 geänderte Dateien mit 40 neuen und 15 gelöschten Zeilen
  1. 18 1
      Makefile
  2. 22 14
      tests/matchers.h

+ 18 - 1
Makefile

@@ -29,21 +29,38 @@ EXECUTE_TESTS := $(patsubst %, %.done, $(TEST_BINARIES))
 EXCLUDED_TESTS := format* content ecmascript_regex zeroTerminatedFloats non_bmp_regex
 EXCLUDED_TESTS := $(shell printf ":*optional_%s" $(EXCLUDED_TESTS) | cut -c2-)
 
-all: .build/bin/validate
+.PHONY: all
+all: validate
 all: run-test
 
+.PHONY: debug
 debug: CXX_FLAGS := $(CXX_FLAGS) -g -fsanitize=address
 debug: LD_FLAGS := $(LD_FLAGS) -fsanitize=address
 debug: test
 
+.PHONY: clean
 clean:
 	@ rm -rf .build
 
+.PHONY: test
 test: $(TEST_BINARIES)
 
+.PHONY: run-test
 run-test: test
 run-test: $(EXECUTE_TESTS)
 
+.PHONY: validate
+validate: .build/bin/validate
+
+.PHONY: annotation_test
+annotation_test: .build/bin/annotation_test.done
+
+.PHONY: extension_test
+extension_test: .build/bin/extension_test.done
+
+.PHONY: selfvalidate_test
+selfvalidate_test: .build/bin/selfvalidate_test.done
+
 # Actual Definitions (non-phony)
 .build/tests/%.o: tests/%.cxx $(HEADERS) $(TEST_HEADERS)
 	@ mkdir -p .build/tests

+ 22 - 14
tests/matchers.h

@@ -1,6 +1,8 @@
+#include "gtest/internal/gtest-internal.h"
 #include <gmock/gmock.h>
 #include <gtest/gtest.h>
 
+#include <ios>
 #include <jvalidate/detail/pointer.h>
 #include <jvalidate/validation_result.h>
 
@@ -31,34 +33,40 @@ 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);
+  *result_listener << "\ninstanceLocation: " << doc_path << " is "
+                   << (arg.has(doc_path) ? "present" : "missing");
+  *result_listener << "\nevaluationLocation: " << schema_path << " is "
+                   << (arg.has(doc_path, schema_path) ? "present" : "missing");
   if (not anno) {
+    *result_listener << "\nannotation: (nil)";
     return false;
   }
+  *result_listener << "\nannotation: " << testing::PrintToString(*anno);
   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);
+template <typename M> auto AnnotationAt(std::string const & key, M && matcher) {
+  return AnnotationAt(jvalidate::detail::Pointer(), jvalidate::detail::Pointer(), key,
+                      std::forward<M>(matcher));
 }
 
 MATCHER_P4(ErrorAt, doc_path, schema_path, key, matcher, "") {
   auto const * anno = arg.error(doc_path, schema_path, key);
+  *result_listener << "\ninstanceLocation: " << doc_path << " is "
+                   << (arg.has(doc_path) ? "present" : "missing");
+  *result_listener << "\nevaluationLocation: " << schema_path << " is "
+                   << (arg.has(doc_path, schema_path) ? "present" : "missing");
   if (not anno) {
+    *result_listener << "\nannotation: (nil)";
     return false;
   }
+  *result_listener << "\nannotation: " << testing::PrintToString(*anno);
   return testing::ExplainMatchResult(matcher, *anno, result_listener);
 }
+
+template <typename M> auto ErrorAt(std::string const & key, M && matcher) {
+  return ErrorAt(jvalidate::detail::Pointer(), jvalidate::detail::Pointer(), key,
+                 std::forward<M>(matcher));
+}