| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- #include <filesystem>
- #include <gmock/gmock.h>
- #include <jvalidate/validation_result.h>
- #include <jvalidate/validator.h>
- 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 auto SchemaTests(std::string_view draft) {
- struct fs_iterator : std::filesystem::recursive_directory_iterator {
- using super = std::filesystem::recursive_directory_iterator;
- using value_type = std::filesystem::path;
- using super::super;
- std::filesystem::path operator*() const { return super::operator*().path(); }
- fs_iterator operator++() {
- do {
- super::operator++();
- } while (*this != fs_iterator() && super::operator*().is_directory());
- return *this;
- }
- };
- auto const dir = JSONSchemaTestSuiteDir() / "tests" / draft;
- return testing::ValuesIn(fs_iterator(dir), fs_iterator());
- }
- static auto SchemaTestName = [](auto const & info) {
- std::string name = std::get<1>(info.param).stem();
- 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));
- }
|