| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- #include <filesystem>
- #include <gmock/gmock.h>
- #include <jvalidate/enum.h>
- #include <jvalidate/validation_result.h>
- #include <jvalidate/validator.h>
- using SchemaParams = std::tuple<jvalidate::schema::Version, std::filesystem::path>;
- 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 <typename T>
- testing::Matcher<T> ValidatesAgainst(jvalidate::Schema const & schema, T const & test) {
- if (not jvalidate::adapter::AdapterFor<T const>(test)["valid"].as_boolean()) {
- return testing::Not(ValidatesAgainst(std::cref(schema)));
- }
- return ValidatesAgainst(std::cref(schema));
- }
|