json_schema_test_suite.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #include <filesystem>
  2. #include <gmock/gmock.h>
  3. #include <jvalidate/validator.h>
  4. inline std::filesystem::path const & JSONSchemaTestSuiteDir() {
  5. static auto const g_root =
  6. std::filesystem::path(__FILE__).parent_path().parent_path().parent_path();
  7. static auto const g_path = g_root / "thirdparty" / "JSON-Schema-Test-Suite";
  8. return g_path;
  9. }
  10. inline auto SchemaTests(std::string_view draft) {
  11. struct fs_iterator : std::filesystem::recursive_directory_iterator {
  12. using super = std::filesystem::recursive_directory_iterator;
  13. using value_type = std::filesystem::path;
  14. using super::super;
  15. std::filesystem::path operator*() const { return super::operator*().path(); }
  16. fs_iterator operator++() {
  17. do {
  18. super::operator++();
  19. } while (*this != fs_iterator() && super::operator*().is_directory());
  20. return *this;
  21. }
  22. };
  23. auto const dir = JSONSchemaTestSuiteDir() / "tests" / draft;
  24. return testing::ValuesIn(fs_iterator(dir), fs_iterator());
  25. }
  26. static auto SchemaTestName = [](auto const & info) {
  27. std::string name = std::get<1>(info.param).stem();
  28. std::transform(name.begin(), name.end(), name.begin(),
  29. [](char c) { return std::isalnum(c) ? c : '_'; });
  30. return name;
  31. };
  32. MATCHER_P(ValidatesAgainst, schema, "") { return jvalidate::Validator(schema).validate(arg); }
  33. template <typename T>
  34. testing::Matcher<T> ValidatesAgainst(jvalidate::Schema const & schema, T const & test) {
  35. if (not jvalidate::adapter::AdapterFor<T const>(test)["valid"].as_boolean()) {
  36. return testing::Not(ValidatesAgainst(std::cref(schema)));
  37. }
  38. return ValidatesAgainst(std::cref(schema));
  39. }