| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- #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>;
- #if !defined(JVALIDATE_JSON_SCHEMA_TEST_SUITE_DIR)
- 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;
- }
- #else
- inline std::filesystem::path const & JSONSchemaTestSuiteDir() {
- static std::filesystem::path const g_path = JVALIDATE_JSON_SCHEMA_TEST_SUITE_DIR;
- return g_path;
- }
- #endif
- 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 FsIterator : std::filesystem::recursive_directory_iterator {
- using super = std::filesystem::recursive_directory_iterator;
- using value_type = SchemaParams;
- using reference = SchemaParams;
- FsIterator() = default;
- explicit FsIterator(jvalidate::schema::Version version)
- : super(JSONSchemaTestSuiteDir() / "tests" / to_string(version)), version(version) {}
- SchemaParams operator*() const { return std::make_tuple(version, super::operator*().path()); }
- FsIterator operator++() {
- super::operator++();
- while (*this != FsIterator() && super::operator*().is_directory()) {
- super::operator++();
- }
- return *this;
- }
- jvalidate::schema::Version version;
- };
- return testing::ValuesIn(FsIterator(version), FsIterator());
- }
- static auto const 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 chr) { return std::isalnum(chr) ? chr : '_'; });
- return name;
- };
- MATCHER_P2(ValidatesAgainst, schema, format, "") {
- jvalidate::ValidationResult result;
- bool valid = jvalidate::Validator(schema, {.validate_format = format}).validate(arg, &result);
- *result_listener << result;
- return valid;
- }
- template <typename T>
- testing::Matcher<T> ValidatesAgainst(jvalidate::Schema const & schema, T const & test,
- bool format) {
- if (not jvalidate::adapter::AdapterFor<T const>(test)["valid"].as_boolean()) {
- return testing::Not(ValidatesAgainst(std::cref(schema), format));
- }
- return ValidatesAgainst(std::cref(schema), format);
- }
|