blob: 3698976caa14617ee2a4c32224442a288408a52e [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()
Bram Moolenaar681b6bc2019-01-24 22:23:58 +0100130 if has('win32')
131 " TODO: fix problem with backslashes
132 return
133 endif
Bram Moolenaared18f2c2019-01-24 20:30:52 +0100134 new
135 let _rtp=&rtp
136 " Make a real long (invalid) runtimepath value,
137 " that should exceed PATH_MAX (hopefully)
138 let newrtp=&rtp.',~'.repeat('/foobar', 1000)
139 let newrtp.=",".expand("$HOME")."/.vim"
140 let &rtp=newrtp
141
142 " determine expected value
143 let expected=split(&rtp, ',')
144 let expected = map(expected, '"set runtimepath+=".v:val')
145 let expected = ['set runtimepath='] + expected
146 let expected = map(expected, {v,w -> substitute(w, $HOME, "~", "g")})
147
148 mksession! Xtest_mks.out
149 let &rtp=_rtp
150 let li = filter(readfile('Xtest_mks.out'), 'v:val =~# "runtimepath"')
151 call assert_equal(expected, li)
152
153 call delete('Xtest_mks.out')
154endfunc
155
Bram Moolenaar79da5632017-02-01 22:52:44 +0100156func Test_mksession_arglist()
157 argdel *
158 next file1 file2 file3 file4
159 mksession! Xtest_mks.out
160 source Xtest_mks.out
161 call assert_equal(['file1', 'file2', 'file3', 'file4'], argv())
162
163 call delete('Xtest_mks.out')
164 argdel *
165endfunc
166
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +0200167func Test_mksession_one_buffer_two_windows()
168 edit Xtest1
169 new Xtest2
170 split
171 mksession! Xtest_mks.out
172 let lines = readfile('Xtest_mks.out')
173 let count1 = 0
174 let count2 = 0
175 let count2buf = 0
176 for line in lines
177 if line =~ 'edit \f*Xtest1$'
178 let count1 += 1
179 endif
180 if line =~ 'edit \f\{-}Xtest2'
181 let count2 += 1
182 endif
183 if line =~ 'buffer \f\{-}Xtest2'
184 let count2buf += 1
185 endif
186 endfor
187 call assert_equal(1, count1, 'Xtest1 count')
188 call assert_equal(2, count2, 'Xtest2 count')
189 call assert_equal(2, count2buf, 'Xtest2 buffer count')
190
191 close
192 bwipe!
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +0200193 call delete('Xtest_mks.out')
194endfunc
195
Bram Moolenaar26d4b892018-07-04 22:26:28 +0200196func Test_mksession_lcd_multiple_tabs()
197 tabnew
198 tabnew
Bram Moolenaar81e2ac72018-07-04 22:44:08 +0200199 lcd .
Bram Moolenaar26d4b892018-07-04 22:26:28 +0200200 tabfirst
Bram Moolenaar81e2ac72018-07-04 22:44:08 +0200201 lcd .
Bram Moolenaar26d4b892018-07-04 22:26:28 +0200202 mksession! Xtest_mks.out
203 tabonly
204 source Xtest_mks.out
205 call assert_true(haslocaldir(), 'Tab 1 localdir')
206 tabnext 2
207 call assert_true(!haslocaldir(), 'Tab 2 localdir')
208 tabnext 3
209 call assert_true(haslocaldir(), 'Tab 3 localdir')
210 call delete('Xtest_mks.out')
211endfunc
212
213func Test_mksession_blank_tabs()
214 tabnew
215 tabnew
216 tabnew
217 tabnext 3
218 mksession! Xtest_mks.out
219 tabnew
220 tabnew
221 tabnext 2
222 source Xtest_mks.out
223 call assert_equal(4, tabpagenr('$'), 'session restore should restore number of tabs')
224 call assert_equal(3, tabpagenr(), 'session restore should restore the active tab')
225 call delete('Xtest_mks.out')
226endfunc
227
Bram Moolenaare3c74d22019-01-12 16:29:30 +0100228if has('extra_search')
229
230func Test_mksession_hlsearch()
231 set hlsearch
232 mksession! Xtest_mks.out
233 nohlsearch
234 source Xtest_mks.out
235 call assert_equal(1, v:hlsearch, 'session should restore search highlighting state')
236 nohlsearch
237 mksession! Xtest_mks.out
238 source Xtest_mks.out
239 call assert_equal(0, v:hlsearch, 'session should restore search highlighting state')
240 call delete('Xtest_mks.out')
241endfunc
242
243endif
244
245
Bram Moolenaar26d4b892018-07-04 22:26:28 +0200246func Test_mksession_blank_windows()
247 split
248 split
249 split
250 3 wincmd w
251 mksession! Xtest_mks.out
252 split
253 split
254 2 wincmd w
255 source Xtest_mks.out
256 call assert_equal(4, winnr('$'), 'session restore should restore number of windows')
257 call assert_equal(3, winnr(), 'session restore should restore the active window')
258 call delete('Xtest_mks.out')
259endfunc
260
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100261if has('terminal')
262
263func Test_mksession_terminal_shell()
264 terminal
265 mksession! Xtest_mks.out
266 let lines = readfile('Xtest_mks.out')
267 let term_cmd = ''
268 for line in lines
269 if line =~ '^terminal'
270 let term_cmd = line
271 elseif line =~ 'badd.*' . &shell
272 call assert_report('unexpected shell line: ' . line)
273 endif
274 endfor
275 call assert_match('terminal ++curwin ++cols=\d\+ ++rows=\d\+\s*$', term_cmd)
276
277 call Stop_shell_in_terminal(bufnr('%'))
278 call delete('Xtest_mks.out')
279endfunc
280
281func Test_mksession_terminal_no_restore_cmdarg()
282 terminal ++norestore
283 mksession! Xtest_mks.out
284 let lines = readfile('Xtest_mks.out')
285 let term_cmd = ''
286 for line in lines
287 if line =~ '^terminal'
288 call assert_report('session must not restore teminal')
289 endif
290 endfor
291
292 call Stop_shell_in_terminal(bufnr('%'))
293 call delete('Xtest_mks.out')
294endfunc
295
296func Test_mksession_terminal_no_restore_funcarg()
297 call term_start(&shell, {'norestore': 1})
298 mksession! Xtest_mks.out
299 let lines = readfile('Xtest_mks.out')
300 let term_cmd = ''
301 for line in lines
302 if line =~ '^terminal'
303 call assert_report('session must not restore teminal')
304 endif
305 endfor
306
307 call Stop_shell_in_terminal(bufnr('%'))
308 call delete('Xtest_mks.out')
309endfunc
310
311func Test_mksession_terminal_no_restore_func()
312 terminal
313 call term_setrestore(bufnr('%'), 'NONE')
314 mksession! Xtest_mks.out
315 let lines = readfile('Xtest_mks.out')
316 let term_cmd = ''
317 for line in lines
318 if line =~ '^terminal'
319 call assert_report('session must not restore teminal')
320 endif
321 endfor
322
323 call Stop_shell_in_terminal(bufnr('%'))
324 call delete('Xtest_mks.out')
325endfunc
326
327func Test_mksession_terminal_no_ssop()
328 terminal
329 set sessionoptions-=terminal
330 mksession! Xtest_mks.out
331 let lines = readfile('Xtest_mks.out')
332 let term_cmd = ''
333 for line in lines
334 if line =~ '^terminal'
335 call assert_report('session must not restore teminal')
336 endif
337 endfor
338
339 call Stop_shell_in_terminal(bufnr('%'))
340 call delete('Xtest_mks.out')
341 set sessionoptions&
342endfunc
343
344func Test_mksession_terminal_restore_other()
345 terminal
346 call term_setrestore(bufnr('%'), 'other')
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 let term_cmd = line
353 endif
354 endfor
355 call assert_match('terminal ++curwin ++cols=\d\+ ++rows=\d\+ other', term_cmd)
356
357 call Stop_shell_in_terminal(bufnr('%'))
358 call delete('Xtest_mks.out')
359endfunc
360
361endif " has('terminal')
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +0200362
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200363" Test :mkview with a file argument.
364func Test_mkview_file()
365 " Create a view with line number and a fold.
366 help :mkview
367 set number
Bram Moolenaar2bf4fe02018-08-30 14:04:25 +0200368 norm! V}zf0
Bram Moolenaardd5d18e2018-08-30 14:16:06 +0200369 let pos = getpos('.')
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200370 let linefoldclosed1 = foldclosed('.')
371 mkview! Xview
372 set nonumber
373 norm! zrj
374 " We can close the help window, as mkview with a file name should
375 " generate a command to edit the file.
376 helpclose
377
378 source Xview
379 call assert_equal(1, &number)
380 call assert_match('\*:mkview\*$', getline('.'))
Bram Moolenaardd5d18e2018-08-30 14:16:06 +0200381 call assert_equal(pos, getpos('.'))
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200382 call assert_equal(linefoldclosed1, foldclosed('.'))
383
384 " Creating a view again with the same file name should fail (file
385 " already exists). But with a !, the previous view should be
386 " overwritten without error.
387 help :loadview
388 call assert_fails('mkview Xview', 'E189:')
389 call assert_match('\*:loadview\*$', getline('.'))
390 mkview! Xview
391 call assert_match('\*:loadview\*$', getline('.'))
392
393 call delete('Xview')
394 bwipe
395endfunc
396
397" Test :mkview and :loadview with a custom 'viewdir'.
398func Test_mkview_loadview_with_viewdir()
399 set viewdir=Xviewdir
400
401 help :mkview
402 set number
403 norm! V}zf
Bram Moolenaardd5d18e2018-08-30 14:16:06 +0200404 let pos = getpos('.')
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200405 let linefoldclosed1 = foldclosed('.')
406 mkview 1
407 set nonumber
408 norm! zrj
409
410 loadview 1
411
412 " The directory Xviewdir/ should have been created and the view
413 " should be stored in that directory.
414 call assert_equal('Xviewdir/' .
415 \ substitute(
416 \ substitute(
417 \ expand('%:p'), '/', '=+', 'g'), ':', '=-', 'g') . '=1.vim',
418 \ glob('Xviewdir/*'))
419 call assert_equal(1, &number)
420 call assert_match('\*:mkview\*$', getline('.'))
Bram Moolenaardd5d18e2018-08-30 14:16:06 +0200421 call assert_equal(pos, getpos('.'))
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200422 call assert_equal(linefoldclosed1, foldclosed('.'))
423
424 call delete('Xviewdir', 'rf')
425 set viewdir&
426 helpclose
427endfunc
428
429func Test_mkview_no_file_name()
430 new
431 " :mkview or :mkview {nr} should fail in a unnamed buffer.
432 call assert_fails('mkview', 'E32:')
433 call assert_fails('mkview 1', 'E32:')
434
435 " :mkview {file} should succeed in a unnamed buffer.
436 mkview Xview
437 help
438 source Xview
439 call assert_equal('', bufname('%'))
440
441 call delete('Xview')
442 %bwipe
443endfunc
Bram Moolenaar79da5632017-02-01 22:52:44 +0100444
Bram Moolenaar555de4e2019-01-21 23:03:49 +0100445" A clean session (one empty buffer, one window, and one tab) should not
446" set any error messages when sourced because no commands should fail.
447func Test_mksession_no_errmsg()
448 let v:errmsg = ''
449 %bwipe!
450 mksession! Xtest_mks.out
451 source Xtest_mks.out
452 call assert_equal('', v:errmsg)
453 call delete('Xtest_mks.out')
454endfunc
455
Bram Moolenaarad36a352019-01-24 13:34:42 +0100456func Test_mksession_quote_in_filename()
Bram Moolenaar9e79ccb2019-01-24 15:57:30 +0100457 if !has('unix')
458 " only Unix can handle this weird filename
459 return
460 endif
Bram Moolenaarad36a352019-01-24 13:34:42 +0100461 let v:errmsg = ''
462 %bwipe!
463 split another
464 split x'y\"z
465 mksession! Xtest_mks_quoted.out
466 %bwipe!
467 source Xtest_mks_quoted.out
468 call assert_true(bufexists("x'y\"z"))
469
470 %bwipe!
471 call delete('Xtest_mks_quoted.out')
472endfunc
473
Bram Moolenaar555de4e2019-01-21 23:03:49 +0100474
Bram Moolenaareca626f2016-12-01 18:47:38 +0100475" vim: shiftwidth=2 sts=2 expandtab