robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
a0dc3f0067
master
1" Tests for the undo tree. 2" Since this script is sourced we need to explicitly break changes up in 3" undo-able pieces. Do that by setting 'undolevels'. 4" Also tests :earlier and :later. 5 6source check.vim 7source screendump.vim 8 9func Test_undotree () 10new 11 12normal ! Aabc 13set ul = 100 14let d = undotree () 15call assert_equal ( 1 , d . seq_last ) 16call assert_equal ( 1 , d . seq_cur ) 17call assert_equal ( 0 , d . save_last ) 18call assert_equal ( 0 , d . save_cur ) 19call assert_equal ( 1 , len ( d . entries )) 20call assert_equal ( 1 , d . entries [ 0 ]. newhead ) 21call assert_equal ( 1 , d . entries [ 0 ]. seq ) 22call assert_true ( d . entries [ 0 ]. time <= d . time_cur ) 23 24normal ! Adef 25set ul = 100 26let d = undotree () 27call assert_equal ( 2 , d . seq_last ) 28call assert_equal ( 2 , d . seq_cur ) 29call assert_equal ( 0 , d . save_last ) 30call assert_equal ( 0 , d . save_cur ) 31call assert_equal ( 2 , len ( d . entries )) 32call assert_equal ( 1 , d . entries [ 0 ]. seq ) 33call assert_equal ( 1 , d . entries [ 1 ]. newhead ) 34call assert_equal ( 2 , d . entries [ 1 ]. seq ) 35call assert_true ( d . entries [ 1 ]. time <= d . time_cur ) 36 37undo 38set ul = 100 39let d = undotree () 40call assert_equal ( 2 , d . seq_last ) 41call assert_equal ( 1 , d . seq_cur ) 42call assert_equal ( 0 , d . save_last ) 43call assert_equal ( 0 , d . save_cur ) 44call assert_equal ( 2 , len ( d . entries )) 45call assert_equal ( 1 , d . entries [ 0 ]. seq ) 46call assert_equal ( 1 , d . entries [ 1 ]. curhead ) 47call assert_equal ( 1 , d . entries [ 1 ]. newhead ) 48call assert_equal ( 2 , d . entries [ 1 ]. seq ) 49call assert_true ( d . entries [ 1 ]. time == d . time_cur ) 50 51normal ! Aghi 52set ul = 100 53let d = undotree () 54call assert_equal ( 3 , d . seq_last ) 55call assert_equal ( 3 , d . seq_cur ) 56call assert_equal ( 0 , d . save_last ) 57call assert_equal ( 0 , d . save_cur ) 58call assert_equal ( 2 , len ( d . entries )) 59call assert_equal ( 1 , d . entries [ 0 ]. seq ) 60call assert_equal ( 2 , d . entries [ 1 ]. alt [ 0 ]. seq ) 61call assert_equal ( 1 , d . entries [ 1 ]. newhead ) 62call assert_equal ( 3 , d . entries [ 1 ]. seq ) 63call assert_true ( d . entries [ 1 ]. time <= d . time_cur ) 64 65undo 66set ul = 100 67let d = undotree () 68call assert_equal ( 3 , d . seq_last ) 69call assert_equal ( 1 , d . seq_cur ) 70call assert_equal ( 0 , d . save_last ) 71call assert_equal ( 0 , d . save_cur ) 72call assert_equal ( 2 , len ( d . entries )) 73call assert_equal ( 1 , d . entries [ 0 ]. seq ) 74call assert_equal ( 2 , d . entries [ 1 ]. alt [ 0 ]. seq ) 75call assert_equal ( 1 , d . entries [ 1 ]. curhead ) 76call assert_equal ( 1 , d . entries [ 1 ]. newhead ) 77call assert_equal ( 3 , d . entries [ 1 ]. seq ) 78call assert_true ( d . entries [ 1 ]. time == d . time_cur ) 79 80w ! Xtest 81let d = undotree () 82call assert_equal ( 1 , d . save_cur ) 83call assert_equal ( 1 , d . save_last ) 84call delete ( 'Xtest' ) 85bwipe ! Xtest 86endfunc 87 88func FillBuffer () 89for i in range ( 1 , 13 ) 90put =i 91" Set 'undolevels' to split undo. 92exe "setg ul=" . & g: ul 93endfor 94endfunc 95 96func Test_undotree_bufnr () 97new 98let buf1 = bufnr () 99 100normal ! Aabc 101set ul = 100 102 103" Save undo tree without bufnr as ground truth for buffer 1 104let d1 = undotree () 105 106new 107let buf2 = bufnr () 108 109normal ! Adef 110set ul = 100 111 112normal ! Aghi 113set ul = 100 114 115" Save undo tree without bufnr as ground truth for buffer 2 116let d2 = undotree () 117 118" Check undotree() with bufnr argument 119let d = undotree ( buf1 ) 120call assert_equal ( d1 , d ) 121call assert_notequal ( d2 , d ) 122 123let d = undotree ( buf2 ) 124call assert_notequal ( d1 , d ) 125call assert_equal ( d2 , d ) 126 127" Switch buffers and check again 128wincmd p 129 130let d = undotree ( buf1 ) 131call assert_equal ( d1 , d ) 132 133let d = undotree ( buf2 ) 134call assert_notequal ( d1 , d ) 135call assert_equal ( d2 , d ) 136 137" error cases 138call assert_fails ( 'call undotree(-1)' , 'E158:' ) 139call assert_fails ( 'call undotree("nosuchbuf")' , 'E158:' ) 140 141" after creating a buffer nosuchbuf, undotree('nosuchbuf') should 142" not error out 143new nosuchbuf 144let d = { 'seq_last' : 0 , 'entries' : [], 'time_cur' : 0 , 'save_last' : 0 , 'synced' : 1 , 'save_cur' : 0 , 'seq_cur' : 0 } 145call assert_equal ( d , undotree ( "nosuchbuf" )) 146" clean up 147bw nosuchbuf 148 149" Drop created windows 150set ul & 151new 152only ! 153endfunc 154 155func Test_global_local_undolevels () 156new one 157set undolevels = 5 158call FillBuffer () 159" will only undo the last 5 changes, end up with 13 - (5 + 1) = 7 lines 160earlier 10 161call assert_equal ( 5 , & g: undolevels ) 162call assert_equal ( -123456 , & l: undolevels ) 163call assert_equal ( '7' , getline ( '$' )) 164 165new two 166setlocal undolevels = 2 167call FillBuffer () 168" will only undo the last 2 changes, end up with 13 - (2 + 1) = 10 lines 169earlier 10 170call assert_equal ( 5 , & g: undolevels ) 171call assert_equal ( 2 , & l: undolevels ) 172call assert_equal ( '10' , getline ( '$' )) 173 174setlocal ul = 10 175call assert_equal ( 5 , & g: undolevels ) 176call assert_equal ( 10 , & l: undolevels ) 177 178" Setting local value in "two" must not change local value in "one" 179wincmd p 180call assert_equal ( 5 , & g: undolevels ) 181call assert_equal ( -123456 , & l: undolevels ) 182 183new three 184setglobal ul=50 185call assert_equal ( 50 , & g: undolevels ) 186call assert_equal ( -123456 , & l: undolevels ) 187 188" Resetting the local 'undolevels' value to use the global value 189setlocal undolevels = 5 190"setlocal undolevels< 191set undolevels < 192call assert_equal ( -123456 , & l: undolevels ) 193 194" Drop created windows 195set ul & 196new 197only ! 198endfunc 199 200func BackOne ( expected ) 201call feedkeys ( 'g-' , 'xt' ) 202call assert_equal ( a: expected , getline ( 1 )) 203endfunc 204 205func Test_undo_del_chars () 206throw 'Skipped: Nvim does not support test_settime()' 207" Setup a buffer without creating undo entries 208new 209set ul = -1 210call setline ( 1 , [ '123-456' ]) 211set ul = 100 2121 213call test_settime ( 100 ) 214 215" Delete three characters and undo with g- 216call feedkeys ( 'x' , 'xt' ) 217call feedkeys ( 'x' , 'xt' ) 218call feedkeys ( 'x' , 'xt' ) 219call assert_equal ( '-456' , getline ( 1 )) 220call BackOne ( '3-456' ) 221call BackOne ( '23-456' ) 222call BackOne ( '123-456' ) 223call assert_fails ( "BackOne('123-456')" , "E492: Not an editor command: BackOne('123-456')" ) 224 225: "Delete three other characters and go back in time with g- 226call feedkeys ( '$x' , 'xt' ) 227call feedkeys ( 'x' , 'xt' ) 228call feedkeys ( 'x' , 'xt' ) 229call assert_equal ( '123-' , getline ( 1 )) 230call test_settime ( 101 ) 231 232call BackOne ( '123-4' ) 233call BackOne ( '123-45' ) 234" skips '123-456' because it's older 235call BackOne ( '-456' ) 236call BackOne ( '3-456' ) 237call BackOne ( '23-456' ) 238call BackOne ( '123-456' ) 239call assert_fails ( "BackOne('123-456')" , "E492: Not an editor command: BackOne('123-456')" ) 240normal 10g+ 241call assert_equal ( '123-' , getline ( 1 )) 242 243: "Jump two seconds and go some seconds forward and backward 244call test_settime ( 103 ) 245call feedkeys ( "Aa\<Esc>" , 'xt' ) 246call feedkeys ( "Ab\<Esc>" , 'xt' ) 247call feedkeys ( "Ac\<Esc>" , 'xt' ) 248call assert_equal ( '123-abc' , getline ( 1 )) 249earlier 1s 250call assert_equal ( '123-' , getline ( 1 )) 251earlier 3s 252call assert_equal ( '123-456' , getline ( 1 )) 253later 1s 254call assert_equal ( '123-' , getline ( 1 )) 255later 1h 256call assert_equal ( '123-abc' , getline ( 1 )) 257 258close ! 259endfunc 260 261func Test_undolist () 262new 263set ul = 100 264 265let a = execute ( 'undolist' ) 266call assert_equal ( "\nNothing to undo" , a ) 267 268" 1 leaf (2 changes). 269call feedkeys ( 'achange1' , 'xt' ) 270call feedkeys ( 'achange2' , 'xt' ) 271let a = execute ( 'undolist' ) 272call assert_match ( "^\nnumber changes when *saved\n *2 *2 .*$" , a ) 273 274" 2 leaves. 275call feedkeys ( 'u' , 'xt' ) 276call feedkeys ( 'achange3\<Esc>' , 'xt' ) 277let a = execute ( 'undolist' ) 278call assert_match ( "^\nnumber changes when *saved\n *2 *2 *.*\n *3 *2 .*$" , a ) 279close ! 280endfunc 281 282func Test_U_command () 283new 284set ul = 100 285call feedkeys ( "achange1\<Esc>" , 'xt' ) 286call feedkeys ( "achange2\<Esc>" , 'xt' ) 287norm ! U 288call assert_equal ( '' , getline ( 1 )) 289norm ! U 290call assert_equal ( 'change1change2' , getline ( 1 )) 291close ! 292endfunc 293 294func Test_undojoin () 295new 296call feedkeys ( "Goaaaa\<Esc>" , 'xt' ) 297call feedkeys ( "obbbb\<Esc>" , 'xt' ) 298call assert_equal ([ 'aaaa' , 'bbbb' ], getline ( 2 , '$' )) 299call feedkeys ( "u" , 'xt' ) 300call assert_equal ([ 'aaaa' ], getline ( 2 , '$' )) 301call feedkeys ( "obbbb\<Esc>" , 'xt' ) 302undojoin 303" Note: next change must not be as if typed 304call feedkeys ( "occcc\<Esc>" , 'x' ) 305call assert_equal ([ 'aaaa' , 'bbbb' , 'cccc' ], getline ( 2 , '$' )) 306call feedkeys ( "u" , 'xt' ) 307call assert_equal ([ 'aaaa' ], getline ( 2 , '$' )) 308bwipe ! 309endfunc 310 311func Test_undojoin_redo () 312new 313call setline ( 1 , [ 'first line' , 'second line' ]) 314call feedkeys ( "ixx\<Esc>" , 'xt' ) 315call feedkeys ( ":undojoin | redo\<CR>" , 'xt' ) 316call assert_equal ( 'xxfirst line' , getline ( 1 )) 317call assert_equal ( 'second line' , getline ( 2 )) 318bwipe ! 319endfunc 320 321" undojoin not allowed after undo 322func Test_undojoin_after_undo () 323new 324call feedkeys ( "ixx\<Esc>u" , 'xt' ) 325call assert_fails ( ':undojoin' , 'E790:' ) 326bwipe ! 327endfunc 328 329" undojoin is a noop when no change yet, or when 'undolevels' is negative 330func Test_undojoin_noop () 331new 332call feedkeys ( ":undojoin\<CR>" , 'xt' ) 333call assert_equal ([ '' ], getline ( 1 , '$' )) 334setlocal undolevels = -1 335call feedkeys ( "ixx\<Esc>u" , 'xt' ) 336call feedkeys ( ":undojoin\<CR>" , 'xt' ) 337call assert_equal ([ 'xx' ], getline ( 1 , '$' )) 338bwipe ! 339endfunc 340 341func Test_undo_write () 342call delete ( 'Xtest' ) 343split Xtest 344call feedkeys ( "ione one one\<Esc>" , 'xt' ) 345w ! 346call feedkeys ( "otwo\<Esc>" , 'xt' ) 347call feedkeys ( "otwo\<Esc>" , 'xt' ) 348w 349call feedkeys ( "othree\<Esc>" , 'xt' ) 350call assert_equal ([ 'one one one' , 'two' , 'two' , 'three' ], getline ( 1 , '$' )) 351earlier 1f 352call assert_equal ([ 'one one one' , 'two' , 'two' ], getline ( 1 , '$' )) 353earlier 1f 354call assert_equal ([ 'one one one' ], getline ( 1 , '$' )) 355earlier 1f 356call assert_equal ([ '' ], getline ( 1 , '$' )) 357later 1f 358call assert_equal ([ 'one one one' ], getline ( 1 , '$' )) 359later 1f 360call assert_equal ([ 'one one one' , 'two' , 'two' ], getline ( 1 , '$' )) 361later 1f 362call assert_equal ([ 'one one one' , 'two' , 'two' , 'three' ], getline ( 1 , '$' )) 363 364close ! 365call delete ( 'Xtest' ) 366bwipe ! Xtest 367 368call assert_fails ( 'earlier xyz' , 'E475:' ) 369endfunc 370 371func Test_insert_expr () 372new 373" calling setline() triggers undo sync 374call feedkeys ( "oa\<Esc>" , 'xt' ) 375call feedkeys ( "ob\<Esc>" , 'xt' ) 376set ul = 100 377call feedkeys ( "o1\<Esc>a2\<C-R>=setline('.','1234')\<CR>\<CR>\<Esc>" , 'x' ) 378call assert_equal ([ 'a' , 'b' , '120' , '34' ], getline ( 2 , '$' )) 379call feedkeys ( "u" , 'x' ) 380call assert_equal ([ 'a' , 'b' , '12' ], getline ( 2 , '$' )) 381call feedkeys ( "u" , 'x' ) 382call assert_equal ([ 'a' , 'b' ], getline ( 2 , '$' )) 383 384call feedkeys ( "oc\<Esc>" , 'xt' ) 385set ul = 100 386call feedkeys ( "o1\<Esc>a2\<C-R>=setline('.','1234')\<CR>\<CR>\<Esc>" , 'x' ) 387call assert_equal ([ 'a' , 'b' , 'c' , '120' , '34' ], getline ( 2 , '$' )) 388call feedkeys ( "u" , 'x' ) 389call assert_equal ([ 'a' , 'b' , 'c' , '12' ], getline ( 2 , '$' )) 390 391call feedkeys ( "od\<Esc>" , 'xt' ) 392set ul = 100 393call feedkeys ( "o1\<Esc>a2\<C-R>=string(123)\<CR>\<Esc>" , 'x' ) 394call assert_equal ([ 'a' , 'b' , 'c' , '12' , 'd' , '12123' ], getline ( 2 , '$' )) 395call feedkeys ( "u" , 'x' ) 396call assert_equal ([ 'a' , 'b' , 'c' , '12' , 'd' ], getline ( 2 , '$' )) 397 398close ! 399endfunc 400 401func Test_undofile_earlier () 402throw 'Skipped: Nvim does not support test_settime()' 403if has ( 'win32' ) 404" FIXME: This test is flaky on MS-Windows. 405let g: test_is_flaky = 1 406endif 407 408" Issue #1254 409" create undofile with timestamps older than Vim startup time. 410let t0 = localtime () - 43200 411call test_settime ( t0 ) 412new XfileEarlier 413call feedkeys ( "ione\<Esc>" , 'xt' ) 414set ul = 100 415call test_settime ( t0 + 1 ) 416call feedkeys ( "otwo\<Esc>" , 'xt' ) 417set ul = 100 418call test_settime ( t0 + 2 ) 419call feedkeys ( "othree\<Esc>" , 'xt' ) 420set ul = 100 421w 422wundo Xundofile 423bwipe ! 424" restore normal timestamps. 425call test_settime ( 0 ) 426new XfileEarlier 427rundo Xundofile 428earlier 1d 429call assert_equal ( '' , getline ( 1 )) 430bwipe ! 431call delete ( 'XfileEarlier' ) 432call delete ( 'Xundofile' ) 433endfunc 434 435func Test_wundo_errors () 436new 437call setline ( 1 , 'hello' ) 438call assert_fails ( 'wundo! Xdoesnotexist/Xundofile' , 'E828:' ) 439bwipe ! 440endfunc 441 442" Check that reading a truncated undo file doesn't hang. 443func Test_undofile_truncated () 444new 445call setline ( 1 , 'hello' ) 446set ul = 100 447wundo Xundofile 448let contents = readfile ( 'Xundofile' , 'B' ) 449 450" try several sizes 451for size in range ( 20 , 500 , 33 ) 452call writefile ( contents [ 0 : size ], 'Xundofile' ) 453call assert_fails ( 'rundo Xundofile' , 'E825:' ) 454endfor 455 456bwipe ! 457call delete ( 'Xundofile' ) 458endfunc 459 460func Test_rundo_errors () 461call assert_fails ( 'rundo XfileDoesNotExist' , 'E822:' ) 462 463call writefile ([ 'abc' ], 'Xundofile' ) 464call assert_fails ( 'rundo Xundofile' , 'E823:' ) 465 466call delete ( 'Xundofile' ) 467endfunc 468 469func Test_undofile_next () 470set undofile 471new Xfoo.txt 472execute "norm ix\<c-g>uy\<c-g>uz\<Esc>" 473write 474bwipe 475 476next Xfoo.txt 477call assert_equal ( 'xyz' , getline ( 1 )) 478silent undo 479call assert_equal ( 'xy' , getline ( 1 )) 480silent undo 481call assert_equal ( 'x' , getline ( 1 )) 482bwipe ! 483 484call delete ( 'Xfoo.txt' ) 485call delete ( '.Xfoo.txt.un~' ) 486set undofile & 487endfunc 488 489" Test for undo working properly when executing commands from a register. 490" Also test this in an empty buffer. 491func Test_cmd_in_reg_undo () 492enew ! 493let @a= "Ox\<Esc>jAy\<Esc>kdd" 494edit +/ ^$ test_undo.vim 495normal @au 496call assert_equal ( 0 , & modified ) 497return 498new 499normal @au 500call assert_equal ( 0 , & modified ) 501only ! 502let @a= '' 503endfunc 504 505" This used to cause an illegal memory access 506func Test_undo_append () 507new 508call feedkeys ( "axx\<Esc>v" , 'xt' ) 509undo 510norm o 511quit 512endfunc 513 514func Test_undo_0 () 515new 516set ul = 100 517normal i1 518undo 519normal i2 520undo 521normal i3 522 523undo 0 524let d = undotree () 525call assert_equal ( '' , getline ( 1 )) 526call assert_equal ( 0 , d . seq_cur ) 527 528redo 529let d = undotree () 530call assert_equal ( '3' , getline ( 1 )) 531call assert_equal ( 3 , d . seq_cur ) 532 533undo 2 534undo 0 535let d = undotree () 536call assert_equal ( '' , getline ( 1 )) 537call assert_equal ( 0 , d . seq_cur ) 538 539redo 540let d = undotree () 541call assert_equal ( '2' , getline ( 1 )) 542call assert_equal ( 2 , d . seq_cur ) 543 544undo 1 545undo 0 546let d = undotree () 547call assert_equal ( '' , getline ( 1 )) 548call assert_equal ( 0 , d . seq_cur ) 549 550redo 551let d = undotree () 552call assert_equal ( '1' , getline ( 1 )) 553call assert_equal ( 1 , d . seq_cur ) 554 555bwipe ! 556endfunc 557 558" undo or redo are noop if there is nothing to undo or redo 559func Test_undo_redo_noop () 560new 561call assert_fails ( 'undo 2' , 'E830:' ) 562 563message clear 564undo 565let messages = split ( execute ( 'message' ), "\n" ) 566call assert_equal ( 'Already at oldest change' , messages [ -1 ]) 567 568message clear 569redo 570let messages = split ( execute ( 'message' ), "\n" ) 571call assert_equal ( 'Already at newest change' , messages [ -1 ]) 572 573bwipe ! 574endfunc 575 576func Test_redo_empty_line () 577new 578exe "norm\x16r\x160" 579exe "norm." 580bwipe ! 581endfunc 582 583funct Test_undofile () 584" Test undofile() without setting 'undodir'. 585if has ( 'persistent_undo' ) 586call assert_equal ( fnamemodify ( '.Xundofoo.un~' , ':p' ), undofile ( 'Xundofoo' )) 587else 588call assert_equal ( '' , undofile ( 'Xundofoo' )) 589endif 590call assert_equal ( '' , undofile ( '' )) 591 592" Test undofile() with 'undodir' set to an existing directory. 593call mkdir ( 'Xundodir' ) 594set undodir = Xundodir 595let cwd = getcwd () 596if has ( 'win32' ) 597" Replace windows drive such as C:... into C%... 598let cwd = substitute ( cwd , '^\([a-zA-Z]\):' , '\1%' , 'g' ) 599endif 600let cwd = substitute ( cwd . '/Xundofoo' , '/' , '%' , 'g' ) 601if has ( 'persistent_undo' ) 602call assert_equal ( 'Xundodir/' . cwd , undofile ( 'Xundofoo' )) 603else 604call assert_equal ( '' , undofile ( 'Xundofoo' )) 605endif 606call assert_equal ( '' , undofile ( '' )) 607call delete ( 'Xundodir' , 'd' ) 608 609" Test undofile() with 'undodir' set to a non-existing directory. 610" call assert_equal('', 'Xundofoo'->undofile()) 611 612if isdirectory ( '/tmp' ) 613set undodir = /tmp 614if has ( 'osx' ) 615call assert_equal ( '/tmp/%private%tmp%file' , undofile ( '///tmp/file' )) 616else 617call assert_equal ( '/tmp/%tmp%file' , undofile ( '///tmp/file' )) 618endif 619endif 620 621set undodir & 622endfunc 623 624" Tests for the undo file 625" Explicitly break changes up in undo-able pieces by setting 'undolevels'. 626func Test_undofile_2 () 627set undolevels = 100 undofile 628edit Xtestfile 629call append ( 0 , 'this is one line' ) 630call cursor ( 1 , 1 ) 631 632" first a simple one-line change. 633set undolevels = 100 634s /one/ONE/ 635set undolevels = 100 636write 637bwipe ! 638edit Xtestfile 639undo 640call assert_equal ( 'this is one line' , getline ( 1 )) 641 642" change in original file fails check 643set noundofile 644edit ! Xtestfile 645s /line/Line/ 646write 647set undofile 648bwipe ! 649edit Xtestfile 650undo 651call assert_equal ( 'this is ONE Line' , getline ( 1 )) 652 653" add 10 lines, delete 6 lines, undo 3 654set undofile 655call setbufline ( '%' , 1 , [ 'one' , 'two' , 'three' , 'four' , 'five' , 'six' , 656 \'seven' , 'eight' , 'nine' , 'ten' ]) 657set undolevels = 100 658normal 3Gdd 659set undolevels = 100 660normal dd 661set undolevels = 100 662normal dd 663set undolevels = 100 664normal dd 665set undolevels = 100 666normal dd 667set undolevels = 100 668normal dd 669set undolevels = 100 670write 671bwipe ! 672edit Xtestfile 673normal uuu 674call assert_equal ([ 'one' , 'two' , 'six' , 'seven' , 'eight' , 'nine' , 'ten' ], 675 \getline ( 1 , '$' )) 676 677" Test that reading the undofiles when setting undofile works 678set noundofile undolevels = 0 679exe "normal i\n" 680undo 681edit ! Xtestfile 682set undofile undolevels = 100 683normal uuuuuu 684call assert_equal ([ 'one' , 'two' , 'three' , 'four' , 'five' , 'six' , 'seven' , 685 \'eight' , 'nine' , 'ten' ], getline ( 1 , '$' )) 686 687bwipe ! 688call delete ( 'Xtestfile' ) 689let ufile = has ( 'vms' ) ? '_un_Xtestfile' : '.Xtestfile.un~' 690call delete ( ufile ) 691set undofile & undolevels & 692endfunc 693 694" Test 'undofile' using a file encrypted with 'zip' crypt method 695func Test_undofile_cryptmethod_zip () 696throw 'skipped: Nvim does not support cryptmethod' 697edit Xtestfile 698set undofile cryptmethod = zip 699call append ( 0 , [ 'monday' , 'tuesday' , 'wednesday' , 'thursday' , 'friday' ]) 700call cursor ( 5 , 1 ) 701 702set undolevels = 100 703normal kkkdd 704set undolevels = 100 705normal dd 706set undolevels = 100 707normal dd 708set undolevels = 100 709" encrypt the file using key 'foobar' 710call feedkeys ( "foobar\nfoobar\n" ) 711X 712write ! 713bwipe ! 714 715call feedkeys ( "foobar\n" ) 716edit Xtestfile 717set key = 718normal uu 719call assert_equal ([ 'monday' , 'wednesday' , 'thursday' , 'friday' , '' ], 720 \getline ( 1 , '$' )) 721 722bwipe ! 723call delete ( 'Xtestfile' ) 724let ufile = has ( 'vms' ) ? '_un_Xtestfile' : '.Xtestfile.un~' 725call delete ( ufile ) 726set undofile & undolevels & cryptmethod & 727endfunc 728 729" Test 'undofile' using a file encrypted with 'blowfish' crypt method 730func Test_undofile_cryptmethod_blowfish () 731throw 'skipped: Nvim does not support cryptmethod' 732edit Xtestfile 733set undofile cryptmethod = blowfish 734call append ( 0 , [ 'jan' , 'feb' , 'mar' , 'apr' , 'jun' ]) 735call cursor ( 5 , 1 ) 736 737set undolevels = 100 738exe 'normal kk0ifoo ' 739set undolevels = 100 740normal dd 741set undolevels = 100 742exe 'normal ibar ' 743set undolevels = 100 744" encrypt the file using key 'foobar' 745call feedkeys ( "foobar\nfoobar\n" ) 746X 747write ! 748bwipe ! 749 750call feedkeys ( "foobar\n" ) 751edit Xtestfile 752set key = 753call search ( 'bar' ) 754call assert_equal ( 'bar apr' , getline ( '.' )) 755undo 756call assert_equal ( 'apr' , getline ( '.' )) 757undo 758call assert_equal ( 'foo mar' , getline ( '.' )) 759undo 760call assert_equal ( 'mar' , getline ( '.' )) 761 762bwipe ! 763call delete ( 'Xtestfile' ) 764let ufile = has ( 'vms' ) ? '_un_Xtestfile' : '.Xtestfile.un~' 765call delete ( ufile ) 766set undofile & undolevels & cryptmethod & 767endfunc 768 769" Test 'undofile' using a file encrypted with 'blowfish2' crypt method 770func Test_undofile_cryptmethod_blowfish2 () 771throw 'skipped: Nvim does not support cryptmethod' 772edit Xtestfile 773set undofile cryptmethod = blowfish2 774call append ( 0 , [ 'jan' , 'feb' , 'mar' , 'apr' , 'jun' ]) 775call cursor ( 5 , 1 ) 776 777set undolevels = 100 778exe 'normal kk0ifoo ' 779set undolevels = 100 780normal dd 781set undolevels = 100 782exe 'normal ibar ' 783set undolevels = 100 784" encrypt the file using key 'foo2bar' 785call feedkeys ( "foo2bar\nfoo2bar\n" ) 786X 787write ! 788bwipe ! 789 790call feedkeys ( "foo2bar\n" ) 791edit Xtestfile 792set key = 793call search ( 'bar' ) 794call assert_equal ( 'bar apr' , getline ( '.' )) 795normal u 796call assert_equal ( 'apr' , getline ( '.' )) 797normal u 798call assert_equal ( 'foo mar' , getline ( '.' )) 799normal u 800call assert_equal ( 'mar' , getline ( '.' )) 801 802bwipe ! 803call delete ( 'Xtestfile' ) 804let ufile = has ( 'vms' ) ? '_un_Xtestfile' : '.Xtestfile.un~' 805call delete ( ufile ) 806set undofile & undolevels & cryptmethod & 807endfunc 808 809" Test for redoing with incrementing numbered registers 810func Test_redo_repeat_numbered_register () 811new 812for [ i , v ] in [[ 1 , 'one' ], [ 2 , 'two' ], [ 3 , 'three' ], 813 \[ 4 , 'four' ], [ 5 , 'five' ], [ 6 , 'six' ], 814 \[ 7 , 'seven' ], [ 8 , 'eight' ], [ 9 , 'nine' ]] 815exe 'let @' .. i .. '="' .. v .. '\n"' 816endfor 817call feedkeys ( '"1p.........' , 'xt' ) 818call assert_equal ([ '' , 'one' , 'two' , 'three' , 'four' , 'five' , 'six' , 819 \'seven' , 'eight' , 'nine' , 'nine' ], getline ( 1 , '$' )) 820bwipe ! 821endfunc 822 823" Test for redo in insert mode using CTRL-O with multibyte characters 824func Test_redo_multibyte_in_insert_mode () 825new 826call feedkeys ( "a\<C-K>ft" , 'xt' ) 827call feedkeys ( "uiHe\<C-O>.llo" , 'xt' ) 828call assert_equal ( "He\ufb05llo" , getline ( 1 )) 829bwipe ! 830endfunc 831 832func Test_undo_mark () 833new 834" The undo is applied to the only line. 835call setline ( 1 , 'hello' ) 836call feedkeys ( "ggyiw$p" , 'xt' ) 837undo 838call assert_equal ([ 0 , 1 , 1 , 0 ], getpos ( "'[" )) 839call assert_equal ([ 0 , 1 , 1 , 0 ], getpos ( "']" )) 840" The undo removes the last line. 841call feedkeys ( "Goaaaa\<Esc>" , 'xt' ) 842call feedkeys ( "obbbb\<Esc>" , 'xt' ) 843undo 844call assert_equal ([ 0 , 2 , 1 , 0 ], getpos ( "'[" )) 845call assert_equal ([ 0 , 2 , 1 , 0 ], getpos ( "']" )) 846bwipe ! 847endfunc 848 849func Test_undo_after_write () 850CheckScreendump 851" use a terminal to make undo work like when text is typed 852CheckRunVimInTerminal 853 854let lines =<< trim END 855edit Xtestfile.txt 856set undolevels=100 undofile 857imap . <Cmd>write<CR> 858write 859END 860call writefile ( lines , 'Xtest_undo_after_write' , 'D' ) 861let buf = RunVimInTerminal ( '-S Xtest_undo_after_write' , #{ rows : 6 }) 862 863call term_sendkeys ( buf , "Otest.\<CR>boo!!!\<Esc>" ) 864sleep 100m 865call term_sendkeys ( buf , "u" ) 866call VerifyScreenDump ( buf , 'Test_undo_after_write_1' , {}) 867 868call term_sendkeys ( buf , "u" ) 869call VerifyScreenDump ( buf , 'Test_undo_after_write_2' , {}) 870 871call StopVimInTerminal ( buf ) 872call delete ( 'Xtestfile.txt' ) 873call delete ( '.Xtestfile.txt.un~' ) 874endfunc 875 876func Test_undo_range_normal () 877new 878call setline ( 1 , [ 'asa' , 'bsb' ]) 879let & l: undolevels = & l: undolevels 880 %normal dfs 881call assert_equal ([ 'a' , 'b' ], getline ( 1 , '$' )) 882undo 883call assert_equal ([ 'asa' , 'bsb' ], getline ( 1 , '$' )) 884bwipe ! 885endfunc 886 887func Test_load_existing_undofile () 888CheckFeature persistent_undo 889sp samples/test_undo.txt 890let mess = execute ( ':verbose rundo samples/test_undo.txt.undo' ) 891call assert_match ( 'Finished reading undo file' , mess ) 892 893call assert_equal ([ 'one' , 'two' , 'three' ], getline ( 1 , '$' )) 894norm ! u 895call assert_equal ([ 'one' , 'two' ], getline ( 1 , '$' )) 896norm ! u 897call assert_equal ([ 'one' ], getline ( 1 , '$' )) 898norm ! u 899call assert_equal ([ '' ], getline ( 1 , '$' )) 900let mess = execute ( ':norm! u' ) 901call assert_equal ([ '' ], getline ( 1 , '$' )) 902call assert_match ( 'Already at oldest change' , mess ) 903exe ":norm! \<c-r>" 904call assert_equal ([ 'one' ], getline ( 1 , '$' )) 905exe ":norm! \<c-r>" 906call assert_equal ([ 'one' , 'two' ], getline ( 1 , '$' )) 907exe ":norm! \<c-r>" 908call assert_equal ([ 'one' , 'two' , 'three' ], getline ( 1 , '$' )) 909let mess = execute ( ":norm! \<c-r>" ) 910call assert_equal ([ 'one' , 'two' , 'three' ], getline ( 1 , '$' )) 911call assert_match ( 'Already at newest change' , mess ) 912bw ! 913endfunc 914 915func Test_restore_cursor_position_after_undo () 916CheckFeature persistent_undo 917sp samples/test_undo.txt 918 9193 |exe "norm! gqk" |undojoin |1 delete 920call assert_equal ( 1 , line ( '.' )) 921norm ! u 922call assert_equal ( 3 , line ( '.' )) 923bw ! 924endfunc 925 926func Test_undo_line_backspace_after_insert_cmd_cursor_movement () 927new 928setlocal backspace = eolundolevels = 100 929call setline ( 1 , [ '' , '' , 'abc' , 'def' ]) 930call cursor ( 4 , 1 ) 931 932let v: errmsg = '' 933call feedkeys ( "i\<Cmd>setlocal undolevels=101 | call cursor(3, 1)\<CR>" 934 \.. "\<BS>\<BS>\<Esc>u" , 'xt' ) 935 936call assert_equal ( '' , v: errmsg ) 937call assert_equal ([ '' , '' , 'abc' , 'def' ], getline ( 1 , '$' )) 938bwipe ! 939endfunc 940 941func Test_undo_line_backspace_after_insert_func_edit () 942new 943setlocal backspace = eolundolevels = 100 944 945let v: errmsg = '' 946call feedkeys ( "i\<CR>" 947 \.. "\<Cmd>call setline(2, 'abc')\<CR>" 948 \.. "\<BS>\<Esc>u" , 'xt' ) 949 950call assert_equal ( '' , v: errmsg ) 951call assert_equal ([ '' ], getline ( 1 , '$' )) 952bwipe ! 953endfunc 954 955func Test_undo_line_backspace_after_insert_cmd_edit () 956new 957setlocal backspace = eolundolevels = 100 958 959let v: errmsg = '' 960call feedkeys ( "i\<CR>" 961 \.. "\<Cmd>s/.*/abc/\<CR>" 962 \.. "\<BS>\<Esc>u" , 'xt' ) 963 964call assert_equal ( '' , v: errmsg ) 965call assert_equal ([ '' ], getline ( 1 , '$' )) 966bwipe ! 967endfunc 968 969" Corrupted undo file via cyclic cross-references caused 970" double free 971func Test_corrupted_undofile () 972CheckFeature persistent_undo 973let _uf = & undofile 974set undofile 975new 976call setline ( 1 , 'hello' ) 977let b = eval ( '0z56696D9F556E446FE50002F3AEFE62965A91903610' .. 978 \'F0E23CC8A69D5B87CEA6D28E75489B0D2CA02ED7993C' .. 979 \'00000001000000000000000000000000000000010000' .. 980 \'00010000000100000001000000010000000100000000' .. 981 \'3B9ACA00005FD0000000010000000000000000000000' .. 982 \'00000000010000000100000000000000000000000000' .. 983 \'00000000000000000000000000000000000000000000' .. 984 \'00000000000000000000000000000000000000000000' .. 985 \'00000000000000000000000000000000000000000000' .. 986 \'00000000000000000000000000000000000000000000' .. 987 \'00000000000000000000000000000000000000000000' .. 988 \'00000000000000000000000000000000000000000000' .. 989 \'00000000000000000000000000000000000000000000' .. 990 \'00000000000000000000000000000000000000000000' .. 991 \'00000000000000000000000000000000000000000000' .. 992 \'00000000000000000000000000000000000000000000' .. 993 \'00000000000000000000000000000000000000000000' .. 994 \'00000000000000000000000000000000000000000000' .. 995 \'00000000000000000000000000000000000000000000' .. 996 \'00000000000000000000000000000000000000000000' .. 997 \'00000000000000000000000000000000000000000000' .. 998 \'000000000000000000000000000000000000003B9ACA' .. 999 \'00003581E7AA' ) 1000" Nvim: convert undo file format 1001let b = b ->slice ( 0 , -2 ) + 0 z3581 + b->slice(-2) | let b[10] =0x03 1002call writefile ( b , 'Xundo' , 'bD' ) 1003rundo Xundo 1004bw ! 1005let & undofile = _uf 1006endfunc 1007 1008" Test that an undo file with alternate branches round-trips: the tree 1009" structure and the text at every sequence number survive :wundo + :rundo. 1010func Test_undofile_branches () 1011CheckFeature persistent_undo 1012let save_ul = & undolevels 1013defer execute('let &undolevels = ' .. save_ul) 1014new Xubranches.txt 1015setl noswapfile 1016set ul = 100 1017call setline ( 1 , 'a' ) 1018for i in range ( 5 ) 1019let & undolevels = & undolevels 1020call setline ( 1 , 'main' .. i ) 1021endfor 1022" create two alternate branches 1023silent undo 3 1024let & undolevels = & undolevels 1025call setline ( 1 , 'branch-a' ) 1026silent undo 2 1027let & undolevels = & undolevels 1028call setline ( 1 , 'branch-b' ) 1029 1030write 1031defer delete('Xubranches.txt') 1032wundo ! Xubranches.undo 1033defer delete('Xubranches.undo') 1034let tree_before = undotree () 1035" remember the text at every undo state 1036let texts = {} 1037for seq in range ( 1 , tree_before . seq_last ) 1038exe 'silent undo ' .. seq 1039let texts [ seq ] = getline ( 1 ) 1040endfor 1041bwipe ! 1042 1043edit Xubranches.txt 1044setl noswapfile 1045rundo Xubranches.undo 1046let tree_after = undotree () 1047call assert_equal ( tree_before . seq_last , tree_after . seq_last ) 1048" every field of the entries, including times and save numbers, must 1049" round-trip through the undo file exactly 1050call assert_equal ( tree_before . entries , tree_after . entries ) 1051for [ seq , text ] in items ( texts ) 1052exe 'silent undo ' .. seq 1053call assert_equal ( text , getline ( 1 ), 'text at undo state ' .. seq ) 1054endfor 1055 1056bwipe ! 1057endfunc 1058 1059" Test that a duplicated sequence number in an undo file is detected. 1060func Test_undofile_duplicate_seq () 1061CheckFeature persistent_undo 1062let save_ul = & undolevels 1063defer execute('let &undolevels = ' .. save_ul) 1064new Xudupseq.txt 1065setl noswapfile 1066set ul = 100 1067call setline ( 1 , 'one' ) 1068let & undolevels = & undolevels 1069call setline ( 1 , 'two' ) 1070let & undolevels = & undolevels 1071call setline ( 1 , 'three' ) 1072write 1073defer delete('Xudupseq.txt') 1074wundo ! Xudupseq.undo 1075defer delete('Xudupseq.undo') 1076 1077" Overwrite the uh_seq of the second header with that of the first. A 1078" header starts with the magic bytes 0x5f 0xd0, followed by four 4-byte 1079" header references and then the 4-byte uh_seq. Require the references 1080" and uh_seq to be small numbers, so that a timestamp that happens to 1081" contain the magic bytes is not mistaken for a header. 1082let blob = readfile ( 'Xudupseq.undo' , 'B' ) 1083let headers = [] 1084for i in range ( len ( blob ) - 22 ) 1085if blob [ i ] == 0x5f && blob [ i + 1 ] == 0xd0 1086let ok = v: true 1087for field in range ( 5 ) 1088let off = i + 2 + field * 4 1089if blob [ off ] != 0 || blob [ off + 1 ] != 0 || blob [ off + 2 ] != 0 1090 \|| blob [ off + 3 ] > 8 1091let ok = v: false 1092break 1093endif 1094endfor 1095if ok 1096call add ( headers , i ) 1097endif 1098endif 1099endfor 1100call assert_true ( len ( headers ) >= 2 , 'found undo file headers' ) 1101let first = headers [ 0 ] + 18 1102let second = headers [ 1 ] + 18 1103" Check that the detected headers are the intended ones before patching 1104" any bytes: the headers are written oldest first, so the first two carry 1105" sequence numbers 1 and 2. 1106call assert_equal ( 0 z00000001, blob[first : first +3]) 1107call assert_equal ( 0 z00000002, blob[second : second +3]) 1108let blob [ second : second + 3 ] = blob [ first : first + 3 ] 1109call writefile ( blob , 'Xudupseq.undo' ) 1110call assert_fails ( 'rundo Xudupseq.undo' , 'E825:' ) 1111 1112bwipe ! 1113endfunc 1114 1115" Test reading an undo file with zero undo headers, which is written when 1116" only the line for the "U" command is saved, e.g. with 'undolevels' -1. 1117func Test_undofile_zero_headers () 1118CheckFeature persistent_undo 1119let save_ul = & undolevels 1120defer execute('let &undolevels = ' .. save_ul) 1121" The buffer must not collect any undo header, so create the file on disk 1122" directly and only change the buffer with 'undolevels' already negative. 1123call writefile ([ 'hello' , 'world' ], 'Xuzero.txt' , 'D' ) 1124set undolevels = -1 1125edit Xuzero.txt 1126normal ! x 1127wundo ! Xuzero.undo 1128defer delete('Xuzero.undo') 1129bwipe ! 1130 1131" Make the same change so that the buffer text matches the hash stored in 1132" the undo file, then read the undo file back. 1133edit Xuzero.txt 1134normal ! x 1135let v: errmsg = '' 1136" a silent rejection of the undo file gives a warning, not an error 1137let v: warningmsg = '' 1138rundo Xuzero.undo 1139call assert_equal ( '' , v: errmsg ) 1140call assert_equal ( '' , v: warningmsg ) 1141normal ! U 1142call assert_equal ( 'hello' , getline ( 1 )) 1143 1144bwipe ! 1145endfunc 1146 1147" vim: shiftwidth=2 sts=2 expandtab