-
Notifications
You must be signed in to change notification settings - Fork 34
/
filename_undo_test.go
296 lines (261 loc) · 7.12 KB
/
filename_undo_test.go
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
package main
import (
"os"
"path/filepath"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/rjkroege/edwood/dumpfile"
)
func changeFileName(t *testing.T, g *globals, ffn, _ string) {
t.Helper()
g.row.display.WriteSnarf([]byte("suffix"))
// Mutate
fwt := &g.row.col[0].w[0].tag
// TODO(rjk): WRONG WITH THE UNICODE
fwt.q0 = len(ffn)
fwt.q1 = len(ffn)
t.Log("before", fwt.DebugString())
paste(fwt, fwt, nil, true, false, "")
t.Log("after", fwt.DebugString())
}
func undoFileNameChange(t *testing.T, g *globals, ffn, _ string) {
t.Helper()
changeFileName(t, g, ffn, "")
// TODO(rjk): Move the text position
// to the end?
firstwin := g.row.col[0].w[0]
undo(&firstwin.tag, nil, nil, true /* this is an undo */, false /* ignored */, "")
}
func undoFileNameChangedMultipleEdit(t *testing.T, g *globals, ffn, _ string) {
t.Helper()
// Change name of first.
changeFileName(t, g, ffn, "")
// Mutate both body values.
mutateWithEdit(t, g)
firstwin := g.row.col[0].w[0]
secondwin := g.row.col[0].w[1]
firstwin.body.q0 += 8
firstwin.body.q1 += 10
// Run undo from second window undoes Edit X on both windows.
undo(&secondwin.tag, nil, nil, true /* this is an undo */, false /* ignored */, "")
}
func undoSecondMutateFileNameChange(t *testing.T, g *globals, ffn, _ string) {
t.Helper()
// Mutate both body values.
mutateWithEdit(t, g)
// Change name of first.
changeFileName(t, g, ffn, "")
secondwin := g.row.col[0].w[1]
// TODO(rjk): Move the text position in secondwin
secondwin.body.q0 += 4
secondwin.body.q1 += 5
// Undo on second win only affects the Edit X on second window, filename is still changed.
undo(&secondwin.tag, nil, nil, true /* this is an undo */, false /* ignored */, "")
}
func TestFilenameChangeUndo(t *testing.T) {
dir := t.TempDir()
firstfilename := filepath.Join(dir, "firstfile")
secondfilename := filepath.Join(dir, "secondfile")
cwd, err := os.Getwd()
if err != nil {
t.Fatalf("failed to get current working directory: %v", err)
}
tests := []struct {
name string
fn func(t *testing.T, g *globals, ffn, sfn string)
passing bool
want *dumpfile.Content
}{
{
// Verify that we can edit a file name.
name: "changeFileName",
fn: changeFileName,
passing: true,
want: &dumpfile.Content{
CurrentDir: cwd,
VarFont: defaultVarFont,
FixedFont: defaultFixedFont,
Columns: []dumpfile.Column{
{},
},
Windows: []*dumpfile.Window{
{
Type: dumpfile.Unsaved,
Column: 0,
Tag: dumpfile.Text{
Buffer: firstfilename + "suffix" + " Del Snarf Undo Put | Look Edit ",
Q0: len(firstfilename),
Q1: len(firstfilename) + len("suffix"),
},
// Recall that when the contents match the on-disk state, they are
// elided. Here, while we've not actually changed the body, we've altered
// the tag to be a different filename so the contents no longer match the
// on-disk state.
Body: dumpfile.Text{
Buffer: "This is a\nshort text\nto try addressing\n",
},
},
{
Type: dumpfile.Saved,
Column: 0,
Tag: dumpfile.Text{
Buffer: secondfilename + " Del Snarf | Look Edit ",
},
Body: dumpfile.Text{},
},
},
},
},
{
// Verify that we can edit a file name and then undo the change.
name: "undoFileNameChange",
fn: undoFileNameChange,
// Currently failing. Requires some non-trivial coding adjustments.
passing: true,
want: &dumpfile.Content{
CurrentDir: cwd,
VarFont: defaultVarFont,
FixedFont: defaultFixedFont,
Columns: []dumpfile.Column{
{},
},
Windows: []*dumpfile.Window{
{
Type: dumpfile.Saved,
Column: 0,
Tag: dumpfile.Text{
Buffer: firstfilename + " Del Snarf Redo | Look Edit ",
Q0: len(firstfilename),
Q1: len(firstfilename),
},
Body: dumpfile.Text{},
},
{
Type: dumpfile.Saved,
Column: 0,
Tag: dumpfile.Text{
Buffer: secondfilename + " Del Snarf | Look Edit ",
},
Body: dumpfile.Text{},
},
},
},
},
{
// Verify that multiple file mutation will Undo and leave file name
// change unaffected.
name: "undoFileNameChangedMultipleEdit",
fn: undoFileNameChangedMultipleEdit,
passing: true,
want: &dumpfile.Content{
CurrentDir: cwd,
VarFont: defaultVarFont,
FixedFont: defaultFixedFont,
Columns: []dumpfile.Column{
{},
},
Windows: []*dumpfile.Window{
{
Type: dumpfile.Unsaved,
Column: 0,
Tag: dumpfile.Text{
Buffer: firstfilename + "suffix" + " Del Snarf Undo Redo Put | Look Edit ",
Q0: len(firstfilename),
Q1: len(firstfilename) + len("suffix"),
},
Body: dumpfile.Text{
Buffer: "This is a\nshort text\nto try addressing\n",
Q0: 16,
Q1: 20,
},
},
{
Type: dumpfile.Saved,
Column: 0,
Tag: dumpfile.Text{
Buffer: secondfilename + " Del Snarf Redo | Look Edit ",
},
Body: dumpfile.Text{
Q0: 12,
Q1: 16,
},
},
},
},
},
{
// Mutate both with Edit X, change the name of the first, undo on the
// second. Verify that we can diverge the undo history featuring a file
// name change and just undo the Edit X on second.
name: "undoSecondMutateFileNameChange",
fn: undoSecondMutateFileNameChange,
passing: true,
// Q1 is not correctly updated.
want: &dumpfile.Content{
CurrentDir: cwd,
VarFont: defaultVarFont,
FixedFont: defaultFixedFont,
Columns: []dumpfile.Column{
{},
},
Windows: []*dumpfile.Window{
{
Type: dumpfile.Unsaved,
Column: 0,
Tag: dumpfile.Text{
Buffer: firstfilename + "suffix" + " Del Snarf Undo Put | Look Edit ",
Q0: len(firstfilename),
Q1: len(firstfilename) + len("suffix"),
},
Body: dumpfile.Text{
Buffer: "This is a\nshort TEXT\nto try addressing\n",
Q0: 16,
Q1: 20,
},
},
{
Type: dumpfile.Saved,
Column: 0,
Tag: dumpfile.Text{
Buffer: secondfilename + " Del Snarf Redo | Look Edit ",
},
Body: dumpfile.Text{
Q0: 12,
Q1: 16,
},
},
},
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
// Skip known failures.
if !tc.passing {
return
}
FlexiblyMakeWindowScaffold(
t,
ScWin("firstfile"),
ScBody("firstfile", contents),
ScDir(dir, "firstfile"),
ScWin("secondfile"),
ScBody("secondfile", alt_contents),
ScDir(dir, "secondfile"),
)
// Probably there are other issues here...
t.Log("seq", global.seq)
t.Log("seq, w0", global.row.col[0].w[0].body.file.Seq())
t.Log("seq, w1", global.row.col[0].w[1].body.file.Seq())
tc.fn(t, global, firstfilename, secondfilename)
t.Log(*varfontflag, defaultVarFont)
got, err := global.row.dump()
if err != nil {
t.Fatalf("dump failed: %v", err)
}
if diff := cmp.Diff(tc.want, got); diff != "" {
t.Errorf("dump mismatch (-want +got):\n%s", diff)
}
})
}
}