forked from BYVoid/OpenCC
-
Notifications
You must be signed in to change notification settings - Fork 0
feature: New bazel test for config and dictionaries #9
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
frankslin
merged 4 commits into
master
from
feature/new-bazel-test-for-config-and-dictionaries
Jan 3, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9c2c965
feature: add a new ConfigDictValidationTest.cpp to be executed in bazel
frankslin 5f37fbd
Changeover to JSON-based testcases and clean dictionary outputs
frankslin 0248fd7
Normalize CommandLineConvertTest for CRLF comparisons on Windows
frankslin ea2d7e2
Address review feedback for tests and Bazel-only validation
frankslin 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| /* | ||
| * Open Chinese Convert | ||
| * | ||
| * End-to-end validation of all configs against consolidated testcases.json. | ||
| */ | ||
|
|
||
| #ifndef BAZEL | ||
| // This test is Bazel-only; CMake builds should skip compiling it. | ||
| static_assert(false, "ConfigDictValidationTest is only supported under Bazel"); | ||
| #else | ||
|
|
||
| #include <fstream> | ||
| #include <memory> | ||
| #include <sstream> | ||
| #include <string> | ||
| #include <unordered_map> | ||
|
|
||
| #include "gtest/gtest.h" | ||
| #include "rapidjson/document.h" | ||
| #include "src/SimpleConverter.hpp" | ||
|
|
||
| #include "tools/cpp/runfiles/runfiles.h" | ||
| using bazel::tools::cpp::runfiles::Runfiles; | ||
|
|
||
| namespace opencc { | ||
| namespace { | ||
|
|
||
| class ConfigDictValidationTest : public ::testing::Test { | ||
| protected: | ||
| void SetUp() override { | ||
| #ifdef BAZEL | ||
| runfiles_.reset(Runfiles::CreateForTest()); | ||
| ASSERT_NE(nullptr, runfiles_); | ||
| testcasesPath_ = runfiles_->Rlocation("_main/test/testcases/testcases.json"); | ||
| configDir_ = runfiles_->Rlocation("_main/data/config"); | ||
| dictDir_ = runfiles_->Rlocation("_main/data/dictionary"); | ||
| #else | ||
| FAIL() << "This test expects Bazel runfiles."; | ||
| #endif | ||
| } | ||
|
|
||
| std::string ReadFile(const std::string& path) { | ||
| std::ifstream ifs(path); | ||
| EXPECT_TRUE(ifs.is_open()) << path; | ||
| std::stringstream buffer; | ||
| buffer << ifs.rdbuf(); | ||
| return buffer.str(); | ||
| } | ||
|
|
||
| SimpleConverter& GetConverter(const std::string& config) { | ||
| auto it = converters_.find(config); | ||
| if (it != converters_.end()) { | ||
| return *it->second; | ||
| } | ||
| const std::string configPath = configDir_ + "/" + config + ".json"; | ||
| auto inserted = converters_.emplace( | ||
| config, | ||
| std::make_unique<SimpleConverter>(configPath, | ||
| std::vector<std::string>{ | ||
| configDir_, dictDir_})); | ||
| return *inserted.first->second; | ||
| } | ||
|
|
||
| std::unique_ptr<Runfiles> runfiles_; | ||
| std::string testcasesPath_; | ||
| std::string configDir_; | ||
| std::string dictDir_; | ||
| std::unordered_map<std::string, std::unique_ptr<SimpleConverter>> | ||
| converters_; | ||
| }; | ||
|
|
||
| TEST_F(ConfigDictValidationTest, ConvertExpectedOutputs) { | ||
| const std::string json = ReadFile(testcasesPath_); | ||
| rapidjson::Document doc; | ||
| doc.Parse(json.c_str()); | ||
| ASSERT_FALSE(doc.HasParseError()); | ||
| ASSERT_TRUE(doc.IsObject()); | ||
| ASSERT_TRUE(doc.HasMember("cases")); | ||
| const auto& cases = doc["cases"]; | ||
| ASSERT_TRUE(cases.IsArray()); | ||
|
|
||
| for (auto& testcase : cases.GetArray()) { | ||
| ASSERT_TRUE(testcase.IsObject()); | ||
| ASSERT_TRUE(testcase.HasMember("input")); | ||
| ASSERT_TRUE(testcase["input"].IsString()); | ||
| const std::string input = testcase["input"].GetString(); | ||
| const std::string id = | ||
| testcase.HasMember("id") && testcase["id"].IsString() | ||
| ? testcase["id"].GetString() | ||
| : "(unknown id)"; | ||
| ASSERT_TRUE(testcase.HasMember("expected")); | ||
| const auto& expectedObj = testcase["expected"]; | ||
| ASSERT_TRUE(expectedObj.IsObject()); | ||
| for (auto itr = expectedObj.MemberBegin(); itr != expectedObj.MemberEnd(); | ||
| ++itr) { | ||
| const std::string config = itr->name.GetString(); | ||
| ASSERT_TRUE(itr->value.IsString()); | ||
| const std::string expected = itr->value.GetString(); | ||
| SimpleConverter& converter = GetConverter(config); | ||
| EXPECT_EQ(expected, converter.Convert(input)) | ||
| << "config=" << config << " case=" << id; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } // namespace | ||
| } // namespace opencc | ||
|
|
||
| #endif // BAZEL | ||
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
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 |
|---|---|---|
|
|
@@ -22,5 +22,6 @@ cc_test( | |
| "//src:common", | ||
| "@bazel_tools//tools/cpp/runfiles", | ||
| "@googletest//:gtest_main", | ||
| "@rapidjson", | ||
| ], | ||
| ) | ||
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.
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.