-
Notifications
You must be signed in to change notification settings - Fork 1
/
init_test.lua
437 lines (355 loc) · 10.5 KB
/
init_test.lua
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
-- Copyright 2020-2024 Mitchell. See LICENSE.
local file_diff = require('file_diff')
test('file_diff.start should prompt for files to compare', function()
local f1<close> = test.tmpfile()
local f2<close> = test.tmpfile()
local filenames = {f1.filename, f2.filename}
local select_filename = function() return table.remove(filenames, 1) end
local _<close> = test.mock(ui.dialogs, 'open', select_filename)
file_diff.start()
test.assert_equal(#_VIEWS, 2)
test.assert_equal(_VIEWS[view], 1)
test.assert_equal(view.buffer.filename, f1.filename)
test.assert_equal(_VIEWS[2].buffer.filename, f2.filename)
end)
test('file_diff.start should use existing views', function()
view:split()
buffer.new()
file_diff.start('-', '-')
test.assert_equal(#_VIEWS, 2)
test.assert_equal(_BUFFERS[buffer], 1)
test.assert_equal(_BUFFERS[_VIEWS[2].buffer], 2)
end)
local original = test.lines{
'', --
'modify', --
'unchanged', --
'deleted', --
''
}
local changed = test.lines{
'', --
'added', --
'modified', --
'unchanged', --
''
}
local function start_basic_diff()
buffer:append_text(original)
view:split(true)
buffer.new()
buffer:append_text(changed)
file_diff.start('-', '-')
ui.goto_view(_VIEWS[1])
end
test('file_diff should mark added, modified, and deleted lines', function()
start_basic_diff()
local added_lines1 = test.get_marked_lines(file_diff.MARK_ADDITION, _VIEWS[1].buffer)
local added_lines2 = test.get_marked_lines(file_diff.MARK_ADDITION, _VIEWS[2].buffer)
local modified_lines1 = test.get_marked_lines(file_diff.MARK_MODIFICATION, _VIEWS[1].buffer)
local modified_lines2 = test.get_marked_lines(file_diff.MARK_MODIFICATION, _VIEWS[2].buffer)
local deleted_lines1 = test.get_marked_lines(file_diff.MARK_DELETION, _VIEWS[1].buffer)
local deleted_lines2 = test.get_marked_lines(file_diff.MARK_DELETION, _VIEWS[2].buffer)
test.assert_equal(added_lines1, {})
test.assert_equal(added_lines2, {2})
test.assert_equal(modified_lines1, {2})
test.assert_equal(modified_lines2, {3})
test.assert_equal(deleted_lines1, {4})
test.assert_equal(deleted_lines2, {})
end)
test('file_diff should indicated added and deleted ranges', function()
start_basic_diff()
local deleted1 = test.get_indicated_text(file_diff.INDIC_DELETION, _VIEWS[1].buffer)
local deleted2 = test.get_indicated_text(file_diff.INDIC_DELETION, _VIEWS[2].buffer)
local added1 = test.get_indicated_text(file_diff.INDIC_ADDITION, _VIEWS[1].buffer)
local added2 = test.get_indicated_text(file_diff.INDIC_ADDITION, _VIEWS[2].buffer)
test.assert_equal(deleted1, {'y'}) -- from modify
test.assert_equal(deleted2, {})
test.assert_equal(added1, {})
test.assert_equal(added2, {'ied'}) -- from modified
end)
test('file_diff should add dummy lines in place of additions and deletions', function()
start_basic_diff()
test.assert_equal(_VIEWS[1].buffer.annotation_text[1], ' ') -- across from 'added'
test.assert_equal(_VIEWS[2].buffer.annotation_text[4], ' ') -- across from 'deleted'
end)
test('file_diff.goto_change(true) should jump to the next change (left view)', function()
start_basic_diff()
local lines = {}
for i = 1, 3 do
file_diff.goto_change(true)
lines[#lines + 1] = buffer:line_from_position(buffer.current_pos)
end
test.assert_equal(lines, {2, 4, 1}) -- wraps around back to 1
end)
test('file_diff.goto_change should jump to the previous change (left view)', function()
start_basic_diff()
local lines = {}
for i = 1, 3 do
file_diff.goto_change()
lines[#lines + 1] = buffer:line_from_position(buffer.current_pos)
end
test.assert_equal(lines, {4, 2, 1})
end)
expected_failure()
test('file_diff.goto_change(true) should jump to the next change (right view)', function()
start_basic_diff()
ui.goto_view(_VIEWS[2])
local lines = {}
for i = 1, 3 do
file_diff.goto_change(true)
lines[#lines + 1] = buffer:line_from_position(buffer.current_pos)
end
test.assert_equal(lines, {2, 3, 4})
end)
test('file_diff.goto_change should jump to the previous change (right view)', function()
start_basic_diff()
ui.goto_view(_VIEWS[2])
local lines = {}
for i = 1, 3 do
file_diff.goto_change()
lines[#lines + 1] = buffer:line_from_position(buffer.current_pos)
end
test.assert_equal(lines, {4, 3, 2})
end)
test('file_diff.goto_change should treat multi-line changes as a single change', function()
buffer:append_text(test.lines{
'', --
'unchanged', --
'modify', --
'modify', --
'unchanged', --
'deleted', --
'deleted', --
''
})
view:split(true)
buffer.new()
buffer:append_text(test.lines{
'', --
'added', --
'added', --
'unchanged', --
'modified', --
'modified', --
'unchanged', --
''
})
file_diff.start('-', '-')
ui.goto_view(_VIEWS[1])
local lines = {}
for i = 1, 3 do
file_diff.goto_change(true)
lines[#lines + 1] = buffer:line_from_position(buffer.current_pos)
end
test.assert_equal(lines, {3, 6, 1})
end)
test('file_diff.merge should merge from left to right (left view)', function()
start_basic_diff()
file_diff.merge()
test.assert_equal(_VIEWS[view], 1)
test.assert_equal(buffer:line_from_position(buffer.current_pos), 1)
test.assert_equal(buffer:get_text(), original)
test.assert_equal(_VIEWS[2].buffer:get_text(), test.lines{
'', --
'modified', --
'unchanged', --
''
})
file_diff.goto_change(true)
file_diff.merge()
test.assert_equal(buffer:get_text(), original)
test.assert_equal(_VIEWS[2].buffer:get_text(), test.lines{
'', --
'modify', --
'unchanged', --
'' --
})
file_diff.goto_change(true)
file_diff.merge()
test.assert_equal(buffer:get_text(), original)
test.assert_equal(_VIEWS[2].buffer:get_text(), original)
end)
test('file_diff.merge should merge from right to left (left view)', function()
start_basic_diff()
file_diff.merge(true)
test.assert_equal(_VIEWS[view], 1)
test.assert(buffer:line_from_position(buffer.current_pos), 2)
test.assert_equal(buffer:get_text(), test.lines{
'', --
'added', --
'modify', --
'unchanged', --
'deleted', --
''
})
test.assert_equal(_VIEWS[2].buffer:get_text(), changed)
file_diff.goto_change(true)
file_diff.merge(true)
test.assert_equal(buffer:get_text(), test.lines{
'', --
'added', --
'modified', --
'unchanged', --
'deleted', --
''
})
test.assert_equal(_VIEWS[2].buffer:get_text(), changed)
file_diff.goto_change(true)
file_diff.merge(true)
test.assert_equal(buffer:get_text(), changed)
test.assert_equal(_VIEWS[2].buffer:get_text(), changed)
end)
test('file_diff.merge should merge from left to right (right view)', function()
start_basic_diff()
ui.goto_view(_VIEWS[2])
file_diff.goto_change(true)
file_diff.merge()
test.assert_equal(_VIEWS[view], 2)
test.assert_equal(buffer:line_from_position(buffer.current_pos), 2)
test.assert_equal(buffer:get_text(), test.lines{
'', --
'modified', --
'unchanged', --
''
})
test.assert_equal(_VIEWS[1].buffer:get_text(), original)
file_diff.merge() -- no need to go to next change
test.assert_equal(buffer:get_text(), test.lines{
'', --
'modify', --
'unchanged', --
''
})
test.assert_equal(_VIEWS[1].buffer:get_text(), original)
file_diff.goto_change(true)
file_diff.merge()
test.assert_equal(buffer:get_text(), original)
test.assert_equal(_VIEWS[2].buffer:get_text(), original)
end)
test('file_diff.merge should merge from right to left (right view)', function()
start_basic_diff()
ui.goto_view(_VIEWS[2])
file_diff.goto_change(true)
file_diff.merge(true)
test.assert_equal(_VIEWS[view], 2)
test.assert_equal(buffer:line_from_position(buffer.current_pos), 2)
test.assert_equal(buffer:get_text(), changed)
test.assert_equal(_VIEWS[1].buffer:get_text(), test.lines{
'', --
'added', --
'modify', --
'unchanged', --
'deleted', --
''
})
file_diff.goto_change(true)
file_diff.merge(true)
test.assert_equal(buffer:get_text(), changed)
test.assert_equal(_VIEWS[1].buffer:get_text(), test.lines{
'', --
'added', --
'modified', --
'unchanged', --
'deleted', --
''
})
file_diff.goto_change(true)
file_diff.merge(true)
test.assert_equal(buffer:get_text(), changed)
test.assert_equal(_VIEWS[1].buffer:get_text(), changed)
end)
test('file_diff should fill space for a change with additional lines in the right buffer',
function()
buffer:append_text(test.lines{
'', --
'modify', --
'unchanged'
})
view:split(true)
buffer.new()
buffer:append_text(test.lines{
'', --
'modified', --
'added', --
'unchanged'
})
file_diff.start('-', '-')
test.assert_equal(_VIEWS[1].buffer.annotation_text[2], ' ')
end)
test('file_diff should fill space for a change with additional lines in the left buffer', function()
buffer:append_text(test.lines{
'', --
'added', --
'added', --
'unchanged'
})
view:split(true)
buffer.new()
buffer:append_text(test.lines{
'', --
'', --
'unchanged'
})
file_diff.start('-', '-')
test.assert_equal(_VIEWS[2].buffer.annotation_text[2], ' ')
end)
test('file_diff should fill space for a larger change in the right buffer', function()
buffer:append_text(test.lines{
'', --
'modify', --
'modify', --
'unchanged'
})
view:split(true)
buffer.new()
buffer:append_text(test.lines{
'', --
'modified', --
'unchanged'
})
file_diff.start('-', '-')
test.assert_equal(_VIEWS[2].buffer.annotation_text[2], ' ')
end)
test('file_diff should fill space for an addition with additional lines in the right buffer',
function()
buffer:append_text(test.lines{
'', --
'modified', --
'unchanged'
})
view:split(true)
buffer.new()
buffer:append_text(test.lines{
'', --
'modified more', --
'added', --
'unchanged'
})
file_diff.start('-', '-')
test.assert_equal(_VIEWS[1].buffer.annotation_text[2], ' ')
end)
test('file_diff should synchronize scrolling', function()
local f1<close> = test.tmpfile(test.lines(100))
local f2<close> = test.tmpfile(test.lines(100))
file_diff.start(f1.filename, f2.filename)
buffer:page_down()
ui.update() -- trigger events.UPDATE_UI
if CURSES then events.emit(events.UPDATE_UI, buffer.UPDATE_SELECTION) end
test.assert_equal(_VIEWS[1].first_visible_line, _VIEWS[2].first_visible_line)
end)
if WIN32 and GUI then skip('crashes inside Scintilla') end -- TODO:
test('file_diff should stop when switching buffers', function()
start_basic_diff()
view:goto_buffer(-1)
local added_lines = test.get_marked_lines(file_diff.MARK_ADDITION, _VIEWS[1].buffer)
test.assert_equal(added_lines, {})
end)
-- Coverage tests.
test('file_diff.goto_change should notify when there are no more changes', function()
view:split(true)
buffer.new()
file_diff.start('-', '-')
file_diff.goto_change(true)
-- TODO: how to test statusbar was written to? Cannot mock it.
end)