| 1 | /* |
| 2 | * Open Chinese Convert |
| 3 | * |
| 4 | * End-to-end validation of all configs against consolidated testcases.json. |
| 5 | */ |
| 6 | |
| 7 | #ifndef BAZEL |
| 8 | // This test is Bazel-only; CMake builds should skip compiling it. |
| 9 | static_assert(false, "ConfigDictValidationTest is only supported under Bazel"); |
| 10 | #else |
| 11 | |
| 12 | #include <fstream> |
| 13 | #include <memory> |
| 14 | #include <sstream> |
| 15 | #include <string> |
| 16 | #include <unordered_map> |
| 17 | |
| 18 | #include "gtest/gtest.h" |
| 19 | #include "rapidjson/document.h" |
| 20 | #include "src/SimpleConverter.hpp" |
| 21 | |
| 22 | #include "tools/cpp/runfiles/runfiles.h" |
| 23 | using bazel::tools::cpp::runfiles::Runfiles; |
| 24 | |
| 25 | namespace opencc { |
| 26 | namespace { |
| 27 | |
| 28 | #ifndef OPENCC_CONFIG_DICT_TEST_FILE |
| 29 | #error "OPENCC_CONFIG_DICT_TEST_FILE must be defined" |
| 30 | #endif |
| 31 | |
| 32 | class ConfigDictValidationTest : public ::testing::Test { |
| 33 | protected: |
| 34 | void SetUp() override { |
| 35 | #ifdef BAZEL |
| 36 | runfiles_.reset(Runfiles::CreateForTest()); |
| 37 | ASSERT_NE(nullptr, runfiles_); |
| 38 | testcasesPath_ = runfiles_->Rlocation("_main/test/testcases/testcases.json"); |
| 39 | configName_ = OPENCC_CONFIG_DICT_TEST_FILE; |
| 40 | const std::string suffix = ".json"; |
| 41 | if (configName_.size() >= suffix.size() && |
| 42 | configName_.compare(configName_.size() - suffix.size(), suffix.size(), |
| 43 | suffix) == 0) { |
| 44 | configName_.erase(configName_.size() - suffix.size()); |
| 45 | } |
| 46 | const std::string configFile = runfiles_->Rlocation( |
| 47 | "_main/data/config/" + std::string(OPENCC_CONFIG_DICT_TEST_FILE)); |
| 48 | configDir_ = configFile.substr(0, configFile.find_last_of("/\\")); |
| 49 | const std::string dictFile = |
| 50 | runfiles_->Rlocation("_main/data/dictionary/STCharacters.ocd2"); |
| 51 | dictDir_ = dictFile.substr(0, dictFile.find_last_of("/\\")); |
| 52 | #else |
| 53 | FAIL() << "This test expects Bazel runfiles."; |
| 54 | #endif |
| 55 | } |
| 56 | |
| 57 | std::string ReadFile(const std::string& path) { |
| 58 | std::ifstream ifs(path); |
| 59 | EXPECT_TRUE(ifs.is_open()) << path; |
| 60 | std::stringstream buffer; |
| 61 | buffer << ifs.rdbuf(); |
| 62 | return buffer.str(); |
| 63 | } |
| 64 | |
| 65 | SimpleConverter& GetConverter(const std::string& config) { |
| 66 | auto it = converters_.find(config); |
| 67 | if (it != converters_.end()) { |
| 68 | return *it->second; |
| 69 | } |
| 70 | const std::string configPath = configDir_ + "/" + config + ".json"; |
| 71 | auto inserted = converters_.emplace( |
| 72 | config, |
| 73 | std::make_unique<SimpleConverter>(configPath, |
| 74 | std::vector<std::string>{ |
| 75 | configDir_, dictDir_})); |
| 76 | return *inserted.first->second; |
| 77 | } |
| 78 | |
| 79 | std::unique_ptr<Runfiles> runfiles_; |
| 80 | std::string configName_; |
| 81 | std::string testcasesPath_; |
| 82 | std::string configDir_; |
| 83 | std::string dictDir_; |
| 84 | std::unordered_map<std::string, std::unique_ptr<SimpleConverter>> |
| 85 | converters_; |
| 86 | }; |
| 87 | |
| 88 | TEST_F(ConfigDictValidationTest, ConvertExpectedOutputs) { |
| 89 | const std::string json = ReadFile(testcasesPath_); |
| 90 | rapidjson::Document doc; |
| 91 | doc.Parse<rapidjson::kParseCommentsFlag | |
| 92 | rapidjson::kParseTrailingCommasFlag>(json.c_str()); |
| 93 | ASSERT_FALSE(doc.HasParseError()); |
| 94 | ASSERT_TRUE(doc.IsObject()); |
| 95 | ASSERT_TRUE(doc.HasMember("cases")); |
| 96 | const auto& cases = doc["cases"]; |
| 97 | ASSERT_TRUE(cases.IsArray()); |
| 98 | |
| 99 | size_t checkedCases = 0; |
| 100 | for (auto& testcase : cases.GetArray()) { |
| 101 | ASSERT_TRUE(testcase.IsObject()); |
| 102 | ASSERT_TRUE(testcase.HasMember("input")); |
| 103 | ASSERT_TRUE(testcase["input"].IsString()); |
| 104 | const std::string input = testcase["input"].GetString(); |
| 105 | const std::string id = |
| 106 | testcase.HasMember("id") && testcase["id"].IsString() |
| 107 | ? testcase["id"].GetString() |
| 108 | : "(unknown id)"; |
| 109 | ASSERT_TRUE(testcase.HasMember("expected")); |
| 110 | const auto& expectedObj = testcase["expected"]; |
| 111 | ASSERT_TRUE(expectedObj.IsObject()); |
| 112 | if (!expectedObj.HasMember(configName_.c_str())) { |
| 113 | continue; |
| 114 | } |
| 115 | const auto& expectedValue = expectedObj[configName_.c_str()]; |
| 116 | ASSERT_TRUE(expectedValue.IsString()) |
| 117 | << "config=" << configName_ << " case=" << id; |
| 118 | const std::string expected = expectedValue.GetString(); |
| 119 | SimpleConverter& converter = GetConverter(configName_); |
| 120 | EXPECT_EQ(expected, converter.Convert(input)) |
| 121 | << "config=" << configName_ << " case=" << id; |
| 122 | ++checkedCases; |
| 123 | } |
| 124 | EXPECT_GT(checkedCases, 0) << "No testcases found for config=" << configName_; |
| 125 | } |
| 126 | |
| 127 | } // namespace |
| 128 | } // namespace opencc |
| 129 | |
| 130 | #endif // BAZEL |