json_schema_test_suite.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #include <filesystem>
  2. #include <gmock/gmock.h>
  3. #include <jvalidate/enum.h>
  4. #include <jvalidate/validation_result.h>
  5. #include <jvalidate/validator.h>
  6. using SchemaParams = std::tuple<jvalidate::schema::Version, std::filesystem::path>;
  7. #if !defined(JVALIDATE_JSON_SCHEMA_TEST_SUITE_DIR)
  8. inline std::filesystem::path const & JSONSchemaTestSuiteDir() {
  9. static auto const g_root =
  10. std::filesystem::path(__FILE__).parent_path().parent_path().parent_path();
  11. static auto const g_path = g_root / "thirdparty" / "JSON-Schema-Test-Suite";
  12. return g_path;
  13. }
  14. #else
  15. inline std::filesystem::path const & JSONSchemaTestSuiteDir() {
  16. static std::filesystem::path const g_path = JVALIDATE_JSON_SCHEMA_TEST_SUITE_DIR;
  17. return g_path;
  18. }
  19. #endif
  20. inline std::string to_string(jvalidate::schema::Version version) {
  21. std::stringstream ss;
  22. ss << version;
  23. return ss.str();
  24. }
  25. inline auto SchemaTests(jvalidate::schema::Version version) {
  26. struct fs_iterator : std::filesystem::recursive_directory_iterator {
  27. using super = std::filesystem::recursive_directory_iterator;
  28. using value_type = SchemaParams;
  29. using reference = SchemaParams;
  30. fs_iterator() = default;
  31. explicit fs_iterator(jvalidate::schema::Version version)
  32. : super(JSONSchemaTestSuiteDir() / "tests" / to_string(version)), version(version) {}
  33. SchemaParams operator*() const { return std::make_tuple(version, super::operator*().path()); }
  34. fs_iterator operator++() {
  35. do {
  36. super::operator++();
  37. } while (*this != fs_iterator() && super::operator*().is_directory());
  38. return *this;
  39. }
  40. jvalidate::schema::Version version;
  41. };
  42. return testing::ValuesIn(fs_iterator(version), fs_iterator());
  43. }
  44. static auto SchemaTestName = [](auto const & info) {
  45. auto [version, file] = info.param;
  46. auto base = JSONSchemaTestSuiteDir() / "tests" / to_string(version);
  47. std::string name = std::filesystem::relative(file, base);
  48. name = name.substr(0, name.rfind('.'));
  49. std::transform(name.begin(), name.end(), name.begin(),
  50. [](char c) { return std::isalnum(c) ? c : '_'; });
  51. return name;
  52. };
  53. MATCHER_P(ValidatesAgainst, schema, "") {
  54. jvalidate::ValidationResult result;
  55. bool valid = jvalidate::Validator(schema).validate(arg, &result);
  56. *result_listener << result;
  57. return valid;
  58. }
  59. template <typename T>
  60. testing::Matcher<T> ValidatesAgainst(jvalidate::Schema const & schema, T const & test) {
  61. if (not jvalidate::adapter::AdapterFor<T const>(test)["valid"].as_boolean()) {
  62. return testing::Not(ValidatesAgainst(std::cref(schema)));
  63. }
  64. return ValidatesAgainst(std::cref(schema));
  65. }