Repository navigation
Expand file tree
/
Copy pathdiffview_test.go
More file actions
368 lines (341 loc) · 11.7 KB
/
Copy pathdiffview_test.go
File metadata and controls
368 lines (341 loc) · 11.7 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
package godemirror
import (
"image"
"slices"
"strings"
"testing"
"time"
"gioui.org/f32"
"gioui.org/io/key"
"gioui.org/io/pointer"
"gioui.org/layout"
"github.com/lichdotdev/godemirror/tags"
)
func newDiffHarness(t *testing.T, opts DiffOptions) (*harness, *DiffView) {
t.Helper()
d, err := NewDiffView("monospace", 12, opts)
if err != nil {
t.Fatal(err)
}
h := &harness{t: t, v: d.unified, diff: d, size: image.Pt(800, 300), now: time.Unix(1, 0)}
h.frame()
return h, d
}
func TestDiffViewShowsTwoDocsUnifiedWithBothNumbers(t *testing.T) {
h, d := newDiffHarness(t, DiffOptions{})
d.SetDocs("main.go", "a\nb\nc\n", "a\nB\nc\n")
h.frame()
var texts, labels []string
for i := range h.v.LineCount() {
texts, labels = append(texts, h.v.LineText(i)), append(labels, h.v.label(i))
}
if got, want := strings.Join(texts, "|"), "a|b|B|c"; got != want {
t.Fatalf("rows = %q, want %q", got, want)
}
if got, want := strings.Join(labels, "|"), "1 1|2 | 2|3 3"; got != want {
t.Fatalf("labels = %q, want %q", got, want)
}
}
func TestDiffViewColorsChangesFromItsTheme(t *testing.T) {
h, d := newDiffHarness(t, DiffOptions{})
d.SetDocs("main.go", "a\nb\nc", "a\nB\nc")
h.frame()
for _, theme := range []Theme{LightTheme, DarkTheme} {
d.SetTheme(theme)
h.frame()
if got := h.v.decoration(1).Background; got != theme.Diff.Deleted.Background {
t.Fatalf("deleted row background = %v, want %v", got, theme.Diff.Deleted.Background)
}
if got := h.v.decoration(2).GutterStrip; got != theme.Diff.Added.Strip {
t.Fatalf("added row strip = %v, want %v", got, theme.Diff.Added.Strip)
}
if h.v.decoration(1).HoverGroup == 0 || h.v.decoration(1).HoverGroup != h.v.decoration(2).HoverGroup {
t.Fatal("a chunk's rows do not light up together")
}
}
}
func TestDiffViewSplitPairsChangesSideBySide(t *testing.T) {
h, d := newDiffHarness(t, DiffOptions{})
d.SetDocs("main.go", "a\nb\nb2\nc", "a\nB\nc")
d.Split = true
h.frame()
left, right := d.split.Left, d.split.Right
if left.LineCount() != 4 || right.LineCount() != 4 {
t.Fatalf("sides have %d and %d rows, want 4 each", left.LineCount(), right.LineCount())
}
if left.LineText(2) != "b2" || right.LineText(2) != "" || right.decoration(2).Hatch != DarkTheme.Diff.Filler {
t.Fatalf("row 2: %q | %q, want b2 paired with a hatched filler", left.LineText(2), right.LineText(2))
}
if left.label(1) != "2" || right.label(1) != "2" || right.label(2) != "" {
t.Fatalf("labels %q | %q, %q", left.label(1), right.label(1), right.label(2))
}
}
func TestDiffViewKeepsItsPlaceForANewVersionOfTheSameFile(t *testing.T) {
h, d := newDiffHarness(t, DiffOptions{})
old := numbered(100)
d.SetDocs("main.go", old, strings.Replace(old, "line 50", "LINE 50", 1))
h.frame()
h.send(pointer.Event{Kind: pointer.Scroll, Source: pointer.Mouse, Position: f32.Pt(200, 100), Scroll: f32.Pt(0, 300)})
d.SetDocs("main.go", old, strings.Replace(old, "line 60", "LINE 60", 1))
waitForHighlight(h)
if h.v.scroll.Y != 300 {
t.Fatalf("scroll = %d after a new version of the file, want 300", h.v.scroll.Y)
}
d.SetDocs("other.go", old, old+"\nmore")
h.frame()
if h.v.scroll.Y != 0 {
t.Fatalf("scroll = %d after another file, want 0", h.v.scroll.Y)
}
}
func TestDiffViewShowsAFileOfAPatch(t *testing.T) {
h, d := newDiffHarness(t, DiffOptions{})
d.SetPatch(parsePatch(t, twoFilePatch)[0])
h.frame()
if got := h.v.LineCount(); got != 9 {
t.Fatalf("%d rows, want 9", got)
}
if chunks := d.Chunks(); len(chunks) != 2 || chunks[0].Lines[0] != (ChangedLine{OldSide, 4, "b"}) {
t.Fatalf("Chunks() = %+v", chunks)
}
}
// commentAcrossGapPatch opens a block comment in its first hunk and closes
// it in the lines its gap leaves out; a later comment's end is the first
// "*/" the text on screen holds.
const commentAcrossGapPatch = `diff --git a/A.java b/A.java
--- a/A.java
+++ b/A.java
@@ -1,2 +1,2 @@
-class A {
+class B {
/**
@@ -6,3 +6,3 @@
- String s = "x";
+ String s = "y";
/** later */
}
`
func TestAGapEndsWhatItsHunkLeftOpen(t *testing.T) {
h, d := newDiffHarness(t, DiffOptions{})
d.SetPatch(parsePatch(t, commentAcrossGapPatch)[0])
waitForHighlight(h)
if got := tagAt(t, h.v, 4, `"x"`); got != tags.String {
t.Fatalf(`unified: "x" after the gap is %v, want %v`, got, tags.String)
}
d.Split = true
h.v = d.split.Right
h.frame()
waitForHighlight(h)
if got := tagAt(t, h.v, 3, `"y"`); got != tags.String {
t.Fatalf(`split: "y" after the gap is %v, want %v`, got, tags.String)
}
}
func TestNewDiffViewFromFontsNeedsNoFontconfig(t *testing.T) {
withoutFontconfig(t)
d, err := NewDiffViewFromFonts(goMono, 12, DiffOptions{})
if err != nil {
t.Fatal(err)
}
d.SetDocs("a.txt", "x", "y")
h := &harness{t: t, v: d.unified, diff: d, size: image.Pt(400, 200), now: time.Unix(1, 0)}
h.frame()
}
// waitForHighlight draws frames until the view has taken its highlighting in.
func waitForHighlight(h *harness) {
h.t.Helper()
for deadline := time.Now().Add(5 * time.Second); h.v.pending != nil; {
if time.Now().After(deadline) {
h.t.Fatal("highlighting never landed")
}
time.Sleep(time.Millisecond)
h.frame()
}
}
// collapsedDocs is a 20-line file with line 10 changed, collapsed so that
// lines 1..6 and 14..20 are left out.
func collapsedDocs(d *DiffView) {
a := strings.Join(fileLines(20), "\n")
d.SetDocs("main.go", a, strings.Replace(a, "l10\n", "L10\n", 1))
}
var defaultCollapse = &Collapse{Margin: 3, MinSize: 4}
func TestExpandingAGapOfTwoDocsFillsItIn(t *testing.T) {
h, d := newDiffHarness(t, DiffOptions{Collapse: defaultCollapse})
collapsedDocs(d)
h.frame()
h.click(h.at(0, 3), 0)
h.frame()
waitForHighlight(h)
if got := h.v.LineText(0); got != "l1" || h.v.LineCount() != 15 {
t.Fatalf("after expanding the top gap: %d rows, first %q; want 15 rows from l1", h.v.LineCount(), got)
}
}
func TestALastRowWithNoTextIsStillShown(t *testing.T) {
h, d := newDiffHarness(t, DiffOptions{Collapse: defaultCollapse})
collapsedDocs(d)
h.frame()
if got := h.v.LineCount(); got != 10 {
t.Fatalf("%d rows, want 10: the separator for lines 14..20 is missing", got)
}
d.SetDocs("other.go", "a\nb\nc", "a\nB")
d.Split = true
h.frame()
if l, r := d.split.Left.LineCount(), d.split.Right.LineCount(); l != 3 || r != 3 {
t.Fatalf("sides have %d and %d rows, want 3 each: the last filler is missing", l, r)
}
}
func TestAPatchGapIsFilledByTheCallerInParts(t *testing.T) {
var asked []Gap
h, d := newDiffHarness(t, DiffOptions{Expand: func(g Gap) { asked = append(asked, g) }})
d.SetPatch(parsePatch(t, twoFilePatch)[0])
h.frame()
h.click(h.at(6, 3), 0)
h.frame()
if len(asked) != 1 || asked[0].From != 6 || asked[0].To != 8 || asked[0].OldFrom != 7 {
t.Fatalf("asked for %+v, want one gap of new lines 6..8", asked)
}
g := asked[0]
d.FillGap(g, []string{"n6"})
g.From++
d.FillGap(g, []string{"n7", "n8", "past the gap"})
waitForHighlight(h)
if got := h.v.LineText(6) + h.v.LineText(7) + h.v.LineText(8) + h.v.LineText(9); got != "n6n7n8c" {
t.Fatalf("rows 6..9 read %q, want n6 n7 n8 then c", got)
}
}
func TestAFillForAnOlderDiffIsDropped(t *testing.T) {
var asked []Gap
h, d := newDiffHarness(t, DiffOptions{Expand: func(g Gap) { asked = append(asked, g) }})
file := parsePatch(t, twoFilePatch)[0]
d.SetPatch(file)
h.frame()
h.click(h.at(6, 3), 0)
h.frame()
file.body += " "
d.SetPatch(file)
waitForHighlight(h)
rows := h.v.LineCount()
d.FillGap(asked[0], []string{"stale"})
waitForHighlight(h)
if h.v.LineCount() != rows {
t.Fatal("lines asked for before the diff changed were laid into the new one")
}
}
func TestRevertHandsTheChunkUnderThePointerToTheCaller(t *testing.T) {
var reverted []Chunk
h, d := newDiffHarness(t, DiffOptions{Revert: func(c Chunk) { reverted = append(reverted, c) }})
d.SetPatch(parsePatch(t, twoFilePatch)[0])
h.frame()
row := h.at(2, 0)
h.pointer(pointer.Move, row, 0)
h.click(f32.Pt(float32(h.size.X)-12, row.Y), 0)
h.frame()
if len(reverted) != 1 || !chunkEqual(reverted[0], d.Chunks()[0]) {
t.Fatalf("reverted %+v, want the first chunk", reverted)
}
}
func TestTheExpandLabelCountsTheHiddenLines(t *testing.T) {
if got := defaultExpandLabel(1); got != "1 unchanged line" {
t.Fatalf("got %q", got)
}
if got := defaultExpandLabel(44); got != "44 unchanged lines" {
t.Fatalf("got %q", got)
}
var hidden []int
h, d := newDiffHarness(t, DiffOptions{Collapse: defaultCollapse, ExpandLabel: func(n int) string {
hidden = append(hidden, n)
return "more"
}})
collapsedDocs(d)
h.frame()
if !slices.Contains(hidden, 6) || !slices.Contains(hidden, 7) {
t.Fatalf("ExpandLabel was asked for %v, want 6 and 7", hidden)
}
}
// selectRows selects rows from to to of the view under test with the
// pointer: a triple click on the first, a Shift+click inside the last.
func selectRows(h *harness, from, to int, offsetX float32) {
h.t.Helper()
first := h.at(from, 1)
first.X += offsetX
h.now = h.now.Add(time.Second)
for range 3 {
h.click(first, 0)
}
last := h.at(to, 1)
last.X += offsetX
h.click(last, key.ModShift)
}
func TestSelectedRangeAndChangesNameFileLines(t *testing.T) {
h, d := newDiffHarness(t, DiffOptions{})
d.SetPatch(parsePatch(t, twoFilePatch)[0])
h.frame()
selectRows(h, 1, 4, 0)
r, ok := d.SelectedRange()
if want := (DiffRange{OldFrom: 3, OldTo: 5, NewFrom: 3, NewTo: 4}); !ok || r != want {
t.Fatalf("SelectedRange() = %+v, %v; want %+v", r, ok, want)
}
want := []ChangedLine{{OldSide, 4, "b"}, {OldSide, 5, "b2"}, {NewSide, 4, "B"}}
if got := d.SelectedChanges(); !slices.Equal(got, want) {
t.Fatalf("SelectedChanges() = %+v, want %+v", got, want)
}
}
func TestASelectionOnTheRightSideNamesOnlyNewLines(t *testing.T) {
h, d := newDiffHarness(t, DiffOptions{})
d.SetPatch(parsePatch(t, twoFilePatch)[0])
d.Split = true
h.v = d.split.Right
h.frame()
selectRows(h, 1, 3, float32(h.size.X/2))
r, ok := d.SelectedRange()
if want := (DiffRange{NewFrom: 3, NewTo: 4}); !ok || r != want {
t.Fatalf("SelectedRange() = %+v, %v; want %+v", r, ok, want)
}
if got, want := d.SelectedChanges(), []ChangedLine{{NewSide, 4, "B"}}; !slices.Equal(got, want) {
t.Fatalf("SelectedChanges() = %+v, want %+v", got, want)
}
}
func TestNothingSelectedIsNoRange(t *testing.T) {
_, d := newDiffHarness(t, DiffOptions{})
d.SetPatch(parsePatch(t, twoFilePatch)[0])
if _, ok := d.SelectedRange(); ok || d.SelectedChanges() != nil {
t.Fatal("a range with nothing selected")
}
}
func TestTheSelectionIsReadAgainstTheRowsOnScreen(t *testing.T) {
h, d := newDiffHarness(t, DiffOptions{})
file := parsePatch(t, twoFilePatch)[0]
d.SetPatch(file)
h.frame()
selectRows(h, 2, 2, 0)
file.hunks = file.hunks[1:]
file.body += " "
d.SetPatch(file)
if got, want := d.SelectedChanges(), []ChangedLine{{OldSide, 4, "b"}}; !slices.Equal(got, want) {
t.Fatalf("before the new version shows, SelectedChanges() = %+v, want %+v", got, want)
}
}
func TestBlocksOpenUnderTheirFileLine(t *testing.T) {
h, d := newDiffHarness(t, DiffOptions{})
d.SetPatch(parsePatch(t, twoFilePatch)[0])
h.frame()
thread := func(gtx layout.Context) layout.Dimensions { return layout.Dimensions{Size: image.Pt(10, 40)} }
d.SetBlocks([]DiffBlock{{At: LineRef{NewSide, 4}, Widget: thread}, {At: LineRef{OldSide, 5}, Widget: thread}, {At: LineRef{NewSide, 7}, Widget: thread}})
waitForHighlight(h)
after := func(v *View) []int {
var out []int
for _, b := range v.content.Blocks {
out = append(out, b.After)
}
return out
}
if got := after(d.unified); !slices.Equal(got, []int{4, 3}) {
t.Fatalf("unified blocks under rows %v, want [4 3]: B, then b2; line 7 is in a gap", got)
}
d.Split = true
h.v = d.split.Left
waitForHighlight(h)
h.v = d.split.Right
waitForHighlight(h)
if l, r := after(d.split.Left), after(d.split.Right); !slices.Equal(l, []int{3}) || !slices.Equal(r, []int{2}) {
t.Fatalf("split blocks under rows %v | %v, want [3] | [2]", l, r)
}
}