-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapply_test.go
More file actions
98 lines (94 loc) · 2.3 KB
/
Copy pathapply_test.go
File metadata and controls
98 lines (94 loc) · 2.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
package main
import (
"testing"
qt "github.com/go-quicktest/qt"
)
func TestApplyDiff(t *testing.T) {
tests := []struct {
name string
old string
new string
wantEdits []edit
}{{
name: "no change",
old: "hello\nworld\n",
new: "hello\nworld\n",
wantEdits: nil,
}, {
name: "simple change",
old: "hello\nold\n",
new: "hello\nnew\n",
wantEdits: []edit{{Addr: "#6,#9", Data: "new"}},
}, {
name: "delete line",
old: "hello\nworld\n",
new: "hello\n",
wantEdits: []edit{{Addr: "#6,#12", Data: ""}},
}, {
name: "add line after",
old: "hello\n",
new: "hello\nworld\n",
wantEdits: []edit{{Addr: "#6,#6", Data: "world\n"}},
}, {
name: "add line before",
old: "hello\n",
new: "world\nhello\n",
wantEdits: []edit{{Addr: "#0,#0", Data: "world\n"}},
}, {
name: "add trailing newline",
old: "hello\nworld",
new: "hello\nworld\n",
wantEdits: []edit{{Addr: "#11,#11", Data: "\n"}},
}, {
name: "remove trailing newline",
old: "hello\nworld\n",
new: "hello\nworld",
wantEdits: []edit{{Addr: "#11,#12", Data: ""}},
}, {
name: "multi-line change",
old: "a\nb\nc\nd\n",
new: "a\nx\ny\nz\nd\n",
wantEdits: []edit{
{Addr: "#2,#3", Data: "x\ny"},
{Addr: "#6,#7", Data: "z"},
},
}, {
name: "multiple hunks",
old: "a\nb\nc\nd\ne\n",
new: "a\nB\nc\nD\ne\n",
wantEdits: []edit{
{Addr: "#2,#3", Data: "B"},
{Addr: "#6,#7", Data: "D"},
},
}, {
name: "replace all",
old: "old\n",
new: "new\n",
wantEdits: []edit{{Addr: "#0,#3", Data: "new"}},
}, {
name: "empty to content",
old: "",
new: "hello\n",
wantEdits: []edit{{Addr: "#0,#0", Data: "hello\n"}},
}, {
name: "content to empty",
old: "hello\n",
new: "",
wantEdits: []edit{{Addr: "#0,#6", Data: ""}},
}}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
var got []edit
err := applyDiff([]byte(test.old), []byte(test.new), func(addr string, data []byte) error {
got = append(got, edit{Addr: addr, Data: string(data)})
return nil
})
qt.Assert(t, qt.IsNil(err))
qt.Assert(t, qt.DeepEquals(got, test.wantEdits))
})
}
}
type edit struct {
Addr string
Data string
}