| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- #include <cstdio>
- #include <cstdlib>
- #include <filesystem>
- #include <iostream>
- #include <gmock/gmock.h>
- #include <gtest/gtest.h>
- #include <jvalidate/adapter.h>
- #include <jvalidate/adapters/jsoncpp.h>
- #include <jvalidate/compat/curl.h>
- #include <jvalidate/enum.h>
- #include <jvalidate/schema.h>
- #include <jvalidate/status.h>
- #include <jvalidate/uri.h>
- #include <jvalidate/validator.h>
- #include <json/reader.h>
- #include <json/value.h>
- #include <json/writer.h>
- #include "./custom_filter.h"
- #include "./json_schema_test_suite.h"
- using jvalidate::schema::Version;
- using testing::TestWithParam;
- using jvalidate::adapter::load_file;
- bool load_external_for_test(jvalidate::URI const & uri, Json::Value & out,
- std::string & error) noexcept {
- constexpr std::string_view g_fake_url = "localhost:1234/";
- if (uri.scheme().starts_with("http") && uri.resource().starts_with(g_fake_url)) {
- std::string_view path = uri.resource().substr(g_fake_url.size());
- return load_file(JSONSchemaTestSuiteDir() / "remotes" / path, out, error);
- }
- return jvalidate::curl_get(uri, out, error);
- }
- class JsonSchema : public TestWithParam<SchemaParams> {
- private:
- static RecursiveTestFilter s_suite_filter;
- static RecursiveTestFilter s_case_filter;
- protected:
- bool skip_suite(std::string const & desc) const { return not s_suite_filter.accepts(desc); }
- bool skip_case(std::string const & desc) const { return not s_case_filter.accepts(desc); }
- static void SetUpTestCase() {
- auto tokenize = [](auto & into, std::string const & in) {
- std::vector<std::string> tokens;
- testing::internal::SplitString(in, ':', &tokens);
- for (size_t i = 1; i < tokens.size();) {
- if (tokens[i].front() != ' ') {
- ++i;
- continue;
- }
- tokens[i - 1] += ":";
- tokens[i - 1] += tokens[i];
- tokens.erase(tokens.begin() + i);
- }
- for (auto & tok : tokens) {
- into.emplace_back(tok);
- }
- };
- for (std::string_view str : testing::internal::GetArgvs()) {
- RecursiveTestFilter * ptr;
- if (str.starts_with("--json_suite_filter=")) {
- str.remove_prefix(20);
- ptr = &s_suite_filter;
- } else if (str.starts_with("--json_case_filter=")) {
- str.remove_prefix(19);
- ptr = &s_case_filter;
- } else {
- continue;
- }
- size_t const pos_end = str[0] == '-' ? 0 : str.find(":-");
- size_t const neg_start =
- pos_end == 0 ? 1 : (pos_end == std::string::npos ? pos_end : pos_end + 2);
- if (pos_end > 0 && not str.empty()) {
- tokenize(ptr->whitelist, std::string(str.substr(0, pos_end)));
- }
- if (neg_start != std::string::npos) {
- tokenize(ptr->blacklist, std::string(str.substr(neg_start)));
- }
- }
- }
- };
- RecursiveTestFilter JsonSchema::s_suite_filter;
- RecursiveTestFilter JsonSchema::s_case_filter;
- TEST_P(JsonSchema, TestSuite) {
- auto const & [version, file] = GetParam();
- Json::Value spec;
- std::string error;
- EXPECT_TRUE(load_file(file, spec, error)) << error;
- for (auto const & suite : spec) {
- if (skip_suite(suite["description"].asString())) {
- continue;
- }
- std::cout << "\033[0;32m[ SUITE ] \033[0;0m" << suite["description"].asString() << std::endl;
- try {
- jvalidate::Schema schema(suite["schema"], version, &load_external_for_test);
- for (auto const & test : suite["tests"]) {
- if (skip_case(test["description"].asString())) {
- continue;
- }
- try {
- std::cout << "\033[0;32m[ CASE ] \033[0;0m " << test["description"].asString()
- << std::endl;
- EXPECT_THAT(test["data"], ValidatesAgainst(schema, test)) << suite["schema"];
- } catch (std::exception const & ex) { ADD_FAILURE() << ex.what() << "\n" << test; }
- }
- } catch (std::exception const & ex) {
- ADD_FAILURE() << "when parsing schema: " << ex.what() << "\n" << suite["schema"];
- }
- }
- }
- INSTANTIATE_TEST_SUITE_P(Draft3, JsonSchema, SchemaTests(Version::Draft03), SchemaTestName);
- INSTANTIATE_TEST_SUITE_P(Draft4, JsonSchema, SchemaTests(Version::Draft04), SchemaTestName);
- INSTANTIATE_TEST_SUITE_P(Draft6, JsonSchema, SchemaTests(Version::Draft06), SchemaTestName);
- INSTANTIATE_TEST_SUITE_P(Draft7, JsonSchema, SchemaTests(Version::Draft07), SchemaTestName);
- INSTANTIATE_TEST_SUITE_P(Draft2019_09, JsonSchema, SchemaTests(Version::Draft2019_09),
- SchemaTestName);
- INSTANTIATE_TEST_SUITE_P(Draft2020_12, JsonSchema, SchemaTests(Version::Draft2020_12),
- SchemaTestName);
- int main(int argc, char ** argv) {
- using std::string_literals::operator""s;
- if (argc != 4) {
- // Skip down to normal GTest things...
- } else if ("--suite"s == argv[1] || "--case"s == argv[1]) {
- std::string arg1 = "--gtest_filter="s + argv[2];
- std::string arg2 = "--json_"s + (argv[1] + 2) + "_filter="s + argv[3];
- std::array<char *, 3> args{argv[0], &*arg1.begin(), &*arg2.begin()};
- return main(args.size(), args.data());
- }
- testing::InitGoogleMock(&argc, argv);
- return RUN_ALL_TESTS();
- }
|