|
|
@@ -0,0 +1,72 @@
|
|
|
+#include "gmock/gmock-matchers.h"
|
|
|
+#include <string_view>
|
|
|
+
|
|
|
+#include <gmock/gmock.h>
|
|
|
+#include <gtest/gtest.h>
|
|
|
+
|
|
|
+#include <jvalidate/adapter.h>
|
|
|
+#include <jvalidate/adapters/jsoncpp.h>
|
|
|
+#include <jvalidate/detail/pointer.h>
|
|
|
+#include <jvalidate/enum.h>
|
|
|
+#include <jvalidate/schema.h>
|
|
|
+#include <jvalidate/status.h>
|
|
|
+#include <jvalidate/uri.h>
|
|
|
+#include <jvalidate/validation_result.h>
|
|
|
+#include <jvalidate/validator.h>
|
|
|
+
|
|
|
+#include <json/reader.h>
|
|
|
+#include <json/value.h>
|
|
|
+
|
|
|
+using enum jvalidate::schema::Version;
|
|
|
+
|
|
|
+auto operator""_jptr(char const * data, size_t len) {
|
|
|
+ return jvalidate::detail::Pointer(std::string_view{data, len});
|
|
|
+}
|
|
|
+
|
|
|
+Json::Value operator""_json(char const * data, size_t len) {
|
|
|
+ Json::Value value;
|
|
|
+
|
|
|
+ Json::CharReaderBuilder builder;
|
|
|
+ std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
|
|
|
+
|
|
|
+ std::string error;
|
|
|
+ if (not reader->parse(data, data + len, &value, &error)) {
|
|
|
+ throw std::runtime_error(error);
|
|
|
+ }
|
|
|
+
|
|
|
+ return value;
|
|
|
+}
|
|
|
+
|
|
|
+MATCHER_P(HasAnnotationsFor, doc_path, "") { return arg.has_annotation(doc_path); }
|
|
|
+
|
|
|
+MATCHER_P2(HasAnnotationAt, doc_path, schema_path, "") {
|
|
|
+ return arg.has_annotation(doc_path, schema_path);
|
|
|
+}
|
|
|
+
|
|
|
+MATCHER_P3(AnnotationAt, doc_path, schema_path, matcher, "") {
|
|
|
+ auto const * anno = arg.annotation(doc_path, schema_path);
|
|
|
+ if (not anno) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return testing::ExplainMatchResult(matcher, *anno, result_listener);
|
|
|
+}
|
|
|
+
|
|
|
+TEST(Annotation, AttachesFormattingAnnotation) {
|
|
|
+ jvalidate::Schema const schema(R"({
|
|
|
+ "format": "uri"
|
|
|
+ })"_json,
|
|
|
+ Draft2020_12);
|
|
|
+
|
|
|
+ auto const instance = R"("http://json-schema.org")"_json;
|
|
|
+
|
|
|
+ jvalidate::ValidationResult result;
|
|
|
+ bool const valid = jvalidate::Validator(schema).validate(instance, &result);
|
|
|
+
|
|
|
+ EXPECT_TRUE(valid);
|
|
|
+ EXPECT_THAT(result, AnnotationAt(""_jptr, "/format"_jptr, "format 'uri'"));
|
|
|
+}
|
|
|
+
|
|
|
+int main(int argc, char ** argv) {
|
|
|
+ testing::InitGoogleMock(&argc, argv);
|
|
|
+ return RUN_ALL_TESTS();
|
|
|
+}
|