-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtokendiff_test.go
More file actions
985 lines (908 loc) · 24 KB
/
tokendiff_test.go
File metadata and controls
985 lines (908 loc) · 24 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
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
package tokendiff
import (
"reflect"
"testing"
)
// TestMotivatingExample tests the exact example from the original dwdiff README
// that motivated token-level diffing. This is the key use case we must support.
// NOTE: Requires explicit delimiters since default delimiters are empty.
func TestMotivatingExample(t *testing.T) {
old := "void someFunction(SomeType var)"
new := "void someFunction(SomeOtherType var)"
// Use parentheses as delimiters to split function arguments
opts := Options{Delimiters: "()"}
diffs := DiffStrings(old, new, opts)
// Find the actual changes (non-Equal diffs)
var deletions, insertions []string
for _, d := range diffs {
switch d.Type {
case Delete:
deletions = append(deletions, d.Token)
case Insert:
insertions = append(insertions, d.Token)
}
}
// The key insight: only "SomeType" should be deleted and "SomeOtherType" inserted
// NOT "someFunction(SomeType" -> "someFunction(SomeOtherType" like wdiff would do
expectedDeletions := []string{"SomeType"}
expectedInsertions := []string{"SomeOtherType"}
if !reflect.DeepEqual(deletions, expectedDeletions) {
t.Errorf("Deletions = %v, want %v", deletions, expectedDeletions)
}
if !reflect.DeepEqual(insertions, expectedInsertions) {
t.Errorf("Insertions = %v, want %v", insertions, expectedInsertions)
}
}
func TestDiffStrings(t *testing.T) {
tests := []struct {
name string
text1 string
text2 string
opts Options
expected []Diff
}{
{
name: "identical strings",
text1: "hello world",
text2: "hello world",
opts: DefaultOptions(),
expected: []Diff{
{Equal, "hello"},
{Equal, "world"},
},
},
{
name: "single word change",
text1: "hello world",
text2: "hello universe",
opts: DefaultOptions(),
expected: []Diff{
{Equal, "hello"},
{Delete, "world"},
{Insert, "universe"},
},
},
{
name: "function parameter type change",
text1: "foo(int x)",
text2: "foo(string x)",
opts: Options{Delimiters: "()"},
expected: []Diff{
{Equal, "foo"},
{Equal, "("},
{Delete, "int"},
{Insert, "string"},
{Equal, "x"},
{Equal, ")"},
},
},
{
name: "added parameter",
text1: "foo(a)",
text2: "foo(a, b)",
opts: Options{Delimiters: "(),"},
expected: []Diff{
{Equal, "foo"},
{Equal, "("},
{Equal, "a"},
{Insert, ","},
{Insert, "b"},
{Equal, ")"},
},
},
{
name: "empty to something",
text1: "",
text2: "hello",
opts: DefaultOptions(),
expected: []Diff{
{Insert, "hello"},
},
},
{
name: "something to empty",
text1: "hello",
text2: "",
opts: DefaultOptions(),
expected: []Diff{
{Delete, "hello"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := DiffStrings(tt.text1, tt.text2, tt.opts)
if !reflect.DeepEqual(result, tt.expected) {
t.Errorf("DiffStrings(%q, %q) = %v, want %v",
tt.text1, tt.text2, result, tt.expected)
}
})
}
}
func TestDiffTokens(t *testing.T) {
// Test that DiffTokens works directly with pre-tokenized input
tokens1 := []string{"a", "b", "c"}
tokens2 := []string{"a", "x", "c"}
diffs := DiffTokens(tokens1, tokens2)
expected := []Diff{
{Equal, "a"},
{Delete, "b"},
{Insert, "x"},
{Equal, "c"},
}
if !reflect.DeepEqual(diffs, expected) {
t.Errorf("DiffTokens() = %v, want %v", diffs, expected)
}
}
// TestOperationString tests the Operation.String() method
func TestOperationString(t *testing.T) {
tests := []struct {
op Operation
expected string
}{
{Equal, "Equal"},
{Insert, "Insert"},
{Delete, "Delete"},
{Operation(99), "Unknown"},
}
for _, tt := range tests {
t.Run(tt.expected, func(t *testing.T) {
result := tt.op.String()
if result != tt.expected {
t.Errorf("Operation(%d).String() = %q, want %q", tt.op, result, tt.expected)
}
})
}
}
// TestHasChanges tests the HasChanges helper function
func TestHasChanges(t *testing.T) {
tests := []struct {
name string
diffs []Diff
expected bool
}{
{
name: "empty",
diffs: []Diff{},
expected: false,
},
{
name: "all equal",
diffs: []Diff{{Equal, "a"}, {Equal, "b"}},
expected: false,
},
{
name: "has insert",
diffs: []Diff{{Equal, "a"}, {Insert, "b"}},
expected: true,
},
{
name: "has delete",
diffs: []Diff{{Delete, "a"}, {Equal, "b"}},
expected: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := HasChanges(tt.diffs)
if result != tt.expected {
t.Errorf("HasChanges() = %v, want %v", result, tt.expected)
}
})
}
}
// TestIgnoreCase tests case-insensitive comparison
func TestIgnoreCase(t *testing.T) {
tests := []struct {
name string
text1 string
text2 string
ignoreCase bool
wantChange bool
}{
{
name: "case differs, ignore case",
text1: "Hello World",
text2: "hello world",
ignoreCase: true,
wantChange: false,
},
{
name: "case differs, case sensitive",
text1: "Hello World",
text2: "hello world",
ignoreCase: false,
wantChange: true,
},
{
name: "same case, ignore case",
text1: "hello world",
text2: "hello world",
ignoreCase: true,
wantChange: false,
},
{
name: "different words, ignore case",
text1: "hello world",
text2: "hello universe",
ignoreCase: true,
wantChange: true,
},
{
name: "mixed case change, ignore case",
text1: "Hello WORLD",
text2: "HELLO world",
ignoreCase: true,
wantChange: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
opts := Options{
Delimiters: DefaultDelimiters,
IgnoreCase: tt.ignoreCase,
}
diffs := DiffStrings(tt.text1, tt.text2, opts)
hasChange := HasChanges(diffs)
if hasChange != tt.wantChange {
t.Errorf("DiffStrings(%q, %q, ignoreCase=%v) hasChanges = %v, want %v",
tt.text1, tt.text2, tt.ignoreCase, hasChange, tt.wantChange)
}
})
}
}
// TestIgnoreCasePreservesOriginal tests that original case is preserved in output
func TestIgnoreCasePreservesOriginal(t *testing.T) {
text1 := "Hello World foo"
text2 := "hello WORLD bar"
opts := Options{
Delimiters: DefaultDelimiters,
IgnoreCase: true,
}
diffs := DiffStrings(text1, text2, opts)
// Should have: Equal "hello", Equal "WORLD", Delete "foo", Insert "bar"
// The equal tokens should use the case from text2 (new file)
var equalTokens []string
var deleteTokens []string
var insertTokens []string
for _, d := range diffs {
switch d.Type {
case Equal:
equalTokens = append(equalTokens, d.Token)
case Delete:
deleteTokens = append(deleteTokens, d.Token)
case Insert:
insertTokens = append(insertTokens, d.Token)
}
}
// Equal tokens should be from text2
expectedEqual := []string{"hello", "WORLD"}
if !reflect.DeepEqual(equalTokens, expectedEqual) {
t.Errorf("Equal tokens = %v, want %v", equalTokens, expectedEqual)
}
// Delete should be from text1
expectedDelete := []string{"foo"}
if !reflect.DeepEqual(deleteTokens, expectedDelete) {
t.Errorf("Delete tokens = %v, want %v", deleteTokens, expectedDelete)
}
// Insert should be from text2
expectedInsert := []string{"bar"}
if !reflect.DeepEqual(insertTokens, expectedInsert) {
t.Errorf("Insert tokens = %v, want %v", insertTokens, expectedInsert)
}
}
func TestDiffTokensWithPreprocessing(t *testing.T) {
tests := []struct {
name string
tokens1 []string
tokens2 []string
wantNoSpuriousEquals bool // if true, check that common tokens like "-" are not Equal
}{
{
name: "simple diff with no confusing tokens",
tokens1: []string{"hello", "world"},
tokens2: []string{"hello", "universe"},
wantNoSpuriousEquals: false,
},
{
name: "diff with high-frequency dash token",
tokens1: []string{"@@", "-117,6", "+117,34", "@@", "#", "Remove", "-", "essential", "for", "integration"},
tokens2: []string{"@@", "-7,6", "+7,8", "@@", "##", "Features", "-", "Display", "HTML", "from", "files"},
wantNoSpuriousEquals: false, // With threshold, "-" may still be kept
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Test that preprocessing produces valid diff output
result := DiffTokensWithPreprocessing(tt.tokens1, tt.tokens2)
// Basic sanity checks
if result == nil {
t.Error("DiffTokensWithPreprocessing() returned nil")
return
}
// Verify that reconstructing from diffs gives back original tokens
var deleted, inserted, equal []string
for _, d := range result {
switch d.Type {
case Delete:
deleted = append(deleted, d.Token)
case Insert:
inserted = append(inserted, d.Token)
case Equal:
equal = append(equal, d.Token)
}
}
// All deleted tokens should come from tokens1
// All inserted tokens should come from tokens2
// Equal tokens should exist in both
t.Logf("Deleted: %v", deleted)
t.Logf("Inserted: %v", inserted)
t.Logf("Equal: %v", equal)
})
}
}
func TestDiffStringsWithPreprocessing(t *testing.T) {
// Test the integration function that combines tokenization + preprocessing
tests := []struct {
name string
text1 string
text2 string
opts Options
}{
{
name: "simple text diff",
text1: "hello world",
text2: "hello universe",
opts: DefaultOptions(),
},
{
name: "code diff",
text1: "func foo(a int) { return a }",
text2: "func foo(b int) { return b }",
opts: Options{Delimiters: "(){}"},
},
{
name: "diff format header scenario",
text1: "@@ -117,6 +117,34 @@ # Remove\n- essential for integration",
text2: "@@ -7,6 +7,8 @@ ## Features\n- Display HTML from files",
opts: DefaultOptions(),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := DiffStringsWithPreprocessing(tt.text1, tt.text2, tt.opts)
// Basic sanity check
if result == nil {
t.Error("DiffStringsWithPreprocessing() returned nil")
return
}
// Log the result for debugging
for _, d := range result {
t.Logf("%s: %q", d.Type, d.Token)
}
})
}
}
// TestPreprocessingVsRawDiff compares preprocessing output to raw diff
func TestPreprocessingVsRawDiff(t *testing.T) {
// This tests how the histogram diff algorithm handles common tokens.
// The algorithm should avoid matching common tokens like "-" and "for"
// as anchors, grouping related changes together for readability.
tokens1 := []string{"@@", "-117,6", "+117,34", "@@", "#", "Remove", "-", "essential", "for", "integration"}
tokens2 := []string{"@@", "-7,6", "+7,8", "@@", "##", "Features", "-", "Display", "HTML", "from", "files"}
rawResult := DiffTokens(tokens1, tokens2)
preprocessedResult := DiffTokensWithPreprocessing(tokens1, tokens2)
t.Log("Raw diff result:")
for _, d := range rawResult {
t.Logf(" %s: %q", d.Type, d.Token)
}
t.Log("\nPreprocessed diff result:")
for _, d := range preprocessedResult {
t.Logf(" %s: %q", d.Type, d.Token)
}
// Count Equal tokens in each result
rawEquals := 0
for _, d := range rawResult {
if d.Type == Equal {
rawEquals++
}
}
prepEquals := 0
for _, d := range preprocessedResult {
if d.Type == Equal {
prepEquals++
}
}
t.Logf("\nRaw diff Equal tokens: %d", rawEquals)
t.Logf("Preprocessed Equal tokens: %d", prepEquals)
}
// TestDiffTokensRaw tests the raw diff function
func TestDiffTokensRaw(t *testing.T) {
tests := []struct {
name string
tokens1 []string
tokens2 []string
}{
{
name: "simple change",
tokens1: []string{"a", "b", "c"},
tokens2: []string{"a", "x", "c"},
},
{
name: "all different",
tokens1: []string{"a", "b"},
tokens2: []string{"x", "y"},
},
{
name: "all same",
tokens1: []string{"a", "b", "c"},
tokens2: []string{"a", "b", "c"},
},
{
name: "empty inputs",
tokens1: []string{},
tokens2: []string{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := DiffTokensRaw(tt.tokens1, tt.tokens2)
// DiffTokensRaw may return nil for empty inputs, which is valid
if result == nil && (len(tt.tokens1) > 0 || len(tt.tokens2) > 0) {
t.Error("DiffTokensRaw() returned nil for non-empty inputs")
return
}
// Verify result is consistent
var deleted, inserted, equal int
for _, d := range result {
switch d.Type {
case Delete:
deleted++
case Insert:
inserted++
case Equal:
equal++
}
}
// Total tokens in result should make sense
totalOps := deleted + inserted + equal
t.Logf("Deleted: %d, Inserted: %d, Equal: %d, Total: %d", deleted, inserted, equal, totalOps)
})
}
}
// TestDiffStringsWithPositions tests diff with position tracking
func TestDiffStringsWithPositions(t *testing.T) {
tests := []struct {
name string
text1 string
text2 string
opts Options
}{
{
name: "simple text",
text1: "hello world",
text2: "hello universe",
opts: DefaultOptions(),
},
{
name: "with delimiters",
text1: "foo(bar)",
text2: "foo(baz)",
opts: Options{Delimiters: "()"},
},
{
name: "multiline",
text1: "line1\nline2",
text2: "line1\nline3",
opts: DefaultOptions(),
},
{
name: "case insensitive",
text1: "Hello World",
text2: "hello WORLD",
opts: Options{IgnoreCase: true},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := DiffStringsWithPositions(tt.text1, tt.text2, tt.opts)
// Check that result contains expected fields
if result.Text1 != tt.text1 {
t.Errorf("Text1 = %q, want %q", result.Text1, tt.text1)
}
if result.Text2 != tt.text2 {
t.Errorf("Text2 = %q, want %q", result.Text2, tt.text2)
}
// Check that diffs are present
if result.Diffs == nil {
t.Error("Diffs is nil")
}
// Check that positions are present and valid
if result.Positions1 == nil {
t.Error("Positions1 is nil")
}
if result.Positions2 == nil {
t.Error("Positions2 is nil")
}
// Verify positions are within bounds
for i, pos := range result.Positions1 {
if pos.Start < 0 || pos.End > len(tt.text1) || pos.Start > pos.End {
t.Errorf("Invalid Position1[%d]: Start=%d, End=%d, text length=%d",
i, pos.Start, pos.End, len(tt.text1))
}
}
for i, pos := range result.Positions2 {
if pos.Start < 0 || pos.End > len(tt.text2) || pos.Start > pos.End {
t.Errorf("Invalid Position2[%d]: Start=%d, End=%d, text length=%d",
i, pos.Start, pos.End, len(tt.text2))
}
}
})
}
}
// TestComputeStatistics tests the statistics computation
func TestComputeStatistics(t *testing.T) {
tests := []struct {
name string
text1 string
text2 string
opts Options
wantOld int
wantNew int
wantDeleted int
wantInserted int
}{
{
name: "identical",
text1: "hello world",
text2: "hello world",
opts: DefaultOptions(),
wantOld: 2,
wantNew: 2,
wantDeleted: 0,
wantInserted: 0,
},
{
name: "one word changed",
text1: "hello world",
text2: "hello universe",
opts: DefaultOptions(),
wantOld: 2,
wantNew: 2,
wantDeleted: 1,
wantInserted: 1,
},
{
name: "word added",
text1: "hello",
text2: "hello world",
opts: DefaultOptions(),
wantOld: 1,
wantNew: 2,
wantDeleted: 0,
wantInserted: 1,
},
{
name: "word removed",
text1: "hello world",
text2: "hello",
opts: DefaultOptions(),
wantOld: 2,
wantNew: 1,
wantDeleted: 1,
wantInserted: 0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
diffs := DiffStrings(tt.text1, tt.text2, tt.opts)
stats := ComputeStatistics(tt.text1, tt.text2, diffs, tt.opts)
if stats.OldWords != tt.wantOld {
t.Errorf("OldWords = %d, want %d", stats.OldWords, tt.wantOld)
}
if stats.NewWords != tt.wantNew {
t.Errorf("NewWords = %d, want %d", stats.NewWords, tt.wantNew)
}
if stats.DeletedWords != tt.wantDeleted {
t.Errorf("DeletedWords = %d, want %d", stats.DeletedWords, tt.wantDeleted)
}
if stats.InsertedWords != tt.wantInserted {
t.Errorf("InsertedWords = %d, want %d", stats.InsertedWords, tt.wantInserted)
}
})
}
}
// TestDiffStringsWithPreprocessingIgnoreCase tests case-insensitive preprocessing
func TestDiffStringsWithPreprocessingIgnoreCase(t *testing.T) {
tests := []struct {
name string
text1 string
text2 string
wantChange bool
}{
{
name: "case only difference - no change expected",
text1: "Hello World",
text2: "hello world",
wantChange: false,
},
{
name: "case and content difference",
text1: "Hello World foo",
text2: "HELLO WORLD bar",
wantChange: true,
},
{
name: "mixed case with stopwords",
text1: "The quick brown fox",
text2: "THE SLOW BROWN FOX",
wantChange: true,
},
{
name: "all same with different case",
text1: "ABC DEF GHI",
text2: "abc def ghi",
wantChange: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
opts := Options{IgnoreCase: true}
diffs := DiffStringsWithPreprocessing(tt.text1, tt.text2, opts)
hasChange := HasChanges(diffs)
if hasChange != tt.wantChange {
t.Errorf("DiffStringsWithPreprocessing() hasChange = %v, want %v", hasChange, tt.wantChange)
for _, d := range diffs {
t.Logf(" %s: %q", d.Type, d.Token)
}
}
})
}
}
// TestDiffStringsWithPreprocessingCaseSensitive tests case-sensitive preprocessing
func TestDiffStringsWithPreprocessingCaseSensitive(t *testing.T) {
tests := []struct {
name string
text1 string
text2 string
wantChange bool
}{
{
name: "case difference - change expected",
text1: "Hello World",
text2: "hello world",
wantChange: true,
},
{
name: "identical - no change",
text1: "hello world",
text2: "hello world",
wantChange: false,
},
{
name: "different content",
text1: "foo bar",
text2: "baz qux",
wantChange: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
opts := Options{IgnoreCase: false}
diffs := DiffStringsWithPreprocessing(tt.text1, tt.text2, opts)
hasChange := HasChanges(diffs)
if hasChange != tt.wantChange {
t.Errorf("DiffStringsWithPreprocessing() hasChange = %v, want %v", hasChange, tt.wantChange)
}
})
}
}
// TestDiffStringsWithPositionsAndPreprocessingIgnoreCase tests case-insensitive with preprocessing
func TestDiffStringsWithPositionsAndPreprocessingIgnoreCase(t *testing.T) {
tests := []struct {
name string
text1 string
text2 string
}{
{
name: "case change only",
text1: "Hello World",
text2: "hello world",
},
{
name: "case change with content change",
text1: "Hello World foo",
text2: "HELLO WORLD bar",
},
{
name: "mixed case with preprocessing triggers",
text1: "The quick brown fox the jumps over the lazy dog",
text2: "THE QUICK BROWN CAT THE LEAPS OVER THE LAZY DOG",
},
{
name: "with deletes only",
text1: "Hello World Extra",
text2: "hello world",
},
{
name: "with inserts only",
text1: "Hello",
text2: "hello world extra",
},
{
name: "complex mixed case changes",
text1: "FUNCTION getData(X int) RETURN",
text2: "function setData(y string) return",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
opts := Options{IgnoreCase: true}
result := DiffStringsWithPositionsAndPreprocessing(tt.text1, tt.text2, opts)
if result.Text1 != tt.text1 {
t.Errorf("Text1 = %q, want %q", result.Text1, tt.text1)
}
if result.Text2 != tt.text2 {
t.Errorf("Text2 = %q, want %q", result.Text2, tt.text2)
}
// Log the result for inspection
for _, d := range result.Diffs {
t.Logf("%s: %q", d.Type, d.Token)
}
})
}
}
// TestExpandFilteredDiffsWithCaseEdgeCases tests edge cases for the case-preserving expansion
func TestExpandFilteredDiffsWithCaseEdgeCases(t *testing.T) {
tests := []struct {
name string
text1 string
text2 string
}{
{
name: "empty filtered result",
text1: "the the the",
text2: "THE THE THE",
},
{
name: "all tokens filtered",
text1: "a a a a a a a a a a",
text2: "A A A A A A A A A A",
},
{
name: "mixed filtered and non-filtered",
text1: "Hello the World a test",
text2: "HELLO THE WORLD A TEST",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
opts := Options{IgnoreCase: true}
result := DiffStringsWithPositionsAndPreprocessing(tt.text1, tt.text2, opts)
// Verify no errors and result structure is valid
if result.Diffs == nil {
t.Error("Diffs should not be nil")
}
t.Logf("Got %d diffs", len(result.Diffs))
})
}
}
// TestExpandFilteredDiffsWithCaseTrailingTokens tests trailing token handling
func TestExpandFilteredDiffsWithCaseTrailingTokens(t *testing.T) {
tests := []struct {
name string
text1 string
text2 string
wantDelete bool
wantInsert bool
}{
{
name: "trailing deletes",
text1: "HELLO WORLD EXTRA MORE",
text2: "hello world",
wantDelete: true,
wantInsert: false,
},
{
name: "trailing inserts",
text1: "HELLO",
text2: "hello world extra more",
wantDelete: false,
wantInsert: true,
},
{
name: "both trailing",
text1: "HELLO WORLD OLD",
text2: "hello universe new",
wantDelete: true,
wantInsert: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
opts := Options{IgnoreCase: true}
result := DiffStringsWithPositionsAndPreprocessing(tt.text1, tt.text2, opts)
hasDelete := false
hasInsert := false
for _, d := range result.Diffs {
if d.Type == Delete {
hasDelete = true
}
if d.Type == Insert {
hasInsert = true
}
}
if tt.wantDelete && !hasDelete {
t.Errorf("Expected Delete diffs, got none")
}
if tt.wantInsert && !hasInsert {
t.Errorf("Expected Insert diffs, got none")
}
})
}
}
// TestExpandFilteredDiffsWithCaseWithSkippedTokens tests cases where tokens are skipped
func TestExpandFilteredDiffsWithCaseWithSkippedTokens(t *testing.T) {
// This tests the inner loops in expandFilteredDiffsWithCase
// where we have to skip over tokens that were filtered out
tests := []struct {
name string
text1 string
text2 string
}{
{
name: "high frequency tokens cause filtering",
text1: "the the the UNIQUE the the",
text2: "the the the DIFFERENT the the",
},
{
name: "filtered tokens before equal",
text1: "a a MATCH b b",
text2: "x x MATCH y y",
},
{
name: "filtered tokens before delete",
text1: "the the OLD the",
text2: "the the the",
},
{
name: "filtered tokens before insert",
text1: "the the the",
text2: "the the NEW the",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
opts := Options{IgnoreCase: true}
result := DiffStringsWithPositionsAndPreprocessing(tt.text1, tt.text2, opts)
if result.Diffs == nil {
t.Error("Diffs should not be nil")
}
// Verify that all tokens from input are accounted for in output
var deleteTokens, insertTokens, equalTokens []string
for _, d := range result.Diffs {
switch d.Type {
case Delete:
deleteTokens = append(deleteTokens, d.Token)
case Insert:
insertTokens = append(insertTokens, d.Token)
case Equal:
equalTokens = append(equalTokens, d.Token)
}
}
t.Logf("Deletes: %v", deleteTokens)
t.Logf("Inserts: %v", insertTokens)
t.Logf("Equals: %v", equalTokens)
})
}
}
// Benchmark full diff
func BenchmarkDiffStrings(b *testing.B) {
text1 := "func processData(input []byte, config *Config) (Result, error) {"
text2 := "func processData(input []byte, options *Options) (Result, error) {"
opts := DefaultOptions()
b.ResetTimer()
for i := 0; i < b.N; i++ {
DiffStrings(text1, text2, opts)
}
}