-
Notifications
You must be signed in to change notification settings - Fork 5
/
matcher_test.go
174 lines (167 loc) · 5.55 KB
/
matcher_test.go
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
package t
import (
"reflect"
"testing"
"golang.org/x/text/language"
)
func TestMatch(t *testing.T) {
type args struct {
supported []string
userAccept []string
}
tests := []struct {
name string
args args
wantTag language.Tag
wantIndex int
wantC language.Confidence
}{
{
name: "exact", // 精确匹配
args: args{supported: []string{"zh-CN"}, userAccept: []string{"zh-CN"}},
wantTag: language.Make("zh-CN"),
wantIndex: 0,
wantC: language.Exact,
},
{
name: "exact2", // 精确匹配
args: args{supported: []string{"en-US", "zh-CN"}, userAccept: []string{"zh-CN"}},
wantTag: language.Make("zh-CN"),
wantIndex: 1,
wantC: language.Exact,
},
{
name: "exact3", // 精确匹配 zh 默认就是指 zh-CN
args: args{supported: []string{"zh"}, userAccept: []string{"zh-CN"}},
wantTag: language.Make("zh-u-rg-cnzzzz"), // 但返回的 tag 和 support / userAccept 都不同(带 -u 后缀指定了地区 rg=region?)
wantIndex: 0, // 使用 supported 的第 0 个,即 zh
wantC: language.Exact,
},
{
name: "exact4", // 精确匹配 zh-Hant 繁体中文默认就是指 中国台湾的繁体中文
args: args{supported: []string{"zh-Hant"}, userAccept: []string{"zh-TW"}},
wantTag: language.Make("zh-Hant-u-rg-twzzzz"),
wantIndex: 0,
wantC: language.Exact,
},
{
name: "exact5", // 精确匹配 zh-HK 中国香港 默认就是 Hant 繁体
args: args{supported: []string{"zh-Hant-HK"}, userAccept: []string{"zh-HK"}},
wantTag: language.Make("zh-Hant-HK"),
wantIndex: 0,
wantC: language.Exact,
},
{
name: "exact6", // 精确匹配 en 默认就是指美国英语
args: args{supported: []string{"en"}, userAccept: []string{"en-US"}},
wantTag: language.Make("en-u-rg-uszzzz"),
wantIndex: 0,
wantC: language.Exact,
},
{
name: "exact7", // 精确匹配
args: args{supported: []string{"en"}, userAccept: []string{"en"}},
wantTag: language.Make("en"),
wantIndex: 0,
wantC: language.Exact,
},
{
name: "high", // 高度匹配 zh-HK中国香港也使用 zh-Hant 繁体中文(未指明地区时其实默认是指中国台湾)
args: args{supported: []string{"zh-Hant"}, userAccept: []string{"zh-HK"}},
wantTag: language.Make("zh-Hant-u-rg-hkzzzz"),
wantIndex: 0,
wantC: language.High,
},
{
name: "high2", // 高度匹配 提供了 zh-CN中国大陆,zh-MO中国澳门, zh-HK中国香港 更匹配同样使用 Hant繁体 的澳门
args: args{supported: []string{"zh-CN", "zh-MO"}, userAccept: []string{"zh-HK"}},
wantTag: language.Make("zh-MO-u-rg-hkzzzz"),
wantIndex: 1,
wantC: language.High,
},
{
name: "high3", // 高度匹配 en 默认指美国英语 en-GB 英国也是英语
args: args{supported: []string{"en"}, userAccept: []string{"en-GB"}},
wantTag: language.Make("en-u-rg-gbzzzz"),
wantIndex: 0,
wantC: language.High,
},
{
name: "low", // 也算找到了最接近的
args: args{supported: []string{"en", "zh"}, userAccept: []string{"zh-HK"}},
wantTag: language.Make("zh-u-rg-hkzzzz"),
wantIndex: 1,
wantC: language.Low,
},
{
name: "low2", // 也算找到了最接近的 zh-CN中国大陆,从 英文,繁体中文(中国台湾) 中选择 繁体中文 最接近
args: args{supported: []string{"en", "zh-TW"}, userAccept: []string{"zh-CN"}},
wantTag: language.Make("zh-TW-u-rg-cnzzzz"),
wantIndex: 1,
wantC: language.Low,
},
{
name: "no", // 不匹配
args: args{supported: []string{"en"}, userAccept: []string{"zh-HK"}},
wantTag: language.Make("en-u-rg-hkzzzz"),
wantIndex: 0,
wantC: language.No,
},
{
name: "no1", // 不匹配
args: args{supported: []string{"en-US"}, userAccept: []string{"zh-HK"}},
wantTag: language.Make("en-US-u-rg-hkzzzz"),
wantIndex: 0,
wantC: language.No,
},
{
name: "no2", // 不匹配
args: args{supported: []string{"en"}, userAccept: []string{"zh"}},
wantTag: language.Make("en"),
wantIndex: 0,
wantC: language.No,
},
{
name: "no3", // 不匹配
args: args{supported: []string{"en"}, userAccept: []string{"zh-Hans"}},
wantTag: language.Make("en"),
wantIndex: 0,
wantC: language.No,
},
{
name: "no4", // 不匹配
args: args{supported: []string{"ja"}, userAccept: []string{"zh-CN"}},
wantTag: language.Make("ja-u-rg-cnzzzz"),
wantIndex: 0,
wantC: language.No,
},
{
name: "no5", // 不匹配
args: args{supported: []string{"ja", "en"}, userAccept: []string{"zh"}},
wantTag: language.Make("ja"), // 不匹配时都是返回第 0 个 supported
wantIndex: 0,
wantC: language.No,
},
{
name: "no6", // 不匹配
args: args{supported: []string{"en", "jp"}, userAccept: []string{"zh"}},
wantTag: language.Make("en"),
wantIndex: 0,
wantC: language.No,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotTag, gotIndex, gotC := Match(tt.args.supported, tt.args.userAccept)
if !reflect.DeepEqual(gotTag, tt.wantTag) {
t.Errorf("Match() gotTag = %v, want %v", gotTag, tt.wantTag)
}
if gotIndex != tt.wantIndex {
t.Errorf("Match() gotIndex = %v, want %v", gotIndex, tt.wantIndex)
}
if !reflect.DeepEqual(gotC, tt.wantC) {
t.Errorf("Match() gotC = %v, want %v", gotC, tt.wantC)
}
})
}
}