annotation_test.cxx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #include <string_view>
  2. #include <gmock/gmock.h>
  3. #include <gtest/gtest.h>
  4. #include <jvalidate/adapter.h>
  5. #include <jvalidate/adapters/jsoncpp.h>
  6. #include <jvalidate/detail/pointer.h>
  7. #include <jvalidate/enum.h>
  8. #include <jvalidate/schema.h>
  9. #include <jvalidate/status.h>
  10. #include <jvalidate/uri.h>
  11. #include <jvalidate/validation_result.h>
  12. #include <jvalidate/validator.h>
  13. #include <json/reader.h>
  14. #include <json/value.h>
  15. using enum jvalidate::schema::Version;
  16. using testing::Not;
  17. auto operator""_jptr(char const * data, size_t len) {
  18. return jvalidate::detail::Pointer(std::string_view{data, len});
  19. }
  20. Json::Value operator""_json(char const * data, size_t len) {
  21. Json::Value value;
  22. Json::CharReaderBuilder builder;
  23. std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
  24. std::string error;
  25. if (not reader->parse(data, data + len, &value, &error)) {
  26. throw std::runtime_error(error);
  27. }
  28. return value;
  29. }
  30. auto validate(Json::Value const & schema_doc, Json::Value const & instance_doc,
  31. jvalidate::schema::Version version = Draft2020_12) {
  32. jvalidate::Schema const schema(schema_doc, version);
  33. jvalidate::ValidationResult result;
  34. (void)jvalidate::Validator(schema).validate(instance_doc, &result);
  35. return result;
  36. }
  37. MATCHER_P(HasAnnotationsFor, doc_path, "") { return arg.has_annotation(doc_path); }
  38. MATCHER_P2(HasAnnotationAt, doc_path, schema_path, "") {
  39. return arg.has_annotation(doc_path, schema_path);
  40. }
  41. MATCHER_P3(AnnotationAt, doc_path, schema_path, matcher, "") {
  42. auto const * anno = arg.annotation(doc_path, schema_path);
  43. if (not anno) {
  44. return false;
  45. }
  46. return testing::ExplainMatchResult(matcher, *anno, result_listener);
  47. }
  48. TEST(Annotation, AttachesFormattingAnnotation) {
  49. auto const schema = R"({
  50. "format": "uri"
  51. })"_json;
  52. auto const instance = R"("http://json-schema.org")"_json;
  53. jvalidate::ValidationResult result = validate(schema, instance);
  54. EXPECT_THAT(result, AnnotationAt(""_jptr, "/format"_jptr, "format 'uri'"));
  55. }
  56. TEST(Annotation, AnnotatesErrors) {
  57. auto const schema = R"({
  58. "minimum": 5
  59. })"_json;
  60. auto const instance = R"(4)"_json;
  61. jvalidate::ValidationResult result = validate(schema, instance);
  62. EXPECT_THAT(result, AnnotationAt(""_jptr, "/minimum"_jptr, "4 < 5"));
  63. }
  64. TEST(Annotation, DoesNotAnnotatesValid) {
  65. auto const schema = R"({
  66. "minimum": 5
  67. })"_json;
  68. auto const instance = R"(6)"_json;
  69. jvalidate::ValidationResult result = validate(schema, instance);
  70. EXPECT_THAT(result, Not(HasAnnotationsFor(""_jptr)));
  71. }
  72. TEST(Annotation, NotSchemaFlipsAnnotationRule) {
  73. auto const schema = R"({
  74. "not": { "minimum": 5 }
  75. })"_json;
  76. auto const instance = R"(6)"_json;
  77. jvalidate::ValidationResult result = validate(schema, instance);
  78. EXPECT_THAT(result, AnnotationAt(""_jptr, "/not/minimum"_jptr, "6 >= 5"));
  79. }
  80. TEST(Annotation, PathFollowsSchemaNotConstraintModel) {
  81. auto const schema = R"({
  82. "$comment": "disallow is implemented in the form of NotConstraint[TypeConstraint]",
  83. "disallow": "string"
  84. })"_json;
  85. auto const instance = R"("hello")"_json;
  86. jvalidate::ValidationResult result = validate(schema, instance, Draft03);
  87. EXPECT_THAT(result, AnnotationAt(""_jptr, "/disallow"_jptr, "type (string) is one of [string]"));
  88. }
  89. TEST(Annotation, SomeConstraintsAnnotateBothValidAndInvalid) {
  90. auto const schema = R"({
  91. "$comment": "accepts any number <= 0 or >= 10",
  92. "oneOf": [
  93. { "minimum": 10 },
  94. { "maximum": 0 }
  95. ]
  96. })"_json;
  97. auto const instance = R"(-1)"_json;
  98. jvalidate::ValidationResult result = validate(schema, instance);
  99. EXPECT_THAT(result, Not(HasAnnotationAt(""_jptr, "/oneOf"_jptr)));
  100. EXPECT_THAT(result, AnnotationAt(""_jptr, "/oneOf/0/minimum"_jptr, "-1 < 10"));
  101. EXPECT_THAT(result, AnnotationAt(""_jptr, "/oneOf/1/maximum"_jptr, "-1 <= 0"));
  102. }
  103. int main(int argc, char ** argv) {
  104. testing::InitGoogleMock(&argc, argv);
  105. return RUN_ALL_TESTS();
  106. }