-
-
Notifications
You must be signed in to change notification settings - Fork 19
Integrate official Draft 2020-12 Basic output tests and preserve annotation value shapes #978
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| #include <sourcemeta/core/test.h> | ||
|
|
||
| #include <sourcemeta/blaze/compiler.h> | ||
| #include <sourcemeta/blaze/evaluator.h> | ||
| #include <sourcemeta/blaze/output.h> | ||
|
|
||
| #include <sourcemeta/blaze/foundation.h> | ||
| #include <sourcemeta/core/json.h> | ||
|
|
||
| #include <cassert> | ||
| #include <cctype> | ||
| #include <cstdio> | ||
| #include <cstdlib> | ||
| #include <filesystem> | ||
| #include <optional> | ||
| #include <string> | ||
| #include <string_view> | ||
| #include <utility> | ||
| #include <vector> | ||
|
|
||
| namespace { | ||
|
|
||
| auto output_schema_resolver(std::string_view identifier) | ||
| -> std::optional<sourcemeta::core::JSON> { | ||
| const std::filesystem::path suite_path{OFFICIAL_OUTPUT_SUITE_PATH}; | ||
|
|
||
| if (identifier == "https://json-schema.org/draft/2020-12/output/schema" || | ||
| identifier == "/draft/2020-12/output/schema") { | ||
| return sourcemeta::core::read_json(suite_path / "draft2020-12" / | ||
| "output-schema.json"); | ||
| } | ||
|
|
||
| if (identifier == "https://json-schema.org/draft/2019-09/output/schema" || | ||
| identifier == "/draft/2019-09/output/schema") { | ||
| return sourcemeta::core::read_json(suite_path / "draft2019-09" / | ||
| "output-schema.json"); | ||
| } | ||
|
|
||
| return sourcemeta::blaze::schema_resolver(identifier); | ||
| } | ||
|
|
||
| auto run_official_output_test(const sourcemeta::core::JSON &input_schema, | ||
| const sourcemeta::core::JSON &data, | ||
| const sourcemeta::core::JSON &output_basic_schema, | ||
| const sourcemeta::blaze::Mode mode, | ||
| const std::string &default_dialect) -> void { | ||
| const auto input_template{sourcemeta::blaze::compile( | ||
| input_schema, sourcemeta::blaze::schema_walker, output_schema_resolver, | ||
| sourcemeta::blaze::default_schema_compiler, mode, default_dialect)}; | ||
|
|
||
| sourcemeta::blaze::Evaluator evaluator; | ||
| const auto blaze_output{ | ||
| sourcemeta::blaze::standard(evaluator, input_template, data, | ||
| sourcemeta::blaze::StandardOutput::Basic)}; | ||
|
|
||
| const auto output_template{sourcemeta::blaze::compile( | ||
| output_basic_schema, sourcemeta::blaze::schema_walker, | ||
| output_schema_resolver, sourcemeta::blaze::default_schema_compiler, mode, | ||
| default_dialect)}; | ||
|
|
||
| const auto valid{evaluator.validate(output_template, blaze_output)}; | ||
| EXPECT_TRUE(valid); | ||
| } | ||
|
|
||
| auto slugify(const std::string &input) -> std::string { | ||
| std::string result; | ||
| result.reserve(input.size()); | ||
| for (const auto character : input) { | ||
| result.push_back(std::isalnum(static_cast<unsigned char>(character)) != 0 | ||
| ? character | ||
| : '_'); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| auto register_tests(const std::filesystem::path &path, | ||
| const std::string &suite_name, | ||
| const std::string &default_dialect) -> void { | ||
| std::fprintf(stderr, "-- Parsing: %s\n", path.string().c_str()); | ||
| const auto suite{sourcemeta::core::read_json(path)}; | ||
| assert(suite.is_array()); | ||
|
|
||
| const auto file_stem{path.stem().string()}; | ||
|
|
||
| for (const auto &case_entry : suite.as_array()) { | ||
| assert(case_entry.is_object()); | ||
| assert(case_entry.defines("description")); | ||
| assert(case_entry.defines("schema")); | ||
| assert(case_entry.defines("tests")); | ||
|
|
||
| const auto &case_description{case_entry.at("description").to_string()}; | ||
| const auto &input_schema{case_entry.at("schema")}; | ||
|
|
||
| for (const auto &test_entry : case_entry.at("tests").as_array()) { | ||
| assert(test_entry.is_object()); | ||
| assert(test_entry.defines("description")); | ||
| assert(test_entry.defines("data")); | ||
| assert(test_entry.defines("output")); | ||
|
|
||
| const auto &test_description{test_entry.at("description").to_string()}; | ||
| const auto &data{test_entry.at("data")}; | ||
| const auto &output_def{test_entry.at("output")}; | ||
|
|
||
| assert(output_def.defines("basic")); | ||
| const auto &output_basic_schema{output_def.at("basic")}; | ||
|
|
||
| const auto title{file_stem + "_" + slugify(case_description) + "_" + | ||
| slugify(test_description)}; | ||
|
|
||
| sourcemeta::core::test_register( | ||
| suite_name, title, __FILE__, __LINE__, | ||
| [input_schema, data, output_basic_schema, default_dialect]() -> void { | ||
| run_official_output_test(input_schema, data, output_basic_schema, | ||
| sourcemeta::blaze::Mode::Exhaustive, | ||
| default_dialect); | ||
| }); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| auto main(int argc, char **argv) -> int { | ||
| try { | ||
| const std::filesystem::path suite_path{OFFICIAL_OUTPUT_SUITE_PATH}; | ||
|
|
||
| const std::vector<std::string> test_files{ | ||
| "escape.json", | ||
| "general.json", | ||
| "readOnly.json", | ||
| "type.json", | ||
| }; | ||
|
|
||
| // 2020-12 | ||
| const std::filesystem::path content_2020_12_dir{suite_path / | ||
| "draft2020-12" / "content"}; | ||
| for (const auto &file : test_files) { | ||
| register_tests(content_2020_12_dir / file, | ||
| "Output_official_2020_12_suite", | ||
| "https://json-schema.org/draft/2020-12/schema"); | ||
| } | ||
|
|
||
| // 2019-09 | ||
| const std::filesystem::path content_2019_09_dir{suite_path / | ||
| "draft2019-09" / "content"}; | ||
| for (const auto &file : test_files) { | ||
| register_tests(content_2019_09_dir / file, | ||
| "Output_official_2019_09_suite", | ||
| "https://json-schema.org/draft/2019-09/schema"); | ||
| } | ||
| } catch (const std::exception &error) { | ||
| std::fprintf(stderr, "Error: %s\n", error.what()); | ||
| return EXIT_FAILURE; | ||
| } | ||
|
|
||
| return sourcemeta::core::test_run(argc, argv); | ||
| } |
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm a bit confused by these changes. Does the schema say
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. According to the JSON Schema Specification (Section 12.3 Basic format & official
Previously, Blaze was wrapping all scalar annotations into single-element arrays |
Uh oh!
There was an error while loading. Please reload this page.