| 1 |
package pinyin |
| 2 |
|
| 3 |
import ( |
| 4 |
"reflect" |
| 5 |
"testing" |
| 6 |
) |
| 7 |
|
| 8 |
type pinyinFunc func(string, Args) [][]string |
| 9 |
type testCase struct { |
| 10 |
args Args |
| 11 |
result [][]string |
| 12 |
} |
| 13 |
|
| 14 |
func testPinyin(t *testing.T, s string, d []testCase, f pinyinFunc) { |
| 15 |
for _, tc := range d { |
| 16 |
v := f(s, tc.args) |
| 17 |
if !reflect.DeepEqual(v, tc.result) { |
| 18 |
t.Errorf("Expected %s, got %s", tc.result, v) |
| 19 |
} |
| 20 |
} |
| 21 |
} |
| 22 |
|
| 23 |
func TestPinyin(t *testing.T) { |
| 24 |
hans := "中国人" |
| 25 |
testData := []testCase{ |
| 26 |
|
| 27 |
{ |
| 28 |
Args{Style: Normal}, |
| 29 |
[][]string{ |
| 30 |
{"zhong"}, |
| 31 |
{"guo"}, |
| 32 |
{"ren"}, |
| 33 |
}, |
| 34 |
}, |
| 35 |
|
| 36 |
{ |
| 37 |
NewArgs(), |
| 38 |
[][]string{ |
| 39 |
{"zhong"}, |
| 40 |
{"guo"}, |
| 41 |
{"ren"}, |
| 42 |
}, |
| 43 |
}, |
| 44 |
|
| 45 |
{ |
| 46 |
Args{Style: Normal}, |
| 47 |
[][]string{ |
| 48 |
{"zhong"}, |
| 49 |
{"guo"}, |
| 50 |
{"ren"}, |
| 51 |
}, |
| 52 |
}, |
| 53 |
|
| 54 |
{ |
| 55 |
Args{Style: Tone}, |
| 56 |
[][]string{ |
| 57 |
{"zhōng"}, |
| 58 |
{"guó"}, |
| 59 |
{"rén"}, |
| 60 |
}, |
| 61 |
}, |
| 62 |
|
| 63 |
{ |
| 64 |
Args{Style: Tone2}, |
| 65 |
[][]string{ |
| 66 |
{"zho1ng"}, |
| 67 |
{"guo2"}, |
| 68 |
{"re2n"}, |
| 69 |
}, |
| 70 |
}, |
| 71 |
|
| 72 |
{ |
| 73 |
Args{Style: Tone3}, |
| 74 |
[][]string{ |
| 75 |
{"zhong1"}, |
| 76 |
{"guo2"}, |
| 77 |
{"ren2"}, |
| 78 |
}, |
| 79 |
}, |
| 80 |
|
| 81 |
{ |
| 82 |
Args{Style: Initials}, |
| 83 |
[][]string{ |
| 84 |
{"zh"}, |
| 85 |
{"g"}, |
| 86 |
{"r"}, |
| 87 |
}, |
| 88 |
}, |
| 89 |
|
| 90 |
{ |
| 91 |
Args{Style: FirstLetter}, |
| 92 |
[][]string{ |
| 93 |
{"z"}, |
| 94 |
{"g"}, |
| 95 |
{"r"}, |
| 96 |
}, |
| 97 |
}, |
| 98 |
|
| 99 |
{ |
| 100 |
Args{Style: Finals}, |
| 101 |
[][]string{ |
| 102 |
{"ong"}, |
| 103 |
{"uo"}, |
| 104 |
{"en"}, |
| 105 |
}, |
| 106 |
}, |
| 107 |
|
| 108 |
{ |
| 109 |
Args{Style: FinalsTone}, |
| 110 |
[][]string{ |
| 111 |
{"ōng"}, |
| 112 |
{"uó"}, |
| 113 |
{"én"}, |
| 114 |
}, |
| 115 |
}, |
| 116 |
|
| 117 |
{ |
| 118 |
Args{Style: FinalsTone2}, |
| 119 |
[][]string{ |
| 120 |
{"o1ng"}, |
| 121 |
{"uo2"}, |
| 122 |
{"e2n"}, |
| 123 |
}, |
| 124 |
}, |
| 125 |
|
| 126 |
{ |
| 127 |
Args{Style: FinalsTone3}, |
| 128 |
[][]string{ |
| 129 |
{"ong1"}, |
| 130 |
{"uo2"}, |
| 131 |
{"en2"}, |
| 132 |
}, |
| 133 |
}, |
| 134 |
|
| 135 |
{ |
| 136 |
Args{Heteronym: true}, |
| 137 |
[][]string{ |
| 138 |
{"zhong"}, |
| 139 |
{"guo"}, |
| 140 |
{"ren"}, |
| 141 |
}, |
| 142 |
}, |
| 143 |
} |
| 144 |
|
| 145 |
testPinyin(t, hans, testData, Pinyin) |
| 146 |
|
| 147 |
字的 Heteronym |
| 148 |
hans = "你" |
| 149 |
testData = []testCase{ |
| 150 |
{ |
| 151 |
Args{}, |
| 152 |
[][]string{ |
| 153 |
{"ni"}, |
| 154 |
}, |
| 155 |
}, |
| 156 |
{ |
| 157 |
Args{Heteronym: true}, |
| 158 |
[][]string{ |
| 159 |
{"ni"}, |
| 160 |
}, |
| 161 |
}, |
| 162 |
} |
| 163 |
testPinyin(t, hans, testData, Pinyin) |
| 164 |
} |
| 165 |
|
| 166 |
func TestNoneHans(t *testing.T) { |
| 167 |
s := "abc" |
| 168 |
v := Pinyin(s, NewArgs()) |
| 169 |
value := [][]string{} |
| 170 |
if !reflect.DeepEqual(v, value) { |
| 171 |
t.Errorf("Expected %s, got %s", value, v) |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
func TestLazyPinyin(t *testing.T) { |
| 176 |
s := "中国人" |
| 177 |
v := LazyPinyin(s, Args{}) |
| 178 |
value := []string{"zhong", "guo", "ren"} |
| 179 |
if !reflect.DeepEqual(v, value) { |
| 180 |
t.Errorf("Expected %s, got %s", value, v) |
| 181 |
} |
| 182 |
|
| 183 |
s = "中国人abc" |
| 184 |
v = LazyPinyin(s, Args{}) |
| 185 |
value = []string{"zhong", "guo", "ren"} |
| 186 |
if !reflect.DeepEqual(v, value) { |
| 187 |
t.Errorf("Expected %s, got %s", value, v) |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
func TestSlug(t *testing.T) { |
| 192 |
s := "中国人" |
| 193 |
v := Slug(s, Args{}) |
| 194 |
value := "zhongguoren" |
| 195 |
if v != value { |
| 196 |
t.Errorf("Expected %s, got %s", value, v) |
| 197 |
} |
| 198 |
|
| 199 |
v = Slug(s, Args{Separator: ","}) |
| 200 |
value = "zhong,guo,ren" |
| 201 |
if v != value { |
| 202 |
t.Errorf("Expected %s, got %s", value, v) |
| 203 |
} |
| 204 |
|
| 205 |
a := NewArgs() |
| 206 |
v = Slug(s, a) |
| 207 |
value = "zhong-guo-ren" |
| 208 |
if v != value { |
| 209 |
t.Errorf("Expected %s, got %s", value, v) |
| 210 |
} |
| 211 |
|
| 212 |
s = "中国人abc,,中" |
| 213 |
v = Slug(s, a) |
| 214 |
value = "zhong-guo-ren-zhong" |
| 215 |
if v != value { |
| 216 |
t.Errorf("Expected %s, got %s", value, v) |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
func TestFinal(t *testing.T) { |
| 221 |
value := "an" |
| 222 |
v := final("an") |
| 223 |
if v != value { |
| 224 |
t.Errorf("Expected %s, got %s", value, v) |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
func TestFallback(t *testing.T) { |
| 229 |
hans := "中国人abc" |
| 230 |
testData := []testCase{ |
| 231 |
|
| 232 |
{ |
| 233 |
NewArgs(), |
| 234 |
[][]string{ |
| 235 |
{"zhong"}, |
| 236 |
{"guo"}, |
| 237 |
{"ren"}, |
| 238 |
}, |
| 239 |
}, |
| 240 |
|
| 241 |
{ |
| 242 |
Args{ |
| 243 |
Fallback: func(r rune, a Args) []string { |
| 244 |
return []string{"la"} |
| 245 |
}, |
| 246 |
}, |
| 247 |
[][]string{ |
| 248 |
{"zhong"}, |
| 249 |
{"guo"}, |
| 250 |
{"ren"}, |
| 251 |
{"la"}, |
| 252 |
{"la"}, |
| 253 |
{"la"}, |
| 254 |
}, |
| 255 |
}, |
| 256 |
|
| 257 |
{ |
| 258 |
Args{ |
| 259 |
Heteronym: true, |
| 260 |
Fallback: func(r rune, a Args) []string { |
| 261 |
return []string{"la", "wo"} |
| 262 |
}, |
| 263 |
}, |
| 264 |
[][]string{ |
| 265 |
{"zhong"}, |
| 266 |
{"guo"}, |
| 267 |
{"ren"}, |
| 268 |
{"la", "wo"}, |
| 269 |
{"la", "wo"}, |
| 270 |
{"la", "wo"}, |
| 271 |
}, |
| 272 |
}, |
| 273 |
} |
| 274 |
testPinyin(t, hans, testData, Pinyin) |
| 275 |
} |
| 276 |
|
| 277 |
type testItem struct { |
| 278 |
hans string |
| 279 |
args Args |
| 280 |
result [][]string |
| 281 |
} |
| 282 |
|
| 283 |
func testPinyinUpdate(t *testing.T, d []testItem, f pinyinFunc) { |
| 284 |
for _, tc := range d { |
| 285 |
v := f(tc.hans, tc.args) |
| 286 |
if !reflect.DeepEqual(v, tc.result) { |
| 287 |
t.Errorf("Expected %s, got %s", tc.result, v) |
| 288 |
} |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
func TestUpdated(t *testing.T) { |
| 293 |
testData := []testItem{ |
| 294 |
到声母列表了 |
| 295 |
{"鱼", Args{Style: Tone2}, [][]string{{"yu2"}}}, |
| 296 |
{"鱼", Args{Style: Tone3}, [][]string{{"yu2"}}}, |
| 297 |
{"鱼", Args{Style: Finals}, [][]string{{"v"}}}, |
| 298 |
{"雨", Args{Style: Tone2}, [][]string{{"yu3"}}}, |
| 299 |
{"雨", Args{Style: Tone3}, [][]string{{"yu3"}}}, |
| 300 |
{"雨", Args{Style: Finals}, [][]string{{"v"}}}, |
| 301 |
{"元", Args{Style: Tone2}, [][]string{{"yua2n"}}}, |
| 302 |
{"元", Args{Style: Tone3}, [][]string{{"yuan2"}}}, |
| 303 |
{"元", Args{Style: Finals}, [][]string{{"van"}}}, |
| 304 |
, yi的韵母是i, wu的韵母是u |
| 305 |
{"呀", Args{Style: Initials}, [][]string{{""}}}, |
| 306 |
{"呀", Args{Style: Tone2}, [][]string{{"ya"}}}, |
| 307 |
{"呀", Args{Style: Tone3}, [][]string{{"ya"}}}, |
| 308 |
{"呀", Args{Style: Finals}, [][]string{{"ia"}}}, |
| 309 |
{"无", Args{Style: Initials}, [][]string{{""}}}, |
| 310 |
{"无", Args{Style: Tone2}, [][]string{{"wu2"}}}, |
| 311 |
{"无", Args{Style: Tone3}, [][]string{{"wu2"}}}, |
| 312 |
{"无", Args{Style: Finals}, [][]string{{"u"}}}, |
| 313 |
{"衣", Args{Style: Tone2}, [][]string{{"yi1"}}}, |
| 314 |
{"衣", Args{Style: Tone3}, [][]string{{"yi1"}}}, |
| 315 |
{"衣", Args{Style: Finals}, [][]string{{"i"}}}, |
| 316 |
{"万", Args{Style: Tone2}, [][]string{{"wa4n"}}}, |
| 317 |
{"万", Args{Style: Tone3}, [][]string{{"wan4"}}}, |
| 318 |
{"万", Args{Style: Finals}, [][]string{{"uan"}}}, |
| 319 |
母应该是 v |
| 320 |
{"具", Args{Style: FinalsTone}, [][]string{{"ǜ"}}}, |
| 321 |
{"具", Args{Style: FinalsTone2}, [][]string{{"v4"}}}, |
| 322 |
{"具", Args{Style: FinalsTone3}, [][]string{{"v4"}}}, |
| 323 |
{"具", Args{Style: Finals}, [][]string{{"v"}}}, |
| 324 |
{"取", Args{Style: FinalsTone}, [][]string{{"ǚ"}}}, |
| 325 |
{"取", Args{Style: FinalsTone2}, [][]string{{"v3"}}}, |
| 326 |
{"取", Args{Style: FinalsTone3}, [][]string{{"v3"}}}, |
| 327 |
{"取", Args{Style: Finals}, [][]string{{"v"}}}, |
| 328 |
{"徐", Args{Style: FinalsTone}, [][]string{{"ǘ"}}}, |
| 329 |
{"徐", Args{Style: FinalsTone2}, [][]string{{"v2"}}}, |
| 330 |
{"徐", Args{Style: FinalsTone3}, [][]string{{"v2"}}}, |
| 331 |
{"徐", Args{Style: Finals}, [][]string{{"v"}}}, |
| 332 |
ń |
| 333 |
{"嗯", Args{Style: Normal}, [][]string{{"n"}}}, |
| 334 |
{"嗯", Args{Style: Tone}, [][]string{{"ń"}}}, |
| 335 |
{"嗯", Args{Style: Tone2}, [][]string{{"n2"}}}, |
| 336 |
{"嗯", Args{Style: Tone3}, [][]string{{"n2"}}}, |
| 337 |
{"嗯", Args{Style: Initials}, [][]string{{""}}}, |
| 338 |
{"嗯", Args{Style: FirstLetter}, [][]string{{"n"}}}, |
| 339 |
{"嗯", Args{Style: Finals}, [][]string{{"n"}}}, |
| 340 |
{"嗯", Args{Style: FinalsTone}, [][]string{{"ń"}}}, |
| 341 |
{"嗯", Args{Style: FinalsTone2}, [][]string{{"n2"}}}, |
| 342 |
{"嗯", Args{Style: FinalsTone3}, [][]string{{"n2"}}}, |
| 343 |
3F |
| 344 |
{"呣", Args{Style: Normal}, [][]string{{"m"}}}, |
| 345 |
{"呣", Args{Style: Tone}, [][]string{{"ḿ"}}}, |
| 346 |
{"呣", Args{Style: Tone2}, [][]string{{"m2"}}}, |
| 347 |
{"呣", Args{Style: Tone3}, [][]string{{"m2"}}}, |
| 348 |
{"呣", Args{Style: Initials}, [][]string{{""}}}, |
| 349 |
{"呣", Args{Style: FirstLetter}, [][]string{{"m"}}}, |
| 350 |
{"呣", Args{Style: Finals}, [][]string{{"m"}}}, |
| 351 |
{"呣", Args{Style: FinalsTone}, [][]string{{"ḿ"}}}, |
| 352 |
{"呣", Args{Style: FinalsTone2}, [][]string{{"m2"}}}, |
| 353 |
{"呣", Args{Style: FinalsTone3}, [][]string{{"m2"}}}, |
| 354 |
除 0 |
| 355 |
{"啊", Args{Style: Tone2}, [][]string{{"a"}}}, |
| 356 |
{"啊", Args{Style: Tone3}, [][]string{{"a"}}}, |
| 357 |
{"侵略", Args{Style: Tone2}, [][]string{{"qi1n"}, {"lve4"}}}, |
| 358 |
{"侵略", Args{Style: FinalsTone2}, [][]string{{"i1n"}, {"ve4"}}}, |
| 359 |
{"侵略", Args{Style: FinalsTone3}, [][]string{{"in1"}, {"ve4"}}}, |
| 360 |
} |
| 361 |
testPinyinUpdate(t, testData, Pinyin) |
| 362 |
} |
| 363 |
|
| 364 |
func TestConvert(t *testing.T) { |
| 365 |
s := "中国人" |
| 366 |
v := Convert(s, nil) |
| 367 |
value := [][]string{{"zhong"}, {"guo"}, {"ren"}} |
| 368 |
if !reflect.DeepEqual(v, value) { |
| 369 |
t.Errorf("Expected %s, got %s", value, v) |
| 370 |
} |
| 371 |
|
| 372 |
a := NewArgs() |
| 373 |
v = Convert(s, &a) |
| 374 |
if !reflect.DeepEqual(v, value) { |
| 375 |
t.Errorf("Expected %s, got %s", value, v) |
| 376 |
} |
| 377 |
} |
| 378 |
|
| 379 |
func TestLazyConvert(t *testing.T) { |
| 380 |
s := "中国人" |
| 381 |
v := LazyConvert(s, nil) |
| 382 |
value := []string{"zhong", "guo", "ren"} |
| 383 |
if !reflect.DeepEqual(v, value) { |
| 384 |
t.Errorf("Expected %s, got %s", value, v) |
| 385 |
} |
| 386 |
|
| 387 |
a := NewArgs() |
| 388 |
v = LazyConvert(s, &a) |
| 389 |
if !reflect.DeepEqual(v, value) { |
| 390 |
t.Errorf("Expected %s, got %s", value, v) |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
func TestPinyin_fallback_issue_35(t *testing.T) { |
| 395 |
a := NewArgs() |
| 396 |
a.Separator = "" |
| 397 |
a.Style = FirstLetter |
| 398 |
a.Fallback = func(r rune, a Args) []string { |
| 399 |
return []string{string(r)} |
| 400 |
} |
| 401 |
var s = "重。,a庆" |
| 402 |
v := Pinyin(s, a) |
| 403 |
expect := [][]string{{"z"}, {"。"}, {","}, {"a"}, {"q"}} |
| 404 |
if !reflect.DeepEqual(v, expect) { |
| 405 |
t.Errorf("Expected %s, got %s", expect, v) |
| 406 |
} |
| 407 |
} |