undo_spec.lua (5178B)
1 local t = require('test.testutil') 2 local n = require('test.functional.testnvim')() 3 4 local clear = n.clear 5 local command = n.command 6 local expect = n.expect 7 local eq = t.eq 8 local feed = n.feed 9 local insert = n.insert 10 local fn = n.fn 11 local exec = n.exec 12 local exec_lua = n.exec_lua 13 local pcall_err = t.pcall_err 14 15 local function lastmessage() 16 local messages = fn.split(fn.execute('messages'), '\n') 17 return messages[#messages] 18 end 19 20 describe('u CTRL-R g- g+', function() 21 before_each(clear) 22 23 local function create_history(num_steps) 24 if num_steps == 0 then 25 return 26 end 27 insert('1') 28 if num_steps == 1 then 29 return 30 end 31 feed('o2<esc>') 32 feed('o3<esc>') 33 feed('u') 34 if num_steps == 2 then 35 return 36 end 37 feed('o4<esc>') 38 if num_steps == 3 then 39 return 40 end 41 feed('u') 42 end 43 44 local function undo_and_redo(hist_pos, undo, redo, expect_str) 45 command('enew!') 46 create_history(hist_pos) 47 local cur_contents = n.curbuf_contents() 48 feed(undo) 49 expect(expect_str) 50 feed(redo) 51 expect(cur_contents) 52 end 53 54 -- TODO Look for message saying 'Already at oldest change' 55 it('does nothing when no changes have happened', function() 56 undo_and_redo(0, 'u', '<C-r>', '') 57 undo_and_redo(0, 'g-', 'g+', '') 58 end) 59 it('undoes a change when at a leaf', function() 60 undo_and_redo(1, 'u', '<C-r>', '') 61 undo_and_redo(1, 'g-', 'g+', '') 62 end) 63 it('undoes a change when in a non-leaf', function() 64 undo_and_redo(2, 'u', '<C-r>', '1') 65 undo_and_redo(2, 'g-', 'g+', '1') 66 end) 67 it('undoes properly around a branch point', function() 68 undo_and_redo( 69 3, 70 'u', 71 '<C-r>', 72 [[ 73 1 74 2]] 75 ) 76 undo_and_redo( 77 3, 78 'g-', 79 'g+', 80 [[ 81 1 82 2 83 3]] 84 ) 85 end) 86 it('can find the previous sequence after undoing to a branch', function() 87 undo_and_redo(4, 'u', '<C-r>', '1') 88 undo_and_redo(4, 'g-', 'g+', '1') 89 end) 90 91 describe('undo works correctly when writing in Insert mode', function() 92 before_each(function() 93 exec([[ 94 edit Xtestfile.txt 95 set undolevels=100 undofile 96 write 97 ]]) 98 end) 99 100 after_each(function() 101 command('bwipe!') 102 os.remove('Xtestfile.txt') 103 os.remove('Xtestfile.txt.un~') 104 end) 105 106 -- oldtest: Test_undo_after_write() 107 it('using <Cmd> mapping', function() 108 command('imap . <Cmd>write<CR>') 109 feed('Otest.<CR>boo!!!<Esc>') 110 expect([[ 111 test 112 boo!!! 113 ]]) 114 115 feed('u') 116 expect([[ 117 test 118 ]]) 119 120 feed('u') 121 expect('') 122 end) 123 124 it('using Lua mapping', function() 125 exec_lua([[ 126 vim.api.nvim_set_keymap('i', '.', '', {callback = function() 127 vim.cmd('write') 128 end}) 129 ]]) 130 feed('Otest.<CR>boo!!!<Esc>') 131 expect([[ 132 test 133 boo!!! 134 ]]) 135 136 feed('u') 137 expect([[ 138 test 139 ]]) 140 141 feed('u') 142 expect('') 143 end) 144 145 it('using RPC call', function() 146 feed('Otest') 147 command('write') 148 feed('<CR>boo!!!<Esc>') 149 expect([[ 150 test 151 boo!!! 152 ]]) 153 154 feed('u') 155 expect([[ 156 test 157 ]]) 158 159 feed('u') 160 expect('') 161 end) 162 end) 163 end) 164 165 describe(':undo! command', function() 166 before_each(function() 167 clear() 168 feed('i1 little bug in the code<Esc>') 169 feed('o1 little bug in the code<Esc>') 170 feed('oTake 1 down, patch it around<Esc>') 171 feed('o99 little bugs in the code<Esc>') 172 end) 173 it('works', function() 174 command('undo!') 175 expect([[ 176 1 little bug in the code 177 1 little bug in the code 178 Take 1 down, patch it around]]) 179 feed('<C-r>') 180 eq('Already at newest change', lastmessage()) 181 end) 182 it('works with arguments', function() 183 command('undo! 2') 184 expect([[ 185 1 little bug in the code 186 1 little bug in the code]]) 187 feed('<C-r>') 188 eq('Already at newest change', lastmessage()) 189 end) 190 it('correctly sets alternative redo', function() 191 feed('uo101 little bugs in the code<Esc>') 192 command('undo!') 193 feed('<C-r>') 194 expect([[ 195 1 little bug in the code 196 1 little bug in the code 197 Take 1 down, patch it around 198 99 little bugs in the code]]) 199 200 feed('uuoTake 2 down, patch them around<Esc>') 201 feed('o101 little bugs in the code<Esc>') 202 command('undo! 2') 203 feed('<C-r><C-r>') 204 expect([[ 205 1 little bug in the code 206 1 little bug in the code 207 Take 1 down, patch it around 208 99 little bugs in the code]]) 209 end) 210 it('fails when attempting to redo or move to different undo branch', function() 211 eq( 212 'Vim(undo):E5767: Cannot use :undo! to redo or move to a different undo branch', 213 pcall_err(command, 'undo! 4') 214 ) 215 feed('u') 216 eq( 217 'Vim(undo):E5767: Cannot use :undo! to redo or move to a different undo branch', 218 pcall_err(command, 'undo! 4') 219 ) 220 feed('o101 little bugs in the code<Esc>') 221 feed('o101 little bugs in the code<Esc>') 222 eq( 223 'Vim(undo):E5767: Cannot use :undo! to redo or move to a different undo branch', 224 pcall_err(command, 'undo! 4') 225 ) 226 end) 227 end)