selfvalidate_test.cxx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #include <array>
  2. #include <cstddef>
  3. #include <cstdio>
  4. #include <cstdlib>
  5. #include <exception>
  6. #include <filesystem>
  7. #include <iostream>
  8. #include <string>
  9. #include <string_view>
  10. #include <vector>
  11. #include <gmock/gmock.h>
  12. #include <gtest/gtest.h>
  13. #include <json/value.h>
  14. #include <json/writer.h> // IWYU pragma: keep
  15. #include <jvalidate/adapter.h>
  16. #include <jvalidate/adapters/jsoncpp.h> // IWYU pragma: keep
  17. #include <jvalidate/compat/curl.h>
  18. #include <jvalidate/enum.h>
  19. #include <jvalidate/schema.h>
  20. #include <jvalidate/uri.h>
  21. #include "./custom_filter.h"
  22. #include "./json_schema_test_suite.h"
  23. using jvalidate::schema::Version;
  24. using testing::TestWithParam;
  25. using jvalidate::adapter::load_file;
  26. bool load_external_for_test(jvalidate::URI const & uri, Json::Value & out,
  27. std::string & error) noexcept {
  28. constexpr std::string_view g_fake_url = "localhost:1234/";
  29. if (uri.scheme().starts_with("http") && uri.resource().starts_with(g_fake_url)) {
  30. std::string_view path = uri.resource().substr(g_fake_url.size());
  31. return load_file(JSONSchemaTestSuiteDir() / "remotes" / path, out, error);
  32. }
  33. return jvalidate::curl_get(uri, out, error);
  34. }
  35. class JsonSchemaTest : public TestWithParam<SchemaParams> {
  36. private:
  37. static RecursiveTestFilter s_suite_filter;
  38. static RecursiveTestFilter s_case_filter;
  39. protected:
  40. bool skip_suite(std::string const & desc) const { return not s_suite_filter.accepts(desc); }
  41. bool skip_case(std::string const & desc) const { return not s_case_filter.accepts(desc); }
  42. static void SetUpTestSuite() {
  43. auto tokenize = [](auto & into, std::string const & in) {
  44. std::vector<std::string> tokens;
  45. testing::internal::SplitString(in, ':', &tokens);
  46. for (size_t i = 1; i < tokens.size();) {
  47. if (tokens[i].front() != ' ') {
  48. ++i;
  49. continue;
  50. }
  51. tokens[i - 1] += ":";
  52. tokens[i - 1] += tokens[i];
  53. tokens.erase(tokens.begin() + static_cast<ptrdiff_t>(i));
  54. }
  55. for (auto & tok : tokens) {
  56. into.emplace_back(tok);
  57. }
  58. };
  59. for (std::string_view str : testing::internal::GetArgvs()) {
  60. RecursiveTestFilter * ptr = nullptr;
  61. if (str.starts_with("--json_suite_filter=")) {
  62. str.remove_prefix(20);
  63. ptr = &s_suite_filter;
  64. } else if (str.starts_with("--json_case_filter=")) {
  65. str.remove_prefix(19);
  66. ptr = &s_case_filter;
  67. } else {
  68. continue;
  69. }
  70. size_t const pos_end = str[0] == '-' ? 0 : str.find(":-");
  71. size_t const neg_start =
  72. // NOLINTNEXTLINE(readability-avoid-nested-conditional-operator)
  73. pos_end == 0 ? 1 : (pos_end == std::string::npos ? pos_end : pos_end + 2);
  74. if (pos_end > 0 && not str.empty()) {
  75. tokenize(ptr->whitelist, std::string(str.substr(0, pos_end)));
  76. }
  77. if (neg_start != std::string::npos) {
  78. tokenize(ptr->blacklist, std::string(str.substr(neg_start)));
  79. }
  80. }
  81. }
  82. };
  83. RecursiveTestFilter JsonSchemaTest::s_suite_filter;
  84. RecursiveTestFilter JsonSchemaTest::s_case_filter;
  85. TEST_P(JsonSchemaTest, TestSuite) {
  86. auto const & [version, file] = GetParam();
  87. Json::Value spec;
  88. std::string error;
  89. EXPECT_TRUE(load_file(file, spec, error)) << error;
  90. bool is_format = file.string().find("optional/format") != std::string::npos;
  91. for (auto const & suite : spec) {
  92. if (skip_suite(suite["description"].asString())) {
  93. continue;
  94. }
  95. std::cout << "\033[0;32m[ SUITE ] \033[0;0m" << suite["description"].asString() << "\n";
  96. try {
  97. jvalidate::Schema schema(suite["schema"], version, &load_external_for_test);
  98. for (auto const & test : suite["tests"]) {
  99. if (skip_case(test["description"].asString())) {
  100. continue;
  101. }
  102. try {
  103. std::cout << "\033[0;32m[ CASE ] \033[0;0m " << test["description"].asString()
  104. << "\n";
  105. EXPECT_THAT(test["data"], ValidatesAgainst(schema, test, is_format)) << suite["schema"];
  106. } catch (std::exception const & ex) { ADD_FAILURE() << ex.what() << "\n" << test; }
  107. }
  108. } catch (std::exception const & ex) {
  109. ADD_FAILURE() << "when parsing schema: " << ex.what() << "\n" << suite["schema"];
  110. }
  111. }
  112. }
  113. INSTANTIATE_TEST_SUITE_P(Draft3, JsonSchemaTest, SchemaTests(Version::Draft03), SchemaTestName);
  114. INSTANTIATE_TEST_SUITE_P(Draft4, JsonSchemaTest, SchemaTests(Version::Draft04), SchemaTestName);
  115. INSTANTIATE_TEST_SUITE_P(Draft6, JsonSchemaTest, SchemaTests(Version::Draft06), SchemaTestName);
  116. INSTANTIATE_TEST_SUITE_P(Draft7, JsonSchemaTest, SchemaTests(Version::Draft07), SchemaTestName);
  117. INSTANTIATE_TEST_SUITE_P(Draft2019_09, JsonSchemaTest, SchemaTests(Version::Draft2019_09),
  118. SchemaTestName);
  119. INSTANTIATE_TEST_SUITE_P(Draft2020_12, JsonSchemaTest, SchemaTests(Version::Draft2020_12),
  120. SchemaTestName);
  121. // NOLINTBEGIN(cppcoreguidelines-pro-bounds-pointer-arithmetic)
  122. int main(int argc, char ** argv) {
  123. using std::string_literals::operator""s;
  124. if (argc != 4) {
  125. // Skip down to normal GTest things...
  126. } else if ("--suite"s == argv[1] || "--case"s == argv[1]) {
  127. std::string arg1 = "--gtest_filter="s + argv[2];
  128. std::string arg2 = "--json_"s + (argv[1] + 2) + "_filter="s + argv[3];
  129. std::array<char *, 3> args{argv[0], &*arg1.begin(), &*arg2.begin()};
  130. return main(args.size(), args.data());
  131. }
  132. testing::InitGoogleMock(&argc, argv);
  133. return RUN_ALL_TESTS();
  134. }
  135. // NOLINTEND(cppcoreguidelines-pro-bounds-pointer-arithmetic)