-
Notifications
You must be signed in to change notification settings - Fork 415
Expand file tree
/
Copy pathstringutil_test.go
More file actions
553 lines (521 loc) · 11.3 KB
/
stringutil_test.go
File metadata and controls
553 lines (521 loc) · 11.3 KB
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
//go:build !integration
package stringutil
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestTruncate(t *testing.T) {
tests := []struct {
name string
s string
maxLen int
expected string
}{
{
name: "string shorter than max length",
s: "hello",
maxLen: 10,
expected: "hello",
},
{
name: "string equal to max length",
s: "hello",
maxLen: 5,
expected: "hello",
},
{
name: "string longer than max length",
s: "hello world",
maxLen: 8,
expected: "hello...",
},
{
name: "max length 3",
s: "hello",
maxLen: 3,
expected: "hel",
},
{
name: "max length 2",
s: "hello",
maxLen: 2,
expected: "he",
},
{
name: "max length 1",
s: "hello",
maxLen: 1,
expected: "h",
},
{
name: "max length zero",
s: "hello",
maxLen: 0,
expected: "",
},
{
name: "exactly three chars",
s: "abc",
maxLen: 3,
expected: "abc",
},
{
name: "four chars exact length",
s: "abcd",
maxLen: 4,
expected: "abcd",
},
{
name: "five chars truncated to four",
s: "abcde",
maxLen: 4,
expected: "a...",
},
{
name: "empty string",
s: "",
maxLen: 5,
expected: "",
},
{
name: "long string truncated",
s: "this is a very long string that needs to be truncated",
maxLen: 20,
expected: "this is a very lo...",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := Truncate(tt.s, tt.maxLen)
assert.Equal(t, tt.expected, result, "Truncate(%q, %d) should return expected output", tt.s, tt.maxLen)
})
}
}
func TestNormalizeWhitespace(t *testing.T) {
tests := []struct {
name string
content string
expected string
}{
{
name: "no trailing whitespace",
content: "hello\nworld",
expected: "hello\nworld\n",
},
{
name: "trailing spaces on lines",
content: "hello \nworld ",
expected: "hello\nworld\n",
},
{
name: "trailing tabs on lines",
content: "hello\t\nworld\t",
expected: "hello\nworld\n",
},
{
name: "multiple trailing newlines",
content: "hello\nworld\n\n\n",
expected: "hello\nworld\n",
},
{
name: "empty string",
content: "",
expected: "",
},
{
name: "single newline",
content: "\n",
expected: "",
},
{
name: "mixed whitespace",
content: "hello \t\nworld \t \n\n",
expected: "hello\nworld\n",
},
{
name: "content with no newline",
content: "hello world",
expected: "hello world\n",
},
{
name: "content already normalized",
content: "hello\nworld\n",
expected: "hello\nworld\n",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := NormalizeWhitespace(tt.content)
assert.Equal(t, tt.expected, result, "NormalizeWhitespace(%q) should normalize trailing whitespace and newlines", tt.content)
})
}
}
func BenchmarkTruncate(b *testing.B) {
s := "this is a very long string that needs to be truncated for testing purposes"
for b.Loop() {
Truncate(s, 30)
}
}
func BenchmarkNormalizeWhitespace(b *testing.B) {
content := "line1 \nline2\t\nline3 \t\nline4\n\n"
for b.Loop() {
NormalizeWhitespace(content)
}
}
// Additional edge case tests
func TestTruncate_Unicode(t *testing.T) {
tests := []struct {
name string
s string
maxLen int
expected string
}{
{
name: "emoji truncation",
s: "Hello 👋 World 🌍",
maxLen: 10,
expected: "Hello \xf0...", // Truncates in middle of emoji byte sequence
},
{
name: "unicode characters",
s: "Café España México",
maxLen: 12,
expected: "Café Esp...", // Actual behavior
},
{
name: "mixed unicode and ascii",
s: "Test-测试-テスト",
maxLen: 8,
expected: "Test-...",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := Truncate(tt.s, tt.maxLen)
assert.Equal(t, tt.expected, result, "Truncate(%q, %d) should handle unicode input as expected", tt.s, tt.maxLen)
})
}
}
func TestNormalizeWhitespace_OnlyWhitespace(t *testing.T) {
tests := []struct {
name string
content string
expected string
}{
{
name: "only spaces",
content: " ",
expected: "", // After trimming trailing spaces and newlines, becomes empty
},
{
name: "only tabs",
content: "\t\t\t",
expected: "", // After trimming trailing tabs and newlines, becomes empty
},
{
name: "mixed spaces and tabs",
content: " \t \t",
expected: "", // After trimming, becomes empty
},
{
name: "only newlines",
content: "\n\n\n",
expected: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := NormalizeWhitespace(tt.content)
assert.Equal(t, tt.expected, result, "NormalizeWhitespace(%q) should handle whitespace-only input", tt.content)
})
}
}
func TestNormalizeWhitespace_ManyLines(t *testing.T) {
// Test with many lines
lines := make([]string, 100)
for i := range 100 {
lines[i] = "line with trailing spaces "
}
var content strings.Builder
for _, line := range lines {
content.WriteString(line + "\n")
}
result := NormalizeWhitespace(content.String())
// Check that all trailing spaces are removed
expectedLines := make([]string, 100)
for i := range 100 {
expectedLines[i] = "line with trailing spaces"
}
var expected strings.Builder
for _, line := range expectedLines {
expected.WriteString(line + "\n")
}
assert.Equal(t, expected.String(), result, "NormalizeWhitespace should remove trailing spaces from all lines in large inputs")
}
func TestNormalizeWhitespace_PreservesContent(t *testing.T) {
// Ensure that non-trailing whitespace is preserved
content := "line1 middle spaces\nline2\t\tmiddle\t\ttabs\n"
result := NormalizeWhitespace(content)
assert.Contains(t, result, "middle spaces", "NormalizeWhitespace should preserve non-trailing spaces inside lines")
assert.Contains(t, result, "middle\t\ttabs", "NormalizeWhitespace should preserve non-trailing tabs inside lines")
}
func BenchmarkTruncate_Short(b *testing.B) {
s := "short"
for b.Loop() {
Truncate(s, 10)
}
}
func BenchmarkTruncate_Long(b *testing.B) {
s := "this is a very very very very very long string that definitely needs truncation"
for b.Loop() {
Truncate(s, 20)
}
}
func BenchmarkNormalizeWhitespace_NoChange(b *testing.B) {
content := "line1\nline2\nline3\n"
for b.Loop() {
NormalizeWhitespace(content)
}
}
func BenchmarkNormalizeWhitespace_ManyChanges(b *testing.B) {
content := "line1 \t \nline2 \t \nline3 \t \n\n\n"
for b.Loop() {
NormalizeWhitespace(content)
}
}
func TestParseVersionValue(t *testing.T) {
tests := []struct {
name string
version any
expected string
}{
// String versions
{
name: "string version",
version: "v1.2.3",
expected: "v1.2.3",
},
{
name: "numeric string",
version: "123",
expected: "123",
},
{
name: "empty string",
version: "",
expected: "",
},
// Integer versions
{
name: "int version",
version: 42,
expected: "42",
},
{
name: "int64 version",
version: int64(100),
expected: "100",
},
{
name: "uint64 version",
version: uint64(999),
expected: "999",
},
// Float versions
{
name: "float64 simple",
version: float64(1.5),
expected: "1.5",
},
{
name: "float64 whole number",
version: float64(2.0),
expected: "2",
},
{
name: "float64 with precision",
version: float64(1.234),
expected: "1.234",
},
// Unsupported types
{
name: "nil",
version: nil,
expected: "",
},
{
name: "bool",
version: true,
expected: "",
},
{
name: "slice",
version: []string{"1", "2"},
expected: "",
},
{
name: "map",
version: map[string]string{"version": "1.0"},
expected: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := ParseVersionValue(tt.version)
assert.Equal(t, tt.expected, result, "ParseVersionValue(%v) should return normalized string representation", tt.version)
})
}
}
func TestFormatList(t *testing.T) {
tests := []struct {
name string
items []string
expected string
}{
{
name: "empty slice",
items: []string{},
expected: "",
},
{
name: "single item",
items: []string{"a"},
expected: "a",
},
{
name: "two items",
items: []string{"a", "b"},
expected: "a and b",
},
{
name: "three items",
items: []string{"a", "b", "c"},
expected: "a, b, and c",
},
{
name: "four items",
items: []string{"a", "b", "c", "d"},
expected: "a, b, c, and d",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := FormatList(tt.items)
assert.Equal(t, tt.expected, result, "FormatList(%v) should return natural-language list formatting", tt.items)
})
}
}
func TestNormalizeLeadingWhitespace(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{
name: "removes consistent leading spaces",
input: " Line 1\n Line 2\n Line 3",
expected: "Line 1\nLine 2\nLine 3",
},
{
name: "handles no leading spaces",
input: "Line 1\nLine 2",
expected: "Line 1\nLine 2",
},
{
name: "preserves relative indentation",
input: " Line 1\n Indented Line 2\n Line 3",
expected: "Line 1\n Indented Line 2\nLine 3",
},
{
name: "handles empty lines",
input: " Line 1\n\n Line 3",
expected: "Line 1\n\nLine 3",
},
{
name: "empty string",
input: "",
expected: "",
},
{
name: "removes consistent leading tabs",
input: "\t\tLine 1\n\t\tLine 2\n\t\tLine 3",
expected: "Line 1\nLine 2\nLine 3",
},
{
name: "removes consistent mixed tab and space indentation",
input: "\t Line 1\n\t Line 2\n\t Line 3",
expected: "Line 1\nLine 2\nLine 3",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := NormalizeLeadingWhitespace(tt.input)
assert.Equal(t, tt.expected, result, "NormalizeLeadingWhitespace should normalize indentation for case %q", tt.name)
})
}
}
func TestIsPositiveInteger(t *testing.T) {
tests := []struct {
name string
s string
want bool
}{
{
name: "positive integer",
s: "123",
want: true,
},
{
name: "one",
s: "1",
want: true,
},
{
name: "large number",
s: "999999999",
want: true,
},
{
name: "zero",
s: "0",
want: false,
},
{
name: "negative",
s: "-5",
want: false,
},
{
name: "leading zeros",
s: "007",
want: false,
},
{
name: "float",
s: "3.14",
want: false,
},
{
name: "not a number",
s: "abc",
want: false,
},
{
name: "empty string",
s: "",
want: false,
},
{
name: "spaces",
s: " 123 ",
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := IsPositiveInteger(tt.s)
assert.Equal(t, tt.want, got, "IsPositiveInteger(%q) should match expected positivity result", tt.s)
})
}
}