|
| 1 | +/* |
| 2 | + * Open Chinese Convert |
| 3 | + * |
| 4 | + * End-to-end validation of all configs against consolidated testcases.json. |
| 5 | + */ |
| 6 | + |
| 7 | +#include <fstream> |
| 8 | +#include <memory> |
| 9 | +#include <sstream> |
| 10 | +#include <string> |
| 11 | +#include <unordered_map> |
| 12 | + |
| 13 | +#include "gtest/gtest.h" |
| 14 | +#include "rapidjson/document.h" |
| 15 | +#include "src/SimpleConverter.hpp" |
| 16 | + |
| 17 | +#ifdef BAZEL |
| 18 | +#include "tools/cpp/runfiles/runfiles.h" |
| 19 | +using bazel::tools::cpp::runfiles::Runfiles; |
| 20 | +#endif |
| 21 | + |
| 22 | +namespace opencc { |
| 23 | +namespace { |
| 24 | + |
| 25 | +class ConfigDictValidationTest : public ::testing::Test { |
| 26 | +protected: |
| 27 | + void SetUp() override { |
| 28 | +#ifdef BAZEL |
| 29 | + runfiles_.reset(Runfiles::CreateForTest()); |
| 30 | + ASSERT_NE(nullptr, runfiles_); |
| 31 | + testcasesPath_ = runfiles_->Rlocation("_main/test/testcases/testcases.json"); |
| 32 | + configDir_ = runfiles_->Rlocation("_main/data/config"); |
| 33 | + dictDir_ = runfiles_->Rlocation("_main/data/dictionary"); |
| 34 | +#else |
| 35 | + FAIL() << "This test expects Bazel runfiles."; |
| 36 | +#endif |
| 37 | + } |
| 38 | + |
| 39 | + std::string ReadFile(const std::string& path) { |
| 40 | + std::ifstream ifs(path); |
| 41 | + EXPECT_TRUE(ifs.is_open()) << path; |
| 42 | + std::stringstream buffer; |
| 43 | + buffer << ifs.rdbuf(); |
| 44 | + return buffer.str(); |
| 45 | + } |
| 46 | + |
| 47 | + SimpleConverter& GetConverter(const std::string& config) { |
| 48 | + auto it = converters_.find(config); |
| 49 | + if (it != converters_.end()) { |
| 50 | + return *it->second; |
| 51 | + } |
| 52 | + const std::string configPath = configDir_ + "/" + config + ".json"; |
| 53 | + auto inserted = converters_.emplace( |
| 54 | + config, |
| 55 | + std::make_unique<SimpleConverter>(configPath, |
| 56 | + std::vector<std::string>{ |
| 57 | + configDir_, dictDir_})); |
| 58 | + return *inserted.first->second; |
| 59 | + } |
| 60 | + |
| 61 | + std::unique_ptr<Runfiles> runfiles_; |
| 62 | + std::string testcasesPath_; |
| 63 | + std::string configDir_; |
| 64 | + std::string dictDir_; |
| 65 | + std::unordered_map<std::string, std::unique_ptr<SimpleConverter>> |
| 66 | + converters_; |
| 67 | +}; |
| 68 | + |
| 69 | +TEST_F(ConfigDictValidationTest, ConvertExpectedOutputs) { |
| 70 | + const std::string json = ReadFile(testcasesPath_); |
| 71 | + rapidjson::Document doc; |
| 72 | + doc.Parse(json.c_str()); |
| 73 | + ASSERT_FALSE(doc.HasParseError()); |
| 74 | + ASSERT_TRUE(doc.IsObject()); |
| 75 | + ASSERT_TRUE(doc.HasMember("cases")); |
| 76 | + const auto& cases = doc["cases"]; |
| 77 | + ASSERT_TRUE(cases.IsArray()); |
| 78 | + |
| 79 | + for (auto& testcase : cases.GetArray()) { |
| 80 | + ASSERT_TRUE(testcase.IsObject()); |
| 81 | + ASSERT_TRUE(testcase.HasMember("input")); |
| 82 | + ASSERT_TRUE(testcase["input"].IsString()); |
| 83 | + const std::string input = testcase["input"].GetString(); |
| 84 | + const std::string id = |
| 85 | + testcase.HasMember("id") && testcase["id"].IsString() |
| 86 | + ? testcase["id"].GetString() |
| 87 | + : "(unknown id)"; |
| 88 | + ASSERT_TRUE(testcase.HasMember("expected")); |
| 89 | + const auto& expectedObj = testcase["expected"]; |
| 90 | + ASSERT_TRUE(expectedObj.IsObject()); |
| 91 | + for (auto itr = expectedObj.MemberBegin(); itr != expectedObj.MemberEnd(); |
| 92 | + ++itr) { |
| 93 | + const std::string config = itr->name.GetString(); |
| 94 | + ASSERT_TRUE(itr->value.IsString()); |
| 95 | + const std::string expected = itr->value.GetString(); |
| 96 | + SimpleConverter& converter = GetConverter(config); |
| 97 | + EXPECT_EQ(expected, converter.Convert(input)) |
| 98 | + << "config=" << config << " case=" << id; |
| 99 | + } |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +} // namespace |
| 104 | +} // namespace opencc |
0 commit comments