blob: 6797ad9ca06791ba9267dcb5acf483bd36825dd8 [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 Moolenaared18f2c2019-01-24 20:30:52 +01006if !has('mksession')
Bram Moolenaareca626f2016-12-01 18:47:38 +01007 finish
8endif
9
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010source shared.vim
11
Bram Moolenaareca626f2016-12-01 18:47:38 +010012func Test_mksession()
13 tabnew
14 let wrap_save = &wrap
15 set sessionoptions=buffers splitbelow fileencoding=latin1
16 call setline(1, [
17 \ 'start:',
18 \ 'no multibyte chAracter',
19 \ ' one leaDing tab',
20 \ ' four leadinG spaces',
21 \ 'two consecutive tabs',
22 \ 'two tabs in one line',
23 \ 'one ä multibyteCharacter',
24 \ 'aä Ä two multiByte characters',
Bram Moolenaar92c1b692018-08-29 21:42:42 +020025 \ 'Aäöü three mulTibyte characters',
26 \ 'short line',
Bram Moolenaareca626f2016-12-01 18:47:38 +010027 \ ])
Bram Moolenaarc9b56b22017-01-29 14:14:09 +010028 let tmpfile = 'Xtemp'
Bram Moolenaareca626f2016-12-01 18:47:38 +010029 exec 'w! ' . tmpfile
30 /^start:
31 set wrap
32 vsplit
33 norm! j16|
34 split
35 norm! j16|
36 split
37 norm! j16|
38 split
39 norm! j8|
40 split
41 norm! j8|
42 split
43 norm! j16|
44 split
45 norm! j16|
46 split
47 norm! j16|
Bram Moolenaar92c1b692018-08-29 21:42:42 +020048 split
49 norm! j$
Bram Moolenaareca626f2016-12-01 18:47:38 +010050 wincmd l
51
52 set nowrap
53 /^start:
54 norm! j16|3zl
55 split
56 norm! j016|3zl
57 split
58 norm! j016|3zl
59 split
60 norm! j08|3zl
61 split
62 norm! j08|3zl
63 split
64 norm! j016|3zl
65 split
66 norm! j016|3zl
67 split
68 norm! j016|3zl
69 split
70 call wincol()
Bram Moolenaarc9b56b22017-01-29 14:14:09 +010071 mksession! Xtest_mks.out
Bram Moolenaar92c1b692018-08-29 21:42:42 +020072 let li = filter(readfile('Xtest_mks.out'), 'v:val =~# "\\(^ *normal! [0$]\\|^ *exe ''normal!\\)"')
Bram Moolenaareca626f2016-12-01 18:47:38 +010073 let expected = [
74 \ 'normal! 016|',
75 \ 'normal! 016|',
76 \ 'normal! 016|',
77 \ 'normal! 08|',
78 \ 'normal! 08|',
79 \ 'normal! 016|',
80 \ 'normal! 016|',
81 \ 'normal! 016|',
Bram Moolenaar92c1b692018-08-29 21:42:42 +020082 \ 'normal! $',
Bram Moolenaareca626f2016-12-01 18:47:38 +010083 \ " exe 'normal! ' . s:c . '|zs' . 16 . '|'",
84 \ " normal! 016|",
85 \ " exe 'normal! ' . s:c . '|zs' . 16 . '|'",
86 \ " normal! 016|",
87 \ " exe 'normal! ' . s:c . '|zs' . 16 . '|'",
88 \ " normal! 016|",
89 \ " exe 'normal! ' . s:c . '|zs' . 8 . '|'",
90 \ " normal! 08|",
91 \ " exe 'normal! ' . s:c . '|zs' . 8 . '|'",
92 \ " normal! 08|",
93 \ " exe 'normal! ' . s:c . '|zs' . 16 . '|'",
94 \ " normal! 016|",
95 \ " exe 'normal! ' . s:c . '|zs' . 16 . '|'",
96 \ " normal! 016|",
97 \ " exe 'normal! ' . s:c . '|zs' . 16 . '|'",
98 \ " normal! 016|",
99 \ " exe 'normal! ' . s:c . '|zs' . 16 . '|'",
100 \ " normal! 016|"
101 \ ]
102 call assert_equal(expected, li)
103 tabclose!
104
Bram Moolenaarc9b56b22017-01-29 14:14:09 +0100105 call delete('Xtest_mks.out')
Bram Moolenaareca626f2016-12-01 18:47:38 +0100106 call delete(tmpfile)
107 let &wrap = wrap_save
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100108 set sessionoptions&
Bram Moolenaareca626f2016-12-01 18:47:38 +0100109endfunc
110
Bram Moolenaar36ae89c2017-01-28 17:11:14 +0100111func Test_mksession_winheight()
112 new
Bram Moolenaar1c3c1042018-06-12 16:49:30 +0200113 set winheight=10
114 set winminheight=2
Bram Moolenaarc9b56b22017-01-29 14:14:09 +0100115 mksession! Xtest_mks.out
116 source Xtest_mks.out
Bram Moolenaar36ae89c2017-01-28 17:11:14 +0100117
Bram Moolenaarc9b56b22017-01-29 14:14:09 +0100118 call delete('Xtest_mks.out')
Bram Moolenaar36ae89c2017-01-28 17:11:14 +0100119endfunc
120
Bram Moolenaar1c3c1042018-06-12 16:49:30 +0200121func Test_mksession_large_winheight()
122 set winheight=999
123 mksession! Xtest_mks_winheight.out
124 set winheight&
125 source Xtest_mks_winheight.out
126 call delete('Xtest_mks_winheight.out')
127endfunc
128
Bram Moolenaared18f2c2019-01-24 20:30:52 +0100129func Test_mksession_rtp()
130 new
131 let _rtp=&rtp
132 " Make a real long (invalid) runtimepath value,
133 " that should exceed PATH_MAX (hopefully)
134 let newrtp=&rtp.',~'.repeat('/foobar', 1000)
135 let newrtp.=",".expand("$HOME")."/.vim"
136 let &rtp=newrtp
137
138 " determine expected value
139 let expected=split(&rtp, ',')
140 let expected = map(expected, '"set runtimepath+=".v:val')
141 let expected = ['set runtimepath='] + expected
142 let expected = map(expected, {v,w -> substitute(w, $HOME, "~", "g")})
143
144 mksession! Xtest_mks.out
145 let &rtp=_rtp
146 let li = filter(readfile('Xtest_mks.out'), 'v:val =~# "runtimepath"')
147 call assert_equal(expected, li)
148
149 call delete('Xtest_mks.out')
150endfunc
151
Bram Moolenaar79da5632017-02-01 22:52:44 +0100152func Test_mksession_arglist()
153 argdel *
154 next file1 file2 file3 file4
155 mksession! Xtest_mks.out
156 source Xtest_mks.out
157 call assert_equal(['file1', 'file2', 'file3', 'file4'], argv())
158
159 call delete('Xtest_mks.out')
160 argdel *
161endfunc
162
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +0200163func Test_mksession_one_buffer_two_windows()
164 edit Xtest1
165 new Xtest2
166 split
167 mksession! Xtest_mks.out
168 let lines = readfile('Xtest_mks.out')
169 let count1 = 0
170 let count2 = 0
171 let count2buf = 0
172 for line in lines
173 if line =~ 'edit \f*Xtest1$'
174 let count1 += 1
175 endif
176 if line =~ 'edit \f\{-}Xtest2'
177 let count2 += 1
178 endif
179 if line =~ 'buffer \f\{-}Xtest2'
180 let count2buf += 1
181 endif
182 endfor
183 call assert_equal(1, count1, 'Xtest1 count')
184 call assert_equal(2, count2, 'Xtest2 count')
185 call assert_equal(2, count2buf, 'Xtest2 buffer count')
186
187 close
188 bwipe!
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +0200189 call delete('Xtest_mks.out')
190endfunc
191
Bram Moolenaar26d4b892018-07-04 22:26:28 +0200192func Test_mksession_lcd_multiple_tabs()
193 tabnew
194 tabnew
Bram Moolenaar81e2ac72018-07-04 22:44:08 +0200195 lcd .
Bram Moolenaar26d4b892018-07-04 22:26:28 +0200196 tabfirst
Bram Moolenaar81e2ac72018-07-04 22:44:08 +0200197 lcd .
Bram Moolenaar26d4b892018-07-04 22:26:28 +0200198 mksession! Xtest_mks.out
199 tabonly
200 source Xtest_mks.out
201 call assert_true(haslocaldir(), 'Tab 1 localdir')
202 tabnext 2
203 call assert_true(!haslocaldir(), 'Tab 2 localdir')
204 tabnext 3
205 call assert_true(haslocaldir(), 'Tab 3 localdir')
206 call delete('Xtest_mks.out')
207endfunc
208
209func Test_mksession_blank_tabs()
210 tabnew
211 tabnew
212 tabnew
213 tabnext 3
214 mksession! Xtest_mks.out
215 tabnew
216 tabnew
217 tabnext 2
218 source Xtest_mks.out
219 call assert_equal(4, tabpagenr('$'), 'session restore should restore number of tabs')
220 call assert_equal(3, tabpagenr(), 'session restore should restore the active tab')
221 call delete('Xtest_mks.out')
222endfunc
223
Bram Moolenaare3c74d22019-01-12 16:29:30 +0100224if has('extra_search')
225
226func Test_mksession_hlsearch()
227 set hlsearch
228 mksession! Xtest_mks.out
229 nohlsearch
230 source Xtest_mks.out
231 call assert_equal(1, v:hlsearch, 'session should restore search highlighting state')
232 nohlsearch
233 mksession! Xtest_mks.out
234 source Xtest_mks.out
235 call assert_equal(0, v:hlsearch, 'session should restore search highlighting state')
236 call delete('Xtest_mks.out')
237endfunc
238
239endif
240
241
Bram Moolenaar26d4b892018-07-04 22:26:28 +0200242func Test_mksession_blank_windows()
243 split
244 split
245 split
246 3 wincmd w
247 mksession! Xtest_mks.out
248 split
249 split
250 2 wincmd w
251 source Xtest_mks.out
252 call assert_equal(4, winnr('$'), 'session restore should restore number of windows')
253 call assert_equal(3, winnr(), 'session restore should restore the active window')
254 call delete('Xtest_mks.out')
255endfunc
256
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100257if has('terminal')
258
259func Test_mksession_terminal_shell()
260 terminal
261 mksession! Xtest_mks.out
262 let lines = readfile('Xtest_mks.out')
263 let term_cmd = ''
264 for line in lines
265 if line =~ '^terminal'
266 let term_cmd = line
267 elseif line =~ 'badd.*' . &shell
268 call assert_report('unexpected shell line: ' . line)
269 endif
270 endfor
271 call assert_match('terminal ++curwin ++cols=\d\+ ++rows=\d\+\s*$', term_cmd)
272
273 call Stop_shell_in_terminal(bufnr('%'))
274 call delete('Xtest_mks.out')
275endfunc
276
277func Test_mksession_terminal_no_restore_cmdarg()
278 terminal ++norestore
279 mksession! Xtest_mks.out
280 let lines = readfile('Xtest_mks.out')
281 let term_cmd = ''
282 for line in lines
283 if line =~ '^terminal'
284 call assert_report('session must not restore teminal')
285 endif
286 endfor
287
288 call Stop_shell_in_terminal(bufnr('%'))
289 call delete('Xtest_mks.out')
290endfunc
291
292func Test_mksession_terminal_no_restore_funcarg()
293 call term_start(&shell, {'norestore': 1})
294 mksession! Xtest_mks.out
295 let lines = readfile('Xtest_mks.out')
296 let term_cmd = ''
297 for line in lines
298 if line =~ '^terminal'
299 call assert_report('session must not restore teminal')
300 endif
301 endfor
302
303 call Stop_shell_in_terminal(bufnr('%'))
304 call delete('Xtest_mks.out')
305endfunc
306
307func Test_mksession_terminal_no_restore_func()
308 terminal
309 call term_setrestore(bufnr('%'), 'NONE')
310 mksession! Xtest_mks.out
311 let lines = readfile('Xtest_mks.out')
312 let term_cmd = ''
313 for line in lines
314 if line =~ '^terminal'
315 call assert_report('session must not restore teminal')
316 endif
317 endfor
318
319 call Stop_shell_in_terminal(bufnr('%'))
320 call delete('Xtest_mks.out')
321endfunc
322
323func Test_mksession_terminal_no_ssop()
324 terminal
325 set sessionoptions-=terminal
326 mksession! Xtest_mks.out
327 let lines = readfile('Xtest_mks.out')
328 let term_cmd = ''
329 for line in lines
330 if line =~ '^terminal'
331 call assert_report('session must not restore teminal')
332 endif
333 endfor
334
335 call Stop_shell_in_terminal(bufnr('%'))
336 call delete('Xtest_mks.out')
337 set sessionoptions&
338endfunc
339
340func Test_mksession_terminal_restore_other()
341 terminal
342 call term_setrestore(bufnr('%'), 'other')
343 mksession! Xtest_mks.out
344 let lines = readfile('Xtest_mks.out')
345 let term_cmd = ''
346 for line in lines
347 if line =~ '^terminal'
348 let term_cmd = line
349 endif
350 endfor
351 call assert_match('terminal ++curwin ++cols=\d\+ ++rows=\d\+ other', term_cmd)
352
353 call Stop_shell_in_terminal(bufnr('%'))
354 call delete('Xtest_mks.out')
355endfunc
356
357endif " has('terminal')
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +0200358
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200359" Test :mkview with a file argument.
360func Test_mkview_file()
361 " Create a view with line number and a fold.
362 help :mkview
363 set number
Bram Moolenaar2bf4fe02018-08-30 14:04:25 +0200364 norm! V}zf0
Bram Moolenaardd5d18e2018-08-30 14:16:06 +0200365 let pos = getpos('.')
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200366 let linefoldclosed1 = foldclosed('.')
367 mkview! Xview
368 set nonumber
369 norm! zrj
370 " We can close the help window, as mkview with a file name should
371 " generate a command to edit the file.
372 helpclose
373
374 source Xview
375 call assert_equal(1, &number)
376 call assert_match('\*:mkview\*$', getline('.'))
Bram Moolenaardd5d18e2018-08-30 14:16:06 +0200377 call assert_equal(pos, getpos('.'))
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200378 call assert_equal(linefoldclosed1, foldclosed('.'))
379
380 " Creating a view again with the same file name should fail (file
381 " already exists). But with a !, the previous view should be
382 " overwritten without error.
383 help :loadview
384 call assert_fails('mkview Xview', 'E189:')
385 call assert_match('\*:loadview\*$', getline('.'))
386 mkview! Xview
387 call assert_match('\*:loadview\*$', getline('.'))
388
389 call delete('Xview')
390 bwipe
391endfunc
392
393" Test :mkview and :loadview with a custom 'viewdir'.
394func Test_mkview_loadview_with_viewdir()
395 set viewdir=Xviewdir
396
397 help :mkview
398 set number
399 norm! V}zf
Bram Moolenaardd5d18e2018-08-30 14:16:06 +0200400 let pos = getpos('.')
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200401 let linefoldclosed1 = foldclosed('.')
402 mkview 1
403 set nonumber
404 norm! zrj
405
406 loadview 1
407
408 " The directory Xviewdir/ should have been created and the view
409 " should be stored in that directory.
410 call assert_equal('Xviewdir/' .
411 \ substitute(
412 \ substitute(
413 \ expand('%:p'), '/', '=+', 'g'), ':', '=-', 'g') . '=1.vim',
414 \ glob('Xviewdir/*'))
415 call assert_equal(1, &number)
416 call assert_match('\*:mkview\*$', getline('.'))
Bram Moolenaardd5d18e2018-08-30 14:16:06 +0200417 call assert_equal(pos, getpos('.'))
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200418 call assert_equal(linefoldclosed1, foldclosed('.'))
419
420 call delete('Xviewdir', 'rf')
421 set viewdir&
422 helpclose
423endfunc
424
425func Test_mkview_no_file_name()
426 new
427 " :mkview or :mkview {nr} should fail in a unnamed buffer.
428 call assert_fails('mkview', 'E32:')
429 call assert_fails('mkview 1', 'E32:')
430
431 " :mkview {file} should succeed in a unnamed buffer.
432 mkview Xview
433 help
434 source Xview
435 call assert_equal('', bufname('%'))
436
437 call delete('Xview')
438 %bwipe
439endfunc
Bram Moolenaar79da5632017-02-01 22:52:44 +0100440
Bram Moolenaar555de4e2019-01-21 23:03:49 +0100441" A clean session (one empty buffer, one window, and one tab) should not
442" set any error messages when sourced because no commands should fail.
443func Test_mksession_no_errmsg()
444 let v:errmsg = ''
445 %bwipe!
446 mksession! Xtest_mks.out
447 source Xtest_mks.out
448 call assert_equal('', v:errmsg)
449 call delete('Xtest_mks.out')
450endfunc
451
Bram Moolenaarad36a352019-01-24 13:34:42 +0100452func Test_mksession_quote_in_filename()
Bram Moolenaar9e79ccb2019-01-24 15:57:30 +0100453 if !has('unix')
454 " only Unix can handle this weird filename
455 return
456 endif
Bram Moolenaarad36a352019-01-24 13:34:42 +0100457 let v:errmsg = ''
458 %bwipe!
459 split another
460 split x'y\"z
461 mksession! Xtest_mks_quoted.out
462 %bwipe!
463 source Xtest_mks_quoted.out
464 call assert_true(bufexists("x'y\"z"))
465
466 %bwipe!
467 call delete('Xtest_mks_quoted.out')
468endfunc
469
Bram Moolenaar555de4e2019-01-21 23:03:49 +0100470
Bram Moolenaareca626f2016-12-01 18:47:38 +0100471" vim: shiftwidth=2 sts=2 expandtab