test_mksession.vim 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203
  1. " Test for :mksession, :mkview and :loadview in latin1 encoding
  2. scriptencoding latin1
  3. source check.vim
  4. CheckFeature mksession
  5. source shared.vim
  6. source term_util.vim
  7. " Test for storing global and local argument list in a session file
  8. " This one must be done first.
  9. func Test__mksession_arglocal()
  10. enew | only
  11. n a b c
  12. new
  13. arglocal
  14. mksession! Xtest_mks.out
  15. %bwipe!
  16. %argdelete
  17. argglobal
  18. source Xtest_mks.out
  19. call assert_equal(2, winnr('$'))
  20. call assert_equal(2, arglistid(1))
  21. call assert_equal(0, arglistid(2))
  22. %bwipe!
  23. %argdelete
  24. argglobal
  25. call delete('Xtest_mks.out')
  26. endfunc
  27. func Test_mksession()
  28. tabnew
  29. let wrap_save = &wrap
  30. set sessionoptions=buffers splitbelow fileencoding=latin1
  31. call setline(1, [
  32. \ 'start:',
  33. \ 'no multibyte chAracter',
  34. \ ' one leaDing tab',
  35. \ ' four leadinG spaces',
  36. \ 'two consecutive tabs',
  37. \ 'two tabs in one line',
  38. \ 'one ä multibyteCharacter',
  39. \ 'aä Ä two multiByte characters',
  40. \ 'Aäöü three mulTibyte characters',
  41. \ 'short line',
  42. \ ])
  43. let tmpfile = 'Xtemp'
  44. exec 'w! ' . tmpfile
  45. /^start:
  46. set wrap
  47. vsplit
  48. norm! j16|
  49. split
  50. norm! j16|
  51. split
  52. norm! j16|
  53. split
  54. norm! j8|
  55. split
  56. norm! j8|
  57. split
  58. norm! j16|
  59. split
  60. norm! j16|
  61. split
  62. norm! j16|
  63. split
  64. norm! j$
  65. wincmd l
  66. set nowrap
  67. /^start:
  68. norm! j16|3zl
  69. split
  70. norm! j016|3zl
  71. split
  72. norm! j016|3zl
  73. split
  74. norm! j08|3zl
  75. split
  76. norm! j08|3zl
  77. split
  78. norm! j016|3zl
  79. split
  80. norm! j016|3zl
  81. split
  82. norm! j016|3zl
  83. split
  84. call wincol()
  85. mksession! Xtest_mks.out
  86. let li = filter(readfile('Xtest_mks.out'), 'v:val =~# "\\(^ *normal! [0$]\\|^ *exe ''normal!\\)"')
  87. let expected = [
  88. \ 'normal! 016|',
  89. \ 'normal! 016|',
  90. \ 'normal! 016|',
  91. \ 'normal! 08|',
  92. \ 'normal! 08|',
  93. \ 'normal! 016|',
  94. \ 'normal! 016|',
  95. \ 'normal! 016|',
  96. \ 'normal! $',
  97. \ " exe 'normal! ' . s:c . '|zs' . 16 . '|'",
  98. \ " normal! 016|",
  99. \ " exe 'normal! ' . s:c . '|zs' . 16 . '|'",
  100. \ " normal! 016|",
  101. \ " exe 'normal! ' . s:c . '|zs' . 16 . '|'",
  102. \ " normal! 016|",
  103. \ " exe 'normal! ' . s:c . '|zs' . 8 . '|'",
  104. \ " normal! 08|",
  105. \ " exe 'normal! ' . s:c . '|zs' . 8 . '|'",
  106. \ " normal! 08|",
  107. \ " exe 'normal! ' . s:c . '|zs' . 16 . '|'",
  108. \ " normal! 016|",
  109. \ " exe 'normal! ' . s:c . '|zs' . 16 . '|'",
  110. \ " normal! 016|",
  111. \ " exe 'normal! ' . s:c . '|zs' . 16 . '|'",
  112. \ " normal! 016|",
  113. \ " exe 'normal! ' . s:c . '|zs' . 16 . '|'",
  114. \ " normal! 016|"
  115. \ ]
  116. call assert_equal(expected, li)
  117. tabclose!
  118. call delete('Xtest_mks.out')
  119. call delete(tmpfile)
  120. let &wrap = wrap_save
  121. set sessionoptions&
  122. endfunc
  123. func Test_mksession_winheight()
  124. new
  125. set winheight=10
  126. set winminheight=2
  127. mksession! Xtest_mks.out
  128. source Xtest_mks.out
  129. call delete('Xtest_mks.out')
  130. endfunc
  131. func Test_mksession_large_winheight()
  132. set winheight=999
  133. mksession! Xtest_mks_winheight.out
  134. set winheight&
  135. source Xtest_mks_winheight.out
  136. call delete('Xtest_mks_winheight.out')
  137. endfunc
  138. func Test_mksession_zero_winheight()
  139. set winminheight=0
  140. edit SomeFile
  141. split
  142. wincmd _
  143. mksession! Xtest_mks_zero
  144. set winminheight&
  145. let text = readfile('Xtest_mks_zero')->join()
  146. call delete('Xtest_mks_zero')
  147. close
  148. " check there is no divide by zero
  149. call assert_notmatch('/ 0[^0-9]', text)
  150. endfunc
  151. func Test_mksession_rtp()
  152. if has('win32')
  153. " TODO: fix problem with backslashes
  154. return
  155. endif
  156. new
  157. let _rtp=&rtp
  158. " Make a real long (invalid) runtimepath value,
  159. " that should exceed PATH_MAX (hopefully)
  160. let newrtp=&rtp.',~'.repeat('/foobar', 1000)
  161. let newrtp.=",".expand("$HOME")."/.vim"
  162. let &rtp=newrtp
  163. " determine expected value
  164. let expected=split(&rtp, ',')
  165. let expected = map(expected, '"set runtimepath+=".v:val')
  166. let expected = ['set runtimepath='] + expected
  167. let expected = map(expected, {v,w -> substitute(w, $HOME, "~", "g")})
  168. mksession! Xtest_mks.out
  169. let &rtp=_rtp
  170. let li = filter(readfile('Xtest_mks.out'), 'v:val =~# "runtimepath"')
  171. call assert_equal(expected, li)
  172. call delete('Xtest_mks.out')
  173. endfunc
  174. func Test_mksession_arglist()
  175. %argdel
  176. next file1 file2 file3 file4
  177. new
  178. next | next
  179. mksession! Xtest_mks.out
  180. source Xtest_mks.out
  181. call assert_equal(['file1', 'file2', 'file3', 'file4'], argv())
  182. call assert_equal(2, argidx())
  183. wincmd w
  184. call assert_equal(0, argidx())
  185. call delete('Xtest_mks.out')
  186. enew | only
  187. argdel *
  188. endfunc
  189. func Test_mksession_one_buffer_two_windows()
  190. edit Xtest1
  191. new Xtest2
  192. split
  193. mksession! Xtest_mks.out
  194. let lines = readfile('Xtest_mks.out')
  195. let count1 = 0
  196. let count2 = 0
  197. let count2buf = 0
  198. let bufexists = 0
  199. for line in lines
  200. if line =~ 'edit \f*Xtest1$'
  201. let count1 += 1
  202. endif
  203. if line =~ 'edit \f\{-}Xtest2'
  204. let count2 += 1
  205. endif
  206. if line =~ 'buffer \f\{-}Xtest2'
  207. let count2buf += 1
  208. endif
  209. if line =~ 'bufexists(fnamemodify(.*, ":p")'
  210. let bufexists += 1
  211. endif
  212. endfor
  213. call assert_equal(1, count1, 'Xtest1 count')
  214. call assert_equal(2, count2, 'Xtest2 count')
  215. call assert_equal(2, count2buf, 'Xtest2 buffer count')
  216. call assert_equal(2, bufexists)
  217. close
  218. bwipe!
  219. call delete('Xtest_mks.out')
  220. endfunc
  221. func Test_mksession_lcd_multiple_tabs()
  222. tabnew
  223. tabnew
  224. lcd .
  225. tabfirst
  226. lcd .
  227. mksession! Xtest_mks.out
  228. tabonly
  229. source Xtest_mks.out
  230. call assert_true(haslocaldir(), 'Tab 1 localdir')
  231. tabnext 2
  232. call assert_true(!haslocaldir(), 'Tab 2 localdir')
  233. tabnext 3
  234. call assert_true(haslocaldir(), 'Tab 3 localdir')
  235. call delete('Xtest_mks.out')
  236. endfunc
  237. " Test for tabpage-local directory
  238. func Test_mksession_tcd_multiple_tabs()
  239. let save_cwd = getcwd()
  240. call mkdir('Xtopdir')
  241. cd Xtopdir
  242. call mkdir('Xtabdir1')
  243. call mkdir('Xtabdir2')
  244. call mkdir('Xtabdir3')
  245. call mkdir('Xwindir1')
  246. call mkdir('Xwindir2')
  247. call mkdir('Xwindir3')
  248. tcd Xtabdir1
  249. botright new
  250. wincmd t
  251. lcd ../Xwindir1
  252. tabnew
  253. tcd ../Xtabdir2
  254. botright new
  255. lcd ../Xwindir2
  256. tabnew
  257. tcd ../Xtabdir3
  258. botright new
  259. lcd ../Xwindir3
  260. tabfirst
  261. 1wincmd w
  262. mksession! Xtest_mks.out
  263. only | tabonly
  264. source Xtest_mks.out
  265. call assert_equal('Xtabdir1', fnamemodify(getcwd(-1, 1), ':t'))
  266. call assert_equal('Xwindir1', fnamemodify(getcwd(1, 1), ':t'))
  267. call assert_equal('Xtabdir1', fnamemodify(getcwd(2, 1), ':t'))
  268. call assert_equal('Xtabdir2', fnamemodify(getcwd(-1, 2), ':t'))
  269. call assert_equal('Xtabdir2', fnamemodify(getcwd(1, 2), ':t'))
  270. call assert_equal('Xwindir2', fnamemodify(getcwd(2, 2), ':t'))
  271. call assert_equal('Xtabdir3', fnamemodify(getcwd(-1, 3), ':t'))
  272. call assert_equal('Xtabdir3', fnamemodify(getcwd(1, 3), ':t'))
  273. call assert_equal('Xwindir3', fnamemodify(getcwd(2, 3), ':t'))
  274. %bwipe
  275. call chdir(save_cwd)
  276. call delete("Xtopdir", "rf")
  277. endfunc
  278. func Test_mksession_blank_tabs()
  279. tabnew
  280. tabnew
  281. tabnew
  282. tabnext 3
  283. mksession! Xtest_mks.out
  284. tabnew
  285. tabnew
  286. tabnext 2
  287. source Xtest_mks.out
  288. call assert_equal(4, tabpagenr('$'), 'session restore should restore number of tabs')
  289. call assert_equal(3, tabpagenr(), 'session restore should restore the active tab')
  290. call delete('Xtest_mks.out')
  291. endfunc
  292. func Test_mksession_buffer_count()
  293. set hidden
  294. " Edit exactly three files in the current session.
  295. %bwipe!
  296. e Xfoo | tabe Xbar | tabe Xbaz
  297. tabdo write
  298. mksession! Xtest_mks.out
  299. " Verify that loading the session does not create additional buffers.
  300. %bwipe!
  301. source Xtest_mks.out
  302. call assert_equal(3, len(getbufinfo()))
  303. " Clean up.
  304. call delete('Xfoo')
  305. call delete('Xbar')
  306. call delete('Xbaz')
  307. call delete('Xtest_mks.out')
  308. %bwipe!
  309. set hidden&
  310. endfunc
  311. func Test_mksession_buffer_order()
  312. %bwipe!
  313. e Xfoo | e Xbar | e Xbaz | e Xqux
  314. bufdo write
  315. mksession! Xtest_mks.out
  316. " Verify that loading the session preserves order of buffers
  317. %bwipe!
  318. source Xtest_mks.out
  319. let s:buf_info = getbufinfo()
  320. call assert_true(s:buf_info[0]['name'] =~# 'Xfoo$')
  321. call assert_true(s:buf_info[1]['name'] =~# 'Xbar$')
  322. call assert_true(s:buf_info[2]['name'] =~# 'Xbaz$')
  323. call assert_true(s:buf_info[3]['name'] =~# 'Xqux$')
  324. " Clean up.
  325. call delete('Xfoo')
  326. call delete('Xbar')
  327. call delete('Xbaz')
  328. call delete('Xqux')
  329. call delete('Xtest_mks.out')
  330. %bwipe!
  331. endfunc
  332. if has('extra_search')
  333. func Test_mksession_hlsearch()
  334. set hlsearch
  335. mksession! Xtest_mks.out
  336. nohlsearch
  337. source Xtest_mks.out
  338. call assert_equal(1, v:hlsearch, 'session should restore search highlighting state')
  339. nohlsearch
  340. mksession! Xtest_mks.out
  341. source Xtest_mks.out
  342. call assert_equal(0, v:hlsearch, 'session should restore search highlighting state')
  343. call delete('Xtest_mks.out')
  344. endfunc
  345. endif
  346. func Test_mksession_blank_windows()
  347. split
  348. split
  349. split
  350. 3 wincmd w
  351. mksession! Xtest_mks.out
  352. split
  353. split
  354. 2 wincmd w
  355. source Xtest_mks.out
  356. call assert_equal(4, winnr('$'), 'session restore should restore number of windows')
  357. call assert_equal(3, winnr(), 'session restore should restore the active window')
  358. call delete('Xtest_mks.out')
  359. endfunc
  360. if has('terminal')
  361. func Test_mksession_terminal_shell()
  362. CheckFeature quickfix
  363. terminal
  364. mksession! Xtest_mks.out
  365. let lines = readfile('Xtest_mks.out')
  366. let term_cmd = ''
  367. for line in lines
  368. if line =~ '^terminal'
  369. let term_cmd = line
  370. elseif line =~ 'badd.*' . &shell
  371. call assert_report('unexpected shell line: ' . line)
  372. endif
  373. endfor
  374. call assert_match('terminal ++curwin ++cols=\d\+ ++rows=\d\+\s*.*$', term_cmd)
  375. call StopShellInTerminal(bufnr('%'))
  376. call delete('Xtest_mks.out')
  377. endfunc
  378. func Test_mksession_terminal_no_restore_cmdarg()
  379. terminal ++norestore
  380. mksession! Xtest_mks.out
  381. let lines = readfile('Xtest_mks.out')
  382. let term_cmd = ''
  383. for line in lines
  384. if line =~ '^terminal'
  385. call assert_report('session must not restore terminal')
  386. endif
  387. endfor
  388. call StopShellInTerminal(bufnr('%'))
  389. call delete('Xtest_mks.out')
  390. endfunc
  391. func Test_mksession_terminal_no_restore_funcarg()
  392. call term_start(&shell, {'norestore': 1})
  393. mksession! Xtest_mks.out
  394. let lines = readfile('Xtest_mks.out')
  395. let term_cmd = ''
  396. for line in lines
  397. if line =~ '^terminal'
  398. call assert_report('session must not restore terminal')
  399. endif
  400. endfor
  401. call StopShellInTerminal(bufnr('%'))
  402. call delete('Xtest_mks.out')
  403. endfunc
  404. func Test_mksession_terminal_no_restore_func()
  405. terminal
  406. call term_setrestore(bufnr('%'), 'NONE')
  407. mksession! Xtest_mks.out
  408. let lines = readfile('Xtest_mks.out')
  409. let term_cmd = ''
  410. for line in lines
  411. if line =~ '^terminal'
  412. call assert_report('session must not restore terminal')
  413. endif
  414. endfor
  415. call StopShellInTerminal(bufnr('%'))
  416. call delete('Xtest_mks.out')
  417. endfunc
  418. func Test_mksession_terminal_no_ssop()
  419. terminal
  420. set sessionoptions-=terminal
  421. mksession! Xtest_mks.out
  422. let lines = readfile('Xtest_mks.out')
  423. let term_cmd = ''
  424. for line in lines
  425. if line =~ '^terminal'
  426. call assert_report('session must not restore terminal')
  427. endif
  428. endfor
  429. call StopShellInTerminal(bufnr('%'))
  430. call delete('Xtest_mks.out')
  431. set sessionoptions&
  432. endfunc
  433. func Test_mksession_terminal_restore_other()
  434. CheckFeature quickfix
  435. terminal
  436. eval bufnr('%')->term_setrestore('other')
  437. mksession! Xtest_mks.out
  438. let lines = readfile('Xtest_mks.out')
  439. let term_cmd = ''
  440. for line in lines
  441. if line =~ '^terminal'
  442. let term_cmd = line
  443. endif
  444. endfor
  445. call assert_match('terminal ++curwin ++cols=\d\+ ++rows=\d\+.*other', term_cmd)
  446. call StopShellInTerminal(bufnr('%'))
  447. call delete('Xtest_mks.out')
  448. endfunc
  449. endif " has('terminal')
  450. func Test_mkview_open_folds()
  451. enew!
  452. call append(0, ['a', 'b', 'c'])
  453. 1,3fold
  454. write! Xtestfile
  455. call assert_notequal(-1, foldclosed(1))
  456. call assert_notequal(-1, foldclosed(2))
  457. call assert_notequal(-1, foldclosed(3))
  458. " Save the view with folds closed
  459. mkview! Xtestview
  460. " zR affects 'foldlevel', make sure the option is applied after the folds
  461. " have been recreated.
  462. " Open folds to ensure they get closed when restoring the view
  463. normal zR
  464. call assert_equal(-1, foldclosed(1))
  465. call assert_equal(-1, foldclosed(2))
  466. call assert_equal(-1, foldclosed(3))
  467. source Xtestview
  468. call assert_notequal(-1, foldclosed(1))
  469. call assert_notequal(-1, foldclosed(2))
  470. call assert_notequal(-1, foldclosed(3))
  471. call delete('Xtestview')
  472. call delete('Xtestfile')
  473. %bwipe
  474. endfunc
  475. func Test_mkview_no_balt()
  476. edit Xtestfile1
  477. edit Xtestfile2
  478. mkview! Xtestview
  479. bdelete Xtestfile1
  480. source Xtestview
  481. call assert_equal(0, buflisted('Xtestfile1'))
  482. call delete('Xtestview')
  483. %bwipe
  484. endfunc
  485. func Test_mksession_no_balt()
  486. edit Xtestfile1
  487. edit Xtestfile2
  488. bdelete Xtestfile1
  489. mksession! Xtestview
  490. source Xtestview
  491. call assert_equal(0, buflisted('Xtestfile1'))
  492. call delete('Xtestview')
  493. %bwipe
  494. endfunc
  495. " Test :mkview with a file argument.
  496. func Test_mkview_file()
  497. " Create a view with line number and a fold.
  498. help :mkview
  499. set number
  500. norm! V}zf0
  501. let pos = getpos('.')
  502. let linefoldclosed1 = foldclosed('.')
  503. mkview! Xview
  504. set nonumber
  505. norm! zrj
  506. " We can close the help window, as mkview with a file name should
  507. " generate a command to edit the file.
  508. helpclose
  509. source Xview
  510. call assert_equal(1, &number)
  511. call assert_match('\*:mkview\*$', getline('.'))
  512. call assert_equal(pos, getpos('.'))
  513. call assert_equal(linefoldclosed1, foldclosed('.'))
  514. " Creating a view again with the same file name should fail (file
  515. " already exists). But with a !, the previous view should be
  516. " overwritten without error.
  517. help :loadview
  518. call assert_fails('mkview Xview', 'E189:')
  519. call assert_match('\*:loadview\*$', getline('.'))
  520. mkview! Xview
  521. call assert_match('\*:loadview\*$', getline('.'))
  522. call delete('Xview')
  523. bwipe
  524. endfunc
  525. " Test :mkview and :loadview with a custom 'viewdir'.
  526. func Test_mkview_loadview_with_viewdir()
  527. set viewdir=Xviewdir
  528. help :mkview
  529. set number
  530. norm! V}zf
  531. let pos = getpos('.')
  532. let linefoldclosed1 = foldclosed('.')
  533. mkview 1
  534. set nonumber
  535. norm! zrj
  536. loadview 1
  537. " The directory Xviewdir/ should have been created and the view
  538. " should be stored in that directory.
  539. call assert_equal('Xviewdir/' .
  540. \ substitute(
  541. \ substitute(
  542. \ expand('%:p'), '/', '=+', 'g'), ':', '=-', 'g') . '=1.vim',
  543. \ glob('Xviewdir/*'))
  544. call assert_equal(1, &number)
  545. call assert_match('\*:mkview\*$', getline('.'))
  546. call assert_equal(pos, getpos('.'))
  547. call assert_equal(linefoldclosed1, foldclosed('.'))
  548. call delete('Xviewdir', 'rf')
  549. set viewdir&
  550. helpclose
  551. endfunc
  552. func Test_mkview_no_file_name()
  553. new
  554. " :mkview or :mkview {nr} should fail in an unnamed buffer.
  555. call assert_fails('mkview', 'E32:')
  556. call assert_fails('mkview 1', 'E32:')
  557. " :mkview {file} should succeed in an unnamed buffer.
  558. mkview Xview
  559. help
  560. source Xview
  561. call assert_equal('', bufname('%'))
  562. call delete('Xview')
  563. %bwipe
  564. endfunc
  565. func Test_mkview_loadview_jumplist()
  566. set viewdir=Xviewdir
  567. au BufWinLeave * silent mkview
  568. " au BufWinEnter * silent loadview
  569. edit Xfile1
  570. call setline(1, ['a', 'bbbbbbb', 'c'])
  571. normal j3l
  572. call assert_equal([2, 4], getcurpos()[1:2])
  573. write
  574. edit Xfile2
  575. call setline(1, ['d', 'eeeeeee', 'f'])
  576. normal j5l
  577. call assert_equal([2, 6], getcurpos()[1:2])
  578. write
  579. edit Xfile3
  580. call setline(1, ['g', 'h', 'iiiii'])
  581. normal jj3l
  582. call assert_equal([3, 4], getcurpos()[1:2])
  583. write
  584. " The commented :au above was moved here so that :mkview (on BufWinLeave) can
  585. " run before :loadview. This is needed because Nvim's :loadview raises E484 if
  586. " the view can't be opened, while Vim's silently fails instead.
  587. au BufWinEnter * silent loadview
  588. edit Xfile1
  589. call assert_equal([2, 4], getcurpos()[1:2])
  590. edit Xfile2
  591. call assert_equal([2, 6], getcurpos()[1:2])
  592. edit Xfile3
  593. call assert_equal([3, 4], getcurpos()[1:2])
  594. exe "normal \<C-O>"
  595. call assert_equal('Xfile2', expand('%'))
  596. call assert_equal([2, 6], getcurpos()[1:2])
  597. exe "normal \<C-O>"
  598. call assert_equal('Xfile1', expand('%'))
  599. call assert_equal([2, 4], getcurpos()[1:2])
  600. au! BufWinLeave
  601. au! BufWinEnter
  602. bwipe!
  603. call delete('Xviewdir', 'rf')
  604. call delete('Xfile1')
  605. call delete('Xfile2')
  606. call delete('Xfile3')
  607. set viewdir&
  608. endfunc
  609. " A clean session (one empty buffer, one window, and one tab) should not
  610. " set any error messages when sourced because no commands should fail.
  611. func Test_mksession_no_errmsg()
  612. let v:errmsg = ''
  613. %bwipe!
  614. mksession! Xtest_mks.out
  615. source Xtest_mks.out
  616. call assert_equal('', v:errmsg)
  617. call delete('Xtest_mks.out')
  618. endfunc
  619. func Test_mksession_quote_in_filename()
  620. if !has('unix')
  621. " only Unix can handle this weird filename
  622. return
  623. endif
  624. let v:errmsg = ''
  625. let filename = has('win32') ? 'x''y' : 'x''y"z'
  626. %bwipe!
  627. split another
  628. execute 'split' escape(filename, '"')
  629. mksession! Xtest_mks_quoted.out
  630. %bwipe!
  631. source Xtest_mks_quoted.out
  632. call assert_true(bufexists(filename))
  633. %bwipe!
  634. call delete('Xtest_mks_quoted.out')
  635. endfunc
  636. " Test for storing global variables in a session file
  637. func Test_mksession_globals()
  638. set sessionoptions+=globals
  639. " create different global variables
  640. let g:Global_string = "Sun is shining\r\n"
  641. let g:Global_count = 100
  642. let g:Global_pi = 3.14
  643. let g:Global_neg_float = -2.68
  644. mksession! Xtest_mks.out
  645. unlet g:Global_string
  646. unlet g:Global_count
  647. unlet g:Global_pi
  648. unlet g:Global_neg_float
  649. source Xtest_mks.out
  650. call assert_equal("Sun is shining\r\n", g:Global_string)
  651. call assert_equal(100, g:Global_count)
  652. call assert_equal(3.14, g:Global_pi)
  653. call assert_equal(-2.68, g:Global_neg_float)
  654. unlet g:Global_string
  655. unlet g:Global_count
  656. unlet g:Global_pi
  657. unlet g:Global_neg_float
  658. call delete('Xtest_mks.out')
  659. set sessionoptions&
  660. endfunc
  661. " Test for changing backslash to forward slash in filenames
  662. func Test_mksession_slash()
  663. if exists('+shellslash')
  664. throw 'Skipped: cannot use backslash in file name'
  665. endif
  666. enew
  667. %bwipe!
  668. e a\\b\\c
  669. mksession! Xtest_mks1.out
  670. set sessionoptions+=slash
  671. mksession! Xtest_mks2.out
  672. %bwipe!
  673. source Xtest_mks1.out
  674. call assert_equal('a/b/c', bufname(''))
  675. %bwipe!
  676. source Xtest_mks2.out
  677. call assert_equal('a/b/c', bufname(''))
  678. %bwipe!
  679. call delete('Xtest_mks1.out')
  680. call delete('Xtest_mks2.out')
  681. set sessionoptions&
  682. endfunc
  683. " Test for changing directory to the session file directory
  684. func Test_mksession_sesdir()
  685. let save_cwd = getcwd()
  686. call mkdir('Xproj')
  687. mksession! Xproj/Xtest_mks1.out
  688. set sessionoptions-=curdir
  689. set sessionoptions+=sesdir
  690. mksession! Xproj/Xtest_mks2.out
  691. source Xproj/Xtest_mks1.out
  692. call assert_equal('testdir', fnamemodify(getcwd(), ':t'))
  693. source Xproj/Xtest_mks2.out
  694. call assert_equal('Xproj', fnamemodify(getcwd(), ':t'))
  695. call chdir(save_cwd)
  696. %bwipe
  697. set sessionoptions&
  698. call delete('Xproj', 'rf')
  699. endfunc
  700. " Test for saving and restoring the tab-local working directory when there is
  701. " only a single tab and 'tabpages' is not in 'sessionoptions'.
  702. func Test_mksession_tcd_single_tabs()
  703. only | tabonly
  704. let save_cwd = getcwd()
  705. set sessionoptions-=tabpages
  706. set sessionoptions+=curdir
  707. call mkdir('Xtopdir1')
  708. call mkdir('Xtopdir2')
  709. " There are two tab pages, the current one has local cwd set to 'Xtopdir2'.
  710. exec 'tcd ' .. save_cwd .. '/Xtopdir1'
  711. tabnew
  712. exec 'tcd ' .. save_cwd .. '/Xtopdir2'
  713. mksession! Xtest_tcd_single
  714. source Xtest_tcd_single
  715. " call assert_equal(2, haslocaldir())
  716. call assert_equal(1, haslocaldir(-1))
  717. call assert_equal('Xtopdir2', fnamemodify(getcwd(-1, 0), ':t'))
  718. %bwipe
  719. set sessionoptions&
  720. call chdir(save_cwd)
  721. call delete('Xtopdir1', 'rf')
  722. call delete('Xtopdir2', 'rf')
  723. endfunc
  724. " Test for storing the 'lines' and 'columns' settings
  725. func Test_mksession_resize()
  726. mksession! Xtest_mks1.out
  727. set sessionoptions+=resize
  728. mksession! Xtest_mks2.out
  729. let lines = readfile('Xtest_mks1.out')
  730. let found_resize = v:false
  731. for line in lines
  732. if line =~ '^set lines='
  733. let found_resize = v:true
  734. break
  735. endif
  736. endfor
  737. call assert_false(found_resize)
  738. let lines = readfile('Xtest_mks2.out')
  739. let found_resize = v:false
  740. for line in lines
  741. if line =~ '^set lines='
  742. let found_resize = v:true
  743. break
  744. endif
  745. endfor
  746. call assert_true(found_resize)
  747. call delete('Xtest_mks1.out')
  748. call delete('Xtest_mks2.out')
  749. set sessionoptions&
  750. endfunc
  751. " Test for mksession with a named scratch buffer
  752. func Test_mksession_scratch()
  753. enew | only
  754. file Xscratch
  755. set buftype=nofile
  756. mksession! Xtest_mks.out
  757. %bwipe
  758. source Xtest_mks.out
  759. call assert_equal('Xscratch', bufname(''))
  760. call assert_equal('nofile', &buftype)
  761. %bwipe
  762. call delete('Xtest_mks.out')
  763. endfunc
  764. " Test for mksession with fold options
  765. func Test_mksession_foldopt()
  766. set sessionoptions-=options
  767. set sessionoptions+=folds
  768. new
  769. setlocal foldenable
  770. setlocal foldmethod=expr
  771. setlocal foldmarker=<<<,>>>
  772. setlocal foldignore=%
  773. setlocal foldlevel=2
  774. setlocal foldminlines=10
  775. setlocal foldnestmax=15
  776. mksession! Xtest_mks.out
  777. close
  778. %bwipe
  779. source Xtest_mks.out
  780. call assert_true(&foldenable)
  781. call assert_equal('expr', &foldmethod)
  782. call assert_equal('<<<,>>>', &foldmarker)
  783. call assert_equal('%', &foldignore)
  784. call assert_equal(2, &foldlevel)
  785. call assert_equal(10, &foldminlines)
  786. call assert_equal(15, &foldnestmax)
  787. close
  788. %bwipe
  789. set sessionoptions&
  790. call delete('Xtest_mks.out')
  791. endfunc
  792. " Test for mksession with "help" but not "options" in 'sessionoptions'
  793. func Test_mksession_help_noopt()
  794. set sessionoptions-=options
  795. set sessionoptions+=help
  796. help
  797. let fname = expand('%')
  798. mksession! Xtest_mks.out
  799. bwipe
  800. source Xtest_mks.out
  801. call assert_equal('help', &buftype)
  802. call assert_equal('help', &filetype)
  803. call assert_equal(fname, expand('%'))
  804. call assert_false(&modifiable)
  805. call assert_true(&readonly)
  806. helpclose
  807. help index
  808. let fname = expand('%')
  809. mksession! Xtest_mks.out
  810. bwipe
  811. source Xtest_mks.out
  812. call assert_equal('help', &buftype)
  813. call assert_equal(fname, expand('%'))
  814. call delete('Xtest_mks.out')
  815. set sessionoptions&
  816. endfunc
  817. " Test for mksession with window position
  818. func Test_mksession_winpos()
  819. if !has('gui_running')
  820. " Only applicable in GUI Vim
  821. return
  822. endif
  823. set sessionoptions+=winpos
  824. mksession! Xtest_mks.out
  825. let found_winpos = v:false
  826. let lines = readfile('Xtest_mks.out')
  827. for line in lines
  828. if line =~ '^winpos '
  829. let found_winpos = v:true
  830. break
  831. endif
  832. endfor
  833. call assert_true(found_winpos)
  834. call delete('Xtest_mks.out')
  835. set sessionoptions&
  836. endfunc
  837. " Test for mksession without options restores winminheight
  838. func Test_mksession_winminheight()
  839. set sessionoptions-=options
  840. split
  841. mksession! Xtest_mks.out
  842. let found_restore = 0
  843. let lines = readfile('Xtest_mks.out')
  844. for line in lines
  845. if line =~ '= s:save_winmin\(width\|height\)'
  846. let found_restore += 1
  847. endif
  848. endfor
  849. call assert_equal(2, found_restore)
  850. call delete('Xtest_mks.out')
  851. close
  852. set sessionoptions&
  853. endfunc
  854. " Test for mksession with and without options restores shortmess
  855. func Test_mksession_shortmess()
  856. " Without options
  857. set sessionoptions-=options
  858. split
  859. mksession! Xtest_mks.out
  860. let found_save = 0
  861. let found_restore = 0
  862. let lines = readfile('Xtest_mks.out')
  863. for line in lines
  864. let line = trim(line)
  865. if line ==# 'let s:shortmess_save = &shortmess'
  866. let found_save += 1
  867. endif
  868. if found_save !=# 0 && line ==# 'let &shortmess = s:shortmess_save'
  869. let found_restore += 1
  870. endif
  871. endfor
  872. call assert_equal(1, found_save)
  873. call assert_equal(1, found_restore)
  874. call delete('Xtest_mks.out')
  875. close
  876. set sessionoptions&
  877. " With options
  878. set sessionoptions+=options
  879. split
  880. mksession! Xtest_mks.out
  881. let found_restore = 0
  882. let lines = readfile('Xtest_mks.out')
  883. for line in lines
  884. if line =~# 's:shortmess_save'
  885. let found_restore += 1
  886. endif
  887. endfor
  888. call assert_equal(0, found_restore)
  889. call delete('Xtest_mks.out')
  890. close
  891. set sessionoptions&
  892. endfunc
  893. " Test that when Vim loading session has 'A' in 'shortmess' it does not
  894. " complain about an existing swapfile.
  895. func Test_mksession_shortmess_with_A()
  896. edit Xtestfile
  897. write
  898. let fname = swapname('%')
  899. let cont = readblob(fname)
  900. set sessionoptions-=options
  901. mksession Xtestsession
  902. bwipe!
  903. " Recreate the swap file to pretend the file is being edited
  904. call writefile(cont, fname, 'D')
  905. set shortmess+=A
  906. source Xtestsession
  907. set shortmess&
  908. set sessionoptions&
  909. call delete('Xtestsession')
  910. call delete('Xtestfile')
  911. endfunc
  912. " Test for mksession with 'compatible' option
  913. func Test_mksession_compatible()
  914. throw 'skipped: Nvim does not support "compatible" option'
  915. mksession! Xtest_mks1.out
  916. set compatible
  917. mksession! Xtest_mks2.out
  918. set nocp
  919. let test_success = v:false
  920. let lines = readfile('Xtest_mks1.out')
  921. for line in lines
  922. if line =~ '^if &cp | set nocp | endif'
  923. let test_success = v:true
  924. break
  925. endif
  926. endfor
  927. call assert_true(test_success)
  928. let test_success = v:false
  929. let lines = readfile('Xtest_mks2.out')
  930. for line in lines
  931. if line =~ '^if !&cp | set cp | endif'
  932. let test_success = v:true
  933. break
  934. endif
  935. endfor
  936. call assert_true(test_success)
  937. call delete('Xtest_mks1.out')
  938. call delete('Xtest_mks2.out')
  939. set compatible&
  940. set sessionoptions&
  941. endfunc
  942. func s:ClearMappings()
  943. mapclear
  944. omapclear
  945. mapclear!
  946. lmapclear
  947. tmapclear
  948. endfunc
  949. func Test_mkvimrc()
  950. let entries = [
  951. \ ['', 'nothing', '<Nop>'],
  952. \ ['n', 'normal', 'NORMAL'],
  953. \ ['v', 'visual', 'VISUAL'],
  954. \ ['s', 'select', 'SELECT'],
  955. \ ['x', 'visualonly', 'VISUALONLY'],
  956. \ ['o', 'operator', 'OPERATOR'],
  957. \ ['i', 'insert', 'INSERT'],
  958. \ ['l', 'lang', 'LANG'],
  959. \ ['c', 'command', 'COMMAND'],
  960. \ ['t', 'terminal', 'TERMINAL'],
  961. \ ]
  962. for entry in entries
  963. exe entry[0] .. 'map ' .. entry[1] .. ' ' .. entry[2]
  964. endfor
  965. mkvimrc Xtestvimrc
  966. call s:ClearMappings()
  967. for entry in entries
  968. call assert_equal('', maparg(entry[1], entry[0]))
  969. endfor
  970. source Xtestvimrc
  971. for entry in entries
  972. call assert_equal(entry[2], maparg(entry[1], entry[0]))
  973. endfor
  974. call s:ClearMappings()
  975. " the 'pastetoggle', 'wildchar' and 'wildcharm' option values should be
  976. " stored as key names in the vimrc file
  977. " set pastetoggle=<F5>
  978. set wildchar=<F6>
  979. set wildcharm=<F7>
  980. call assert_fails('mkvimrc Xtestvimrc')
  981. mkvimrc! Xtestvimrc
  982. " call assert_notequal(-1, index(readfile('Xtestvimrc'), 'set pastetoggle=<F5>'))
  983. call assert_notequal(-1, index(readfile('Xtestvimrc'), 'set wildchar=<F6>'))
  984. call assert_notequal(-1, index(readfile('Xtestvimrc'), 'set wildcharm=<F7>'))
  985. " set pastetoggle& wildchar& wildcharm&
  986. set wildchar& wildcharm&
  987. call delete('Xtestvimrc')
  988. endfunc
  989. func Test_scrolloff()
  990. set sessionoptions+=localoptions
  991. setlocal so=1 siso=1
  992. mksession! Xtest_mks.out
  993. setlocal so=-1 siso=-1
  994. source Xtest_mks.out
  995. call assert_equal(1, &l:so)
  996. call assert_equal(1, &l:siso)
  997. call delete('Xtest_mks.out')
  998. setlocal so& siso&
  999. set sessionoptions&
  1000. endfunc
  1001. func Test_altfile()
  1002. edit Xone
  1003. split Xtwo
  1004. edit Xtwoalt
  1005. edit #
  1006. wincmd w
  1007. edit Xonealt
  1008. edit #
  1009. mksession! Xtest_altfile
  1010. only
  1011. bwipe Xonealt
  1012. bwipe Xtwoalt
  1013. bwipe!
  1014. source Xtest_altfile
  1015. call assert_equal('Xone', bufname())
  1016. call assert_equal('Xonealt', bufname('#'))
  1017. wincmd w
  1018. call assert_equal('Xtwo', bufname())
  1019. call assert_equal('Xtwoalt', bufname('#'))
  1020. only
  1021. bwipe!
  1022. call delete('Xtest_altfile')
  1023. endfunc
  1024. " Test for creating views with manual folds
  1025. func Test_mkview_manual_fold()
  1026. call writefile(range(1,10), 'Xfile')
  1027. new Xfile
  1028. " create recursive folds
  1029. 5,6fold
  1030. 4,7fold
  1031. mkview Xview
  1032. normal zE
  1033. source Xview
  1034. call assert_equal([-1, 4, 4, 4, 4, -1], [foldclosed(3), foldclosed(4),
  1035. \ foldclosed(5), foldclosed(6), foldclosed(7), foldclosed(8)])
  1036. " open one level of fold
  1037. 4foldopen
  1038. mkview! Xview
  1039. normal zE
  1040. source Xview
  1041. call assert_equal([-1, -1, 5, 5, -1, -1], [foldclosed(3), foldclosed(4),
  1042. \ foldclosed(5), foldclosed(6), foldclosed(7), foldclosed(8)])
  1043. " open all the folds
  1044. %foldopen!
  1045. mkview! Xview
  1046. normal zE
  1047. source Xview
  1048. call assert_equal([-1, -1, -1, -1, -1, -1], [foldclosed(3), foldclosed(4),
  1049. \ foldclosed(5), foldclosed(6), foldclosed(7), foldclosed(8)])
  1050. call delete('Xfile')
  1051. call delete('Xview')
  1052. bw!
  1053. endfunc
  1054. " vim: shiftwidth=2 sts=2 expandtab