annotation_test.cxx 3.4 KB

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