-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstringCompare_test.go
307 lines (290 loc) · 6.42 KB
/
stringCompare_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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package jsonpath
import (
"testing"
"golang.org/x/text/language"
)
func TestStandardCompareStrings(t *testing.T) {
tests := []struct {
name string
a string
operator string
b string
want bool
}{
// 基本比较测试
{"equal strings", "hello", "==", "hello", true},
{"not equal strings", "hello", "!=", "world", true},
{"less than", "apple", "<", "banana", true},
{"less than or equal", "apple", "<=", "apple", true},
{"greater than", "banana", ">", "apple", true},
{"greater than or equal", "banana", ">=", "banana", true},
// Unicode 字符测试
{"unicode equal", "你好", "==", "你好", true},
{"unicode not equal", "你好", "!=", "世界", true},
{"unicode less than", "一", "<", "二", true},
{"unicode greater than", "二", ">", "一", true},
// 特殊字符测试
{"special chars equal", "hello!", "==", "hello!", true},
{"special chars not equal", "hello!", "!=", "hello?", true},
{"special chars less than", "hello!", "<", "hello?", true},
{"special chars greater than", "hello?", ">", "hello!", true},
// 空字符串测试
{"empty string equal", "", "==", "", true},
{"empty string not equal", "", "!=", "hello", true},
{"empty string less than", "", "<", "hello", true},
{"empty string greater than", "hello", ">", "", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := standardCompareStrings(tt.a, tt.operator, tt.b)
if got != tt.want {
t.Errorf("standardCompareStrings(%q, %q, %q) = %v, want %v",
tt.a, tt.operator, tt.b, got, tt.want)
}
})
}
}
func TestStringComparerWithOptions(t *testing.T) {
tests := []struct {
name string
options stringCompareOptions
a string
operator string
b string
want bool
}{
// 大小写不敏感测试
{
name: "case insensitive equal",
options: stringCompareOptions{
caseSensitive: false,
locale: language.English,
},
a: "Hello",
operator: "==",
b: "hello",
want: true,
},
{
name: "case insensitive not equal",
options: stringCompareOptions{
caseSensitive: false,
locale: language.English,
},
a: "Hello",
operator: "!=",
b: "world",
want: true,
},
// 不同语言环境测试
{
name: "german locale",
options: stringCompareOptions{
caseSensitive: true,
locale: language.German,
},
a: "ä",
operator: "<",
b: "b",
want: true,
},
{
name: "chinese locale",
options: stringCompareOptions{
caseSensitive: true,
locale: language.Chinese,
},
a: "你",
operator: "<",
b: "我",
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
sc := newStringComparer(tt.options)
var got bool
switch tt.operator {
case "==":
got = sc.equals(tt.a, tt.b)
case "!=":
got = !sc.equals(tt.a, tt.b)
case "<":
got = sc.lessThan(tt.a, tt.b)
case "<=":
got = sc.lessThanOrEqual(tt.a, tt.b)
case ">":
got = sc.greaterThan(tt.a, tt.b)
case ">=":
got = sc.greaterThanOrEqual(tt.a, tt.b)
}
if got != tt.want {
t.Errorf("stringComparer.compare(%q, %q, %q) with options %+v = %v, want %v",
tt.a, tt.operator, tt.b, tt.options, got, tt.want)
}
})
}
}
func TestCompareStringsFunction(t *testing.T) {
tests := []struct {
name string
a string
operator string
b string
want bool
}{
// 相等性比较
{
name: "equal strings",
a: "hello",
operator: "==",
b: "hello",
want: true,
},
{
name: "not equal strings",
a: "hello",
operator: "!=",
b: "world",
want: true,
},
{
name: "equal empty strings",
a: "",
operator: "==",
b: "",
want: true,
},
{
name: "empty and non-empty strings",
a: "",
operator: "!=",
b: "hello",
want: true,
},
// 大小比较
{
name: "less than",
a: "apple",
operator: "<",
b: "banana",
want: true,
},
{
name: "less than or equal (equal)",
a: "apple",
operator: "<=",
b: "apple",
want: true,
},
{
name: "less than or equal (less)",
a: "apple",
operator: "<=",
b: "banana",
want: true,
},
{
name: "greater than",
a: "banana",
operator: ">",
b: "apple",
want: true,
},
{
name: "greater than or equal (equal)",
a: "banana",
operator: ">=",
b: "banana",
want: true,
},
{
name: "greater than or equal (greater)",
a: "banana",
operator: ">=",
b: "apple",
want: true,
},
// 特殊字符
{
name: "strings with spaces",
a: "hello world",
operator: "==",
b: "hello world",
want: true,
},
{
name: "strings with newlines",
a: "hello\nworld",
operator: "==",
b: "hello\nworld",
want: true,
},
{
name: "strings with tabs",
a: "hello\tworld",
operator: "==",
b: "hello\tworld",
want: true,
},
// Unicode 字符
{
name: "unicode strings equal",
a: "你好",
operator: "==",
b: "你好",
want: true,
},
{
name: "unicode strings not equal",
a: "你好",
operator: "!=",
b: "世界",
want: true,
},
{
name: "unicode strings comparison",
a: "a",
operator: "<",
b: "你",
want: true, // ASCII 字符小于 Unicode 字符
},
// 大小写敏感性
{
name: "case sensitive equal",
a: "Hello",
operator: "==",
b: "hello",
want: false,
},
{
name: "case sensitive less than",
a: "Hello",
operator: "<",
b: "hello",
want: true, // 大写字母的 ASCII 值小于小写字母
},
// 无效操作符
{
name: "invalid operator",
a: "hello",
operator: "invalid",
b: "world",
want: false,
},
{
name: "empty operator",
a: "hello",
operator: "",
b: "world",
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := compareStrings(tt.a, tt.operator, tt.b); got != tt.want {
t.Errorf("compareStrings() = %v, want %v", got, tt.want)
}
})
}
}