robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 1dc9728daa1d8a2702

master
9.2 KiB360 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3
4local describe, it, before_each, after_each = t.describe, t.it, t.before_each, t.after_each
5local clear = n.clear
6local command = n.command
7local expect = n.expect
8local eq = t.eq
9local feed = n.feed
10local insert = n.insert
11local fn = n.fn
12local exec = n.exec
13local exec_lua = n.exec_lua
14local pcall_err = t.pcall_err
15
16local function lastmessage()
17  local messages = fn.split(fn.execute('messages'), '\n')
18  return messages[#messages]
19end
20
21describe('u CTRL-R g- g+', function()
22  before_each(clear)
23
24  local function create_history(num_steps)
25    if num_steps == 0 then
26      return
27    end
28    insert('1')
29    if num_steps == 1 then
30      return
31    end
32    feed('o2<esc>')
33    feed('o3<esc>')
34    feed('u')
35    if num_steps == 2 then
36      return
37    end
38    feed('o4<esc>')
39    if num_steps == 3 then
40      return
41    end
42    feed('u')
43  end
44
45  local function undo_and_redo(hist_pos, undo, redo, expect_str)
46    command('enew!')
47    create_history(hist_pos)
48    local cur_contents = n.curbuf_contents()
49    feed(undo)
50    expect(expect_str)
51    feed(redo)
52    expect(cur_contents)
53  end
54
55  -- TODO Look for message saying 'Already at oldest change'
56  it('does nothing when no changes have happened', function()
57    undo_and_redo(0, 'u', '<C-r>', '')
58    undo_and_redo(0, 'g-', 'g+', '')
59  end)
60
61  it('undoes a change when at a leaf', function()
62    undo_and_redo(1, 'u', '<C-r>', '')
63    undo_and_redo(1, 'g-', 'g+', '')
64  end)
65
66  it('undoes a change when in a non-leaf', function()
67    undo_and_redo(2, 'u', '<C-r>', '1')
68    undo_and_redo(2, 'g-', 'g+', '1')
69  end)
70
71  it('undoes properly around a branch point', function()
72    undo_and_redo(
73      3,
74      'u',
75      '<C-r>',
76      [[
77      1
78      2]]
79    )
80    undo_and_redo(
81      3,
82      'g-',
83      'g+',
84      [[
85      1
86      2
87      3]]
88    )
89  end)
90
91  it('can find the previous sequence after undoing to a branch', function()
92    undo_and_redo(4, 'u', '<C-r>', '1')
93    undo_and_redo(4, 'g-', 'g+', '1')
94  end)
95
96  it('u restores the pre-change cursor position #5989', function()
97    local api = n.api
98    local function undo_restores(row, col, keys, pre)
99      if pre then
100        command(pre)
101      end
102      api.nvim_buf_set_lines(0, 0, -1, true, { 'this is a test' })
103      api.nvim_win_set_cursor(0, { row, col })
104      feed(keys)
105      feed('u')
106      eq({ row, col }, api.nvim_win_get_cursor(0))
107    end
108    undo_restores(1, 5, 'diw') -- Operator moves to the word start before deleting.
109    undo_restores(1, 5, 'd^') -- Backwards motion.
110    undo_restores(1, 5, 'atest<Esc>') -- Insert entered after the cursor.
111    undo_restores(1, 5, 'viwd')
112    undo_restores(1, 2, ',x', 'nnoremap ,x wdiw') -- Mapping: where it was triggered.
113    -- i_CTRL-G_u breaks: each block restores to its own start.
114    api.nvim_win_set_cursor(0, { 1, 5 })
115    feed('ifoo<C-G>ubar<Esc>u')
116    eq({ 1, 8 }, api.nvim_win_get_cursor(0))
117    feed('u')
118    eq({ 1, 5 }, api.nvim_win_get_cursor(0))
119  end)
120
121  describe('undo works correctly when writing in Insert mode', function()
122    before_each(function()
123      exec([[
124        edit Xtestfile.txt
125        set undolevels=100 undofile
126        write
127      ]])
128    end)
129
130    after_each(function()
131      command('bwipe!')
132      os.remove('Xtestfile.txt')
133      os.remove('Xtestfile.txt.un~')
134    end)
135
136    -- oldtest: Test_undo_after_write()
137    it('using <Cmd> mapping', function()
138      command('imap . <Cmd>write<CR>')
139      feed('Otest.<CR>boo!!!<Esc>')
140      expect([[
141        test
142        boo!!!
143        ]])
144
145      feed('u')
146      expect([[
147        test
148        ]])
149
150      feed('u')
151      expect('')
152    end)
153
154    it('using Lua mapping', function()
155      exec_lua([[
156        vim.api.nvim_set_keymap('i', '.', '', {callback = function()
157          vim.cmd('write')
158        end})
159      ]])
160      feed('Otest.<CR>boo!!!<Esc>')
161      expect([[
162        test
163        boo!!!
164        ]])
165
166      feed('u')
167      expect([[
168        test
169        ]])
170
171      feed('u')
172      expect('')
173    end)
174
175    it('using RPC call', function()
176      feed('Otest')
177      command('write')
178      feed('<CR>boo!!!<Esc>')
179      expect([[
180        test
181        boo!!!
182        ]])
183
184      feed('u')
185      expect([[
186        test
187        ]])
188
189      feed('u')
190      expect('')
191    end)
192  end)
193end)
194
195describe(':undo! command', function()
196  before_each(function()
197    clear()
198    feed('i1 little bug in the code<Esc>')
199    feed('o1 little bug in the code<Esc>')
200    feed('oTake 1 down, patch it around<Esc>')
201    feed('o99 little bugs in the code<Esc>')
202  end)
203
204  it('works', function()
205    command('undo!')
206    expect([[
207      1 little bug in the code
208      1 little bug in the code
209      Take 1 down, patch it around]])
210    feed('<C-r>')
211    eq('Already at newest change', lastmessage())
212  end)
213
214  it('works with arguments', function()
215    command('undo! 2')
216    expect([[
217      1 little bug in the code
218      1 little bug in the code]])
219    feed('<C-r>')
220    eq('Already at newest change', lastmessage())
221  end)
222
223  it('correctly sets alternative redo', function()
224    feed('uo101 little bugs in the code<Esc>')
225    command('undo!')
226    feed('<C-r>')
227    expect([[
228      1 little bug in the code
229      1 little bug in the code
230      Take 1 down, patch it around
231      99 little bugs in the code]])
232
233    feed('uuoTake 2 down, patch them around<Esc>')
234    feed('o101 little bugs in the code<Esc>')
235    command('undo! 2')
236    feed('<C-r><C-r>')
237    expect([[
238      1 little bug in the code
239      1 little bug in the code
240      Take 1 down, patch it around
241      99 little bugs in the code]])
242
243    -- The root header: its alternate must take over as the root, not only as the redo branch.
244    command('new')
245    feed('ia<Esc>')
246    feed('u')
247    feed('ib<Esc>')
248    -- seq 2 is the root, with seq 1 nested as its alternate.
249    local tree = fn.undotree()
250    eq(2, tree.seq_last)
251    eq(1, #tree.entries)
252    eq(2, tree.entries[1].seq)
253    eq(1, tree.entries[1].alt[1].seq)
254    command('undo!')
255    expect('')
256    -- seq 1 is the root now, nothing nested, and it is the redo position.
257    tree = fn.undotree()
258    eq(1, tree.seq_last)
259    eq(1, #tree.entries)
260    eq({ seq = 1, curhead = 1 }, t.pick(tree.entries[1], 'seq', 'curhead', 'alt'))
261    feed('<C-r>')
262    expect('a')
263  end)
264
265  it('fails when attempting to redo or move to different undo branch', function()
266    eq(
267      'Vim(undo):E5767: Cannot use :undo! to redo or move to a different undo branch',
268      pcall_err(command, 'undo! 4')
269    )
270    feed('u')
271    eq(
272      'Vim(undo):E5767: Cannot use :undo! to redo or move to a different undo branch',
273      pcall_err(command, 'undo! 4')
274    )
275    feed('o101 little bugs in the code<Esc>')
276    feed('o101 little bugs in the code<Esc>')
277    eq(
278      'Vim(undo):E5767: Cannot use :undo! to redo or move to a different undo branch',
279      pcall_err(command, 'undo! 4')
280    )
281  end)
282end)
283
284describe("opening file when 'undofile' is on", function()
285  before_each(function()
286    clear({ args = { '--cmd', 'set undofile' } })
287  end)
288
289  it("does not crash when 'undodir' contains empty entry", function()
290    command('set undodir=,.')
291    command('edit test/functional/fixtures/bigfile.txt')
292    n.assert_alive()
293  end)
294end)
295
296describe('undo file', function()
297  before_each(clear)
298
299  --- Offset of the first undo entry in `blob`: magic bytes 0xf5 0x18, followed by 4-byte ue_top,
300  --- ue_bot, ue_lcount, ue_size.
301  local function find_entry(blob)
302    for i = 1, #blob - 18 do
303      if blob:byte(i) == 0xf5 and blob:byte(i + 1) == 0x18 then
304        local ok = true
305        -- This expects the ue_xx values to be small, so other data (e.g. a timestamp) containing
306        -- the magic bytes is not mistaken for an entry.
307        for field = 0, 3 do
308          local off = i + 2 + field * 4
309          ok = ok
310            and blob:byte(off) == 0
311            and blob:byte(off + 1) == 0
312            and blob:byte(off + 2) == 0
313            and blob:byte(off + 3) <= 8
314        end
315        if ok then
316          return i
317        end
318      end
319    end
320  end
321
322  -- If a corrupted entry is not rejected on load, it would be linked into the undo tree, then
323  -- u_undoredo() acts on its line numbers and u_freeentry() walks ue_array.
324  it('rejects corrupted entry (E825)', function()
325    local txt = t.tmpname()
326    local undo = txt .. '.undo'
327
328    t.write_file(txt, 'one\ntwo\n')
329    command('edit ' .. txt)
330    command('set undolevels=100')
331    command("call setline(1, ['three', 'four'])")
332    command('write')
333    command('wundo! ' .. undo)
334
335    local blob = assert(t.read_file(undo))
336    local entry = assert(find_entry(blob))
337
338    --- Writes `bytes` over `blob` at `pos` and reads it via :rundo.
339    local function rundo(pos, bytes)
340      t.write_file(undo, blob:sub(1, pos - 1) .. bytes .. blob:sub(pos + #bytes))
341      return pcall_err(command, 'rundo ' .. undo)
342    end
343
344    -- ue_size larger than the file can hold.
345    for _, bad in ipairs({ '\127\255\255\240', '\255\255\255\255' }) do
346      eq(
347        ('Vim(rundo):E825: Corrupted undo file (entry size): %s'):format(undo),
348        rundo(entry + 14, bad)
349      )
350    end
351
352    -- Negative ue_top/ue_bot/ue_lcount.
353    for field = 0, 2 do
354      eq(
355        ('Vim(rundo):E825: Corrupted undo file (entry lnum): %s'):format(undo),
356        rundo(entry + 2 + field * 4, '\255\255\255\251')
357      )
358    end
359  end)
360end)