| 1 | /* |
| 2 | * Open Chinese Convert |
| 3 | * |
| 4 | * Copyright 2026 Carbo Kuo and contributors |
| 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 <filesystem> |
| 21 | #include <sstream> |
| 22 | #include <unordered_map> |
| 23 | #include <vector> |
| 24 | |
| 25 | #include "gtest/gtest.h" |
| 26 | #include "rapidjson/document.h" |
| 27 | |
| 28 | #ifndef BAZEL |
| 29 | #include "JiebaPluginIntegrationTestConfig.hpp" |
| 30 | #endif |
| 31 | #include "test/PortableUtil.hpp" |
| 32 | |
| 33 | #if defined(_WIN32) || defined(_WIN64) |
| 34 | #include <windows.h> |
| 35 | |
| 36 | std::wstring WideFromUtf8(const std::string& text) { |
| 37 | if (text.empty()) { |
| 38 | return L""; |
| 39 | } |
| 40 | const int size = |
| 41 | MultiByteToWideChar(CP_UTF8, 0, text.c_str(), -1, nullptr, 0); |
| 42 | if (size <= 1) { |
| 43 | return L""; |
| 44 | } |
| 45 | std::wstring wide(static_cast<size_t>(size), L'\0'); |
| 46 | MultiByteToWideChar(CP_UTF8, 0, text.c_str(), -1, &wide[0], size); |
| 47 | wide.resize(static_cast<size_t>(size - 1)); |
| 48 | return wide; |
| 49 | } |
| 50 | |
| 51 | void CopyRegularFiles(const std::filesystem::path& sourceDir, |
| 52 | const std::filesystem::path& targetDir) { |
| 53 | for (const auto& entry : std::filesystem::directory_iterator(sourceDir)) { |
| 54 | if (!entry.is_regular_file()) { |
| 55 | continue; |
| 56 | } |
| 57 | std::filesystem::copy_file(entry.path(), targetDir / entry.path().filename(), |
| 58 | std::filesystem::copy_options::overwrite_existing); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | int RunProcessWide(const std::filesystem::path& application, |
| 63 | const std::wstring& arguments, |
| 64 | const std::filesystem::path& workingDirectory) { |
| 65 | STARTUPINFOW startupInfo = {}; |
| 66 | startupInfo.cb = sizeof(startupInfo); |
| 67 | PROCESS_INFORMATION processInfo = {}; |
| 68 | std::wstring commandLine = arguments; |
| 69 | if (!CreateProcessW(WideFromUtf8(application.u8string()).c_str(), |
| 70 | commandLine.data(), nullptr, nullptr, FALSE, 0, nullptr, |
| 71 | WideFromUtf8(workingDirectory.u8string()).c_str(), |
| 72 | &startupInfo, &processInfo)) { |
| 73 | return -1; |
| 74 | } |
| 75 | WaitForSingleObject(processInfo.hProcess, INFINITE); |
| 76 | DWORD exitCode = 1; |
| 77 | GetExitCodeProcess(processInfo.hProcess, &exitCode); |
| 78 | CloseHandle(processInfo.hThread); |
| 79 | CloseHandle(processInfo.hProcess); |
| 80 | return static_cast<int>(exitCode); |
| 81 | } |
| 82 | #endif |
| 83 | |
| 84 | #ifdef BAZEL |
| 85 | #include "tools/cpp/runfiles/runfiles.h" |
| 86 | using bazel::tools::cpp::runfiles::Runfiles; |
| 87 | #endif |
| 88 | |
| 89 | namespace opencc { |
| 90 | |
| 91 | #ifdef BAZEL |
| 92 | namespace { |
| 93 | |
| 94 | std::string ParentDirectory(const std::string& path) { |
| 95 | const std::string::size_type pos = path.find_last_of("/\\"); |
| 96 | if (pos == std::string::npos) { |
| 97 | return ""; |
| 98 | } |
| 99 | return path.substr(0, pos); |
| 100 | } |
| 101 | |
| 102 | bool IsReadableFile(const std::string& path) { |
| 103 | std::ifstream ifs(path.c_str()); |
| 104 | return ifs.is_open(); |
| 105 | } |
| 106 | |
| 107 | } // namespace |
| 108 | #else |
| 109 | namespace { |
| 110 | |
| 111 | std::string ParentDirectory(const std::string& path) { |
| 112 | const std::string::size_type pos = path.find_last_of("/\\"); |
| 113 | if (pos == std::string::npos) { |
| 114 | return ""; |
| 115 | } |
| 116 | return path.substr(0, pos); |
| 117 | } |
| 118 | |
| 119 | bool IsReadableFile(const std::string& path) { |
| 120 | std::ifstream ifs(path.c_str()); |
| 121 | return ifs.is_open(); |
| 122 | } |
| 123 | |
| 124 | } // namespace |
| 125 | #endif |
| 126 | |
| 127 | struct CaseInput { |
| 128 | std::string input; |
| 129 | std::string expected; |
| 130 | }; |
| 131 | |
| 132 | using CasesByConfig = std::unordered_map<std::string, std::vector<CaseInput>>; |
| 133 | |
| 134 | class JiebaPluginIntegrationTest : public ::testing::Test { |
| 135 | protected: |
| 136 | JiebaPluginIntegrationTest() { originalWorkingDirectory_ = portable_getcwd(); } |
| 137 | |
| 138 | ~JiebaPluginIntegrationTest() override { free(originalWorkingDirectory_); } |
| 139 | |
| 140 | void SetUp() override { |
| 141 | #ifdef BAZEL |
| 142 | runfiles_.reset(Runfiles::CreateForTest()); |
| 143 | ASSERT_TRUE(runfiles_ != nullptr); |
| 144 | std::filesystem::create_directories(OutputDirectory()); |
| 145 | #else |
| 146 | ASSERT_EQ(0, portable_chdir(PROJECT_BINARY_DIR "/data")); |
| 147 | std::filesystem::create_directories(OutputDirectory()); |
| 148 | #endif |
| 149 | } |
| 150 | |
| 151 | void TearDown() override { |
| 152 | ASSERT_EQ(0, portable_chdir(originalWorkingDirectory_)); |
| 153 | } |
| 154 | |
| 155 | std::string OpenccCommand() const { |
| 156 | #ifdef BAZEL |
| 157 | #ifdef _WIN32 |
| 158 | std::string path = runfiles_->Rlocation("_main/src/tools/command_line.exe"); |
| 159 | if (path.empty()) { |
| 160 | path = runfiles_->Rlocation("_main/src/tools/command_line"); |
| 161 | } |
| 162 | return path; |
| 163 | #else |
| 164 | return runfiles_->Rlocation("_main/src/tools/command_line"); |
| 165 | #endif |
| 166 | #else |
| 167 | return OPENCC_TEST_COMMAND; |
| 168 | #endif |
| 169 | } |
| 170 | |
| 171 | static std::string QuotePath(const std::string& path) { |
| 172 | return "\"" + path + "\""; |
| 173 | } |
| 174 | |
| 175 | static std::string JsonString(const std::string& value) { |
| 176 | std::string escaped = "\""; |
| 177 | for (char ch : value) { |
| 178 | if (ch == '\\' || ch == '"') { |
| 179 | escaped.push_back('\\'); |
| 180 | } |
| 181 | escaped.push_back(ch); |
| 182 | } |
| 183 | escaped.push_back('"'); |
| 184 | return escaped; |
| 185 | } |
| 186 | |
| 187 | std::string OutputDirectory() const { |
| 188 | #ifdef BAZEL |
| 189 | return ::testing::TempDir() + "opencc-jieba/"; |
| 190 | #else |
| 191 | return PROJECT_BINARY_DIR "/plugins/jieba/tests/"; |
| 192 | #endif |
| 193 | } |
| 194 | |
| 195 | std::string InputFile(const std::string& config) const { |
| 196 | return OutputDirectory() + config + ".in"; |
| 197 | } |
| 198 | |
| 199 | std::string OutputFile(const std::string& config) const { |
| 200 | return OutputDirectory() + config + ".out"; |
| 201 | } |
| 202 | |
| 203 | std::string ConfigPath(const std::string& config) const { |
| 204 | #ifdef BAZEL |
| 205 | return runfiles_->Rlocation("_main/plugins/jieba/data/config/" + config + |
| 206 | ".json"); |
| 207 | #else |
| 208 | if (config.size() >= 6 && |
| 209 | config.compare(config.size() - 6, 6, "_jieba") == 0) { |
| 210 | return std::string(PROJECT_SOURCE_DIR) + "/plugins/jieba/data/config/" + |
| 211 | config + ".json"; |
| 212 | } |
| 213 | return std::string(PROJECT_SOURCE_DIR) + "/data/config/" + config + ".json"; |
| 214 | #endif |
| 215 | } |
| 216 | |
| 217 | std::string DictionaryDirectory() const { |
| 218 | #ifdef BAZEL |
| 219 | const std::string dictFile = |
| 220 | runfiles_->Rlocation("_main/data/dictionary/STCharacters.ocd2"); |
| 221 | return ParentDirectory(dictFile); |
| 222 | #else |
| 223 | return std::string(PROJECT_BINARY_DIR) + "/data"; |
| 224 | #endif |
| 225 | } |
| 226 | |
| 227 | std::string PluginDirectory() const { |
| 228 | #ifdef BAZEL |
| 229 | const std::vector<std::string> candidates = { |
| 230 | #ifdef _WIN32 |
| 231 | "_main/plugins/jieba/opencc-jieba.dll", |
| 232 | "_main/plugins/jieba/opencc-jieba.exe", |
| 233 | #elif defined(__APPLE__) |
| 234 | "_main/plugins/jieba/libopencc-jieba.dylib", |
| 235 | "_main/plugins/jieba/opencc-jieba.dylib", |
| 236 | "_main/plugins/jieba/libopencc-jieba.so", |
| 237 | #else |
| 238 | "_main/plugins/jieba/libopencc-jieba.so", |
| 239 | "_main/plugins/jieba/opencc-jieba.so", |
| 240 | #endif |
| 241 | }; |
| 242 | for (const std::string& candidate : candidates) { |
| 243 | const std::string path = runfiles_->Rlocation(candidate); |
| 244 | if (!path.empty() && IsReadableFile(path)) { |
| 245 | return ParentDirectory(path); |
| 246 | } |
| 247 | } |
| 248 | return ""; |
| 249 | #else |
| 250 | return OPENCC_PLUGIN_TEST_DIR; |
| 251 | #endif |
| 252 | } |
| 253 | |
| 254 | std::string MergedJiebaDictPath() const { |
| 255 | #ifdef BAZEL |
| 256 | return runfiles_->Rlocation("_main/plugins/jieba/jieba_dict/jieba_merged.ocd2"); |
| 257 | #else |
| 258 | const std::string pluginDir = PluginDirectory(); |
| 259 | const std::string pluginParent = ParentDirectory(pluginDir); |
| 260 | |
| 261 | std::vector<std::string> candidates; |
| 262 | candidates.push_back(OPENCC_JIEBA_MERGED_DICT_PATH); |
| 263 | candidates.push_back(pluginDir + "/jieba_dict/jieba_merged.ocd2"); |
| 264 | if (!pluginParent.empty()) { |
| 265 | candidates.push_back(pluginParent + "/jieba_dict/jieba_merged.ocd2"); |
| 266 | } |
| 267 | #if defined(_WIN32) || defined(_WIN64) |
| 268 | const char* const configs[] = {"Debug", "Release", "RelWithDebInfo", |
| 269 | "MinSizeRel"}; |
| 270 | for (const char* config : configs) { |
| 271 | candidates.push_back(pluginDir + "/" + config + |
| 272 | "/jieba_dict/jieba_merged.ocd2"); |
| 273 | if (!pluginParent.empty()) { |
| 274 | candidates.push_back(pluginParent + "/" + config + |
| 275 | "/jieba_dict/jieba_merged.ocd2"); |
| 276 | } |
| 277 | } |
| 278 | #endif |
| 279 | |
| 280 | for (const std::string& candidate : candidates) { |
| 281 | if (IsReadableFile(candidate)) { |
| 282 | return candidate; |
| 283 | } |
| 284 | } |
| 285 | return candidates.front(); |
| 286 | #endif |
| 287 | } |
| 288 | |
| 289 | std::string JiebaModelPath() const { |
| 290 | #ifdef BAZEL |
| 291 | return runfiles_->Rlocation( |
| 292 | "_main/plugins/jieba/deps/cppjieba/dict/hmm_model.utf8"); |
| 293 | #else |
| 294 | return std::string(PROJECT_SOURCE_DIR) + |
| 295 | "/plugins/jieba/deps/cppjieba/dict/hmm_model.utf8"; |
| 296 | #endif |
| 297 | } |
| 298 | |
| 299 | std::string EnvPrefix() const { |
| 300 | const std::string pluginDir = PluginDirectory(); |
| 301 | #ifdef _WIN32 |
| 302 | return "set \"OPENCC_SEGMENTATION_PLUGIN_PATH=" + pluginDir + "\" && "; |
| 303 | #else |
| 304 | return "OPENCC_SEGMENTATION_PLUGIN_PATH=" + QuotePath(pluginDir) + " "; |
| 305 | #endif |
| 306 | } |
| 307 | |
| 308 | std::string BuildCommand(const std::string& config, |
| 309 | const std::string& inputFile, |
| 310 | const std::string& outputFile) const { |
| 311 | std::string cmd = |
| 312 | BuildCommandWithPaths(ConfigPath(config), inputFile, outputFile, |
| 313 | DictionaryDirectory() + "/", PluginDirectory()); |
| 314 | #ifdef _WIN32 |
| 315 | return "\"" + cmd + "\""; |
| 316 | #else |
| 317 | return cmd; |
| 318 | #endif |
| 319 | } |
| 320 | |
| 321 | std::string BuildCommandWithPaths(const std::string& configPath, |
| 322 | const std::string& inputFile, |
| 323 | const std::string& outputFile, |
| 324 | const std::string& dictionaryDir, |
| 325 | const std::string& pluginDir) const { |
| 326 | #ifdef _WIN32 |
| 327 | std::string envPrefix = |
| 328 | "set \"OPENCC_SEGMENTATION_PLUGIN_PATH=" + pluginDir + "\" && "; |
| 329 | #else |
| 330 | std::string envPrefix = |
| 331 | "OPENCC_SEGMENTATION_PLUGIN_PATH=" + QuotePath(pluginDir) + " "; |
| 332 | #endif |
| 333 | return envPrefix + QuotePath(OpenccCommand()) + " -i " + QuotePath(inputFile) + |
| 334 | " -o " + QuotePath(outputFile) + " -c " + QuotePath(configPath) + |
| 335 | " --path " + QuotePath(dictionaryDir); |
| 336 | } |
| 337 | |
| 338 | #if defined(_WIN32) || defined(_WIN64) |
| 339 | static int RunCommandUtf8(const std::string& command) { |
| 340 | const int size = |
| 341 | MultiByteToWideChar(CP_UTF8, 0, command.c_str(), -1, nullptr, 0); |
| 342 | if (size <= 1) { |
| 343 | return -1; |
| 344 | } |
| 345 | std::wstring wide(static_cast<size_t>(size), L'\0'); |
| 346 | MultiByteToWideChar(CP_UTF8, 0, command.c_str(), -1, &wide[0], size); |
| 347 | return _wsystem(wide.c_str()); |
| 348 | } |
| 349 | #else |
| 350 | static int RunCommandUtf8(const std::string& command) { |
| 351 | return system(command.c_str()); |
| 352 | } |
| 353 | #endif |
| 354 | |
| 355 | static CasesByConfig LoadCases(const std::string& jsonPath) { |
| 356 | CasesByConfig cases; |
| 357 | std::ifstream ifs(jsonPath); |
| 358 | if (!ifs.is_open()) { |
| 359 | throw std::runtime_error("Cannot open " + jsonPath); |
| 360 | } |
| 361 | std::stringstream buffer; |
| 362 | buffer << ifs.rdbuf(); |
| 363 | rapidjson::Document doc; |
| 364 | std::string content = buffer.str(); |
| 365 | doc.Parse<rapidjson::kParseCommentsFlag | |
| 366 | rapidjson::kParseTrailingCommasFlag>(content.c_str()); |
| 367 | if (doc.HasParseError() || !doc.IsObject() || !doc.HasMember("cases")) { |
| 368 | throw std::runtime_error("Invalid testcase format"); |
| 369 | } |
| 370 | for (auto& entry : doc["cases"].GetArray()) { |
| 371 | const std::string input = entry["input"].GetString(); |
| 372 | for (auto itr = entry["expected"].MemberBegin(); |
| 373 | itr != entry["expected"].MemberEnd(); ++itr) { |
| 374 | if (!itr->value.IsString()) { |
| 375 | continue; |
| 376 | } |
| 377 | const std::string config = itr->name.GetString(); |
| 378 | if (config.size() < 6 || |
| 379 | config.compare(config.size() - 6, 6, "_jieba") != 0) { |
| 380 | continue; |
| 381 | } |
| 382 | cases[config].push_back({input, itr->value.GetString()}); |
| 383 | } |
| 384 | } |
| 385 | return cases; |
| 386 | } |
| 387 | |
| 388 | char* originalWorkingDirectory_ = nullptr; |
| 389 | |
| 390 | #ifdef BAZEL |
| 391 | std::unique_ptr<Runfiles> runfiles_; |
| 392 | #endif |
| 393 | }; |
| 394 | |
| 395 | TEST_F(JiebaPluginIntegrationTest, ConvertFromJsonCases) { |
| 396 | const CasesByConfig cases = LoadCases( |
| 397 | #ifdef BAZEL |
| 398 | runfiles_->Rlocation( |
| 399 | "_main/plugins/jieba/tests/data/jieba_comparison_testcases.json") |
| 400 | #else |
| 401 | std::string(PROJECT_SOURCE_DIR) + |
| 402 | "/plugins/jieba/tests/data/jieba_comparison_testcases.json" |
| 403 | #endif |
| 404 | ); |
| 405 | |
| 406 | for (const auto& entry : cases) { |
| 407 | const std::string& config = entry.first; |
| 408 | const std::string inputFile = InputFile(config); |
| 409 | const std::string outputFile = OutputFile(config); |
| 410 | |
| 411 | { |
| 412 | std::ofstream ofs(inputFile, std::ios::binary); |
| 413 | ASSERT_TRUE(ofs.is_open()); |
| 414 | for (const auto& item : entry.second) { |
| 415 | ofs << item.input << "\n"; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | ASSERT_EQ(0, RunCommandUtf8(BuildCommand(config, inputFile, outputFile))); |
| 420 | |
| 421 | std::ifstream ifs(outputFile, std::ios::binary); |
| 422 | ASSERT_TRUE(ifs.is_open()); |
| 423 | std::string line; |
| 424 | size_t idx = 0; |
| 425 | while (std::getline(ifs, line)) { |
| 426 | if (!line.empty() && line.back() == '\r') { |
| 427 | line.pop_back(); |
| 428 | } |
| 429 | ASSERT_LT(idx, entry.second.size()); |
| 430 | EXPECT_EQ(entry.second[idx].expected, line) |
| 431 | << "config=" << config << " index=" << idx; |
| 432 | idx++; |
| 433 | } |
| 434 | EXPECT_EQ(idx, entry.second.size()) << "config=" << config; |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | TEST_F(JiebaPluginIntegrationTest, ConvertWithMergedOcd2JiebaDictionary) { |
| 439 | const std::string inputFile = InputFile("s2twp_jieba_merged"); |
| 440 | const std::string outputFile = OutputFile("s2twp_jieba_merged"); |
| 441 | const std::string configFile = OutputDirectory() + "s2twp_jieba_merged.json"; |
| 442 | |
| 443 | { |
| 444 | std::ofstream ofs(inputFile, std::ios::binary); |
| 445 | ASSERT_TRUE(ofs.is_open()); |
| 446 | ofs << "生活着名为正敏的少女\n"; |
| 447 | } |
| 448 | |
| 449 | { |
| 450 | std::ofstream ofs(configFile, std::ios::binary); |
| 451 | ASSERT_TRUE(ofs.is_open()); |
| 452 | ofs << "{\n" |
| 453 | << " \"name\": \"merged jieba dict test\",\n" |
| 454 | << " \"segmentation\": {\n" |
| 455 | << " \"type\": \"jieba\",\n" |
| 456 | << " \"resources\": {\n" |
| 457 | << " \"dict_path\": " << JsonString(MergedJiebaDictPath()) << ",\n" |
| 458 | << " \"model_path\": " << JsonString(JiebaModelPath()) << "\n" |
| 459 | << " }\n" |
| 460 | << " },\n" |
| 461 | << " \"conversion_chain\": [{\n" |
| 462 | << " \"dict\": {\n" |
| 463 | << " \"type\": \"group\",\n" |
| 464 | << " \"dicts\": [{\n" |
| 465 | << " \"type\": \"ocd2\",\n" |
| 466 | << " \"file\": \"STPhrases.ocd2\"\n" |
| 467 | << " }, {\n" |
| 468 | << " \"type\": \"ocd2\",\n" |
| 469 | << " \"file\": \"STCharacters.ocd2\"\n" |
| 470 | << " }]\n" |
| 471 | << " }\n" |
| 472 | << " }, {\n" |
| 473 | << " \"dict\": {\n" |
| 474 | << " \"type\": \"ocd2\",\n" |
| 475 | << " \"file\": \"TWPhrases.ocd2\"\n" |
| 476 | << " }\n" |
| 477 | << " }, {\n" |
| 478 | << " \"dict\": {\n" |
| 479 | << " \"type\": \"ocd2\",\n" |
| 480 | << " \"file\": \"TWVariants.ocd2\"\n" |
| 481 | << " }\n" |
| 482 | << " }]\n" |
| 483 | << "}\n"; |
| 484 | } |
| 485 | |
| 486 | ASSERT_EQ(0, RunCommandUtf8(BuildCommandWithPaths( |
| 487 | configFile, inputFile, outputFile, DictionaryDirectory() + "/", |
| 488 | PluginDirectory()))); |
| 489 | |
| 490 | std::ifstream ifs(outputFile, std::ios::binary); |
| 491 | ASSERT_TRUE(ifs.is_open()); |
| 492 | std::string line; |
| 493 | ASSERT_TRUE(std::getline(ifs, line)); |
| 494 | if (!line.empty() && line.back() == '\r') { |
| 495 | line.pop_back(); |
| 496 | } |
| 497 | EXPECT_EQ("生活著名為正敏的少女", line); |
| 498 | } |
| 499 | |
| 500 | #if defined(_WIN32) || defined(_WIN64) |
| 501 | TEST_F(JiebaPluginIntegrationTest, ConvertFromUnicodePluginPath) { |
| 502 | namespace fs = std::filesystem; |
| 503 | |
| 504 | const fs::path tempRoot = |
| 505 | fs::temp_directory_path() / |
| 506 | fs::u8path("opencc-jieba-中文路径-" + |
| 507 | std::to_string(GetCurrentProcessId())); |
| 508 | const fs::path tempBin = tempRoot / "bin"; |
| 509 | const fs::path tempShareOpencc = tempRoot / "share" / "opencc"; |
| 510 | const fs::path tempJiebaDict = tempShareOpencc / "jieba_dict"; |
| 511 | const fs::path tempPlugins = tempBin / "plugins"; |
| 512 | const fs::path inputFile = tempBin / "input.txt"; |
| 513 | const fs::path outputFile = tempBin / "output.txt"; |
| 514 | const fs::path configFile = tempShareOpencc / "s2twp_jieba.json"; |
| 515 | const fs::path sourceConfig = fs::u8path(ConfigPath("s2twp_jieba")); |
| 516 | const fs::path sourcePluginRoot = |
| 517 | sourceConfig.parent_path().parent_path().parent_path(); |
| 518 | const fs::path sourceJiebaDict = |
| 519 | sourcePluginRoot / "deps" / "cppjieba" / "dict"; |
| 520 | const fs::path sourceMergedDict = fs::u8path(MergedJiebaDictPath()); |
| 521 | const fs::path sourcePluginDir = fs::u8path(PluginDirectory()); |
| 522 | const fs::path sourceOpenccExe = fs::u8path(OpenccCommand()); |
| 523 | const fs::path sourceBinDir = sourceOpenccExe.parent_path(); |
| 524 | |
| 525 | fs::remove_all(tempRoot); |
| 526 | fs::create_directories(tempBin); |
| 527 | fs::create_directories(tempJiebaDict); |
| 528 | fs::create_directories(tempPlugins); |
| 529 | CopyRegularFiles(sourceBinDir, tempBin); |
| 530 | CopyRegularFiles(fs::u8path(DictionaryDirectory()), tempShareOpencc); |
| 531 | fs::copy_file(sourceConfig, configFile, fs::copy_options::overwrite_existing); |
| 532 | CopyRegularFiles(sourceJiebaDict, tempJiebaDict); |
| 533 | fs::copy_file(sourceMergedDict, tempJiebaDict / sourceMergedDict.filename(), |
| 534 | fs::copy_options::overwrite_existing); |
| 535 | CopyRegularFiles(sourcePluginDir, tempPlugins); |
| 536 | |
| 537 | try { |
| 538 | { |
| 539 | std::ofstream ofs(inputFile, std::ios::binary); |
| 540 | ASSERT_TRUE(ofs.is_open()); |
| 541 | ofs << "生活着名为正敏的少女\n"; |
| 542 | } |
| 543 | |
| 544 | const fs::path tempOpenccExe = tempBin / sourceOpenccExe.filename(); |
| 545 | const std::wstring args = |
| 546 | WideFromUtf8(sourceOpenccExe.filename().u8string()) + |
| 547 | L" -i input.txt -o output.txt -c s2twp_jieba.json"; |
| 548 | ASSERT_EQ(0, RunProcessWide(tempOpenccExe, args, tempBin)); |
| 549 | |
| 550 | std::ifstream ifs(outputFile, std::ios::binary); |
| 551 | ASSERT_TRUE(ifs.is_open()); |
| 552 | std::string line; |
| 553 | ASSERT_TRUE(std::getline(ifs, line)); |
| 554 | if (!line.empty() && line.back() == '\r') { |
| 555 | line.pop_back(); |
| 556 | } |
| 557 | EXPECT_EQ("生活著名為正敏的少女", line); |
| 558 | } catch (...) { |
| 559 | fs::remove_all(tempRoot); |
| 560 | throw; |
| 561 | } |
| 562 | |
| 563 | fs::remove_all(tempRoot); |
| 564 | } |
| 565 | #endif |
| 566 | |
| 567 | } // namespace opencc |