Browse Source

test: cover oneOf annotating valid and invalid

Sam Jaffe 1 năm trước cách đây
mục cha
commit
20c8efc594
2 tập tin đã thay đổi với 18 bổ sung2 xóa
  1. 1 1
      include/jvalidate/validation_visitor.h
  2. 17 1
      tests/annotation_test.cxx

+ 1 - 1
include/jvalidate/validation_visitor.h

@@ -129,10 +129,10 @@ public:
   }
 
   Status visit(constraint::OneOfConstraint const & cons) const {
-    scoped_state(tracking_, StoreResults::ForAnything);
     std::set<size_t> matches;
 
     for (auto const & [index, subschema] : detail::enumerate(cons.children)) {
+      scoped_state(tracking_, StoreResults::ForAnything);
       if (validate_subschema(subschema, index)) {
         matches.insert(index);
       }

+ 17 - 1
tests/annotation_test.cxx

@@ -1,4 +1,3 @@
-#include "gmock/gmock-matchers.h"
 #include <string_view>
 
 #include <gmock/gmock.h>
@@ -123,6 +122,23 @@ TEST(Annotation, PathFollowsSchemaNotConstraintModel) {
   EXPECT_THAT(result, AnnotationAt(""_jptr, "/disallow"_jptr, "type (string) is one of [string]"));
 }
 
+TEST(Annotation, SomeConstraintsAnnotateBothValidAndInvalid) {
+  auto const schema = R"({
+    "$comment": "accepts any number <= 0 or >= 10",
+    "oneOf": [
+      { "minimum": 10 },
+      { "maximum": 0 }
+    ]
+  })"_json;
+
+  auto const instance = R"(-1)"_json;
+  jvalidate::ValidationResult result = validate(schema, instance);
+
+  EXPECT_THAT(result, Not(HasAnnotationAt(""_jptr, "/oneOf"_jptr)));
+  EXPECT_THAT(result, AnnotationAt(""_jptr, "/oneOf/0/minimum"_jptr, "-1 < 10"));
+  EXPECT_THAT(result, AnnotationAt(""_jptr, "/oneOf/1/maximum"_jptr, "-1 <= 0"));
+}
+
 int main(int argc, char ** argv) {
   testing::InitGoogleMock(&argc, argv);
   return RUN_ALL_TESTS();