selfvalidate_test.cxx 5.1 KB

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