Skip to content

Commit 26663b4

Browse files
committed
added test for GranularStrings
This also tests GranularDiffs itself. Also added a helper function to compare two diff slices.
1 parent 0749d74 commit 26663b4

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

diff_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,39 @@ func TestDiffBA(t *testing.T) {
118118
}
119119
}
120120

121+
func diffsEqual(a, b []diff.Change) bool {
122+
if len(a) != len(b) {
123+
return false
124+
}
125+
for i := 0; i < len(a); i++ {
126+
if a[i] != b[i] {
127+
return false
128+
}
129+
}
130+
return true
131+
}
132+
133+
func TestGranularStrings(t *testing.T) {
134+
a := "abcdefghijklmnopqrstuvwxyza"
135+
b := "AbCdeFghiJklmnOpqrstUvwxyzab"
136+
// each iteration of i increases granularity and will absorb one more lower-letter-followed-by-upper-letters sequence
137+
changesI := [][]diff.Change{
138+
{{0, 0, 1, 1}, {2, 2, 1, 1}, {5, 5, 1, 1}, {9, 9, 1, 1}, {14, 14, 1, 1}, {20, 20, 1, 1}, {27, 27, 0, 1}},
139+
{{0, 0, 3, 3}, {5, 5, 1, 1}, {9, 9, 1, 1}, {14, 14, 1, 1}, {20, 20, 1, 1}, {27, 27, 0, 1}},
140+
{{0, 0, 6, 6}, {9, 9, 1, 1}, {14, 14, 1, 1}, {20, 20, 1, 1}, {27, 27, 0, 1}},
141+
{{0, 0, 10, 10}, {14, 14, 1, 1}, {20, 20, 1, 1}, {27, 27, 0, 1}},
142+
{{0, 0, 15, 15}, {20, 20, 1, 1}, {27, 27, 0, 1}},
143+
{{0, 0, 21, 21}, {27, 27, 0, 1}},
144+
{{0, 0, 27, 28}},
145+
}
146+
for i := 0; i < len(changesI); i++ {
147+
diffs := diff.GranularStrings(a, b, i)
148+
if !diffsEqual(diffs, changesI[i]) {
149+
t.Errorf("expected %v, got %v", diffs, changesI[i])
150+
}
151+
}
152+
}
153+
121154
func TestDiffRunes(t *testing.T) {
122155
a := []rune("brown fox jumps over the lazy dog")
123156
b := []rune("brwn faax junps ovver the lay dago")

0 commit comments

Comments
 (0)