-
Notifications
You must be signed in to change notification settings - Fork 8
/
case_test.py
233 lines (202 loc) · 6.27 KB
/
case_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
"""Unit tests for case detection and application."""
import unittest
import case
class CaseTests(unittest.TestCase):
"""Tests of the helper methods for the casing system."""
# Helpers.
def assertEmpty(self, arg):
self.assertTrue(not arg)
def assertSimpleTcEqual(self, token, expected_case):
(tc, pattern) = case.get_tc(token)
self.assertEqual(tc, expected_case)
self.assertEmpty(pattern)
def assertMixedTcEqual(self, token, expected_pattern):
(tc, pattern) = case.get_tc(token)
self.assertEqual(tc, case.TokenCase.MIXED)
self.assertEqual(pattern, expected_pattern)
# Data definitions.
lower = [
"über",
"año",
"cöoperation",
"résumé",
"être",
"očudit",
"pająk",
"fracoð",
"þæt",
"açai",
"ealneġ",
"2pac",
]
upper = [
"ÜBER",
"AÑO",
"CÖOPERATION",
"RÉSUMÉ",
"ÊTRE",
"OČUDIT",
"PAJĄK",
"FRACOÐ",
"ÞÆT",
"AÇAI",
"EALNEĠ",
"2PAC",
]
title = [
"Über",
"Año",
"Cöoperation",
"Résumé",
"Être",
"Očudit",
"Pająk",
"Fracoð",
"Þæt",
"Açai",
"Ealneġ",
"2Pac",
]
mixed = [
(
"SMiLE",
[
case.CharCase.UPPER,
case.CharCase.UPPER,
case.CharCase.LOWER,
case.CharCase.UPPER,
case.CharCase.UPPER,
],
),
(
"m.A.A.d",
[
case.CharCase.LOWER,
case.CharCase.DC,
case.CharCase.UPPER,
case.CharCase.DC,
case.CharCase.UPPER,
case.CharCase.DC,
case.CharCase.LOWER,
],
),
(
"iFoo",
[
case.CharCase.LOWER,
case.CharCase.UPPER,
case.CharCase.LOWER,
case.CharCase.LOWER,
],
),
(
"IJmuiden",
[
case.CharCase.UPPER,
case.CharCase.UPPER,
case.CharCase.LOWER,
case.CharCase.LOWER,
case.CharCase.LOWER,
case.CharCase.LOWER,
case.CharCase.LOWER,
case.CharCase.LOWER,
],
),
(
"tRuEcasIng",
[
case.CharCase.LOWER,
case.CharCase.UPPER,
case.CharCase.LOWER,
case.CharCase.UPPER,
case.CharCase.LOWER,
case.CharCase.LOWER,
case.CharCase.LOWER,
case.CharCase.UPPER,
case.CharCase.LOWER,
case.CharCase.LOWER,
],
),
]
# Basics.
def testLower(self):
for token in self.lower:
self.assertSimpleTcEqual(token, case.TokenCase.LOWER)
def testUpper(self):
for token in self.upper:
self.assertSimpleTcEqual(token, case.TokenCase.UPPER)
def testTitle(self):
for token in self.title:
self.assertSimpleTcEqual(token, case.TokenCase.TITLE)
def testMixed(self):
for (token, expected_pattern) in self.mixed:
self.assertMixedTcEqual(token, expected_pattern)
# Conversions.
def testLowerToUpper(self):
for (ltoken, utoken) in zip(self.lower, self.upper):
self.assertEqual(
case.apply_tc(utoken, case.TokenCase.LOWER), ltoken
)
self.assertEqual(
case.apply_tc(ltoken, case.TokenCase.UPPER), utoken
)
def testLowerToTitle(self):
for (ltoken, ttoken) in zip(self.lower, self.title):
self.assertEqual(
case.apply_tc(ttoken, case.TokenCase.LOWER), ltoken
)
self.assertEqual(
case.apply_tc(ltoken, case.TokenCase.TITLE), ttoken
)
def testLowerToMixed(self):
for (token, pattern) in self.mixed:
self.assertEqual(
case.apply_tc(token.casefold(), case.TokenCase.MIXED, pattern),
token,
)
# Hard cases.
def testFiLigature(self):
finger = "finger"
self.assertEqual(case.apply_tc(finger, case.TokenCase.UPPER), "FINGER")
self.assertEqual(case.apply_tc(finger, case.TokenCase.TITLE), "Finger")
def testEszet(self):
strasse = "Straße"
self.assertEqual(
case.apply_tc(strasse, case.TokenCase.UPPER), "STRASSE"
)
def testUnicodeFonts(self):
quirky_titlecase = ["The", "𝐓𝐡𝐞", "𝕿𝖍𝖊", "𝑻𝒉𝒆", "𝓣𝓱𝓮", "𝕋𝕙𝕖", "𝚃𝚑𝚎"]
for token in quirky_titlecase:
self.assertSimpleTcEqual(token, case.TokenCase.TITLE)
def testNumbers(self):
numbers = ["212", "97000", "123", "١٢٣"]
for token in numbers:
self.assertSimpleTcEqual(token, case.TokenCase.DC)
def testFuzz(self):
fuzz = [
# Wide characters.
"田中さんにあげて下さい" "パーティーへ行かないか",
"和製漢語",
"部落格",
"사회과학원어학연구소",
"찦차를타고온펲시맨과쑛다리똠방각하",
"社會科學院語學研究所",
"울란바토르",
"𠜎𠜱𠝹𠱓𠱸𠲖𠳏",
"新しい日の誕生",
# Emoji and kaomoji.
"ヽ༼ຈل͜ຈ༽ノ ヽ༼ຈل͜ຈ༽ノ",
"(。◕ ∀ ◕。)",
"`ィ( ́∀`∩",
"__ロ(,_,*)",
"・( ̄∀ ̄)・:*:",
"・✿ヾ╲(。◕‿◕。)╱✿・゚",
"(╯°□°)╯( ┻━┻)",
"😍",
"👾 🙇 💁 🙅 🙆 🙋 🙎 🙍",
"🐵 🙈 🙉 ❤️ 💔 💌 💕 💞 💓 💗 💖 💘 💝 💟 💜 💛 💚 💙",
]
for token in fuzz:
self.assertSimpleTcEqual(token, case.TokenCase.DC)
if __name__ == "__main__":
unittest.main()