blob: e7aa25ca7aa5c05212bc6832be8be78ae22a4bb7 [file] [log] [blame]
Bram Moolenaareca626f2016-12-01 18:47:38 +01001" Test for :mksession, :mkview and :loadview in latin1 encoding
2
3set encoding=latin1
4scriptencoding latin1
5
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02006source check.vim
7CheckFeature mksession
Bram Moolenaareca626f2016-12-01 18:47:38 +01008
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01009source shared.vim
10
Bram Moolenaareca626f2016-12-01 18:47:38 +010011func Test_mksession()
12 tabnew
13 let wrap_save = &wrap
14 set sessionoptions=buffers splitbelow fileencoding=latin1
15 call setline(1, [
16 \ 'start:',
17 \ 'no multibyte chAracter',
18 \ ' one leaDing tab',
19 \ ' four leadinG spaces',
20 \ 'two consecutive tabs',
21 \ 'two tabs in one line',
22 \ 'one ä multibyteCharacter',
23 \ 'aä Ä two multiByte characters',
Bram Moolenaar92c1b692018-08-29 21:42:42 +020024 \ 'Aäöü three mulTibyte characters',
25 \ 'short line',
Bram Moolenaareca626f2016-12-01 18:47:38 +010026 \ ])
Bram Moolenaarc9b56b22017-01-29 14:14:09 +010027 let tmpfile = 'Xtemp'
Bram Moolenaareca626f2016-12-01 18:47:38 +010028 exec 'w! ' . tmpfile
29 /^start:
30 set wrap
31 vsplit
32 norm! j16|
33 split
34 norm! j16|
35 split
36 norm! j16|
37 split
38 norm! j8|
39 split
40 norm! j8|
41 split
42 norm! j16|
43 split
44 norm! j16|
45 split
46 norm! j16|
Bram Moolenaar92c1b692018-08-29 21:42:42 +020047 split
48 norm! j$
Bram Moolenaareca626f2016-12-01 18:47:38 +010049 wincmd l
50
51 set nowrap
52 /^start:
53 norm! j16|3zl
54 split
55 norm! j016|3zl
56 split
57 norm! j016|3zl
58 split
59 norm! j08|3zl
60 split
61 norm! j08|3zl
62 split
63 norm! j016|3zl
64 split
65 norm! j016|3zl
66 split
67 norm! j016|3zl
68 split
69 call wincol()
Bram Moolenaarc9b56b22017-01-29 14:14:09 +010070 mksession! Xtest_mks.out
Bram Moolenaar92c1b692018-08-29 21:42:42 +020071 let li = filter(readfile('Xtest_mks.out'), 'v:val =~# "\\(^ *normal! [0$]\\|^ *exe ''normal!\\)"')
Bram Moolenaareca626f2016-12-01 18:47:38 +010072 let expected = [
73 \ 'normal! 016|',
74 \ 'normal! 016|',
75 \ 'normal! 016|',
76 \ 'normal! 08|',
77 \ 'normal! 08|',
78 \ 'normal! 016|',
79 \ 'normal! 016|',
80 \ 'normal! 016|',
Bram Moolenaar92c1b692018-08-29 21:42:42 +020081 \ 'normal! $',
Bram Moolenaareca626f2016-12-01 18:47:38 +010082 \ " exe 'normal! ' . s:c . '|zs' . 16 . '|'",
83 \ " normal! 016|",
84 \ " exe 'normal! ' . s:c . '|zs' . 16 . '|'",
85 \ " normal! 016|",
86 \ " exe 'normal! ' . s:c . '|zs' . 16 . '|'",
87 \ " normal! 016|",
88 \ " exe 'normal! ' . s:c . '|zs' . 8 . '|'",
89 \ " normal! 08|",
90 \ " exe 'normal! ' . s:c . '|zs' . 8 . '|'",
91 \ " normal! 08|",
92 \ " exe 'normal! ' . s:c . '|zs' . 16 . '|'",
93 \ " normal! 016|",
94 \ " exe 'normal! ' . s:c . '|zs' . 16 . '|'",
95 \ " normal! 016|",
96 \ " exe 'normal! ' . s:c . '|zs' . 16 . '|'",
97 \ " normal! 016|",
98 \ " exe 'normal! ' . s:c . '|zs' . 16 . '|'",
99 \ " normal! 016|"
100 \ ]
101 call assert_equal(expected, li)
102 tabclose!
103
Bram Moolenaarc9b56b22017-01-29 14:14:09 +0100104 call delete('Xtest_mks.out')
Bram Moolenaareca626f2016-12-01 18:47:38 +0100105 call delete(tmpfile)
106 let &wrap = wrap_save
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100107 set sessionoptions&
Bram Moolenaareca626f2016-12-01 18:47:38 +0100108endfunc
109
Bram Moolenaar36ae89c2017-01-28 17:11:14 +0100110func Test_mksession_winheight()
111 new
Bram Moolenaar1c3c1042018-06-12 16:49:30 +0200112 set winheight=10
113 set winminheight=2
Bram Moolenaarc9b56b22017-01-29 14:14:09 +0100114 mksession! Xtest_mks.out
115 source Xtest_mks.out
Bram Moolenaar36ae89c2017-01-28 17:11:14 +0100116
Bram Moolenaarc9b56b22017-01-29 14:14:09 +0100117 call delete('Xtest_mks.out')
Bram Moolenaar36ae89c2017-01-28 17:11:14 +0100118endfunc
119
Bram Moolenaar1c3c1042018-06-12 16:49:30 +0200120func Test_mksession_large_winheight()
121 set winheight=999
122 mksession! Xtest_mks_winheight.out
123 set winheight&
124 source Xtest_mks_winheight.out
125 call delete('Xtest_mks_winheight.out')
126endfunc
127
Bram Moolenaared18f2c2019-01-24 20:30:52 +0100128func Test_mksession_rtp()
Bram Moolenaar681b6bc2019-01-24 22:23:58 +0100129 if has('win32')
130 " TODO: fix problem with backslashes
131 return
132 endif
Bram Moolenaared18f2c2019-01-24 20:30:52 +0100133 new
134 let _rtp=&rtp
135 " Make a real long (invalid) runtimepath value,
136 " that should exceed PATH_MAX (hopefully)
137 let newrtp=&rtp.',~'.repeat('/foobar', 1000)
138 let newrtp.=",".expand("$HOME")."/.vim"
139 let &rtp=newrtp
140
141 " determine expected value
142 let expected=split(&rtp, ',')
143 let expected = map(expected, '"set runtimepath+=".v:val')
144 let expected = ['set runtimepath='] + expected
145 let expected = map(expected, {v,w -> substitute(w, $HOME, "~", "g")})
146
147 mksession! Xtest_mks.out
148 let &rtp=_rtp
149 let li = filter(readfile('Xtest_mks.out'), 'v:val =~# "runtimepath"')
150 call assert_equal(expected, li)
151
152 call delete('Xtest_mks.out')
153endfunc
154
Bram Moolenaar79da5632017-02-01 22:52:44 +0100155func Test_mksession_arglist()
156 argdel *
157 next file1 file2 file3 file4
158 mksession! Xtest_mks.out
159 source Xtest_mks.out
160 call assert_equal(['file1', 'file2', 'file3', 'file4'], argv())
161
162 call delete('Xtest_mks.out')
163 argdel *
164endfunc
165
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +0200166func Test_mksession_one_buffer_two_windows()
167 edit Xtest1
168 new Xtest2
169 split
170 mksession! Xtest_mks.out
171 let lines = readfile('Xtest_mks.out')
172 let count1 = 0
173 let count2 = 0
174 let count2buf = 0
175 for line in lines
176 if line =~ 'edit \f*Xtest1$'
177 let count1 += 1
178 endif
179 if line =~ 'edit \f\{-}Xtest2'
180 let count2 += 1
181 endif
182 if line =~ 'buffer \f\{-}Xtest2'
183 let count2buf += 1
184 endif
185 endfor
186 call assert_equal(1, count1, 'Xtest1 count')
187 call assert_equal(2, count2, 'Xtest2 count')
188 call assert_equal(2, count2buf, 'Xtest2 buffer count')
189
190 close
191 bwipe!
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +0200192 call delete('Xtest_mks.out')
193endfunc
194
Bram Moolenaar26d4b892018-07-04 22:26:28 +0200195func Test_mksession_lcd_multiple_tabs()
196 tabnew
197 tabnew
Bram Moolenaar81e2ac72018-07-04 22:44:08 +0200198 lcd .
Bram Moolenaar26d4b892018-07-04 22:26:28 +0200199 tabfirst
Bram Moolenaar81e2ac72018-07-04 22:44:08 +0200200 lcd .
Bram Moolenaar26d4b892018-07-04 22:26:28 +0200201 mksession! Xtest_mks.out
202 tabonly
203 source Xtest_mks.out
204 call assert_true(haslocaldir(), 'Tab 1 localdir')
205 tabnext 2
206 call assert_true(!haslocaldir(), 'Tab 2 localdir')
207 tabnext 3
208 call assert_true(haslocaldir(), 'Tab 3 localdir')
209 call delete('Xtest_mks.out')
210endfunc
211
Bram Moolenaar00aa0692019-04-27 20:37:57 +0200212" Test for tabpage-local directory
213func Test_mksession_tcd_multiple_tabs()
214 let save_cwd = getcwd()
215 call mkdir('Xtopdir')
216 cd Xtopdir
217 call mkdir('Xtabdir1')
218 call mkdir('Xtabdir2')
219 call mkdir('Xtabdir3')
220 call mkdir('Xwindir1')
221 call mkdir('Xwindir2')
222 call mkdir('Xwindir3')
223 tcd Xtabdir1
224 botright new
225 wincmd t
226 lcd ../Xwindir1
227 tabnew
228 tcd ../Xtabdir2
229 botright new
230 lcd ../Xwindir2
231 tabnew
232 tcd ../Xtabdir3
233 botright new
234 lcd ../Xwindir3
235 tabfirst
236 1wincmd w
237 mksession! Xtest_mks.out
238 only | tabonly
239 source Xtest_mks.out
240 call assert_equal('Xtabdir1', fnamemodify(getcwd(-1, 1), ':t'))
241 call assert_equal('Xwindir1', fnamemodify(getcwd(1, 1), ':t'))
242 call assert_equal('Xtabdir1', fnamemodify(getcwd(2, 1), ':t'))
243 call assert_equal('Xtabdir2', fnamemodify(getcwd(-1, 2), ':t'))
244 call assert_equal('Xtabdir2', fnamemodify(getcwd(1, 2), ':t'))
245 call assert_equal('Xwindir2', fnamemodify(getcwd(2, 2), ':t'))
246 call assert_equal('Xtabdir3', fnamemodify(getcwd(-1, 3), ':t'))
247 call assert_equal('Xtabdir3', fnamemodify(getcwd(1, 3), ':t'))
248 call assert_equal('Xwindir3', fnamemodify(getcwd(2, 3), ':t'))
249 only | tabonly
250 exe 'cd ' . save_cwd
251 call delete("Xtopdir", "rf")
252endfunc
253
Bram Moolenaar26d4b892018-07-04 22:26:28 +0200254func Test_mksession_blank_tabs()
255 tabnew
256 tabnew
257 tabnew
258 tabnext 3
259 mksession! Xtest_mks.out
260 tabnew
261 tabnew
262 tabnext 2
263 source Xtest_mks.out
264 call assert_equal(4, tabpagenr('$'), 'session restore should restore number of tabs')
265 call assert_equal(3, tabpagenr(), 'session restore should restore the active tab')
266 call delete('Xtest_mks.out')
267endfunc
268
Bram Moolenaard39e2752019-01-26 20:07:38 +0100269func Test_mksession_buffer_count()
270 set hidden
271
272 " Edit exactly three files in the current session.
273 %bwipe!
274 e Xfoo | tabe Xbar | tabe Xbaz
275 tabdo write
276 mksession! Xtest_mks.out
277
278 " Verify that loading the session does not create additional buffers.
279 %bwipe!
280 source Xtest_mks.out
281 call assert_equal(3, len(getbufinfo()))
282
283 " Clean up.
284 call delete('Xfoo')
285 call delete('Xbar')
286 call delete('Xbaz')
287 call delete('Xtest_mks.out')
288 %bwipe!
289 set hidden&
290endfunc
291
Bram Moolenaare3c74d22019-01-12 16:29:30 +0100292if has('extra_search')
293
294func Test_mksession_hlsearch()
295 set hlsearch
296 mksession! Xtest_mks.out
297 nohlsearch
298 source Xtest_mks.out
299 call assert_equal(1, v:hlsearch, 'session should restore search highlighting state')
300 nohlsearch
301 mksession! Xtest_mks.out
302 source Xtest_mks.out
303 call assert_equal(0, v:hlsearch, 'session should restore search highlighting state')
304 call delete('Xtest_mks.out')
305endfunc
306
307endif
308
309
Bram Moolenaar26d4b892018-07-04 22:26:28 +0200310func Test_mksession_blank_windows()
311 split
312 split
313 split
314 3 wincmd w
315 mksession! Xtest_mks.out
316 split
317 split
318 2 wincmd w
319 source Xtest_mks.out
320 call assert_equal(4, winnr('$'), 'session restore should restore number of windows')
321 call assert_equal(3, winnr(), 'session restore should restore the active window')
322 call delete('Xtest_mks.out')
323endfunc
324
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100325if has('terminal')
326
327func Test_mksession_terminal_shell()
328 terminal
329 mksession! Xtest_mks.out
330 let lines = readfile('Xtest_mks.out')
331 let term_cmd = ''
332 for line in lines
333 if line =~ '^terminal'
334 let term_cmd = line
335 elseif line =~ 'badd.*' . &shell
336 call assert_report('unexpected shell line: ' . line)
337 endif
338 endfor
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100339 call assert_match('terminal ++curwin ++cols=\d\+ ++rows=\d\+\s*.*$', term_cmd)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100340
341 call Stop_shell_in_terminal(bufnr('%'))
342 call delete('Xtest_mks.out')
343endfunc
344
345func Test_mksession_terminal_no_restore_cmdarg()
346 terminal ++norestore
347 mksession! Xtest_mks.out
348 let lines = readfile('Xtest_mks.out')
349 let term_cmd = ''
350 for line in lines
351 if line =~ '^terminal'
352 call assert_report('session must not restore teminal')
353 endif
354 endfor
355
356 call Stop_shell_in_terminal(bufnr('%'))
357 call delete('Xtest_mks.out')
358endfunc
359
360func Test_mksession_terminal_no_restore_funcarg()
361 call term_start(&shell, {'norestore': 1})
362 mksession! Xtest_mks.out
363 let lines = readfile('Xtest_mks.out')
364 let term_cmd = ''
365 for line in lines
366 if line =~ '^terminal'
367 call assert_report('session must not restore teminal')
368 endif
369 endfor
370
371 call Stop_shell_in_terminal(bufnr('%'))
372 call delete('Xtest_mks.out')
373endfunc
374
375func Test_mksession_terminal_no_restore_func()
376 terminal
377 call term_setrestore(bufnr('%'), 'NONE')
378 mksession! Xtest_mks.out
379 let lines = readfile('Xtest_mks.out')
380 let term_cmd = ''
381 for line in lines
382 if line =~ '^terminal'
383 call assert_report('session must not restore teminal')
384 endif
385 endfor
386
387 call Stop_shell_in_terminal(bufnr('%'))
388 call delete('Xtest_mks.out')
389endfunc
390
391func Test_mksession_terminal_no_ssop()
392 terminal
393 set sessionoptions-=terminal
394 mksession! Xtest_mks.out
395 let lines = readfile('Xtest_mks.out')
396 let term_cmd = ''
397 for line in lines
398 if line =~ '^terminal'
399 call assert_report('session must not restore teminal')
400 endif
401 endfor
402
403 call Stop_shell_in_terminal(bufnr('%'))
404 call delete('Xtest_mks.out')
405 set sessionoptions&
406endfunc
407
408func Test_mksession_terminal_restore_other()
409 terminal
410 call term_setrestore(bufnr('%'), 'other')
411 mksession! Xtest_mks.out
412 let lines = readfile('Xtest_mks.out')
413 let term_cmd = ''
414 for line in lines
415 if line =~ '^terminal'
416 let term_cmd = line
417 endif
418 endfor
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100419 call assert_match('terminal ++curwin ++cols=\d\+ ++rows=\d\+.*other', term_cmd)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100420
421 call Stop_shell_in_terminal(bufnr('%'))
422 call delete('Xtest_mks.out')
423endfunc
424
425endif " has('terminal')
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +0200426
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200427" Test :mkview with a file argument.
428func Test_mkview_file()
429 " Create a view with line number and a fold.
430 help :mkview
431 set number
Bram Moolenaar2bf4fe02018-08-30 14:04:25 +0200432 norm! V}zf0
Bram Moolenaardd5d18e2018-08-30 14:16:06 +0200433 let pos = getpos('.')
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200434 let linefoldclosed1 = foldclosed('.')
435 mkview! Xview
436 set nonumber
437 norm! zrj
438 " We can close the help window, as mkview with a file name should
439 " generate a command to edit the file.
440 helpclose
441
442 source Xview
443 call assert_equal(1, &number)
444 call assert_match('\*:mkview\*$', getline('.'))
Bram Moolenaardd5d18e2018-08-30 14:16:06 +0200445 call assert_equal(pos, getpos('.'))
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200446 call assert_equal(linefoldclosed1, foldclosed('.'))
447
448 " Creating a view again with the same file name should fail (file
449 " already exists). But with a !, the previous view should be
450 " overwritten without error.
451 help :loadview
452 call assert_fails('mkview Xview', 'E189:')
453 call assert_match('\*:loadview\*$', getline('.'))
454 mkview! Xview
455 call assert_match('\*:loadview\*$', getline('.'))
456
457 call delete('Xview')
458 bwipe
459endfunc
460
461" Test :mkview and :loadview with a custom 'viewdir'.
462func Test_mkview_loadview_with_viewdir()
463 set viewdir=Xviewdir
464
465 help :mkview
466 set number
467 norm! V}zf
Bram Moolenaardd5d18e2018-08-30 14:16:06 +0200468 let pos = getpos('.')
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200469 let linefoldclosed1 = foldclosed('.')
470 mkview 1
471 set nonumber
472 norm! zrj
473
474 loadview 1
475
476 " The directory Xviewdir/ should have been created and the view
477 " should be stored in that directory.
478 call assert_equal('Xviewdir/' .
479 \ substitute(
480 \ substitute(
481 \ expand('%:p'), '/', '=+', 'g'), ':', '=-', 'g') . '=1.vim',
482 \ glob('Xviewdir/*'))
483 call assert_equal(1, &number)
484 call assert_match('\*:mkview\*$', getline('.'))
Bram Moolenaardd5d18e2018-08-30 14:16:06 +0200485 call assert_equal(pos, getpos('.'))
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200486 call assert_equal(linefoldclosed1, foldclosed('.'))
487
488 call delete('Xviewdir', 'rf')
489 set viewdir&
490 helpclose
491endfunc
492
493func Test_mkview_no_file_name()
494 new
495 " :mkview or :mkview {nr} should fail in a unnamed buffer.
496 call assert_fails('mkview', 'E32:')
497 call assert_fails('mkview 1', 'E32:')
498
499 " :mkview {file} should succeed in a unnamed buffer.
500 mkview Xview
501 help
502 source Xview
503 call assert_equal('', bufname('%'))
504
505 call delete('Xview')
506 %bwipe
507endfunc
Bram Moolenaar79da5632017-02-01 22:52:44 +0100508
Bram Moolenaar555de4e2019-01-21 23:03:49 +0100509" A clean session (one empty buffer, one window, and one tab) should not
510" set any error messages when sourced because no commands should fail.
511func Test_mksession_no_errmsg()
512 let v:errmsg = ''
513 %bwipe!
514 mksession! Xtest_mks.out
515 source Xtest_mks.out
516 call assert_equal('', v:errmsg)
517 call delete('Xtest_mks.out')
518endfunc
519
Bram Moolenaarad36a352019-01-24 13:34:42 +0100520func Test_mksession_quote_in_filename()
Bram Moolenaar9e79ccb2019-01-24 15:57:30 +0100521 if !has('unix')
522 " only Unix can handle this weird filename
523 return
524 endif
Bram Moolenaarad36a352019-01-24 13:34:42 +0100525 let v:errmsg = ''
526 %bwipe!
527 split another
528 split x'y\"z
529 mksession! Xtest_mks_quoted.out
530 %bwipe!
531 source Xtest_mks_quoted.out
532 call assert_true(bufexists("x'y\"z"))
533
534 %bwipe!
535 call delete('Xtest_mks_quoted.out')
536endfunc
537
Bram Moolenaar555de4e2019-01-21 23:03:49 +0100538
Bram Moolenaareca626f2016-12-01 18:47:38 +0100539" vim: shiftwidth=2 sts=2 expandtab