| 1 | /* |
| 2 | * Open Chinese Convert |
| 3 | * |
| 4 | * Copyright 2015-2024 Carbo Kuo <[email protected]> |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | */ |
| 18 | |
| 19 | #include <fstream> |
| 20 | #include <iostream> |
| 21 | #include <map> |
| 22 | #include <sstream> |
| 23 | #include <unordered_map> |
| 24 | #include <vector> |
| 25 | |
| 26 | #include "src/Common.hpp" |
| 27 | #include "rapidjson/document.h" |
| 28 | #include "gtest/gtest.h" |
| 29 | |
| 30 | #ifdef BAZEL |
| 31 | #include "tools/cpp/runfiles/runfiles.h" |
| 32 | using bazel::tools::cpp::runfiles::Runfiles; |
| 33 | #endif |
| 34 | |
| 35 | namespace opencc { |
| 36 | |
| 37 | class CommandLineConvertTest : public ::testing::Test { |
| 38 | protected: |
| 39 | CommandLineConvertTest() { GetCurrentWorkingDirectory(); } |
| 40 | |
| 41 | virtual ~CommandLineConvertTest() { free(originalWorkingDirectory); } |
| 42 | |
| 43 | virtual void SetUp() { |
| 44 | #ifdef BAZEL |
| 45 | runfiles_.reset(Runfiles::CreateForTest()); |
| 46 | #else |
| 47 | ASSERT_NE("", CMAKE_BINARY_DIR); |
| 48 | ASSERT_NE("", CMAKE_SOURCE_DIR); |
| 49 | ASSERT_EQ(0, chdir("/usr/share/opencc/")); |
| 50 | #endif |
| 51 | } |
| 52 | |
| 53 | virtual void TearDown() { ASSERT_EQ(0, chdir(originalWorkingDirectory)); } |
| 54 | |
| 55 | std::string GetFileContents(const std::string& fileName) const { |
| 56 | std::ifstream fs(fileName); |
| 57 | EXPECT_TRUE(fs.is_open()) << fileName; |
| 58 | const std::string content((std::istreambuf_iterator<char>(fs)), |
| 59 | (std::istreambuf_iterator<char>())); |
| 60 | fs.close(); |
| 61 | return content; |
| 62 | } |
| 63 | |
| 64 | void GetCurrentWorkingDirectory() { |
| 65 | originalWorkingDirectory = getcwd(nullptr, 0); |
| 66 | } |
| 67 | |
| 68 | std::string OpenccCommand() const { |
| 69 | return PROJECT_BINARY_PATH; |
| 70 | } |
| 71 | |
| 72 | std::string OutputDirectory() const { |
| 73 | #ifdef BAZEL |
| 74 | return ::testing::TempDir() + "/"; |
| 75 | #else |
| 76 | return PROJECT_BINARY_DIR "/test/"; |
| 77 | #endif |
| 78 | } |
| 79 | |
| 80 | std::string ConfigurationDirectory() const { |
| 81 | #ifdef BAZEL |
| 82 | return ""; |
| 83 | #else |
| 84 | // Use the configurations installed by libopencc-data. |
| 85 | return "/usr/share/opencc/"; |
| 86 | #endif |
| 87 | } |
| 88 | |
| 89 | std::string InputFile(const char* config) const { |
| 90 | return OutputDirectory() + config + ".in"; |
| 91 | } |
| 92 | |
| 93 | std::string OutputFile(const char* config) const { |
| 94 | return OutputDirectory() + config + ".out"; |
| 95 | } |
| 96 | |
| 97 | std::string TestCommand(const std::string& config, |
| 98 | const std::string& inputFile, |
| 99 | const std::string& outputFile) const { |
| 100 | // testcases.json is written against conversions with the tofu-risk |
| 101 | // dictionaries enabled, matching the upstream ConvertFromJson test. |
| 102 | std::string cmd = OpenccCommand() + " --include-tofu-risk-dictionaries" + |
| 103 | " -i " + inputFile + " -o " + outputFile + " -c " + |
| 104 | ConfigurationDirectory() + config + ".json"; |
| 105 | #ifdef BAZEL |
| 106 | cmd += " --path " + runfiles_->Rlocation("_main/data/dictionary") + "/" + |
| 107 | " --path " + runfiles_->Rlocation("_main/data/config") + "/"; |
| 108 | #endif |
| 109 | return cmd; |
| 110 | } |
| 111 | |
| 112 | char* originalWorkingDirectory; |
| 113 | |
| 114 | #ifdef BAZEL |
| 115 | std::unique_ptr<Runfiles> runfiles_; |
| 116 | #endif |
| 117 | }; |
| 118 | |
| 119 | struct CaseInput { |
| 120 | std::string input; |
| 121 | std::string expected; |
| 122 | }; |
| 123 | |
| 124 | using CasesByConfig = std::unordered_map<std::string, std::vector<CaseInput>>; |
| 125 | |
| 126 | CasesByConfig LoadCases(const std::string& jsonPath) { |
| 127 | CasesByConfig cases; |
| 128 | std::string content; |
| 129 | { |
| 130 | std::ifstream ifs(jsonPath); |
| 131 | if (!ifs.is_open()) { |
| 132 | throw std::runtime_error("Cannot open " + jsonPath); |
| 133 | } |
| 134 | std::stringstream buffer; |
| 135 | buffer << ifs.rdbuf(); |
| 136 | content = buffer.str(); |
| 137 | } |
| 138 | |
| 139 | rapidjson::Document doc; |
| 140 | doc.Parse<rapidjson::kParseCommentsFlag | |
| 141 | rapidjson::kParseTrailingCommasFlag>(content.c_str()); |
| 142 | if (doc.HasParseError() || !doc.IsObject() || !doc.HasMember("cases") || |
| 143 | !doc["cases"].IsArray()) { |
| 144 | throw std::runtime_error("Invalid testcases.json format"); |
| 145 | } |
| 146 | |
| 147 | for (auto& entry : doc["cases"].GetArray()) { |
| 148 | if (!entry.IsObject() || !entry.HasMember("input") || |
| 149 | !entry["input"].IsString() || !entry.HasMember("expected") || |
| 150 | !entry["expected"].IsObject()) { |
| 151 | continue; |
| 152 | } |
| 153 | const std::string input = entry["input"].GetString(); |
| 154 | for (auto itr = entry["expected"].MemberBegin(); |
| 155 | itr != entry["expected"].MemberEnd(); ++itr) { |
| 156 | if (!itr->value.IsString()) { |
| 157 | continue; |
| 158 | } |
| 159 | const std::string config = itr->name.GetString(); |
| 160 | cases[config].push_back({input, itr->value.GetString()}); |
| 161 | } |
| 162 | } |
| 163 | return cases; |
| 164 | } |
| 165 | |
| 166 | TEST_F(CommandLineConvertTest, ConvertFromJson) { |
| 167 | #ifdef BAZEL |
| 168 | const std::string casesPath = |
| 169 | runfiles_->Rlocation("_main/test/testcases/testcases.json"); |
| 170 | #else |
| 171 | // CMAKE_SOURCE_DIR is debian/tests; the testcases live in the top-level |
| 172 | // source tree, which autopkgtest provides as the working tree. |
| 173 | const std::string casesPath = |
| 174 | CMAKE_SOURCE_DIR "/../../test/testcases/testcases.json"; |
| 175 | #endif |
| 176 | const CasesByConfig cases = LoadCases(casesPath); |
| 177 | |
| 178 | for (const auto& entry : cases) { |
| 179 | const std::string& config = entry.first; |
| 180 | const std::string inputFile = InputFile(config.c_str()); |
| 181 | const std::string outputFile = OutputFile(config.c_str()); |
| 182 | |
| 183 | // Write inputs into a temp file (one per line). |
| 184 | { |
| 185 | std::ofstream ofs(inputFile, std::ios::binary); |
| 186 | ASSERT_TRUE(ofs.is_open()) << "Failed to open input file for writing: " |
| 187 | << inputFile; |
| 188 | for (const auto& item : entry.second) { |
| 189 | ofs << item.input << "\n"; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | ASSERT_EQ(0, system(TestCommand(config, inputFile, outputFile).c_str())); |
| 194 | |
| 195 | // Read outputs and compare line by line. |
| 196 | std::ifstream ifs(outputFile, std::ios::binary); |
| 197 | ASSERT_TRUE(ifs.is_open()); |
| 198 | std::string line; |
| 199 | size_t idx = 0; |
| 200 | while (std::getline(ifs, line)) { |
| 201 | if (!line.empty() && line.back() == '\r') { |
| 202 | line.pop_back(); // normalize Windows CRLF |
| 203 | } |
| 204 | ASSERT_LT(idx, entry.second.size()); |
| 205 | EXPECT_EQ(entry.second[idx].expected, line) |
| 206 | << "config=" << config << " index=" << idx; |
| 207 | idx++; |
| 208 | } |
| 209 | EXPECT_EQ(idx, entry.second.size()) << "config=" << config; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | } // namespace opencc |