-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_advanced_test.go
More file actions
608 lines (544 loc) · 12.9 KB
/
main_advanced_test.go
File metadata and controls
608 lines (544 loc) · 12.9 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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
package main
import (
"os"
"path/filepath"
"strings"
"testing"
"unicode/utf8"
)
// Advanced Edge Case Tests
func TestReplaceInLine_UnicodeEdgeCases(t *testing.T) {
tests := []struct {
name string
line string
search string
replace string
expected string
}{
{
"emoji replacement",
"hello 👋 world",
"👋",
"🌍",
"hello 🌍 world",
},
{
"multi-byte unicode",
"こんにちは世界",
"世界",
"ワールド",
"こんにちはワールド",
},
{
"combining characters",
"café résumé",
"café",
"coffee",
"coffee résumé",
},
{
"right-to-left text",
"hello مرحبا world",
"مرحبا",
"שלום",
"hello שלום world",
},
{
"zero-width characters",
"hello\u200Bworld",
"hello\u200Bworld",
"goodbye",
"goodbye",
},
{
"null byte in middle",
"hello\x00world",
"hello\x00world",
"test",
"test",
},
{
"mixed scripts",
"Привет hello 你好",
"hello",
"hola",
"Привет hola 你好",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := replaceInLine(tt.line, tt.search, tt.replace, false, false)
if result != tt.expected {
t.Errorf("replaceInLine(%q, %q, %q) = %q, want %q",
tt.line, tt.search, tt.replace, result, tt.expected)
}
})
}
}
func TestReplaceInLine_BoundaryConditions(t *testing.T) {
tests := []struct {
name string
line string
search string
replace string
expected string
}{
{
"empty line",
"",
"test",
"exam",
"",
},
{
"empty search",
"hello",
"",
"X",
"hello",
},
{
"empty replace",
"hello world",
"world",
"",
"hello ",
},
{
"search longer than line",
"hi",
"hello world",
"test",
"hi",
},
{
"replace entire line",
"test",
"test",
"exam",
"exam",
},
{
"very long line",
strings.Repeat("a", 100000) + "target" + strings.Repeat("b", 100000),
"target",
"replaced",
strings.Repeat("a", 100000) + "replaced" + strings.Repeat("b", 100000),
},
{
"many occurrences",
strings.Repeat("x", 10000),
"x",
"y",
strings.Repeat("y", 10000),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := replaceInLine(tt.line, tt.search, tt.replace, false, false)
if result != tt.expected {
// For very long strings, just check length
if len(tt.line) > 1000 {
if len(result) != len(tt.expected) {
t.Errorf("Length mismatch: got %d, want %d", len(result), len(tt.expected))
}
} else {
t.Errorf("replaceInLine(%q, %q, %q) = %q, want %q",
tt.line, tt.search, tt.replace, result, tt.expected)
}
}
})
}
}
func TestReplaceInLine_SpecialCharacters(t *testing.T) {
tests := []struct {
name string
line string
search string
replace string
expected string
}{
{
"newline in content",
"hello\nworld",
"hello\nworld",
"test",
"test",
},
{
"tab characters",
"hello\tworld",
"\t",
" ",
"hello world",
},
{
"carriage return",
"hello\rworld",
"\r",
"",
"helloworld",
},
{
"multiple whitespace types",
"hello \t\n\r world",
" \t\n\r ",
" ",
"hello world",
},
{
"backslash",
"path\\to\\file",
"\\",
"/",
"path/to/file",
},
{
"quotes",
`"hello" 'world'`,
`"hello"`,
`'hi'`,
`'hi' 'world'`,
},
{
"regex special chars",
"hello.*world+test?",
".*world+",
"REPLACED",
"helloREPLACEDtest?",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := replaceInLine(tt.line, tt.search, tt.replace, false, false)
if result != tt.expected {
t.Errorf("replaceInLine(%q, %q, %q) = %q, want %q",
tt.line, tt.search, tt.replace, result, tt.expected)
}
})
}
}
func TestContainsWholeWord_ComplexBoundaries(t *testing.T) {
tests := []struct {
name string
text string
word string
expected bool
}{
{"unicode boundary", "hello世界world", "world", true},
{"emoji boundary", "test👋word", "word", true},
{"emoji boundary fail", "test👋word", "test", true},
{"multiple underscores", "___word___", "word", false},
{"hyphen boundary", "test-word-test", "word", true},
{"parentheses", "(word)", "word", true},
{"brackets", "[word]", "word", true},
{"braces", "{word}", "word", true},
{"angle brackets", "<word>", "word", true},
{"at start with special", "@word", "word", true},
{"at end with special", "word!", "word", true},
{"dot boundary", "word.com", "word", true},
{"comma boundary", "word,test", "word", true},
{"semicolon boundary", "word;test", "word", true},
{"colon boundary", "word:test", "word", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := containsWholeWord(tt.text, tt.word)
if result != tt.expected {
t.Errorf("containsWholeWord(%q, %q) = %v, want %v",
tt.text, tt.word, result, tt.expected)
}
})
}
}
// File System Edge Cases
func TestReplaceInFile_BinaryContent(t *testing.T) {
tmpDir := setupTestDir(t)
defer cleanupTestDir(t, tmpDir)
// Create file with binary content
binaryContent := []byte{0x00, 0x01, 0x02, 't', 'e', 's', 't', 0xFF, 0xFE}
filePath := filepath.Join(tmpDir, "binary.bin")
if err := os.WriteFile(filePath, binaryContent, 0644); err != nil {
t.Fatalf("Failed to create binary file: %v", err)
}
config := Config{
Search: "test",
Replace: "exam",
DryRun: false,
}
// Should handle binary content without crashing
_, _, err := replaceInFile(filePath, config)
if err != nil {
t.Fatalf("replaceInFile failed on binary content: %v", err)
}
}
func TestReplaceInFile_InvalidUTF8(t *testing.T) {
tmpDir := setupTestDir(t)
defer cleanupTestDir(t, tmpDir)
// Create file with invalid UTF-8 sequences
invalidUTF8 := []byte("hello \xFF\xFE world test\n")
filePath := filepath.Join(tmpDir, "invalid.txt")
if err := os.WriteFile(filePath, invalidUTF8, 0644); err != nil {
t.Fatalf("Failed to create file: %v", err)
}
config := Config{
Search: "test",
Replace: "exam",
DryRun: false,
}
// Should handle invalid UTF-8 without crashing
linesChanged, _, err := replaceInFile(filePath, config)
if err != nil {
t.Fatalf("replaceInFile failed on invalid UTF-8: %v", err)
}
if linesChanged != 1 {
t.Errorf("Expected 1 line changed, got %d", linesChanged)
}
}
func TestReplaceInFile_NoTrailingNewline(t *testing.T) {
tmpDir := setupTestDir(t)
defer cleanupTestDir(t, tmpDir)
// File without trailing newline
content := "line1\nline2\nline3 with target"
filePath := createTestFile(t, tmpDir, "nonewline.txt", content)
config := Config{
Search: "target",
Replace: "REPLACED",
DryRun: false,
}
linesChanged, _, err := replaceInFile(filePath, config)
if err != nil {
t.Fatalf("replaceInFile failed: %v", err)
}
if linesChanged != 1 {
t.Errorf("Expected 1 line changed, got %d", linesChanged)
}
// Verify file structure preserved
actualContent := readFileContent(t, filePath)
if !strings.Contains(actualContent, "REPLACED") {
t.Error("Replacement not found")
}
}
func TestReplaceInFile_OnlyNewlines(t *testing.T) {
tmpDir := setupTestDir(t)
defer cleanupTestDir(t, tmpDir)
content := "\n\n\n\n\n"
filePath := createTestFile(t, tmpDir, "newlines.txt", content)
config := Config{
Search: "test",
Replace: "exam",
DryRun: false,
}
linesChanged, replacements, err := replaceInFile(filePath, config)
if err != nil {
t.Fatalf("replaceInFile failed: %v", err)
}
if linesChanged != 0 {
t.Errorf("Expected 0 lines changed, got %d", linesChanged)
}
if replacements != 0 {
t.Errorf("Expected 0 replacements, got %d", replacements)
}
}
// Case Insensitive Edge Cases
func TestCaseInsensitiveReplace_UnicodeCase(t *testing.T) {
tests := []struct {
name string
line string
search string
replace string
expected string
}{
{
"German eszett",
"straße",
"strasse",
"street",
"straße", // ß doesn't lowercase to ss in simple lowercase
},
{
"Turkish I problem",
"Istanbul",
"istanbul",
"CITY",
"CITY",
},
{
"Greek sigma variants",
"σίσυφος",
"σίσυφος",
"sisyphus",
"sisyphus",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := caseInsensitiveReplace(tt.line, tt.search, tt.replace)
if result != tt.expected {
t.Logf("Note: Unicode case folding may behave differently")
t.Logf("Got: %q, Expected: %q", result, tt.expected)
}
})
}
}
// Complex Exclude Filter Tests
func TestReplaceInFile_ComplexExcludePatterns(t *testing.T) {
tmpDir := setupTestDir(t)
defer cleanupTestDir(t, tmpDir)
content := `result = calculate()
dirresult = process()
tempresult = temp()
finalresult = final()
return result
`
filePath := createTestFile(t, tmpDir, "test.txt", content)
config := Config{
Search: "result",
Replace: "res",
ExcludeLines: []string{"dirresult", "tempresult"},
DryRun: false,
}
linesChanged, _, err := replaceInFile(filePath, config)
if err != nil {
t.Fatalf("replaceInFile failed: %v", err)
}
actualContent := readFileContent(t, filePath)
// Should replace in first and last line, and finalresult line
if !strings.Contains(actualContent, "res = calculate()") {
t.Error("First line should be replaced")
}
if !strings.Contains(actualContent, "return res") {
t.Error("Last line should be replaced")
}
if !strings.Contains(actualContent, "finalres") {
t.Error("finalresult should be replaced")
}
// Should NOT replace these
if !strings.Contains(actualContent, "dirresult") {
t.Error("dirresult should not be replaced")
}
if !strings.Contains(actualContent, "tempresult") {
t.Error("tempresult should not be replaced")
}
// Count lines changed - should be 3 (first, finalresult, last)
if linesChanged != 3 {
t.Errorf("Expected 3 lines changed, got %d", linesChanged)
}
}
func TestReplaceInFile_ExcludeWithUnicode(t *testing.T) {
tmpDir := setupTestDir(t)
defer cleanupTestDir(t, tmpDir)
content := "test normal\ntest 世界\ntest emoji 👋\n"
filePath := createTestFile(t, tmpDir, "unicode.txt", content)
config := Config{
Search: "test",
Replace: "exam",
ExcludeLines: []string{"世界"},
DryRun: false,
}
linesChanged, _, err := replaceInFile(filePath, config)
if err != nil {
t.Fatalf("replaceInFile failed: %v", err)
}
if linesChanged != 2 {
t.Errorf("Expected 2 lines changed, got %d", linesChanged)
}
actualContent := readFileContent(t, filePath)
if !strings.Contains(actualContent, "test 世界") {
t.Error("Unicode excluded line should not be replaced")
}
}
// Whole Word Replacement Edge Cases
func TestWholeWordReplace_AdjacentMatches(t *testing.T) {
tests := []struct {
line string
search string
replace string
expected string
}{
{
"log log log",
"log",
"X",
"X X X",
},
{
"logloglog",
"log",
"X",
"logloglog",
},
{
"log,log,log",
"log",
"X",
"X,X,X",
},
{
"log\tlog\tlog",
"log",
"X",
"X\tX\tX",
},
}
for _, tt := range tests {
t.Run(tt.line, func(t *testing.T) {
result := wholeWordReplace(tt.line, tt.search, tt.replace)
if result != tt.expected {
t.Errorf("wholeWordReplace(%q, %q, %q) = %q, want %q",
tt.line, tt.search, tt.replace, result, tt.expected)
}
})
}
}
// Positional Correctness
func TestReplaceInLine_AllPositions(t *testing.T) {
// Test replacing a pattern at every possible position
// Use a base string that doesn't contain the search pattern
base := "abcdefghijklmnopqrstuvw"
search := "XYZ"
replace := "123"
for i := 0; i <= len(base); i++ {
line := base[:i] + search + base[i:]
expected := base[:i] + replace + base[i:]
result := replaceInLine(line, search, replace, false, false)
if result != expected {
t.Errorf("Position %d: got %q, want %q", i, result, expected)
}
}
}
// UTF-8 Validation Tests
func TestUTF8Handling(t *testing.T) {
tests := []struct {
name string
input string
valid bool
}{
{"valid ASCII", "hello world", true},
{"valid UTF-8", "hello 世界", true},
{"valid emoji", "hello 👋🌍", true},
{"invalid UTF-8 sequence", "hello \xFF\xFE", false},
{"truncated UTF-8", "hello \xE4\xB8", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
isValid := utf8.ValidString(tt.input)
if isValid != tt.valid {
t.Errorf("UTF-8 validation mismatch: got %v, want %v", isValid, tt.valid)
}
// Test that our functions don't crash on invalid UTF-8
_ = replaceInLine(tt.input, "world", "test", false, false)
_ = containsWholeWord(tt.input, "hello")
})
}
}
// Search Pattern Edge Cases
func TestCountReplacements_LongSearchPattern(t *testing.T) {
// Test with very long search pattern
longPattern := strings.Repeat("abcdefghij", 100) // 1000 chars
line := "prefix " + longPattern + " suffix"
count := countReplacements(line, longPattern, false, false)
if count != 1 {
t.Errorf("Expected 1 replacement, got %d", count)
}
}