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