-
Notifications
You must be signed in to change notification settings - Fork 732
serde/json/test: add JSONTestSuite parsing conformance suite #30211
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
Merged
nvartolomei
merged 2 commits into
redpanda-data:dev
from
nvartolomei:nv/json-compliance
Apr 21, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| /* | ||
| * Copyright 2026 Redpanda Data, Inc. | ||
| * | ||
| * Use of this software is governed by the Business Source License | ||
| * included in the file licenses/BSL.md | ||
| * | ||
| * As of the Change Date specified in that file, in accordance with | ||
| * the Business Source License, use of this software will be governed | ||
| * by the Apache License, Version 2.0 | ||
| */ | ||
|
|
||
| #include "serde/json/parser.h" | ||
| #include "serde/json/tests/test_cases.h" | ||
| #include "test_utils/runfiles.h" | ||
| #include "test_utils/test.h" | ||
| #include "utils/file_io.h" | ||
|
|
||
| #include <gtest/gtest.h> | ||
|
|
||
|
nvartolomei marked this conversation as resolved.
|
||
| #include <filesystem> | ||
| #include <string> | ||
|
|
||
| using namespace serde::json; | ||
|
|
||
| /// Test suite from https://github.com/nst/JSONTestSuite. | ||
| /// | ||
| /// File-name prefixes encode the expected parser behavior per RFC 8259: | ||
| /// - y_ must be accepted | ||
| /// - n_ must be rejected | ||
| /// - i_ parsers are free to accept or reject | ||
| class json_test_suite_test | ||
| : public seastar_test | ||
| , public ::testing::WithParamInterface<std::string> {}; | ||
|
|
||
| TEST_P_CORO(json_test_suite_test, test_parsing) { | ||
| const auto& test_case_path = GetParam(); | ||
| auto filename = std::filesystem::path(test_case_path).filename().string(); | ||
|
|
||
| auto contents = co_await read_fully(test_case_path); | ||
| auto parser = serde::json::parser(std::move(contents), parser_config{}); | ||
|
|
||
| while (co_await parser.next()) { | ||
| } | ||
|
|
||
| auto final_token = parser.token(); | ||
| ASSERT_TRUE_CORO(final_token == token::eof || final_token == token::error) | ||
| << "parser::next() returned false but final token is neither eof nor " | ||
| "error: " | ||
| << final_token; | ||
| bool accepted = final_token == token::eof; | ||
|
|
||
| if (filename.starts_with("y_")) { | ||
| EXPECT_TRUE(accepted) << filename << ": expected accept, got reject"; | ||
| } else if (filename.starts_with("n_")) { | ||
| EXPECT_FALSE(accepted) << filename << ": expected reject, got accept"; | ||
| } else { | ||
| vassert( | ||
| filename.starts_with("i_"), | ||
| "Unexpected test case name prefix: {}", | ||
| filename); | ||
| // Implementation-defined: either outcome is acceptable. The parser | ||
| // must not crash. | ||
| } | ||
| } | ||
|
|
||
| INSTANTIATE_TEST_SUITE_P( | ||
| json_test_suite, | ||
| json_test_suite_test, | ||
| ::testing::ValuesIn( | ||
| serde::json::testing::collect_json_test_cases( | ||
| test_utils::get_runfile_path("test_parsing", "nst_json_test_suite"))), | ||
| [](const ::testing::TestParamInfo<std::string>& info) { | ||
| // GTest requires parameter names to match [a-zA-Z0-9_] and to be | ||
| // unique. Use the filename stem and hex-escape any non-alphanumeric | ||
| // chars (using `_XX`) to preserve uniqueness across similar names. | ||
| auto stem = std::filesystem::path(info.param).stem().string(); | ||
| std::string out; | ||
| out.reserve(stem.size()); | ||
| for (auto c : stem) { | ||
| auto uc = static_cast<unsigned char>(c); | ||
| if (std::isalnum(uc) || uc == '_') { | ||
| out.push_back(c); | ||
| } else { | ||
| fmt::format_to(std::back_inserter(out), "_{:02x}", uc); | ||
| } | ||
| } | ||
| return out; | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| /* | ||
| * Copyright 2026 Redpanda Data, Inc. | ||
| * | ||
| * Use of this software is governed by the Business Source License | ||
| * included in the file licenses/BSL.md | ||
| * | ||
| * As of the Change Date specified in that file, in accordance with | ||
| * the Business Source License, use of this software will be governed | ||
| * by the Apache License, Version 2.0 | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "base/vassert.h" | ||
|
|
||
| #include <algorithm> | ||
| #include <filesystem> | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| namespace serde::json::testing { | ||
|
|
||
| /// Returns the sorted list of full paths to `.json` files found directly | ||
| /// under `dir`. The directory is required to exist and to contain at | ||
| /// least one `.json` file — this helper is intended to back | ||
| /// INSTANTIATE_TEST_SUITE_P calls where an empty list would silently | ||
| /// produce zero test cases. Full paths (rather than bare filenames) are | ||
| /// returned so callers don't have to carry the directory through to the | ||
| /// test body. | ||
| inline std::vector<std::string> | ||
| collect_json_test_cases(const std::filesystem::path& dir) { | ||
| vassert( | ||
| std::filesystem::exists(dir), | ||
| "Directory does not exist: {}", | ||
| dir.string()); | ||
| vassert( | ||
| std::filesystem::is_directory(dir), | ||
| "Path is not a directory: {}", | ||
| dir.string()); | ||
|
|
||
| std::vector<std::string> test_cases; | ||
| for (const auto& entry : std::filesystem::directory_iterator(dir)) { | ||
| vassert( | ||
| entry.is_regular_file() || entry.is_symlink(), | ||
| "Expected only files under {}, found: {}", | ||
| dir.string(), | ||
| entry.path().string()); | ||
| if (entry.path().extension() == ".json") { | ||
| test_cases.push_back(entry.path().string()); | ||
| } | ||
| } | ||
|
|
||
| vassert( | ||
| !test_cases.empty(), "No test cases found in directory {}", dir.string()); | ||
|
|
||
| std::ranges::sort(test_cases); | ||
| return test_cases; | ||
| } | ||
|
|
||
| } // namespace serde::json::testing |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.