Open Chinese Convert: TextDictTestBase.hpp Source File

Open Chinese Convert 1.0.6 A project for conversion between Traditional and Simplified Chinese

TextDictTestBase.hpp
1 /*
2 * Open Chinese Convert
3 *
4 * Copyright 2015 BYVoid <byvoid@byvoid.com>
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 #pragma once
20
21 #include "Lexicon.hpp"
22 #include "TextDict.hpp"
23 #include "TestUtils.hpp"
24
25 namespace opencc {
26
27 class TextDictTestBase : public ::testing::Test {
28 protected:
29 TextDictTestBase() : textDict(CreateTextDictForText()){};
30
31 TextDictPtr CreateTextDictForText() const {
32 LexiconPtr lexicon(new Lexicon);
33 lexicon->Add(DictEntryFactory::New("BYVoid", "byv"));
34 lexicon->Add(DictEntryFactory::New("zigzagzig", "zag"));
35 lexicon->Add(DictEntryFactory::New(utf8("積羽沉舟"), utf8("羣輕折軸")));
36 lexicon->Add(DictEntryFactory::New(utf8("清"), "Tsing"));
37 lexicon->Add(DictEntryFactory::New(utf8("清華"), "Tsinghua"));
38 lexicon->Add(DictEntryFactory::New(utf8("清華大學"), "TsinghuaUniversity"));
39 lexicon->Sort();
40 return TextDictPtr(new TextDict(lexicon));
41 }
42
43 DictPtr CreateDictForCharacters() const {
44 LexiconPtr lexicon(new Lexicon);
45 lexicon->Add(DictEntryFactory::New(utf8("后"),
46 vector<string>{utf8("后"), utf8("後")}));
47 lexicon->Add(DictEntryFactory::New(utf8("发"),
48 vector<string>{utf8("發"), utf8("髮")}));
49 lexicon->Add(DictEntryFactory::New(
50 utf8("干"), vector<string>{utf8("幹"), utf8("乾"), utf8("干")}));
51 lexicon->Add(DictEntryFactory::New(utf8("里"),
52 vector<string>{utf8("裏"), utf8("里")}));
53 lexicon->Sort();
54 return TextDictPtr(new TextDict(lexicon));
55 }
56
57 DictPtr CreateDictForPhrases() const {
58 LexiconPtr lexicon(new Lexicon);
59 lexicon->Add(DictEntryFactory::New(utf8("太后"), utf8("太后")));
60 lexicon->Add(DictEntryFactory::New(utf8("头发"), utf8("頭髮")));
61 lexicon->Add(DictEntryFactory::New(utf8("干燥"), utf8("乾燥")));
62 lexicon->Add(DictEntryFactory::New(utf8("鼠标"), utf8("鼠標")));
63 lexicon->Sort();
64 return TextDictPtr(new TextDict(lexicon));
65 }
66
67 DictPtr CreateDictForTaiwanVariants() const {
68 LexiconPtr lexicon(new Lexicon);
69 lexicon->Add(DictEntryFactory::New(utf8("裏"), utf8("裡")));
70 TextDictPtr textDict(new TextDict(lexicon));
71 return textDict;
72 }
73
74 DictPtr CreateTaiwanPhraseDict() const {
75 LexiconPtr lexicon(new Lexicon);
76 lexicon->Add(DictEntryFactory::New(utf8("鼠标"), utf8("滑鼠")));
77 lexicon->Add(DictEntryFactory::New(utf8("服务器"), utf8("伺服器")));
78 lexicon->Add(DictEntryFactory::New(utf8("克罗地亚"), utf8("克羅埃西亞")));
79 lexicon->Sort();
80 return TextDictPtr(new TextDict(lexicon));
81 }
82
83 void TestDict(const DictPtr dict) const {
84 Optional<const DictEntry*> entry = dict->MatchPrefix("BYVoid");
85 EXPECT_TRUE(!entry.IsNull());
86 EXPECT_EQ(utf8("BYVoid"), entry.Get()->Key());
87 EXPECT_EQ(utf8("byv"), entry.Get()->GetDefault());
88
89 entry = dict->MatchPrefix("BYVoid123");
90 EXPECT_TRUE(!entry.IsNull());
91 EXPECT_EQ(utf8("BYVoid"), entry.Get()->Key());
92 EXPECT_EQ(utf8("byv"), entry.Get()->GetDefault());
93
94 entry = dict->MatchPrefix(utf8("積羽沉舟"));
95 EXPECT_TRUE(!entry.IsNull());
96 EXPECT_EQ(utf8("積羽沉舟"), entry.Get()->Key());
97 EXPECT_EQ(utf8("羣輕折軸"), entry.Get()->GetDefault());
98
99 entry = dict->MatchPrefix("Unknown");
100 EXPECT_TRUE(entry.IsNull());
101
102 const vector<const DictEntry*> matches =
103 dict->MatchAllPrefixes(utf8("清華大學計算機系"));
104 EXPECT_EQ(3, matches.size());
105 EXPECT_EQ(utf8("清華大學"), matches.at(0)->Key());
106 EXPECT_EQ(utf8("TsinghuaUniversity"), matches.at(0)->GetDefault());
107 EXPECT_EQ(utf8("清華"), matches.at(1)->Key());
108 EXPECT_EQ(utf8("Tsinghua"), matches.at(1)->GetDefault());
109 EXPECT_EQ(utf8("清"), matches.at(2)->Key());
110 EXPECT_EQ(utf8("Tsing"), matches.at(2)->GetDefault());
111 }
112
113 const TextDictPtr textDict;
114 };
115
116 } // namespace opencc
opencc::TextDict
Text dictionary.
Definition: TextDict.hpp:29
opencc::Optional::Get
const T & Get() const
Returns the containing data of the instance.
Definition: Optional.hpp:41
opencc::TextDictTestBase
Definition: TextDictTestBase.hpp:27
opencc::Lexicon
Storage of all entries.
Definition: Lexicon.hpp:29
opencc::Optional::IsNull
bool IsNull() const
Returns true if the instance is null.
Definition: Optional.hpp:36
opencc::Optional
A class that wraps type T into a nullable type.
Definition: Optional.hpp:26