Browse Source

test: add tests around normal constraints, not gates

Sam Jaffe 1 năm trước cách đây
mục cha
commit
fe7dfd495d
2 tập tin đã thay đổi với 51 bổ sung3 xóa
  1. 14 3
      Makefile
  2. 37 0
      tests/annotation_test.cxx

+ 14 - 3
Makefile

@@ -23,6 +23,7 @@ TEST_HEADERS := $(wildcard $(TEST_DIR)*.h)
 TEST_SOURCES := $(wildcard $(TEST_DIR)*.cxx)
 TEST_OBJECTS := $(patsubst %.cxx, .build/%.o, $(TEST_SOURCES))
 TEST_BINARIES := .build/bin/selfvalidate .build/bin/annotation_test
+EXECUTE_TESTS := $(patsubst %, %.done, $(TEST_BINARIES))
 
 EXCLUDED_TESTS := format* content ecmascript_regex zeroTerminatedFloats
 EXCLUDED_TESTS := $(shell printf ":*optional_%s" $(EXCLUDED_TESTS) | cut -c2-)
@@ -39,10 +40,9 @@ clean:
 test: $(TEST_BINARIES)
 
 run-test: test
-run-test:
-	.build/bin/selfvalidate --gtest_filter=-$(EXCLUDED_TESTS) $(CLEAN_ANSI)
-	.build/bin/annotation_test $(CLEAN_ANSI)
+run-test: $(EXECUTE_TESTS)
 
+# Actual Definitions (non-phony)
 .build/tests/%.o: tests/%.cxx $(HEADERS) $(TEST_HEADERS)
 	@ mkdir -p .build/tests
 	$(CXX) $(CXX_FLAGS) -c $< -o $@
@@ -50,8 +50,19 @@ run-test:
 
 .build/bin/selfvalidate: .build/tests/selfvalidate_test.o
 	@ mkdir -p .build/bin
+	@ rm -f $@.done
 	$(CXX) $< -o $@ $(LD_FLAGS) -ljsoncpp -lgmock -lcurl -lgtest
 
+.build/bin/selfvalidate.done: .build/bin/selfvalidate
+	.build/bin/selfvalidate --gtest_filter=-$(EXCLUDED_TESTS) $(CLEAN_ANSI)
+	@ touch $@
+
+
 .build/bin/annotation_test: .build/tests/annotation_test.o
 	@ mkdir -p .build/bin
+	@ rm -f .build/test/annotation_test.done
 	$(CXX) $< -o $@ $(LD_FLAGS) -ljsoncpp -lgmock -lgtest
+
+.build/bin/annotation_test.done: .build/bin/annotation_test
+	.build/bin/annotation_test $(CLEAN_ANSI)
+	@ touch $@

+ 37 - 0
tests/annotation_test.cxx

@@ -18,6 +18,7 @@
 #include <json/value.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});
@@ -72,6 +73,42 @@ TEST(Annotation, AttachesFormattingAnnotation) {
   EXPECT_THAT(result, AnnotationAt(""_jptr, "/format"_jptr, "format 'uri'"));
 }
 
+TEST(Annotation, AnnotatesErrors) {
+  auto const schema = R"({
+    "minimum": 5
+  })"_json;
+
+  auto const instance = R"(4)"_json;
+
+  jvalidate::ValidationResult result = validate(schema, instance);
+
+  EXPECT_THAT(result, AnnotationAt(""_jptr, "/minimum"_jptr, "4 < 5"));
+}
+
+TEST(Annotation, DoesNotAnnotatesValid) {
+  auto const schema = R"({
+    "minimum": 5
+  })"_json;
+
+  auto const instance = R"(6)"_json;
+
+  jvalidate::ValidationResult result = validate(schema, instance);
+
+  EXPECT_THAT(result, Not(HasAnnotationsFor(""_jptr)));
+}
+
+TEST(Annotation, NotSchemaFlipsAnnotationRule) {
+  auto const schema = R"({
+    "not": { "minimum": 5 }
+  })"_json;
+
+  auto const instance = R"(6)"_json;
+
+  jvalidate::ValidationResult result = validate(schema, instance);
+
+  EXPECT_THAT(result, AnnotationAt(""_jptr, "/not/minimum"_jptr, "6 >= 5"));
+}
+
 int main(int argc, char ** argv) {
   testing::InitGoogleMock(&argc, argv);
   return RUN_ALL_TESTS();