robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1" Test for :mksession, :mkview and :loadview in latin1 encoding 2 3scriptencoding latin1 4 5source check.vim 6CheckFeature mksession 7 8source shared.vim 9source term_util.vim 10 11" Test for storing global and local argument list in a session file 12" This one must be done first. 13func Test__mksession_arglocal () 14enew |only 15n a b c 16new 17arglocal 18mksession ! Xtest_mks.out 19 20 %bwipe ! 21 %argdelete 22argglobal 23source Xtest_mks.out 24call assert_equal ( 2 , winnr ( '$' )) 25call assert_equal ( 2 , arglistid ( 1 )) 26call assert_equal ( 0 , arglistid ( 2 )) 27 28 %bwipe ! 29 %argdelete 30argglobal 31call delete ( 'Xtest_mks.out' ) 32endfunc 33 34func Test_mksession_arglocal_localdir () 35argglobal 36 %argdelete 37 38call mkdir ( 'Xa' , 'R' ) 39call writefile ([ 'This is Xb' ], 'Xa/Xb.txt' , 'D' ) 40let olddir = getcwd () 41let oldargs = argv () 42 43for tabpage in [ v: false , v: true ] 44let msg = tabpage ? 'tabpage-local' : 'window-local' 45 46exe tabpage ? 'tabnew' : 'botright new' 47exe tabpage ? 'tcd Xa' : 'lcd Xa' 48let localdir = getcwd () 49arglocal 50$ argadd Xb.txt 51let localargs = argv () 52exe tabpage ? 'tabprev' : 'wincmd p' 53call assert_equal ( olddir , getcwd (), msg ) 54call assert_equal ( oldargs , argv (), msg ) 55mksession ! Xtest_mks_localdir.out 56exe tabpage ? '+tabclose' : '$close' 57bwipe ! Xa/Xb.txt 58 59source Xtest_mks_localdir.out 60exe tabpage ? 'tabnext' : 'wincmd b' 61call assert_equal ( localdir , getcwd (), msg ) 62call assert_equal ( localargs , argv (), msg ) 63$ argument 64call assert_equal ( 'This is Xb' , getline ( 1 ), msg ) 65 66bwipe ! 67call assert_equal ( olddir , getcwd (), msg ) 68call assert_equal ( oldargs , argv (), msg ) 69call delete ( 'Xtest_mks_localdir.out' ) 70endfor 71endfunc 72 73func Test_mksession () 74tabnew 75let wrap_save = & wrap 76set sessionoptions = bufferssplitbelow fileencoding = latin1 77defer execute('set sessionoptions& splitbelow&') 78call setline ( 1 , [ 79 \'start:' , 80 \'no multibyte chAracter' , 81 \' one leaDing tab' , 82 \' four leadinG spaces' , 83 \'two consecutive tabs' , 84 \'two tabs in one line' , 85 \'one � multibyteCharacter' , 86 \'a� � two multiByte characters' , 87 \'A��� three mulTibyte characters' , 88 \'short line' , 89 \]) 90let tmpfile = 'Xtemp' 91exec 'w! ' . tmpfile 92/ ^start: 93set wrap 94vsplit 95norm ! j16| 96split 97norm ! j16| 98split 99norm ! j16| 100split 101norm ! j8| 102split 103norm ! j8| 104split 105norm ! j16| 106split 107norm ! j16| 108split 109norm ! j16| 110split 111norm ! j$ 112wincmd l 113 114set nowrap 115/ ^start: 116norm ! j16|3zl 117split 118norm ! j016|3zl 119split 120norm ! j016|3zl 121split 122norm ! j08|3zl 123split 124norm ! j08|3zl 125split 126norm ! j016|3zl 127split 128norm ! j016|3zl 129split 130norm ! j016|3zl 131split 132call wincol () 133mksession ! Xtest_mks.out 134let li = filter ( readfile ( 'Xtest_mks.out' ), 'v:val =~# "\\(^ *normal! [0$]\\|^ *exe ''normal!\\)"' ) 135let expected = [ 136 \'normal! 016|' , 137 \'normal! 016|' , 138 \'normal! 016|' , 139 \'normal! 08|' , 140 \'normal! 08|' , 141 \'normal! 016|' , 142 \'normal! 016|' , 143 \'normal! 016|' , 144 \'normal! $' , 145 \" exe 'normal! ' . s:c . '|zs' . 16 . '|'" , 146 \" normal! 016|" , 147 \" exe 'normal! ' . s:c . '|zs' . 16 . '|'" , 148 \" normal! 016|" , 149 \" exe 'normal! ' . s:c . '|zs' . 16 . '|'" , 150 \" normal! 016|" , 151 \" exe 'normal! ' . s:c . '|zs' . 8 . '|'" , 152 \" normal! 08|" , 153 \" exe 'normal! ' . s:c . '|zs' . 8 . '|'" , 154 \" normal! 08|" , 155 \" exe 'normal! ' . s:c . '|zs' . 16 . '|'" , 156 \" normal! 016|" , 157 \" exe 'normal! ' . s:c . '|zs' . 16 . '|'" , 158 \" normal! 016|" , 159 \" exe 'normal! ' . s:c . '|zs' . 16 . '|'" , 160 \" normal! 016|" , 161 \" exe 'normal! ' . s:c . '|zs' . 16 . '|'" , 162 \" normal! 016|" 163 \] 164call assert_equal ( expected , li ) 165tabclose ! 166 167call delete ( 'Xtest_mks.out' ) 168call delete ( tmpfile ) 169let & wrap = wrap_save 170endfunc 171 172func Test_mksession_winheight () 173new 174set winheight = 10 175defer execute('set winheight&') 176set winminheight = 2 177defer execute('set winminheight&') 178mksession ! Xtest_mks.out 179source Xtest_mks.out 180 181call delete ( 'Xtest_mks.out' ) 182endfunc 183 184func Test_mksession_large_winheight () 185set winheight = 999 186defer execute('set winheight&') 187mksession ! Xtest_mks_winheight.out 188set winheight & 189source Xtest_mks_winheight.out 190call delete ( 'Xtest_mks_winheight.out' ) 191endfunc 192 193func Test_mksession_zero_winheight () 194set winminheight = 0 195defer execute('set winminheight&') 196edit SomeFile 197split 198wincmd _ 199mksession ! Xtest_mks_zero 200set winminheight & 201let text = readfile ( 'Xtest_mks_zero' ) ->join () 202call delete ( 'Xtest_mks_zero' ) 203close 204" check there is no divide by zero 205call assert_notmatch ( '/ 0[^0-9]' , text ) 206endfunc 207 208func Test_mksession_rtp () 209if has ( 'win32' ) 210" TODO: fix problem with backslashes 211return 212endif 213new 214let _rtp =& rtp 215" Make a real long (invalid) runtimepath value, 216" that should exceed PATH_MAX (hopefully) 217let newrtp =& rtp . ',~' . repeat ( '/foobar' , 1000 ) 218let newrtp .= "," . expand ( "$HOME" ) . "/.vim" 219let & rtp = newrtp 220 221" determine expected value 222let expected = split ( & rtp , ',' ) 223let expected = map ( expected , '"set runtimepath+=".v:val' ) 224let expected = [ 'set runtimepath=' ] + expected 225let expected = map ( expected , { v , w ->substitute ( w , $ HOME , "~" , "g" )}) 226 227mksession ! Xtest_mks.out 228let & rtp = _rtp 229let li = filter ( readfile ( 'Xtest_mks.out' ), 'v:val =~# "runtimepath"' ) 230call assert_equal ( expected , li ) 231 232call delete ( 'Xtest_mks.out' ) 233endfunc 234 235func Test_mksession_arglist () 236 %argdel 237next file1 file2 file3 file4 238new 239next |next 240mksession ! Xtest_mks.out 241source Xtest_mks.out 242call assert_equal ([ 'file1' , 'file2' , 'file3' , 'file4' ], argv ()) 243call assert_equal ( 2 , argidx ()) 244wincmd w 245call assert_equal ( 0 , argidx ()) 246 247call delete ( 'Xtest_mks.out' ) 248enew |only 249argdel * 250endfunc 251 252func Test_mksession_one_buffer_two_windows () 253set splitbelow 254defer execute('set splitbelow&') 255 256edit Xtest1 257new Xtest2 258split 259mksession ! Xtest_mks.out 260let lines = readfile ( 'Xtest_mks.out' ) 261let count1 = 0 262let count2 = 0 263let count2buf = 0 264let bufexists = 0 265for line in lines 266if line =~ 'edit \f*Xtest1$' 267let count1 += 1 268endif 269if line =~ 'edit \f\{-}Xtest2' 270let count2 += 1 271endif 272if line =~ 'buffer \f\{-}Xtest2' 273let count2buf += 1 274endif 275if line =~ 'bufexists(fnamemodify(.*, ":p")' 276let bufexists += 1 277endif 278endfor 279call assert_equal ( 1 , count1 , 'Xtest1 count' ) 280call assert_equal ( 2 , count2 , 'Xtest2 count' ) 281call assert_equal ( 2 , count2buf , 'Xtest2 buffer count' ) 282call assert_equal ( 2 , bufexists ) 283 284close 285bwipe ! 286call delete ( 'Xtest_mks.out' ) 287endfunc 288 289func Test_mksession_lcd_multiple_tabs () 290tabnew 291tabnew 292lcd . 293tabfirst 294lcd . 295mksession ! Xtest_mks.out 296tabonly 297source Xtest_mks.out 298call assert_true ( haslocaldir (), 'Tab 1 localdir' ) 299tabnext 2 300call assert_true ( ! haslocaldir (), 'Tab 2 localdir' ) 301tabnext 3 302call assert_true ( haslocaldir (), 'Tab 3 localdir' ) 303call delete ( 'Xtest_mks.out' ) 304endfunc 305 306" Test for tabpage-local directory 307func Test_mksession_tcd_multiple_tabs () 308let save_cwd = getcwd () 309call mkdir ( 'Xtopdir' ) 310cd Xtopdir 311call mkdir ( 'Xtabdir1' ) 312call mkdir ( 'Xtabdir2' ) 313call mkdir ( 'Xtabdir3' ) 314call mkdir ( 'Xwindir1' ) 315call mkdir ( 'Xwindir2' ) 316call mkdir ( 'Xwindir3' ) 317tcd Xtabdir1 318botright new 319wincmd t 320lcd ../Xwindir1 321tabnew 322tcd ../Xtabdir2 323botright new 324lcd ../Xwindir2 325tabnew 326tcd ../Xtabdir3 327botright new 328lcd ../Xwindir3 329tabfirst 3301 wincmd w 331mksession ! Xtest_mks.out 332only |tabonly 333source Xtest_mks.out 334call assert_equal ( 'Xtabdir1' , fnamemodify ( getcwd ( -1 , 1 ), ':t' )) 335call assert_equal ( 'Xwindir1' , fnamemodify ( getcwd ( 1 , 1 ), ':t' )) 336call assert_equal ( 'Xtabdir1' , fnamemodify ( getcwd ( 2 , 1 ), ':t' )) 337call assert_equal ( 'Xtabdir2' , fnamemodify ( getcwd ( -1 , 2 ), ':t' )) 338call assert_equal ( 'Xtabdir2' , fnamemodify ( getcwd ( 1 , 2 ), ':t' )) 339call assert_equal ( 'Xwindir2' , fnamemodify ( getcwd ( 2 , 2 ), ':t' )) 340call assert_equal ( 'Xtabdir3' , fnamemodify ( getcwd ( -1 , 3 ), ':t' )) 341call assert_equal ( 'Xtabdir3' , fnamemodify ( getcwd ( 1 , 3 ), ':t' )) 342call assert_equal ( 'Xwindir3' , fnamemodify ( getcwd ( 2 , 3 ), ':t' )) 343 %bwipe 344call chdir ( save_cwd ) 345call delete ( "Xtopdir" , "rf" ) 346endfunc 347 348func Test_mksession_blank_tabs () 349tabnew 350tabnew 351tabnew 352tabnext 3 353mksession ! Xtest_mks.out 354tabnew 355tabnew 356tabnext 2 357source Xtest_mks.out 358call assert_equal ( 4 , tabpagenr ( '$' ), 'session restore should restore number of tabs' ) 359call assert_equal ( 3 , tabpagenr (), 'session restore should restore the active tab' ) 360call delete ( 'Xtest_mks.out' ) 361endfunc 362 363func Test_mksession_buffer_count () 364argglobal 365 %argdelete 366 367set hidden 368 369" Edit exactly three files in the current session. 370 %bwipe ! 371e Xfoo |tabe Xbar |tabe Xbaz 372tabdo write 373mksession ! Xtest_mks.out 374 375" Verify that loading the session does not create additional buffers. 376 %bwipe ! 377source Xtest_mks.out 378call assert_equal ( 3 , len ( getbufinfo ())) 379 380" Clean up. 381call delete ( 'Xfoo' ) 382call delete ( 'Xbar' ) 383call delete ( 'Xbaz' ) 384call delete ( 'Xtest_mks.out' ) 385 %bwipe ! 386set hidden & 387endfunc 388 389func Test_mksession_buffer_order () 390 %bwipe ! 391e Xfoo |e Xbar |e Xbaz |e Xqux 392bufdo write 393mksession ! Xtest_mks.out 394 395" Verify that loading the session preserves order of buffers 396 %bwipe ! 397source Xtest_mks.out 398 399let s: buf_info = getbufinfo () 400call assert_true ( s: buf_info [ 0 ][ 'name' ] =~# 'Xfoo$' ) 401call assert_true ( s: buf_info [ 1 ][ 'name' ] =~# 'Xbar$' ) 402call assert_true ( s: buf_info [ 2 ][ 'name' ] =~# 'Xbaz$' ) 403call assert_true ( s: buf_info [ 3 ][ 'name' ] =~# 'Xqux$' ) 404 405" Clean up. 406call delete ( 'Xfoo' ) 407call delete ( 'Xbar' ) 408call delete ( 'Xbaz' ) 409call delete ( 'Xqux' ) 410call delete ( 'Xtest_mks.out' ) 411 %bwipe ! 412endfunc 413 414if has ( 'extra_search' ) 415 416func Test_mksession_hlsearch () 417set hlsearch 418mksession ! Xtest_mks.out 419nohlsearch 420source Xtest_mks.out 421call assert_equal ( 1 , v: hlsearch , 'session should restore search highlighting state' ) 422nohlsearch 423mksession ! Xtest_mks.out 424source Xtest_mks.out 425call assert_equal ( 0 , v: hlsearch , 'session should restore search highlighting state' ) 426call delete ( 'Xtest_mks.out' ) 427endfunc 428 429endif 430 431 432func Test_mksession_blank_windows () 433split 434split 435split 4363 wincmd w 437mksession ! Xtest_mks.out 438split 439split 4402 wincmd w 441source Xtest_mks.out 442call assert_equal ( 4 , winnr ( '$' ), 'session restore should restore number of windows' ) 443call assert_equal ( 3 , winnr (), 'session restore should restore the active window' ) 444call delete ( 'Xtest_mks.out' ) 445endfunc 446 447if has ( 'terminal' ) 448 449func Test_mksession_terminal_shell () 450CheckFeature quickfix 451 452terminal 453mksession ! Xtest_mks.out 454let lines = readfile ( 'Xtest_mks.out' ) 455let term_cmd = '' 456for line in lines 457if line =~ '^terminal' 458let term_cmd = line 459elseif line =~ 'badd.*' . & shell 460call assert_report ( 'unexpected shell line: ' . line ) 461endif 462endfor 463call assert_match ( 'terminal ++curwin ++cols=\d\+ ++rows=\d\+\s*.*$' , term_cmd ) 464 465call StopShellInTerminal ( bufnr ( '%' )) 466call delete ( 'Xtest_mks.out' ) 467endfunc 468 469func Test_mksession_terminal_no_restore_cmdarg () 470terminal ++norestore 471mksession ! Xtest_mks.out 472let lines = readfile ( 'Xtest_mks.out' ) 473let term_cmd = '' 474for line in lines 475if line =~ '^terminal' 476call assert_report ( 'session must not restore terminal' ) 477endif 478endfor 479 480call StopShellInTerminal ( bufnr ( '%' )) 481call delete ( 'Xtest_mks.out' ) 482endfunc 483 484func Test_mksession_terminal_no_restore_funcarg () 485call term_start ( & shell , { 'norestore' : 1 }) 486mksession ! Xtest_mks.out 487let lines = readfile ( 'Xtest_mks.out' ) 488let term_cmd = '' 489for line in lines 490if line =~ '^terminal' 491call assert_report ( 'session must not restore terminal' ) 492endif 493endfor 494 495call StopShellInTerminal ( bufnr ( '%' )) 496call delete ( 'Xtest_mks.out' ) 497endfunc 498 499func Test_mksession_terminal_no_restore_func () 500terminal 501call term_setrestore ( bufnr ( '%' ), 'NONE' ) 502mksession ! Xtest_mks.out 503let lines = readfile ( 'Xtest_mks.out' ) 504let term_cmd = '' 505for line in lines 506if line =~ '^terminal' 507call assert_report ( 'session must not restore terminal' ) 508endif 509endfor 510 511call StopShellInTerminal ( bufnr ( '%' )) 512call delete ( 'Xtest_mks.out' ) 513endfunc 514 515func Test_mksession_terminal_no_ssop () 516terminal 517set sessionoptions -= terminal 518mksession ! Xtest_mks.out 519let lines = readfile ( 'Xtest_mks.out' ) 520let term_cmd = '' 521for line in lines 522if line =~ '^terminal' 523call assert_report ( 'session must not restore terminal' ) 524endif 525endfor 526 527call StopShellInTerminal ( bufnr ( '%' )) 528call delete ( 'Xtest_mks.out' ) 529set sessionoptions & 530endfunc 531 532func Test_mksession_terminal_restore_other () 533CheckFeature quickfix 534 535terminal 536eval bufnr ( '%' ) ->term_setrestore ( 'other' ) 537mksession ! Xtest_mks.out 538let lines = readfile ( 'Xtest_mks.out' ) 539let term_cmd = '' 540for line in lines 541if line =~ '^terminal' 542let term_cmd = line 543endif 544endfor 545call assert_match ( 'terminal ++curwin ++cols=\d\+ ++rows=\d\+.*other' , term_cmd ) 546 547call StopShellInTerminal ( bufnr ( '%' )) 548call delete ( 'Xtest_mks.out' ) 549endfunc 550 551endif " has('terminal') 552 553func Test_mkview_open_folds () 554enew ! 555 556call append ( 0 , [ 'a' , 'b' , 'c' ]) 5571 , 3 fold 558write ! Xtestfile 559 560call assert_notequal ( -1 , foldclosed ( 1 )) 561call assert_notequal ( -1 , foldclosed ( 2 )) 562call assert_notequal ( -1 , foldclosed ( 3 )) 563 564" Save the view with folds closed 565mkview ! Xtestview 566 567" zR affects 'foldlevel', make sure the option is applied after the folds 568" have been recreated. 569" Open folds to ensure they get closed when restoring the view 570normal zR 571 572call assert_equal ( -1 , foldclosed ( 1 )) 573call assert_equal ( -1 , foldclosed ( 2 )) 574call assert_equal ( -1 , foldclosed ( 3 )) 575 576source Xtestview 577 578call assert_notequal ( -1 , foldclosed ( 1 )) 579call assert_notequal ( -1 , foldclosed ( 2 )) 580call assert_notequal ( -1 , foldclosed ( 3 )) 581 582call delete ( 'Xtestview' ) 583call delete ( 'Xtestfile' ) 584 %bwipe 585endfunc 586 587func Test_mkview_no_balt () 588edit Xtestfile1 589edit Xtestfile2 590 591mkview ! Xtestview 592bdelete Xtestfile1 593 594source Xtestview 595call assert_equal ( 0 , buflisted ( 'Xtestfile1' )) 596 597call delete ( 'Xtestview' ) 598 %bwipe 599endfunc 600 601func Test_mksession_no_balt () 602edit Xtestfile1 603edit Xtestfile2 604 605bdelete Xtestfile1 606mksession ! Xtestview 607 608source Xtestview 609call assert_equal ( 0 , buflisted ( 'Xtestfile1' )) 610 611call delete ( 'Xtestview' ) 612 %bwipe 613endfunc 614 615" Test :mkview with a file argument. 616func Test_mkview_file () 617" Create a view with line number and a fold. 618help :mkview 619set number 620norm ! V}zf0 621let pos = getpos ( '.' ) 622let linefoldclosed1 = foldclosed ( '.' ) 623mkview ! Xview 624set nonumber 625norm ! zrj 626" We can close the help window, as mkview with a file name should 627" generate a command to edit the file. 628helpclose 629 630source Xview 631call assert_equal ( 1 , & number ) 632call assert_match ( '\*:mkview\*$' , getline ( '.' )) 633call assert_equal ( pos , getpos ( '.' )) 634call assert_equal ( linefoldclosed1 , foldclosed ( '.' )) 635 636" Creating a view again with the same file name should fail (file 637" already exists). But with a !, the previous view should be 638" overwritten without error. 639help :loadview 640call assert_fails ( 'mkview Xview' , 'E189:' ) 641call assert_match ( '\*:loadview\*$' , getline ( '.' )) 642mkview ! Xview 643call assert_match ( '\*:loadview\*$' , getline ( '.' )) 644 645call delete ( 'Xview' ) 646bwipe 647endfunc 648 649" Test :mkview and :loadview with a custom 'viewdir'. 650func Test_mkview_loadview_with_viewdir () 651set viewdir = Xviewdir 652 653help :mkview 654set number 655norm ! V}zf 656let pos = getpos ( '.' ) 657let linefoldclosed1 = foldclosed ( '.' ) 658mkview 1 659set nonumber 660norm ! zrj 661 662loadview 1 663 664" The directory Xviewdir/ should have been created and the view 665" should be stored in that directory. 666call assert_equal ( 'Xviewdir/' . 667 \substitute ( 668 \substitute ( 669 \expand ( '%:p' ), '/' , '=+' , 'g' ), ':' , '=-' , 'g' ) . '=1.vim' , 670 \glob ( 'Xviewdir/*' )) 671call assert_equal ( 1 , & number ) 672call assert_match ( '\*:mkview\*$' , getline ( '.' )) 673call assert_equal ( pos , getpos ( '.' )) 674call assert_equal ( linefoldclosed1 , foldclosed ( '.' )) 675 676call delete ( 'Xviewdir' , 'rf' ) 677set viewdir & 678helpclose 679endfunc 680 681func Test_mkview_no_file_name () 682new 683" :mkview or :mkview {nr} should fail in an unnamed buffer. 684call assert_fails ( 'mkview' , 'E32:' ) 685call assert_fails ( 'mkview 1' , 'E32:' ) 686 687" :mkview {file} should succeed in an unnamed buffer. 688mkview Xview 689help 690source Xview 691call assert_equal ( '' , bufname ( '%' )) 692 693call delete ( 'Xview' ) 694 %bwipe 695endfunc 696 697func Test_mkview_loadview_jumplist () 698set viewdir = Xviewdir 699au BufWinLeave * silent mkview 700" au BufWinEnter * silent loadview 701 702edit Xfile1 703call setline ( 1 , [ 'a' , 'bbbbbbb' , 'c' ]) 704normal j3l 705call assert_equal ([ 2 , 4 ], getcurpos ()[ 1 : 2 ]) 706write 707 708edit Xfile2 709call setline ( 1 , [ 'd' , 'eeeeeee' , 'f' ]) 710normal j5l 711call assert_equal ([ 2 , 6 ], getcurpos ()[ 1 : 2 ]) 712write 713 714edit Xfile3 715call setline ( 1 , [ 'g' , 'h' , 'iiiii' ]) 716normal jj3l 717call assert_equal ([ 3 , 4 ], getcurpos ()[ 1 : 2 ]) 718write 719 720" The commented :au above was moved here so that :mkview (on BufWinLeave) can 721" run before :loadview. This is needed because Nvim's :loadview raises E484 if 722" the view can't be opened, while Vim's silently fails instead. 723au BufWinEnter * silent loadview 724 725edit Xfile1 726call assert_equal ([ 2 , 4 ], getcurpos ()[ 1 : 2 ]) 727edit Xfile2 728call assert_equal ([ 2 , 6 ], getcurpos ()[ 1 : 2 ]) 729edit Xfile3 730call assert_equal ([ 3 , 4 ], getcurpos ()[ 1 : 2 ]) 731 732exe "normal \<C-O>" 733call assert_equal ( 'Xfile2' , expand ( '%' )) 734call assert_equal ([ 2 , 6 ], getcurpos ()[ 1 : 2 ]) 735exe "normal \<C-O>" 736call assert_equal ( 'Xfile1' , expand ( '%' )) 737call assert_equal ([ 2 , 4 ], getcurpos ()[ 1 : 2 ]) 738 739au ! BufWinLeave 740au ! BufWinEnter 741bwipe ! 742call delete ( 'Xviewdir' , 'rf' ) 743call delete ( 'Xfile1' ) 744call delete ( 'Xfile2' ) 745call delete ( 'Xfile3' ) 746set viewdir & 747endfunc 748 749" A clean session (one empty buffer, one window, and one tab) should not 750" set any error messages when sourced because no commands should fail. 751func Test_mksession_no_errmsg () 752let v: errmsg = '' 753 %bwipe ! 754mksession ! Xtest_mks.out 755source Xtest_mks.out 756call assert_equal ( '' , v: errmsg ) 757call delete ( 'Xtest_mks.out' ) 758endfunc 759 760func Test_mksession_quote_in_filename () 761if ! has ( 'unix' ) 762" only Unix can handle this weird filename 763return 764endif 765let v: errmsg = '' 766let filename = has ( 'win32' ) ? 'x''y' : 'x''y"z' 767 %bwipe ! 768split another 769execute 'split' escape ( filename , '"' ) 770mksession ! Xtest_mks_quoted.out 771 %bwipe ! 772source Xtest_mks_quoted.out 773call assert_true ( bufexists ( filename )) 774 775 %bwipe ! 776call delete ( 'Xtest_mks_quoted.out' ) 777endfunc 778 779" Test for storing global variables in a session file 780func Test_mksession_globals () 781set sessionoptions += globals 782 783" create different global variables 784let g: Global_string = "Sun is shining\r\n" 785let g: Global_count = 100 786let g: Global_pi = 3.14 787let g: Global_neg_float = -2.68 788 789mksession ! Xtest_mks.out 790 791unlet g: Global_string 792unlet g: Global_count 793unlet g: Global_pi 794unlet g: Global_neg_float 795 796source Xtest_mks.out 797call assert_equal ( "Sun is shining\r\n" , g: Global_string ) 798call assert_equal ( 100 , g: Global_count ) 799call assert_equal ( 3.14 , g: Global_pi ) 800call assert_equal ( -2.68 , g: Global_neg_float ) 801 802unlet g: Global_string 803unlet g: Global_count 804unlet g: Global_pi 805unlet g: Global_neg_float 806call delete ( 'Xtest_mks.out' ) 807set sessionoptions & 808endfunc 809 810" Test for changing backslash to forward slash in filenames 811func Test_mksession_slash () 812if exists ( '+shellslash' ) 813throw 'Skipped: cannot use backslash in file name' 814endif 815enew 816 %bwipe ! 817e a\\b\\c 818mksession ! Xtest_mks1.out 819set sessionoptions += slash 820mksession ! Xtest_mks2.out 821 822 %bwipe ! 823source Xtest_mks1.out 824call assert_equal ( 'a/b/c' , bufname ( '' )) 825 %bwipe ! 826source Xtest_mks2.out 827call assert_equal ( 'a/b/c' , bufname ( '' )) 828 829 %bwipe ! 830call delete ( 'Xtest_mks1.out' ) 831call delete ( 'Xtest_mks2.out' ) 832set sessionoptions & 833endfunc 834 835" Test for changing directory to the session file directory 836func Test_mksession_sesdir () 837let save_cwd = getcwd () 838call mkdir ( 'Xproj' ) 839mksession ! Xproj/Xtest_mks1.out 840set sessionoptions -= curdir 841set sessionoptions += sesdir 842mksession ! Xproj/Xtest_mks2.out 843 844source Xproj/Xtest_mks1.out 845call assert_equal ( 'testdir' , fnamemodify ( getcwd (), ':t' )) 846source Xproj/Xtest_mks2.out 847call assert_equal ( 'Xproj' , fnamemodify ( getcwd (), ':t' )) 848call chdir ( save_cwd ) 849 %bwipe 850 851set sessionoptions & 852call delete ( 'Xproj' , 'rf' ) 853endfunc 854 855" Test for saving and restoring the tab-local working directory when there is 856" only a single tab and 'tabpages' is not in 'sessionoptions'. 857func Test_mksession_tcd_single_tabs () 858only |tabonly 859 860let save_cwd = getcwd () 861set sessionoptions -= tabpages 862set sessionoptions += curdir 863call mkdir ( 'Xtopdir1' ) 864call mkdir ( 'Xtopdir2' ) 865 866" There are two tab pages, the current one has local cwd set to 'Xtopdir2'. 867exec 'tcd ' .. save_cwd .. '/Xtopdir1' 868tabnew 869exec 'tcd ' .. save_cwd .. '/Xtopdir2' 870mksession ! Xtest_tcd_single 871 872source Xtest_tcd_single 873" call assert_equal(2, haslocaldir()) 874call assert_equal ( 1 , haslocaldir ( -1 )) 875call assert_equal ( 'Xtopdir2' , fnamemodify ( getcwd ( -1 , 0 ), ':t' )) 876 %bwipe 877 878set sessionoptions & 879call chdir ( save_cwd ) 880call delete ( 'Xtopdir1' , 'rf' ) 881call delete ( 'Xtopdir2' , 'rf' ) 882endfunc 883 884" Test for storing the 'lines' and 'columns' settings 885func Test_mksession_resize () 886mksession ! Xtest_mks1.out 887set sessionoptions += resize 888mksession ! Xtest_mks2.out 889 890let lines = readfile ( 'Xtest_mks1.out' ) 891let found_resize = v: false 892for line in lines 893if line =~ '^set lines=' 894let found_resize = v: true 895break 896endif 897endfor 898call assert_false ( found_resize ) 899let lines = readfile ( 'Xtest_mks2.out' ) 900let found_resize = v: false 901for line in lines 902if line =~ '^set lines=' 903let found_resize = v: true 904break 905endif 906endfor 907call assert_true ( found_resize ) 908 909call delete ( 'Xtest_mks1.out' ) 910call delete ( 'Xtest_mks2.out' ) 911set sessionoptions & 912endfunc 913 914" Test for mksession with a named scratch buffer 915func Test_mksession_scratch () 916enew |only 917file Xscratch 918set buftype = nofile 919mksession ! Xtest_mks.out 920 %bwipe 921source Xtest_mks.out 922call assert_equal ( 'Xscratch' , bufname ( '' )) 923call assert_equal ( 'nofile' , & buftype ) 924 %bwipe 925call delete ( 'Xtest_mks.out' ) 926endfunc 927 928" Test for mksession with fold options 929func Test_mksession_foldopt () 930set sessionoptions -= options 931set sessionoptions += folds 932new 933setlocal foldenable 934setlocal foldmethod = expr 935setlocal foldmarker = <<<,>>> 936setlocal foldignore = % 937setlocal foldlevel = 2 938setlocal foldminlines = 10 939setlocal foldnestmax = 15 940mksession ! Xtest_mks.out 941close 942 %bwipe 943 944source Xtest_mks.out 945call assert_true ( & foldenable ) 946call assert_equal ( 'expr' , & foldmethod ) 947call assert_equal ( '<<<,>>>' , & foldmarker ) 948call assert_equal ( '%' , & foldignore ) 949call assert_equal ( 2 , & foldlevel ) 950call assert_equal ( 10 , & foldminlines ) 951call assert_equal ( 15 , & foldnestmax ) 952 953close 954 %bwipe 955set sessionoptions & 956call delete ( 'Xtest_mks.out' ) 957endfunc 958 959" Test for mksession with "help" but not "options" in 'sessionoptions' 960func Test_mksession_help_noopt () 961set sessionoptions -= options 962set sessionoptions += help 963help 964let fname = expand ( '%' ) 965mksession ! Xtest_mks.out 966bwipe 967 968source Xtest_mks.out 969call assert_equal ( 'help' , & buftype ) 970call assert_equal ( 'help' , & filetype ) 971call assert_equal ( fname , expand ( '%' )) 972call assert_false ( & modifiable ) 973call assert_true ( & readonly ) 974 975helpclose 976help index 977let fname = expand ( '%' ) 978mksession ! Xtest_mks.out 979bwipe 980 981source Xtest_mks.out 982call assert_equal ( 'help' , & buftype ) 983call assert_equal ( fname , expand ( '%' )) 984 985call delete ( 'Xtest_mks.out' ) 986set sessionoptions & 987endfunc 988 989" Test for mksession with window position 990func Test_mksession_winpos () 991if ! has ( 'gui_running' ) 992" Only applicable in GUI Vim 993return 994endif 995set sessionoptions += winpos 996mksession ! Xtest_mks.out 997let found_winpos = v: false 998let lines = readfile ( 'Xtest_mks.out' ) 999for line in lines 1000if line =~ '^winpos ' 1001let found_winpos = v: true 1002break 1003endif 1004endfor 1005call assert_true ( found_winpos ) 1006call delete ( 'Xtest_mks.out' ) 1007set sessionoptions & 1008endfunc 1009 1010" Test for mksession without options restores winminheight 1011func Test_mksession_winminheight () 1012set winheight = 10 winwidth = 10 winminheight & winminwidth & 1013defer execute('set winheight& winwidth&') 1014set sessionoptions -= options 1015defer execute('set sessionoptions&') 1016split 1017mksession ! Xtest_mks.out 1018defer delete('Xtest_mks.out') 1019let found_restore = 0 1020let lines = readfile ( 'Xtest_mks.out' ) 1021for line in lines 1022if line =~ '= s:save_winmin\(width\|height\)' 1023let found_restore += 1 1024endif 1025endfor 1026call assert_equal ( 2 , found_restore ) 1027" Test with multiple tabpages 1028tab split |split |tab split |split 1029call assert_equal ( 3 , tabpagenr ( '$' )) 1030mksession ! Xtest_mks.out 1031tabclose |tabclose |close 1032call assert_equal ( 1 , tabpagenr ( '$' )) 1033set winminheight = 2 winminwidth = 2 1034defer execute('set winminheight& winminwidth&') 1035source Xtest_mks.out 1036call assert_equal ( 3 , tabpagenr ( '$' )) 1037call assert_equal ([ 2 , 2 ], [ & winminheight , & winminwidth ]) 1038tabclose |tabclose |close 1039endfunc 1040 1041" Test for mksession with and without options restores shortmess 1042func Test_mksession_shortmess () 1043" Without options 1044set sessionoptions -= options 1045split 1046mksession ! Xtest_mks.out 1047let found_save = 0 1048let found_restore = 0 1049let lines = readfile ( 'Xtest_mks.out' ) 1050for line in lines 1051let line = trim ( line ) 1052 1053if line ==# 'let s:shortmess_save = &shortmess' 1054let found_save += 1 1055endif 1056 1057if found_save !=# 0 && line ==# 'let &shortmess = s:shortmess_save' 1058let found_restore += 1 1059endif 1060endfor 1061call assert_equal ( 1 , found_save ) 1062call assert_equal ( 1 , found_restore ) 1063call delete ( 'Xtest_mks.out' ) 1064close 1065set sessionoptions & 1066 1067" With options 1068set sessionoptions += options 1069split 1070mksession ! Xtest_mks.out 1071let found_restore = 0 1072let lines = readfile ( 'Xtest_mks.out' ) 1073for line in lines 1074if line =~# 's:shortmess_save' 1075let found_restore += 1 1076endif 1077endfor 1078call assert_equal ( 0 , found_restore ) 1079call delete ( 'Xtest_mks.out' ) 1080close 1081set sessionoptions & 1082endfunc 1083 1084" Test that when Vim loading session has 'A' in 'shortmess' it does not 1085" complain about an existing swapfile. 1086func Test_mksession_shortmess_with_A () 1087edit Xtestfile 1088write 1089let fname = swapname ( '%' ) 1090let cont = readblob ( fname ) 1091set sessionoptions -= options 1092mksession Xtestsession 1093bwipe ! 1094 1095" Recreate the swap file to pretend the file is being edited 1096call writefile ( cont , fname , 'D' ) 1097set shortmess += A 1098source Xtestsession 1099 1100set shortmess & 1101set sessionoptions & 1102call delete ( 'Xtestsession' ) 1103call delete ( 'Xtestfile' ) 1104endfunc 1105 1106" Test for mksession with 'compatible' option 1107func Test_mksession_compatible () 1108throw 'skipped: Nvim does not support "compatible" option' 1109mksession ! Xtest_mks1.out 1110set compatible 1111mksession ! Xtest_mks2.out 1112set nocp 1113 1114let test_success = v: false 1115let lines = readfile ( 'Xtest_mks1.out' ) 1116for line in lines 1117if line =~ '^if &cp | set nocp | endif' 1118let test_success = v: true 1119break 1120endif 1121endfor 1122call assert_true ( test_success ) 1123 1124let test_success = v: false 1125let lines = readfile ( 'Xtest_mks2.out' ) 1126for line in lines 1127if line =~ '^if !&cp | set cp | endif' 1128let test_success = v: true 1129break 1130endif 1131endfor 1132call assert_true ( test_success ) 1133 1134call delete ( 'Xtest_mks1.out' ) 1135call delete ( 'Xtest_mks2.out' ) 1136set compatible & 1137set sessionoptions & 1138endfunc 1139 1140func s: ClearMappings () 1141mapclear 1142omapclear 1143mapclear ! 1144lmapclear 1145tmapclear 1146endfunc 1147 1148func Test_mkvimrc () 1149let entries = [ 1150 \[ '' , 'nothing' , '<Nop>' ], 1151 \[ 'n' , 'normal' , 'NORMAL<Up>' ], 1152 \[ 'v' , 'visual' , 'VISUAL<S-Down>' ], 1153 \[ 's' , 'select' , 'SELECT<C-Left>' ], 1154 \[ 'x' , 'visualonly' , 'VISUALONLY<M-Right>' ], 1155 \[ 'o' , 'operator' , 'OPERATOR' ], 1156 \[ 'i' , 'insert' , 'INSERT' ], 1157 \[ 'l' , 'lang' , 'LANG' ], 1158 \[ 'c' , 'command' , 'COMMAND' ], 1159 \[ 't' , 'terminal' , 'TERMINAL' ], 1160 \] 1161for entry in entries 1162exe entry [ 0 ] .. 'map ' .. entry [ 1 ] .. ' ' .. entry [ 2 ] 1163endfor 1164 1165mkvimrc Xtestvimrc 1166 1167call s: ClearMappings () 1168for entry in entries 1169call assert_equal ( '' , maparg ( entry [ 1 ], entry [ 0 ])) 1170endfor 1171 1172source Xtestvimrc 1173 1174for entry in entries 1175call assert_equal ( entry [ 2 ], maparg ( entry [ 1 ], entry [ 0 ])) 1176endfor 1177 1178call s: ClearMappings () 1179 1180" the 'pastetoggle', 'wildchar' and 'wildcharm' option values should be 1181" stored as key names in the vimrc file 1182" set pastetoggle=<F5> 1183set wildchar = <F6> 1184set wildcharm = <F7> 1185call assert_fails ( 'mkvimrc Xtestvimrc' , 'E189: "Xtestvimrc" exists' ) 1186mkvimrc ! Xtestvimrc 1187" call assert_notequal(-1, index(readfile('Xtestvimrc'), 'set pastetoggle=<F5>')) 1188call assert_notequal ( -1 , index ( readfile ( 'Xtestvimrc' ), 'set wildchar=<F6>' )) 1189call assert_notequal ( -1 , index ( readfile ( 'Xtestvimrc' ), 'set wildcharm=<F7>' )) 1190" set pastetoggle& wildchar& wildcharm& 1191set wildchar & wildcharm & 1192 1193call delete ( 'Xtestvimrc' ) 1194endfunc 1195 1196func Test_scrolloff () 1197set sessionoptions += localoptions 1198setlocal so = 1 siso = 1 1199mksession ! Xtest_mks.out 1200setlocal so = -1siso = -1 1201source Xtest_mks.out 1202call assert_equal ( 1 , & l: so ) 1203call assert_equal ( 1 , & l: siso ) 1204call delete ( 'Xtest_mks.out' ) 1205setlocal so & siso & 1206set sessionoptions & 1207endfunc 1208 1209func Test_altfile () 1210edit Xone 1211split Xtwo 1212edit Xtwoalt 1213edit # 1214wincmd w 1215edit Xonealt 1216edit # 1217mksession ! Xtest_altfile 1218only 1219bwipe Xonealt 1220bwipe Xtwoalt 1221bwipe ! 1222source Xtest_altfile 1223call assert_equal ( 'Xone' , bufname ()) 1224call assert_equal ( 'Xonealt' , bufname ( '#' )) 1225wincmd w 1226call assert_equal ( 'Xtwo' , bufname ()) 1227call assert_equal ( 'Xtwoalt' , bufname ( '#' )) 1228only 1229bwipe ! 1230call delete ( 'Xtest_altfile' ) 1231endfunc 1232 1233" Test for creating views with manual folds 1234func Test_mkview_manual_fold () 1235call writefile ( range ( 1 , 10 ), 'Xmkvfile' , 'D' ) 1236new Xmkvfile 1237" create recursive folds 12385 , 6 fold 12394 , 7 fold 1240mkview Xview 1241normal zE 1242source Xview 1243call assert_equal ([ -1 , 4 , 4 , 4 , 4 , -1 ], [ foldclosed ( 3 ), foldclosed ( 4 ), 1244 \foldclosed ( 5 ), foldclosed ( 6 ), foldclosed ( 7 ), foldclosed ( 8 )]) 1245" open one level of fold 12464 foldopen 1247mkview ! Xview 1248normal zE 1249source Xview 1250call assert_equal ([ -1 , -1 , 5 , 5 , -1 , -1 ], [ foldclosed ( 3 ), foldclosed ( 4 ), 1251 \foldclosed ( 5 ), foldclosed ( 6 ), foldclosed ( 7 ), foldclosed ( 8 )]) 1252" open all the folds 1253 %foldopen ! 1254mkview ! Xview 1255normal zE 1256source Xview 1257call assert_equal ([ -1 , -1 , -1 , -1 , -1 , -1 ], [ foldclosed ( 3 ), foldclosed ( 4 ), 1258 \foldclosed ( 5 ), foldclosed ( 6 ), foldclosed ( 7 ), foldclosed ( 8 )]) 1259call delete ( 'Xview' ) 1260bw ! 1261endfunc 1262 1263" Test for handling invalid folds within views 1264func Test_mkview_ignore_invalid_folds () 1265call writefile ( range ( 1 , 10 ), 'Xmkvfile' , 'D' ) 1266new Xmkvfile 1267" create some folds 12685 , 6 fold 12694 , 7 fold 1270mkview Xview 1271normal zE 1272" delete lines to make folds invalid 1273call deletebufline ( '' , 6 , '$' ) 1274source Xview 1275call assert_equal ([ -1 , -1 , -1 , -1 , -1 , -1 ], [ foldclosed ( 3 ), foldclosed ( 4 ), 1276 \foldclosed ( 5 ), foldclosed ( 6 ), foldclosed ( 7 ), foldclosed ( 8 )]) 1277call delete ( 'Xview' ) 1278bw ! 1279endfunc 1280 1281" Test default 'viewdir' value 1282func Test_mkview_default_home () 1283throw 'Skipped: N/A' 1284if has ( 'win32' ) 1285" use escape() to handle backslash path separators 1286call assert_match ( '^' .. escape ( $ ORIGHOME , '\' ) .. '/vimfiles' , & viewdir ) 1287elseif has ( 'unix' ) 1288call assert_match ( 1289 \'^' .. $ ORIGHOME .. '/.vim\|' .. 1290 \'^' .. $ XDG_CONFIG_HOME .. '/vim' 1291 \, & viewdir ) 1292elseif has ( 'amiga' ) 1293call assert_match ( '^home:vimfiles' , & viewdir ) 1294elseif has ( 'mac' ) 1295call assert_match ( '^' .. $ VIM .. '/vimfiles' , & viewdir ) 1296endif 1297endfunc 1298 1299" Test vim9 expression mappings 1300func Test_mksession_vim9_expr_mappings () 1301throw 'Skipped: Vim9 script is N/A' 1302CheckFeature packages 1303 1304" Create a dummy vim9 plugin 1305const base = getcwd () . '/rtdir' 1306const root = base . '/pack/test/opt/dummy9' 1307call mkdir ( root . '/plugin' , 'p' ) 1308let plugin_sources =<< trim END 1309vim9script 1310import autoload 'dummy9.vim' 1311nnoremap <expr> dummy-test dummy9.Test() .. "<CR>" 1312END 1313call writefile ( plugin_sources , root . '/plugin/dummy9.vim' ) 1314 1315call mkdir ( root . '/autoload' , 'p' ) 1316let auto_sources =<< trim END 1317vim9script 1318const ref_txt = 'Hello from vim9 dummy plugin!' 1319export def Test(): string 1320writefile([ref_txt], 'XDummyOutput') 1321return has("gui_running") ? '' : $':echomsg "{ref_txt}"' 1322enddef 1323END 1324call writefile ( auto_sources , root . '/autoload/dummy9.vim' ) 1325 1326" clean up later 1327defer delete(base,'rf' ) 1328 1329" Load and check the plugin 1330const ref_txt = 'Hello from vim9 dummy plugin!' 1331let orig_packpath = & packpath 1332let & packpath .= ',' . base 1333let orig_runtimepath = & runtimepath 1334packadd dummy9 1335defer execute('let &packpath = orig_runtimepath') 1336defer execute('let &runtimepath = orig_runtimepath') 1337messages clear 1338normal dummy-test 1339 1340if ! has ( 'gui_running' ) 1341call assert_match ( ref_txt , execute ( 'messages' ), 'No vim9 plugin dummy.Test() execution' ) 1342endif 1343call assert_true ( filereadable ( 'XDummyOutput' ), 'Output file was not created by Vim9 plugin' ) 1344call assert_equal ([ ref_txt ], readfile ( 'XDummyOutput' )) 1345call delete ( 'XDummyOutput' ) 1346 1347" Create a session file 1348mksession ! XDummySession.vim 1349defer delete('XDummySession.vim') 1350call assert_true ( filereadable ( 'XDummySession.vim' ), 'Session file was not created' ) 1351 1352" Check the session file mappings are operational 1353let test_sources =<< trim END 1354" load session 1355source XDummySession.vim 1356" execute vim9 expression mapping 1357normal dummy-test 1358" on my way 1359cq 1360END 1361call writefile ( test_sources , 'XTest.vim' , 'D' ) 1362" spawn a new Vim instance to load the session and execute the mapping 1363call system ( GetVimCommand ( 'XTest.vim' )) 1364defer delete('XDummyOutput') 1365call assert_true ( filereadable ( 'XDummyOutput' ), 1366 \'Expected output file was not created by Vim9 plugin' ) 1367call assert_equal ([ ref_txt ], readfile ( 'XDummyOutput' )) 1368 1369endfunc 1370 1371" Test legacy vimscript expression mappings 1372func Test_mksession_legacy_expr_mappings () 1373 1374CheckFeature packages 1375 1376" Create a dummy vim9 plugin 1377const base = getcwd () . '/rtdir' 1378const root = base . '/pack/test/opt/dummy' 1379call mkdir ( root . '/plugin' , 'p' ) 1380 1381" clean up later 1382defer delete(base,'rf' ) 1383 1384let plugin_sources =<< trim END 1385nnoremap <expr> dummy-test dummy#Test() . "<CR>" 1386END 1387call writefile ( plugin_sources , root . '/plugin/dummy.vim' ) 1388 1389call mkdir ( root . '/autoload' , 'p' ) 1390let auto_sources =<< trim END 1391const s:ref_txt = 'Hello from good old dummy plugin!' 1392func dummy#Test() 1393call writefile([s:ref_txt], 'XDummyOutput') 1394return has("gui_running") ? '' : $':echomsg "{s:ref_txt}"' 1395endfunc 1396END 1397call writefile ( auto_sources , root . '/autoload/dummy.vim' ) 1398 1399" Load and check the plugin 1400const ref_txt = 'Hello from good old dummy plugin!' 1401let orig_packpath = & packpath 1402let & packpath .= ',' . base 1403let orig_runtimepath = & runtimepath 1404packadd dummy 1405defer execute('let &packpath = orig_runtimepath') 1406defer execute('let &runtimepath = orig_runtimepath') 1407messages clear 1408normal dummy-test 1409 1410if ! has ( "gui_running" ) 1411call assert_match ( ref_txt , execute ( 'messages' ), 'No vim9 plugin dummy.Test() execution' ) 1412endif 1413call assert_true ( filereadable ( 'XDummyOutput' ), 'Output file was not created by legacy plugin' ) 1414call assert_equal ([ ref_txt ], readfile ( 'XDummyOutput' )) 1415call delete ( 'XDummyOutput' ) 1416 1417" Create a session file 1418mksession ! XDummySession.vim 1419defer delete('XDummySession.vim') 1420call assert_true ( filereadable ( 'XDummySession.vim' ), 'Session file was not created' ) 1421 1422" Check the session file mappings are operational 1423let test_sources =<< trim END 1424" load session 1425source XDummySession.vim 1426" execute legacy vimscript expression mapping 1427normal dummy-test 1428" on my way 1429cq 1430END 1431call writefile ( test_sources , 'XTest.vim' , 'D' ) 1432" spawn a new Vim instance to load the session and execute the mapping 1433call system ( GetVimCommand ( 'XTest.vim' )) 1434defer delete('XDummyOutput') 1435call assert_true ( filereadable ( 'XDummyOutput' ), 1436 \'Expected output file was not created by legacy vim plugin' ) 1437call assert_equal ([ ref_txt ], readfile ( 'XDummyOutput' )) 1438 1439endfunc 1440 1441" Test sessions cursor position management 1442func Test_mksession_cursor_position () 1443 1444" Set windows test scenario 1445let files = [] 1446for i in range ( 10 ) 1447let file = $ 'Xfile{i}' 1448exe $ "{i ? 'split' : 'edit '} {file}" 1449call append ( 0 , $ "Session file cursor position testing {i}") 1450" Force cursor position restoring commands 1451setlocal nowrap 1452normal dd29zl 1453" Check expected position 1454call assert_equal ([ 0 , 1 , 30 , 0 ], getpos ( '.' ), $ "Fail to set cursor position for {file}") 1455write ! 1456let files += [ file ] 1457endfor 1458 1459" Save session 1460mksession ! Xtest_curpos 1461 1462" Test restoring session 1463 %bwipe ! 1464try 1465source Xtest_curpos 1466catch 1467call assert_report ( "Failure sourcing session file" ) 1468endtry 1469 1470" Check cursor position 1471for file in files 1472exe $ "drop {file}" 1473call assert_equal ([ 0 , 1 , 30 , 0 ], getpos ( '.' ), $ "Cursor position not restored correctly for {file}") 1474endfor 1475 1476" Clean up 1477call delete ( 'Xtest_curpos' ) 1478for file in files 1479call delete ( file ) 1480endfor 1481 1482 %bwipe 1483endfunc 1484 1485func Test_mksession_with_Ctrl_I_map () 1486set sessionoptions = options 1487 1488" <Tab> and g<Tab> not mapped explicitly 1489imapclear 1490inoremap < C-I > foo 1491inoremap g< C-I > bar 1492imap < F2 > < C-I > 1493imap g< F2 > g< C-I > 1494mksession ! Xtest_mks.out 1495 1496" Check that the session doesn't create spurious simplified mappings 1497imapclear 1498source Xtest_mks.out 1499call assert_equal ( 'i <C-I> * foo' , execute ( 'imap <C-I>' ) ->trim ()) 1500call assert_equal ( 'No mapping found' , execute ( 'imap <Tab>' ) ->trim ()) 1501call assert_equal ( 'i g<C-I> * bar' , execute ( 'imap g<C-I>' ) ->trim ()) 1502call assert_equal ( 'No mapping found' , execute ( 'imap g<Tab>' ) ->trim ()) 1503 1504" Check that the restored mappings are working properly 1505new 1506call feedkeys ( "i\<*C-I>\<F2>g\<*C-I>g\<F2>\<Esc>" , 'tx' ) 1507call assert_equal ( 'foofoobarbar' , getline ( '.' )) 1508bwipe ! 1509 1510" <Tab> and g<Tab> mapped explicitly 1511imapclear 1512inoremap < C-I > foo 1513inoremap < Tab > FOO 1514inoremap g< C-I > bar 1515inoremap g< Tab > BAR 1516mksession ! Xtest_mks.out 1517 1518" Check that the session restores mappings properly 1519imapclear 1520source Xtest_mks.out 1521call assert_equal ( 'i <C-I> * foo' , execute ( 'imap <C-I>' ) ->trim ()) 1522call assert_equal ( 'i <Tab> * FOO' , execute ( 'imap <Tab>' ) ->trim ()) 1523call assert_equal ( 'i g<C-I> * bar' , execute ( 'imap g<C-I>' ) ->trim ()) 1524call assert_equal ( 'i g<Tab> * BAR' , execute ( 'imap g<Tab>' ) ->trim ()) 1525 1526" Check that the restored mappings are working properly 1527new 1528call feedkeys ( "i\<*C-I>\<Tab>g\<*C-I>g\<Tab>\<Esc>" , 'tx' ) 1529call assert_equal ( 'fooFOObarBAR' , getline ( '.' )) 1530bwipe ! 1531 1532call delete ( 'Xtest_mks.out' ) 1533imapclear 1534set sessionoptions & 1535endfunc 1536 1537" Test sessions global and local mappings 1538func Test_mksession_localmappings () 1539 1540" Create sessions. Mapping execution is tested running a file 1541let valid_sessions = [] " keep map info 1542let invalid_sessions = [] " do not keep map info 1543" localoptions requires a buffer 1544setlocal noswapfile 1545silent write XDummy 1546defer delete('XDummy') 1547 1548" Nvim: default 'sessionoptions' doesn't include "options" 1549"for option in ["&", "=options", "=localoptions"] 1550for option in [ "=options" , "=localoptions" ] 1551for global in [ 0 , 1 ] 1552 1553try 1554" select options 1555exe "set sessionoptions" .. option 1556 1557" mapping 1558exe "nnoremap" . ( global ? " " : " <buffer> " ) 1559 \. "dummy-test <Cmd>silent write XDummyOutput<CR>" 1560let case = $ "mapping_{global ? "global" : "local"}_{option}" 1561 1562" test mapping 1563normal dummy-test 1564call assert_true ( filereadable ( "XDummyOutput" ), $ "Output file was not created by {case}") 1565 1566" session 1567let sessionfile = "XSession_" . case 1568exe $ "mksession {sessionfile}" 1569 1570if global && option =~ "localoptions" 1571let invalid_sessions += [ sessionfile ] 1572else 1573let valid_sessions += [ sessionfile ] 1574endif 1575 1576finally 1577call delete ( "XDummyOutput" ) 1578 1579" clear mappings 1580nmapclear 1581nmapclear <buffer> 1582set sessionoptions & 1583endtry 1584 1585endfor 1586endfor 1587 1588" Check the session files are operational 1589for session in valid_sessions 1590 1591let test_sources =<< trim eval END 1592" load session 1593silent source {session} 1594" execute legacy vimscript expression mapping 1595normal dummy-test 1596" on my way 1597cq 1598END 1599 1600call writefile ( test_sources , 'XTest.vim' ) 1601call system ( GetVimCommand ( 'XTest.vim' )) 1602call assert_true ( filereadable ( 'XDummyOutput' ), 1603 \$ "Expected map not defined in session file {session}") 1604call delete ( 'XDummyOutput' ) 1605call delete ( session ) 1606 1607endfor 1608 1609for session in invalid_sessions 1610 1611let test_sources =<< trim eval END 1612" load session 1613silent source {session} 1614" execute legacy vimscript expression mapping 1615normal dummy-test 1616" on my way 1617cq 1618END 1619 1620call writefile ( test_sources , 'XTest.vim' ) 1621call system ( GetVimCommand ( 'XTest.vim' )) 1622call assert_false ( filereadable ( 'XDummyOutput' ), 1623 \$ "Unexpected map defined in session file {session}") 1624if filereadable ( 'XDummyOutput' ) 1625call delete ( 'XDummyOutput' ) 1626endif 1627call delete ( session ) 1628endfor 1629 1630endfunc 1631 1632" vim: shiftwidth=2 sts=2 expandtab