selfvalidate_test.cxx 5.1 KB

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