소스 검색

test: demonstrate annotation propagation using if-then-else

Sam Jaffe 2 주 전
부모
커밋
12514fdd24
1개의 변경된 파일68개의 추가작업 그리고 0개의 파일을 삭제
  1. 68 0
      tests/annotation_test.cxx

+ 68 - 0
tests/annotation_test.cxx

@@ -136,6 +136,74 @@ TEST(Annotation, AttachesAlwaysFalseSensibly) {
   EXPECT_THAT(result, ErrorAt("/A"_jptr, "/properties/A"_jptr, "", "always false"));
 }
 
+TEST(Annotation, IfThenCanGiveABecauseReason) {
+  auto const schema = R"({
+    "if": {
+      "properties": {
+        "A": { "const": true }
+      },
+      "required": [ "A" ]
+    },
+    "then": {
+      "properties": {
+        "B": { "multipleOf": 3 }
+      }
+    },
+    "else": {
+      "properties": {
+        "B": { "multipleOf": 5 }
+      }
+    }
+  })"_json;
+
+  auto const instance = R"({
+    "A": true,
+    "B": 4
+  })"_json;
+
+  jvalidate::ValidationResult result = validate(schema, instance);
+
+  EXPECT_THAT(result,
+              ErrorAt(""_jptr, "/if"_jptr, "required", "contains all required properties A"));
+  EXPECT_THAT(result, ErrorAt("/A"_jptr, "/if/properties/A"_jptr, "const", "matches value"));
+  EXPECT_THAT(result, ErrorAt("/B"_jptr, "/then/properties/B"_jptr, "multipleOf",
+                              "4 is not a multiple of 3"));
+}
+
+TEST(Annotation, IfElseCanGiveABecauseReason) {
+  auto const schema = R"({
+    "if": {
+      "properties": {
+        "A": { "const": true }
+      },
+      "required": [ "A" ]
+    },
+    "then": {
+      "properties": {
+        "B": { "multipleOf": 3 }
+      }
+    },
+    "else": {
+      "properties": {
+        "B": { "multipleOf": 5 }
+      }
+    }
+  })"_json;
+
+  auto const instance = R"({
+    "A": false,
+    "B": 4
+  })"_json;
+
+  jvalidate::ValidationResult result = validate(schema, instance);
+
+  EXPECT_THAT(result,
+              ErrorAt(""_jptr, "/if"_jptr, "required", "contains all required properties A"));
+  EXPECT_THAT(result, ErrorAt("/A"_jptr, "/if/properties/A"_jptr, "const", "true was expected"));
+  EXPECT_THAT(result, ErrorAt("/B"_jptr, "/else/properties/B"_jptr, "multipleOf",
+                              "4 is not a multiple of 5"));
+}
+
 int main(int argc, char ** argv) {
   testing::InitGoogleMock(&argc, argv);
   return RUN_ALL_TESTS();