#include #include #include #include #include using SchemaParams = std::tuple; inline std::filesystem::path const & JSONSchemaTestSuiteDir() { static auto const g_root = std::filesystem::path(__FILE__).parent_path().parent_path().parent_path(); static auto const g_path = g_root / "thirdparty" / "JSON-Schema-Test-Suite"; return g_path; } inline std::string to_string(jvalidate::schema::Version version) { std::stringstream ss; ss << version; return ss.str(); } inline auto SchemaTests(jvalidate::schema::Version version) { struct fs_iterator : std::filesystem::recursive_directory_iterator { using super = std::filesystem::recursive_directory_iterator; using value_type = SchemaParams; using reference = SchemaParams; fs_iterator() = default; explicit fs_iterator(jvalidate::schema::Version version) : super(JSONSchemaTestSuiteDir() / "tests" / to_string(version)), version(version) {} SchemaParams operator*() const { return std::make_tuple(version, super::operator*().path()); } fs_iterator operator++() { do { super::operator++(); } while (*this != fs_iterator() && super::operator*().is_directory()); return *this; } jvalidate::schema::Version version; }; return testing::ValuesIn(fs_iterator(version), fs_iterator()); } static auto SchemaTestName = [](auto const & info) { auto [version, file] = info.param; auto base = JSONSchemaTestSuiteDir() / "tests" / to_string(version); std::string name = std::filesystem::relative(file, base); name = name.substr(0, name.rfind('.')); std::transform(name.begin(), name.end(), name.begin(), [](char c) { return std::isalnum(c) ? c : '_'; }); return name; }; MATCHER_P(ValidatesAgainst, schema, "") { jvalidate::ValidationResult result; bool valid = jvalidate::Validator(schema).validate(arg, &result); *result_listener << result; return valid; } template testing::Matcher ValidatesAgainst(jvalidate::Schema const & schema, T const & test) { if (not jvalidate::adapter::AdapterFor(test)["valid"].as_boolean()) { return testing::Not(ValidatesAgainst(std::cref(schema))); } return ValidatesAgainst(std::cref(schema)); }