-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff_test.go
More file actions
94 lines (71 loc) · 2.31 KB
/
Copy pathdiff_test.go
File metadata and controls
94 lines (71 loc) · 2.31 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
//nolint:testpackage // Internal tests for unexported functions.
package testastic
import (
"strconv"
"testing"
)
func TestComputeDiffFallsBackWhenMatrixExceedsLimit(t *testing.T) {
t.Parallel()
// given: line sets whose LCS matrix would exceed the cell limit.
const lineCount = 1024
expected := make([]string, lineCount)
actual := make([]string, lineCount)
expected[0] = "prefix"
actual[0] = "prefix"
expected[lineCount-1] = "suffix"
actual[lineCount-1] = "suffix"
actual[1] = "inserted"
for i := 1; i < lineCount-1; i++ {
expected[i] = "line-" + strconv.Itoa(i)
}
copy(actual[2:lineCount-1], expected[1:lineCount-2])
want := make([]string, 0, 2*lineCount-2)
want = append(want, " prefix")
for _, line := range expected[1 : lineCount-1] {
want = append(want, red("- "+line))
}
for _, line := range actual[1 : lineCount-1] {
want = append(want, green("+ "+line))
}
want = append(want, " suffix")
// when: computing the diff.
got := computeDiff(expected, actual)
// then: the fallback preserves the common ends and replaces the middle.
if len(got) != len(want) {
t.Fatalf("unexpected diff length: got %d, want %d", len(got), len(want))
}
for i := range want {
if got[i] != want[i] {
t.Fatalf("unexpected diff line %d: got %q, want %q", i, got[i], want[i])
}
}
}
func TestComputeDiffFallsBackWhenDimensionExceedsLimit(t *testing.T) {
t.Parallel()
// given: a skewed line set whose larger dimension exceeds the line limit.
const lineCount = 4097
expected := make([]string, lineCount)
expected[0] = "prefix"
expected[lineCount-1] = "suffix"
for i := 1; i < lineCount-1; i++ {
expected[i] = "line-" + strconv.Itoa(i)
}
actual := []string{"prefix", expected[lineCount/2], "suffix"}
want := make([]string, 0, lineCount+1)
want = append(want, " prefix")
for _, line := range expected[1 : lineCount-1] {
want = append(want, red("- "+line))
}
want = append(want, green("+ "+actual[1]), " suffix")
// when: computing the diff.
got := computeDiff(expected, actual)
// then: the fallback avoids the skewed matrix and preserves every changed line.
if len(got) != len(want) {
t.Fatalf("unexpected diff length: got %d, want %d", len(got), len(want))
}
for i := range want {
if got[i] != want[i] {
t.Fatalf("unexpected diff line %d: got %q, want %q", i, got[i], want[i])
}
}
}