| 1 | /* |
| 2 | * Open Chinese Convert |
| 3 | * |
| 4 | * Copyright 2010-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 "ConfigTestBase.hpp" |
| 20 | #include "ConversionInspection.hpp" |
| 21 | #include "Segments.hpp" |
| 22 | #include "SimpleConverter.hpp" |
| 23 | #include "TestUtilsUTF8.hpp" |
| 24 | |
| 25 | #include <string_view> |
| 26 | |
| 27 | namespace opencc { |
| 28 | |
| 29 | // ---- Segments::ToVector tests ---- |
| 30 | |
| 31 | class SegmentsToVectorTest : public ::testing::Test {}; |
| 32 | |
| 33 | TEST_F(SegmentsToVectorTest, ManagedSegments) { |
| 34 | Segments segs; |
| 35 | segs.AddSegment(std::string("hello")); |
| 36 | segs.AddSegment(std::string("world")); |
| 37 | const auto vec = segs.ToVector(); |
| 38 | ASSERT_EQ(2u, vec.size()); |
| 39 | EXPECT_EQ("hello", vec[0]); |
| 40 | EXPECT_EQ("world", vec[1]); |
| 41 | } |
| 42 | |
| 43 | TEST_F(SegmentsToVectorTest, UnmanagedSegments) { |
| 44 | // Unmanaged segments use const char* directly |
| 45 | const char* a = "foo"; |
| 46 | const char* b = "bar"; |
| 47 | Segments segs; |
| 48 | segs.AddSegment(a); |
| 49 | segs.AddSegment(b); |
| 50 | const auto vec = segs.ToVector(); |
| 51 | ASSERT_EQ(2u, vec.size()); |
| 52 | EXPECT_EQ("foo", vec[0]); |
| 53 | EXPECT_EQ("bar", vec[1]); |
| 54 | } |
| 55 | |
| 56 | TEST_F(SegmentsToVectorTest, MixedSegments) { |
| 57 | const char* a = "unmanaged"; |
| 58 | Segments segs; |
| 59 | segs.AddSegment(a); |
| 60 | segs.AddSegment(std::string("managed")); |
| 61 | segs.AddSegment(a); |
| 62 | const auto vec = segs.ToVector(); |
| 63 | ASSERT_EQ(3u, vec.size()); |
| 64 | EXPECT_EQ("unmanaged", vec[0]); |
| 65 | EXPECT_EQ("managed", vec[1]); |
| 66 | EXPECT_EQ("unmanaged", vec[2]); |
| 67 | } |
| 68 | |
| 69 | TEST_F(SegmentsToVectorTest, EmptySegments) { |
| 70 | Segments segs; |
| 71 | const auto vec = segs.ToVector(); |
| 72 | EXPECT_TRUE(vec.empty()); |
| 73 | } |
| 74 | |
| 75 | // ---- SimpleConverter::Inspect / Converter::Inspect tests ---- |
| 76 | |
| 77 | class ConversionInspectionTest : public ConfigTestBase { |
| 78 | protected: |
| 79 | ConversionInspectionTest() {} |
| 80 | }; |
| 81 | |
| 82 | TEST_F(ConversionInspectionTest, OutputMatchesConvert) { |
| 83 | const SimpleConverter converter(CONFIG_TEST_JSON_PATH); |
| 84 | const std::string text = utf8("燕燕于飞差池其羽之子于归远送于野"); |
| 85 | const ConversionInspectionResult result = converter.Inspect(text); |
| 86 | const std::string converted = converter.Convert(std::string_view(text)); |
| 87 | EXPECT_EQ(converted, result.output); |
| 88 | } |
| 89 | |
| 90 | TEST_F(ConversionInspectionTest, InputPreserved) { |
| 91 | const SimpleConverter converter(CONFIG_TEST_JSON_PATH); |
| 92 | const std::string text = utf8("燕燕于飞差池其羽之子于归远送于野"); |
| 93 | const ConversionInspectionResult result = converter.Inspect(text); |
| 94 | EXPECT_EQ(text, result.input); |
| 95 | } |
| 96 | |
| 97 | TEST_F(ConversionInspectionTest, SegmentsNonEmpty) { |
| 98 | const SimpleConverter converter(CONFIG_TEST_JSON_PATH); |
| 99 | const std::string text = utf8("燕燕于飞差池其羽之子于归远送于野"); |
| 100 | const ConversionInspectionResult result = converter.Inspect(text); |
| 101 | EXPECT_FALSE(result.segments.empty()); |
| 102 | // The segments concatenated should equal the original input |
| 103 | std::string joined; |
| 104 | for (const auto& s : result.segments) { |
| 105 | joined += s; |
| 106 | } |
| 107 | EXPECT_EQ(text, joined); |
| 108 | } |
| 109 | |
| 110 | TEST_F(ConversionInspectionTest, StagesCountMatchesChain) { |
| 111 | // The config_test.json has one conversion chain entry -> 1 stage |
| 112 | const SimpleConverter converter(CONFIG_TEST_JSON_PATH); |
| 113 | const std::string text = utf8("燕燕于飞差池其羽之子于归远送于野"); |
| 114 | const ConversionInspectionResult result = converter.Inspect(text); |
| 115 | EXPECT_EQ(1u, result.stages.size()); |
| 116 | } |
| 117 | |
| 118 | TEST_F(ConversionInspectionTest, StageIndexStartsAtOne) { |
| 119 | const SimpleConverter converter(CONFIG_TEST_JSON_PATH); |
| 120 | const std::string text = utf8("燕燕于飞差池其羽之子于归远送于野"); |
| 121 | const ConversionInspectionResult result = converter.Inspect(text); |
| 122 | ASSERT_FALSE(result.stages.empty()); |
| 123 | EXPECT_EQ(1u, result.stages[0].index); |
| 124 | } |
| 125 | |
| 126 | TEST_F(ConversionInspectionTest, EmptyInput) { |
| 127 | const SimpleConverter converter(CONFIG_TEST_JSON_PATH); |
| 128 | const ConversionInspectionResult result = converter.Inspect(""); |
| 129 | EXPECT_EQ("", result.input); |
| 130 | EXPECT_EQ("", result.output); |
| 131 | // segments should be empty or contain one empty segment |
| 132 | std::string joined; |
| 133 | for (const auto& s : result.segments) { |
| 134 | joined += s; |
| 135 | } |
| 136 | EXPECT_EQ("", joined); |
| 137 | } |
| 138 | |
| 139 | } // namespace opencc |