Debian Codegraph @

test/BazelOpenccTest.cpp

raw symbols SCIP
1 /*
2 * Open Chinese Convert
3 *
4 * Copyright 2024-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 "opencc.h"
20 #include "gtest/gtest.h"
21
22 #include <string_view>
23
24 #ifdef BAZEL
25 #include "tools/cpp/runfiles/runfiles.h"
26 using bazel::tools::cpp::runfiles::Runfiles;
27 #endif
28
29 #include "test/PortableUtil.hpp"
30
31 namespace opencc {
32
33 class BazelOpenccTest : public ::testing::Test {
34 protected:
35 static void SetUpTestSuite() {
36 #ifdef BAZEL
37 runfiles_.reset(Runfiles::CreateForTest());
38 const std::string configFile =
39 runfiles_->Rlocation("_main/data/config/s2t.json");
40 configDir_ = configFile.substr(0, configFile.find_last_of("/\\"));
41 const std::string dictFile =
42 runfiles_->Rlocation("_main/data/dictionary/STCharacters.ocd2");
43 dictDir_ = dictFile.substr(0, dictFile.find_last_of("/\\"));
44 #endif
45 }
46
47 void SetUp() override {
48 // opencc_open() has no paths argument; set cwd to dictDir so
49 // LoadDictWithPaths finds .ocd2 files, and OPENCC_DATA_DIR to configDir
50 // so FindConfigFile finds .json files.
51 const char* prev = std::getenv("OPENCC_DATA_DIR");
52 savedOpenccDataDir_ = prev ? prev : "";
53 portable_chdir(dictDir_.c_str());
54 portable_putenv("OPENCC_DATA_DIR", configDir_.c_str());
55 }
56
57 void TearDown() override {
58 if (savedOpenccDataDir_.empty()) {
59 portable_unsetenv("OPENCC_DATA_DIR");
60 } else {
61 portable_putenv("OPENCC_DATA_DIR", savedOpenccDataDir_.c_str());
62 }
63 }
64
65 static std::unique_ptr<Runfiles> runfiles_;
66 static std::string configDir_;
67 static std::string dictDir_;
68 std::string savedOpenccDataDir_;
69 };
70
71 std::unique_ptr<Runfiles> BazelOpenccTest::runfiles_;
72 std::string BazelOpenccTest::configDir_;
73 std::string BazelOpenccTest::dictDir_;
74
75 TEST_F(BazelOpenccTest, SimpleConverter_s2t) {
76 SimpleConverter converter(OPENCC_DEFAULT_CONFIG_SIMP_TO_TRAD,
77 {configDir_, dictDir_});
78 EXPECT_EQ(converter.Convert(std::string_view("简化字测试")), "簡化字測試");
79 }
80
81 TEST_F(BazelOpenccTest, SimpleConverter_t2s) {
82 SimpleConverter converter(OPENCC_DEFAULT_CONFIG_TRAD_TO_SIMP,
83 {configDir_, dictDir_});
84 EXPECT_EQ(converter.Convert(std::string_view("簡化字測試")), "简化字测试");
85 EXPECT_EQ(converter.Convert(std::string_view("㑮")), "𫝈");
86 }
87
88 TEST_F(BazelOpenccTest, CInterface_s2t) {
89 std::string text = "简化字测试";
90 opencc_t od = opencc_open(OPENCC_DEFAULT_CONFIG_SIMP_TO_TRAD);
91 ASSERT_NE(od, reinterpret_cast<opencc_t>(-1));
92 char* converted = opencc_convert_utf8(od, text.c_str(), (size_t)-1);
93 EXPECT_STREQ("簡化字測試", converted);
94 opencc_convert_utf8_free(converted);
95 EXPECT_EQ(0, opencc_close(od));
96 }
97
98 TEST_F(BazelOpenccTest, CInterface_t2s) {
99 std::string text = "簡化字測試";
100 opencc_t od = opencc_open(OPENCC_DEFAULT_CONFIG_TRAD_TO_SIMP);
101 ASSERT_NE(od, reinterpret_cast<opencc_t>(-1));
102 char* converted = opencc_convert_utf8(od, text.c_str(), (size_t)-1);
103 EXPECT_STREQ("简化字测试", converted);
104 opencc_convert_utf8_free(converted);
105
106 converted = opencc_convert_utf8(od, "㑮", (size_t)-1);
107 EXPECT_STREQ("𫝈", converted);
108 opencc_convert_utf8_free(converted);
109 EXPECT_EQ(0, opencc_close(od));
110 }
111
112 } // namespace opencc