blob: 8eed111a85dd268c68994ea8f640850cdad295a1 [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
Bram Moolenaar7a39dd72019-06-23 00:50:15 +020010source term_util.vim
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011
Bram Moolenaar1e15e612019-11-09 17:18:52 +010012" Test for storing global and local argument list in a session file
13" This one must be done first.
14func Test__mksession_arglocal()
15 enew | only
16 n a b c
17 new
18 arglocal
19 mksession! Xtest_mks.out
20
21 %bwipe!
22 %argdelete
23 argglobal
24 source Xtest_mks.out
25 call assert_equal(2, winnr('$'))
26 call assert_equal(2, arglistid(1))
27 call assert_equal(0, arglistid(2))
28
29 %bwipe!
30 %argdelete
31 argglobal
32 call delete('Xtest_mks.out')
33endfunc
34
Bram Moolenaareca626f2016-12-01 18:47:38 +010035func Test_mksession()
36 tabnew
37 let wrap_save = &wrap
38 set sessionoptions=buffers splitbelow fileencoding=latin1
39 call setline(1, [
40 \ 'start:',
41 \ 'no multibyte chAracter',
42 \ ' one leaDing tab',
43 \ ' four leadinG spaces',
44 \ 'two consecutive tabs',
45 \ 'two tabs in one line',
46 \ 'one ä multibyteCharacter',
47 \ 'aä Ä two multiByte characters',
Bram Moolenaar92c1b692018-08-29 21:42:42 +020048 \ 'Aäöü three mulTibyte characters',
49 \ 'short line',
Bram Moolenaareca626f2016-12-01 18:47:38 +010050 \ ])
Bram Moolenaarc9b56b22017-01-29 14:14:09 +010051 let tmpfile = 'Xtemp'
Bram Moolenaareca626f2016-12-01 18:47:38 +010052 exec 'w! ' . tmpfile
53 /^start:
54 set wrap
55 vsplit
56 norm! j16|
57 split
58 norm! j16|
59 split
60 norm! j16|
61 split
62 norm! j8|
63 split
64 norm! j8|
65 split
66 norm! j16|
67 split
68 norm! j16|
69 split
70 norm! j16|
Bram Moolenaar92c1b692018-08-29 21:42:42 +020071 split
72 norm! j$
Bram Moolenaareca626f2016-12-01 18:47:38 +010073 wincmd l
74
75 set nowrap
76 /^start:
77 norm! j16|3zl
78 split
79 norm! j016|3zl
80 split
81 norm! j016|3zl
82 split
83 norm! j08|3zl
84 split
85 norm! j08|3zl
86 split
87 norm! j016|3zl
88 split
89 norm! j016|3zl
90 split
91 norm! j016|3zl
92 split
93 call wincol()
Bram Moolenaarc9b56b22017-01-29 14:14:09 +010094 mksession! Xtest_mks.out
Bram Moolenaar92c1b692018-08-29 21:42:42 +020095 let li = filter(readfile('Xtest_mks.out'), 'v:val =~# "\\(^ *normal! [0$]\\|^ *exe ''normal!\\)"')
Bram Moolenaareca626f2016-12-01 18:47:38 +010096 let expected = [
97 \ 'normal! 016|',
98 \ 'normal! 016|',
99 \ 'normal! 016|',
100 \ 'normal! 08|',
101 \ 'normal! 08|',
102 \ 'normal! 016|',
103 \ 'normal! 016|',
104 \ 'normal! 016|',
Bram Moolenaar92c1b692018-08-29 21:42:42 +0200105 \ 'normal! $',
Bram Moolenaareca626f2016-12-01 18:47:38 +0100106 \ " exe 'normal! ' . s:c . '|zs' . 16 . '|'",
107 \ " normal! 016|",
108 \ " exe 'normal! ' . s:c . '|zs' . 16 . '|'",
109 \ " normal! 016|",
110 \ " exe 'normal! ' . s:c . '|zs' . 16 . '|'",
111 \ " normal! 016|",
112 \ " exe 'normal! ' . s:c . '|zs' . 8 . '|'",
113 \ " normal! 08|",
114 \ " exe 'normal! ' . s:c . '|zs' . 8 . '|'",
115 \ " normal! 08|",
116 \ " exe 'normal! ' . s:c . '|zs' . 16 . '|'",
117 \ " normal! 016|",
118 \ " exe 'normal! ' . s:c . '|zs' . 16 . '|'",
119 \ " normal! 016|",
120 \ " exe 'normal! ' . s:c . '|zs' . 16 . '|'",
121 \ " normal! 016|",
122 \ " exe 'normal! ' . s:c . '|zs' . 16 . '|'",
123 \ " normal! 016|"
124 \ ]
125 call assert_equal(expected, li)
126 tabclose!
127
Bram Moolenaarc9b56b22017-01-29 14:14:09 +0100128 call delete('Xtest_mks.out')
Bram Moolenaareca626f2016-12-01 18:47:38 +0100129 call delete(tmpfile)
130 let &wrap = wrap_save
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100131 set sessionoptions&
Bram Moolenaareca626f2016-12-01 18:47:38 +0100132endfunc
133
Bram Moolenaar635bd602021-04-16 19:58:22 +0200134def Test_mksession_skiprtp()
135 mksession! Xtest_mks.out
136 var found = 0
137 for line in readfile('Xtest_mks.out')
138 if line =~ 'set runtimepath'
139 found = 1
140 break
141 endif
142 endfor
143 assert_equal(1, found)
144 delete('Xtest_mks.out')
145
146 set sessionoptions+=skiprtp
147 mksession! Xtest_mks.out
148 found = 0
149 for line in readfile('Xtest_mks.out')
150 if line =~ 'set runtimepath'
151 found = 1
152 break
153 endif
154 endfor
155 assert_equal(0, found)
156 delete('Xtest_mks.out')
157 set sessionoptions&
158enddef
159
Bram Moolenaar36ae89c2017-01-28 17:11:14 +0100160func Test_mksession_winheight()
161 new
Bram Moolenaar1c3c1042018-06-12 16:49:30 +0200162 set winheight=10
163 set winminheight=2
Bram Moolenaarc9b56b22017-01-29 14:14:09 +0100164 mksession! Xtest_mks.out
165 source Xtest_mks.out
Bram Moolenaar36ae89c2017-01-28 17:11:14 +0100166
Bram Moolenaarc9b56b22017-01-29 14:14:09 +0100167 call delete('Xtest_mks.out')
Bram Moolenaar36ae89c2017-01-28 17:11:14 +0100168endfunc
169
Bram Moolenaar1c3c1042018-06-12 16:49:30 +0200170func Test_mksession_large_winheight()
171 set winheight=999
172 mksession! Xtest_mks_winheight.out
173 set winheight&
174 source Xtest_mks_winheight.out
175 call delete('Xtest_mks_winheight.out')
176endfunc
177
Bram Moolenaared18f2c2019-01-24 20:30:52 +0100178func Test_mksession_rtp()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200179 " TODO: fix problem with backslashes on Win32
180 CheckNotMSWindows
181
Bram Moolenaared18f2c2019-01-24 20:30:52 +0100182 new
183 let _rtp=&rtp
184 " Make a real long (invalid) runtimepath value,
185 " that should exceed PATH_MAX (hopefully)
186 let newrtp=&rtp.',~'.repeat('/foobar', 1000)
187 let newrtp.=",".expand("$HOME")."/.vim"
188 let &rtp=newrtp
189
190 " determine expected value
191 let expected=split(&rtp, ',')
192 let expected = map(expected, '"set runtimepath+=".v:val')
193 let expected = ['set runtimepath='] + expected
194 let expected = map(expected, {v,w -> substitute(w, $HOME, "~", "g")})
195
196 mksession! Xtest_mks.out
197 let &rtp=_rtp
198 let li = filter(readfile('Xtest_mks.out'), 'v:val =~# "runtimepath"')
199 call assert_equal(expected, li)
200
201 call delete('Xtest_mks.out')
202endfunc
203
Bram Moolenaar79da5632017-02-01 22:52:44 +0100204func Test_mksession_arglist()
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200205 %argdel
Bram Moolenaar79da5632017-02-01 22:52:44 +0100206 next file1 file2 file3 file4
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200207 new
208 next | next
Bram Moolenaar79da5632017-02-01 22:52:44 +0100209 mksession! Xtest_mks.out
210 source Xtest_mks.out
211 call assert_equal(['file1', 'file2', 'file3', 'file4'], argv())
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200212 call assert_equal(2, argidx())
213 wincmd w
214 call assert_equal(0, argidx())
Bram Moolenaar79da5632017-02-01 22:52:44 +0100215
216 call delete('Xtest_mks.out')
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200217 enew | only
Bram Moolenaar79da5632017-02-01 22:52:44 +0100218 argdel *
219endfunc
220
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +0200221func Test_mksession_one_buffer_two_windows()
222 edit Xtest1
223 new Xtest2
224 split
225 mksession! Xtest_mks.out
226 let lines = readfile('Xtest_mks.out')
227 let count1 = 0
228 let count2 = 0
229 let count2buf = 0
230 for line in lines
231 if line =~ 'edit \f*Xtest1$'
232 let count1 += 1
233 endif
234 if line =~ 'edit \f\{-}Xtest2'
235 let count2 += 1
236 endif
237 if line =~ 'buffer \f\{-}Xtest2'
238 let count2buf += 1
239 endif
240 endfor
241 call assert_equal(1, count1, 'Xtest1 count')
242 call assert_equal(2, count2, 'Xtest2 count')
243 call assert_equal(2, count2buf, 'Xtest2 buffer count')
244
245 close
246 bwipe!
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +0200247 call delete('Xtest_mks.out')
248endfunc
249
Bram Moolenaar26d4b892018-07-04 22:26:28 +0200250func Test_mksession_lcd_multiple_tabs()
251 tabnew
252 tabnew
Bram Moolenaar81e2ac72018-07-04 22:44:08 +0200253 lcd .
Bram Moolenaar26d4b892018-07-04 22:26:28 +0200254 tabfirst
Bram Moolenaar81e2ac72018-07-04 22:44:08 +0200255 lcd .
Bram Moolenaar26d4b892018-07-04 22:26:28 +0200256 mksession! Xtest_mks.out
257 tabonly
258 source Xtest_mks.out
259 call assert_true(haslocaldir(), 'Tab 1 localdir')
260 tabnext 2
261 call assert_true(!haslocaldir(), 'Tab 2 localdir')
262 tabnext 3
263 call assert_true(haslocaldir(), 'Tab 3 localdir')
264 call delete('Xtest_mks.out')
265endfunc
266
Bram Moolenaar00aa0692019-04-27 20:37:57 +0200267" Test for tabpage-local directory
268func Test_mksession_tcd_multiple_tabs()
269 let save_cwd = getcwd()
270 call mkdir('Xtopdir')
271 cd Xtopdir
272 call mkdir('Xtabdir1')
273 call mkdir('Xtabdir2')
274 call mkdir('Xtabdir3')
275 call mkdir('Xwindir1')
276 call mkdir('Xwindir2')
277 call mkdir('Xwindir3')
278 tcd Xtabdir1
279 botright new
280 wincmd t
281 lcd ../Xwindir1
282 tabnew
283 tcd ../Xtabdir2
284 botright new
285 lcd ../Xwindir2
286 tabnew
287 tcd ../Xtabdir3
288 botright new
289 lcd ../Xwindir3
290 tabfirst
291 1wincmd w
292 mksession! Xtest_mks.out
293 only | tabonly
294 source Xtest_mks.out
295 call assert_equal('Xtabdir1', fnamemodify(getcwd(-1, 1), ':t'))
296 call assert_equal('Xwindir1', fnamemodify(getcwd(1, 1), ':t'))
297 call assert_equal('Xtabdir1', fnamemodify(getcwd(2, 1), ':t'))
298 call assert_equal('Xtabdir2', fnamemodify(getcwd(-1, 2), ':t'))
299 call assert_equal('Xtabdir2', fnamemodify(getcwd(1, 2), ':t'))
300 call assert_equal('Xwindir2', fnamemodify(getcwd(2, 2), ':t'))
301 call assert_equal('Xtabdir3', fnamemodify(getcwd(-1, 3), ':t'))
302 call assert_equal('Xtabdir3', fnamemodify(getcwd(1, 3), ':t'))
303 call assert_equal('Xwindir3', fnamemodify(getcwd(2, 3), ':t'))
Bram Moolenaar5bf46e92019-11-09 18:06:04 +0100304 %bwipe
305 call chdir(save_cwd)
Bram Moolenaar00aa0692019-04-27 20:37:57 +0200306 call delete("Xtopdir", "rf")
307endfunc
308
Bram Moolenaar26d4b892018-07-04 22:26:28 +0200309func Test_mksession_blank_tabs()
310 tabnew
311 tabnew
312 tabnew
313 tabnext 3
314 mksession! Xtest_mks.out
315 tabnew
316 tabnew
317 tabnext 2
318 source Xtest_mks.out
319 call assert_equal(4, tabpagenr('$'), 'session restore should restore number of tabs')
320 call assert_equal(3, tabpagenr(), 'session restore should restore the active tab')
321 call delete('Xtest_mks.out')
322endfunc
323
Bram Moolenaard39e2752019-01-26 20:07:38 +0100324func Test_mksession_buffer_count()
325 set hidden
326
327 " Edit exactly three files in the current session.
328 %bwipe!
329 e Xfoo | tabe Xbar | tabe Xbaz
330 tabdo write
331 mksession! Xtest_mks.out
332
333 " Verify that loading the session does not create additional buffers.
334 %bwipe!
335 source Xtest_mks.out
336 call assert_equal(3, len(getbufinfo()))
337
338 " Clean up.
339 call delete('Xfoo')
340 call delete('Xbar')
341 call delete('Xbaz')
342 call delete('Xtest_mks.out')
343 %bwipe!
344 set hidden&
345endfunc
346
Bram Moolenaare3c74d22019-01-12 16:29:30 +0100347if has('extra_search')
348
349func Test_mksession_hlsearch()
350 set hlsearch
351 mksession! Xtest_mks.out
352 nohlsearch
353 source Xtest_mks.out
354 call assert_equal(1, v:hlsearch, 'session should restore search highlighting state')
355 nohlsearch
356 mksession! Xtest_mks.out
357 source Xtest_mks.out
358 call assert_equal(0, v:hlsearch, 'session should restore search highlighting state')
359 call delete('Xtest_mks.out')
360endfunc
361
362endif
363
364
Bram Moolenaar26d4b892018-07-04 22:26:28 +0200365func Test_mksession_blank_windows()
366 split
367 split
368 split
369 3 wincmd w
370 mksession! Xtest_mks.out
371 split
372 split
373 2 wincmd w
374 source Xtest_mks.out
375 call assert_equal(4, winnr('$'), 'session restore should restore number of windows')
376 call assert_equal(3, winnr(), 'session restore should restore the active window')
377 call delete('Xtest_mks.out')
378endfunc
379
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100380func Test_mksession_terminal_shell()
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200381 CheckFeature terminal
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100382 CheckFeature quickfix
383
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100384 terminal
385 mksession! Xtest_mks.out
386 let lines = readfile('Xtest_mks.out')
387 let term_cmd = ''
388 for line in lines
389 if line =~ '^terminal'
390 let term_cmd = line
391 elseif line =~ 'badd.*' . &shell
392 call assert_report('unexpected shell line: ' . line)
393 endif
394 endfor
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100395 call assert_match('terminal ++curwin ++cols=\d\+ ++rows=\d\+\s*.*$', term_cmd)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100396
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200397 call StopShellInTerminal(bufnr('%'))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100398 call delete('Xtest_mks.out')
399endfunc
400
401func Test_mksession_terminal_no_restore_cmdarg()
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200402 CheckFeature terminal
403
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100404 terminal ++norestore
405 mksession! Xtest_mks.out
406 let lines = readfile('Xtest_mks.out')
407 let term_cmd = ''
408 for line in lines
409 if line =~ '^terminal'
Bram Moolenaarac665c22020-12-08 20:39:15 +0100410 call assert_report('session must not restore terminal')
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100411 endif
412 endfor
413
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200414 call StopShellInTerminal(bufnr('%'))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100415 call delete('Xtest_mks.out')
416endfunc
417
418func Test_mksession_terminal_no_restore_funcarg()
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200419 CheckFeature terminal
420
Bram Moolenaar3a6aadb2021-04-04 15:28:59 +0200421 let buf = Run_shell_in_terminal({'norestore': 1})
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100422 mksession! Xtest_mks.out
423 let lines = readfile('Xtest_mks.out')
424 let term_cmd = ''
425 for line in lines
426 if line =~ '^terminal'
Bram Moolenaarac665c22020-12-08 20:39:15 +0100427 call assert_report('session must not restore terminal')
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100428 endif
429 endfor
430
Bram Moolenaar3a6aadb2021-04-04 15:28:59 +0200431 call StopShellInTerminal(buf)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100432 call delete('Xtest_mks.out')
433endfunc
434
435func Test_mksession_terminal_no_restore_func()
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200436 CheckFeature terminal
437
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100438 terminal
439 call term_setrestore(bufnr('%'), 'NONE')
440 mksession! Xtest_mks.out
441 let lines = readfile('Xtest_mks.out')
442 let term_cmd = ''
443 for line in lines
444 if line =~ '^terminal'
Bram Moolenaarac665c22020-12-08 20:39:15 +0100445 call assert_report('session must not restore terminal')
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100446 endif
447 endfor
448
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200449 call StopShellInTerminal(bufnr('%'))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100450 call delete('Xtest_mks.out')
451endfunc
452
453func Test_mksession_terminal_no_ssop()
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200454 CheckFeature terminal
455
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100456 terminal
457 set sessionoptions-=terminal
458 mksession! Xtest_mks.out
459 let lines = readfile('Xtest_mks.out')
460 let term_cmd = ''
461 for line in lines
462 if line =~ '^terminal'
Bram Moolenaarac665c22020-12-08 20:39:15 +0100463 call assert_report('session must not restore terminal')
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100464 endif
465 endfor
466
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200467 call StopShellInTerminal(bufnr('%'))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100468 call delete('Xtest_mks.out')
469 set sessionoptions&
470endfunc
471
472func Test_mksession_terminal_restore_other()
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200473 CheckFeature terminal
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100474 CheckFeature quickfix
475
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100476 terminal
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200477 eval bufnr('%')->term_setrestore('other')
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100478 mksession! Xtest_mks.out
479 let lines = readfile('Xtest_mks.out')
480 let term_cmd = ''
481 for line in lines
482 if line =~ '^terminal'
483 let term_cmd = line
484 endif
485 endfor
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100486 call assert_match('terminal ++curwin ++cols=\d\+ ++rows=\d\+.*other', term_cmd)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100487
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200488 call StopShellInTerminal(bufnr('%'))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100489 call delete('Xtest_mks.out')
490endfunc
491
Bram Moolenaar0e655112020-09-11 20:36:36 +0200492func Test_mksession_terminal_shared_windows()
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200493 CheckFeature terminal
494
Bram Moolenaar0e655112020-09-11 20:36:36 +0200495 terminal
496 let term_buf = bufnr()
497 new
498 execute "buffer" term_buf
499 mksession! Xtest_mks.out
500
501 let lines = readfile('Xtest_mks.out')
502 let found_creation = 0
503 let found_use = 0
504
505 for line in lines
506 if line =~ '^terminal'
507 let found_creation = 1
508 call assert_match('terminal ++curwin ++cols=\d\+ ++rows=\d\+', line)
509 elseif line =~ "^execute 'buffer ' . s:term_buf_" . term_buf . "$"
510 let found_use = 1
511 endif
512 endfor
513
514 call assert_true(found_creation && found_use)
515
516 call StopShellInTerminal(term_buf)
517 call delete('Xtest_mks.out')
518endfunc
519
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200520func Test_mkview_terminal_windows()
521 CheckFeature terminal
522
523 " create two window on the same terminal to check this is handled OK
524 terminal
525 let term_buf = bufnr()
526 exe 'sbuf ' .. term_buf
527 mkview! Xtestview
528
529 call StopShellInTerminal(term_buf)
530 call delete('Xtestview')
531endfunc
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +0200532
Bram Moolenaarf9547eb2021-02-01 19:24:55 +0100533func Test_mkview_open_folds()
534 enew!
535
536 call append(0, ['a', 'b', 'c'])
537 1,3fold
538 " zR affects 'foldlevel', make sure the option is applied after the folds
539 " have been recreated.
540 normal zR
541 write! Xtestfile
542
543 call assert_equal(-1, foldclosed(1))
544 call assert_equal(-1, foldclosed(2))
545 call assert_equal(-1, foldclosed(3))
546
547 mkview! Xtestview
548 source Xtestview
549
550 call assert_equal(-1, foldclosed(1))
551 call assert_equal(-1, foldclosed(2))
552 call assert_equal(-1, foldclosed(3))
553
554 call delete('Xtestview')
555 call delete('Xtestfile')
556 %bwipe
557endfunc
558
Bram Moolenaar139348f2021-02-05 21:55:53 +0100559func Test_mkview_no_balt()
560 edit Xtestfile1
561 edit Xtestfile2
562
563 mkview! Xtestview
564 bdelete Xtestfile1
565
566 source Xtestview
567 call assert_equal(0, buflisted('Xtestfile1'))
568
569 call delete('Xtestview')
570 %bwipe
571endfunc
572
Bram Moolenaar0756f752021-03-13 13:52:33 +0100573func Test_mksession_no_balt()
574 edit Xtestfile1
575 edit Xtestfile2
576
577 bdelete Xtestfile1
578 mksession! Xtestview
579
580 source Xtestview
581 call assert_equal(0, buflisted('Xtestfile1'))
582
583 call delete('Xtestview')
584 %bwipe
585endfunc
586
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200587" Test :mkview with a file argument.
588func Test_mkview_file()
589 " Create a view with line number and a fold.
590 help :mkview
591 set number
Bram Moolenaar2bf4fe02018-08-30 14:04:25 +0200592 norm! V}zf0
Bram Moolenaardd5d18e2018-08-30 14:16:06 +0200593 let pos = getpos('.')
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200594 let linefoldclosed1 = foldclosed('.')
595 mkview! Xview
596 set nonumber
597 norm! zrj
598 " We can close the help window, as mkview with a file name should
599 " generate a command to edit the file.
600 helpclose
601
602 source Xview
603 call assert_equal(1, &number)
604 call assert_match('\*:mkview\*$', getline('.'))
Bram Moolenaardd5d18e2018-08-30 14:16:06 +0200605 call assert_equal(pos, getpos('.'))
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200606 call assert_equal(linefoldclosed1, foldclosed('.'))
607
608 " Creating a view again with the same file name should fail (file
609 " already exists). But with a !, the previous view should be
610 " overwritten without error.
611 help :loadview
612 call assert_fails('mkview Xview', 'E189:')
613 call assert_match('\*:loadview\*$', getline('.'))
614 mkview! Xview
615 call assert_match('\*:loadview\*$', getline('.'))
616
617 call delete('Xview')
618 bwipe
619endfunc
620
621" Test :mkview and :loadview with a custom 'viewdir'.
622func Test_mkview_loadview_with_viewdir()
623 set viewdir=Xviewdir
624
625 help :mkview
626 set number
627 norm! V}zf
Bram Moolenaardd5d18e2018-08-30 14:16:06 +0200628 let pos = getpos('.')
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200629 let linefoldclosed1 = foldclosed('.')
630 mkview 1
631 set nonumber
632 norm! zrj
633
634 loadview 1
635
636 " The directory Xviewdir/ should have been created and the view
637 " should be stored in that directory.
638 call assert_equal('Xviewdir/' .
639 \ substitute(
640 \ substitute(
641 \ expand('%:p'), '/', '=+', 'g'), ':', '=-', 'g') . '=1.vim',
642 \ glob('Xviewdir/*'))
643 call assert_equal(1, &number)
644 call assert_match('\*:mkview\*$', getline('.'))
Bram Moolenaardd5d18e2018-08-30 14:16:06 +0200645 call assert_equal(pos, getpos('.'))
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200646 call assert_equal(linefoldclosed1, foldclosed('.'))
647
648 call delete('Xviewdir', 'rf')
649 set viewdir&
650 helpclose
651endfunc
652
653func Test_mkview_no_file_name()
654 new
655 " :mkview or :mkview {nr} should fail in a unnamed buffer.
656 call assert_fails('mkview', 'E32:')
657 call assert_fails('mkview 1', 'E32:')
658
659 " :mkview {file} should succeed in a unnamed buffer.
660 mkview Xview
661 help
662 source Xview
663 call assert_equal('', bufname('%'))
664
665 call delete('Xview')
666 %bwipe
667endfunc
Bram Moolenaar79da5632017-02-01 22:52:44 +0100668
Bram Moolenaar3482be62020-11-27 11:00:38 +0100669func Test_mkview_loadview_jumplist()
670 set viewdir=Xviewdir
671 au BufWinLeave * silent mkview
672 au BufWinEnter * silent loadview
673
674 edit Xfile1
675 call setline(1, ['a', 'bbbbbbb', 'c'])
676 normal j3l
677 call assert_equal([2, 4], getcurpos()[1:2])
678 write
679
680 edit Xfile2
681 call setline(1, ['d', 'eeeeeee', 'f'])
682 normal j5l
683 call assert_equal([2, 6], getcurpos()[1:2])
684 write
685
686 edit Xfile3
687 call setline(1, ['g', 'h', 'iiiii'])
688 normal jj3l
689 call assert_equal([3, 4], getcurpos()[1:2])
690 write
691
692 edit Xfile1
693 call assert_equal([2, 4], getcurpos()[1:2])
694 edit Xfile2
695 call assert_equal([2, 6], getcurpos()[1:2])
696 edit Xfile3
697 call assert_equal([3, 4], getcurpos()[1:2])
698
699 exe "normal \<C-O>"
700 call assert_equal('Xfile2', expand('%'))
701 call assert_equal([2, 6], getcurpos()[1:2])
702 exe "normal \<C-O>"
703 call assert_equal('Xfile1', expand('%'))
704 call assert_equal([2, 4], getcurpos()[1:2])
705
706 au! BufWinLeave
707 au! BufWinEnter
708 bwipe!
709 call delete('Xviewdir', 'rf')
710 call delete('Xfile1')
711 call delete('Xfile2')
712 call delete('Xfile3')
713 set viewdir&
714endfunc
715
Bram Moolenaar555de4e2019-01-21 23:03:49 +0100716" A clean session (one empty buffer, one window, and one tab) should not
717" set any error messages when sourced because no commands should fail.
718func Test_mksession_no_errmsg()
719 let v:errmsg = ''
720 %bwipe!
721 mksession! Xtest_mks.out
722 source Xtest_mks.out
723 call assert_equal('', v:errmsg)
724 call delete('Xtest_mks.out')
725endfunc
726
Bram Moolenaarad36a352019-01-24 13:34:42 +0100727func Test_mksession_quote_in_filename()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200728 " only Unix can handle this weird filename
729 CheckUnix
730
Bram Moolenaarad36a352019-01-24 13:34:42 +0100731 let v:errmsg = ''
732 %bwipe!
733 split another
734 split x'y\"z
735 mksession! Xtest_mks_quoted.out
736 %bwipe!
737 source Xtest_mks_quoted.out
738 call assert_true(bufexists("x'y\"z"))
739
740 %bwipe!
741 call delete('Xtest_mks_quoted.out')
742endfunc
743
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200744" Test for storing global variables in a session file
745func Test_mksession_globals()
746 set sessionoptions+=globals
747
748 " create different global variables
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200749 let g:Global_string = "Sun is shining\r\n"
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200750 let g:Global_count = 100
751 let g:Global_pi = 3.14
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200752 let g:Global_neg_float = -2.68
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200753
754 mksession! Xtest_mks.out
755
756 unlet g:Global_string
757 unlet g:Global_count
758 unlet g:Global_pi
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200759 unlet g:Global_neg_float
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200760
761 source Xtest_mks.out
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200762 call assert_equal("Sun is shining\r\n", g:Global_string)
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200763 call assert_equal(100, g:Global_count)
764 call assert_equal(3.14, g:Global_pi)
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200765 call assert_equal(-2.68, g:Global_neg_float)
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200766
767 unlet g:Global_string
768 unlet g:Global_count
769 unlet g:Global_pi
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200770 unlet g:Global_neg_float
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200771 call delete('Xtest_mks.out')
772 set sessionoptions&
773endfunc
774
775" Test for changing backslash to forward slash in filenames
776func Test_mksession_slash()
Bram Moolenaar37ac4a52019-09-28 19:29:04 +0200777 if exists('+shellslash')
778 throw 'Skipped: cannot use backslash in file name'
779 endif
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200780 enew
781 %bwipe!
782 e a\\b\\c
783 mksession! Xtest_mks1.out
784 set sessionoptions+=slash
785 mksession! Xtest_mks2.out
786
787 %bwipe!
788 source Xtest_mks1.out
789 call assert_equal('a\b\c', bufname(''))
790 %bwipe!
791 source Xtest_mks2.out
792 call assert_equal('a/b/c', bufname(''))
793
794 %bwipe!
795 call delete('Xtest_mks1.out')
796 call delete('Xtest_mks2.out')
797 set sessionoptions&
798endfunc
799
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200800" Test for changing directory to the session file directory
801func Test_mksession_sesdir()
Bram Moolenaar5bf46e92019-11-09 18:06:04 +0100802 let save_cwd = getcwd()
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200803 call mkdir('Xproj')
804 mksession! Xproj/Xtest_mks1.out
805 set sessionoptions-=curdir
806 set sessionoptions+=sesdir
807 mksession! Xproj/Xtest_mks2.out
808
809 source Xproj/Xtest_mks1.out
810 call assert_equal('testdir', fnamemodify(getcwd(), ':t'))
811 source Xproj/Xtest_mks2.out
812 call assert_equal('Xproj', fnamemodify(getcwd(), ':t'))
Bram Moolenaar5bf46e92019-11-09 18:06:04 +0100813 call chdir(save_cwd)
814 %bwipe
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200815
816 set sessionoptions&
817 call delete('Xproj', 'rf')
818endfunc
819
820" Test for storing the 'lines' and 'columns' settings
821func Test_mksession_resize()
822 mksession! Xtest_mks1.out
823 set sessionoptions+=resize
824 mksession! Xtest_mks2.out
825
826 let lines = readfile('Xtest_mks1.out')
827 let found_resize = v:false
828 for line in lines
829 if line =~ '^set lines='
830 let found_resize = v:true
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200831 break
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200832 endif
833 endfor
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200834 call assert_false(found_resize)
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200835 let lines = readfile('Xtest_mks2.out')
836 let found_resize = v:false
837 for line in lines
838 if line =~ '^set lines='
839 let found_resize = v:true
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200840 break
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200841 endif
842 endfor
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200843 call assert_true(found_resize)
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200844
845 call delete('Xtest_mks1.out')
846 call delete('Xtest_mks2.out')
847 set sessionoptions&
848endfunc
849
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200850" Test for mksession with a named scratch buffer
851func Test_mksession_scratch()
852 enew | only
853 file Xscratch
854 set buftype=nofile
855 mksession! Xtest_mks.out
856 %bwipe
857 source Xtest_mks.out
858 call assert_equal('Xscratch', bufname(''))
859 call assert_equal('nofile', &buftype)
860 %bwipe
861 call delete('Xtest_mks.out')
862endfunc
863
864" Test for mksession with fold options
865func Test_mksession_foldopt()
866 set sessionoptions-=options
867 set sessionoptions+=folds
868 new
869 setlocal foldenable
870 setlocal foldmethod=expr
871 setlocal foldmarker=<<<,>>>
872 setlocal foldignore=%
873 setlocal foldlevel=2
874 setlocal foldminlines=10
875 setlocal foldnestmax=15
876 mksession! Xtest_mks.out
877 close
878 %bwipe
879
880 source Xtest_mks.out
881 call assert_true(&foldenable)
882 call assert_equal('expr', &foldmethod)
883 call assert_equal('<<<,>>>', &foldmarker)
884 call assert_equal('%', &foldignore)
885 call assert_equal(2, &foldlevel)
886 call assert_equal(10, &foldminlines)
887 call assert_equal(15, &foldnestmax)
888
889 close
890 %bwipe
891 set sessionoptions&
892endfunc
893
894" Test for mksession with window position
895func Test_mksession_winpos()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200896 " Only applicable in GUI Vim
897 CheckGui
898
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200899 set sessionoptions+=winpos
900 mksession! Xtest_mks.out
901 let found_winpos = v:false
902 let lines = readfile('Xtest_mks.out')
903 for line in lines
904 if line =~ '^winpos '
905 let found_winpos = v:true
906 break
907 endif
908 endfor
909 call assert_true(found_winpos)
910 call delete('Xtest_mks.out')
911 set sessionoptions&
912endfunc
913
914" Test for mksession with 'compatible' option
915func Test_mksession_compatible()
916 mksession! Xtest_mks1.out
917 set compatible
918 mksession! Xtest_mks2.out
919 set nocp
920
921 let test_success = v:false
922 let lines = readfile('Xtest_mks1.out')
923 for line in lines
924 if line =~ '^if &cp | set nocp | endif'
925 let test_success = v:true
926 break
927 endif
928 endfor
929 call assert_true(test_success)
930
931 let test_success = v:false
932 let lines = readfile('Xtest_mks2.out')
933 for line in lines
934 if line =~ '^if !&cp | set cp | endif'
935 let test_success = v:true
936 break
937 endif
938 endfor
939 call assert_true(test_success)
940
941 call delete('Xtest_mks1.out')
942 call delete('Xtest_mks2.out')
943 set compatible&
944 set sessionoptions&
945endfunc
946
Bram Moolenaar87500262019-08-01 23:05:49 +0200947func s:ClearMappings()
948 mapclear
949 omapclear
950 mapclear!
951 lmapclear
952 tmapclear
953endfunc
954
955func Test_mkvimrc()
956 let entries = [
957 \ ['', 'nothing', '<Nop>'],
958 \ ['n', 'normal', 'NORMAL'],
959 \ ['v', 'visual', 'VISUAL'],
960 \ ['s', 'select', 'SELECT'],
961 \ ['x', 'visualonly', 'VISUALONLY'],
962 \ ['o', 'operator', 'OPERATOR'],
963 \ ['i', 'insert', 'INSERT'],
964 \ ['l', 'lang', 'LANG'],
965 \ ['c', 'command', 'COMMAND'],
966 \ ['t', 'terminal', 'TERMINAL'],
967 \ ]
968 for entry in entries
969 exe entry[0] .. 'map ' .. entry[1] .. ' ' .. entry[2]
970 endfor
971
972 mkvimrc Xtestvimrc
973
974 call s:ClearMappings()
975 for entry in entries
976 call assert_equal('', maparg(entry[1], entry[0]))
977 endfor
978
979 source Xtestvimrc
980
981 for entry in entries
982 call assert_equal(entry[2], maparg(entry[1], entry[0]))
983 endfor
984
985 call s:ClearMappings()
986 call delete('Xtestvimrc')
987endfunc
988
Bram Moolenaar38890832020-11-01 17:40:54 +0100989func 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&
1000endfunc
1001
Bram Moolenaar3482be62020-11-27 11:00:38 +01001002func Test_altfile()
1003 edit Xone
1004 split Xtwo
1005 edit Xtwoalt
1006 edit #
1007 wincmd w
1008 edit Xonealt
1009 edit #
1010 mksession! Xtest_altfile
1011 only
1012 bwipe Xonealt
1013 bwipe Xtwoalt
1014 bwipe!
1015 source Xtest_altfile
1016 call assert_equal('Xone', bufname())
1017 call assert_equal('Xonealt', bufname('#'))
1018 wincmd w
1019 call assert_equal('Xtwo', bufname())
1020 call assert_equal('Xtwoalt', bufname('#'))
1021 only
1022 bwipe!
Bram Moolenaarac665c22020-12-08 20:39:15 +01001023 call delete('Xtest_altfile')
Bram Moolenaar3482be62020-11-27 11:00:38 +01001024endfunc
1025
Bram Moolenaar5c504f62021-04-01 13:39:51 +02001026" Test for creating views with manual folds
1027func Test_mkview_manual_fold()
1028 call writefile(range(1,10), 'Xfile')
1029 new Xfile
1030 " create recursive folds
1031 5,6fold
1032 4,7fold
1033 mkview Xview
1034 normal zE
1035 source Xview
1036 call assert_equal([-1, 4, 4, 4, 4, -1], [foldclosed(3), foldclosed(4),
1037 \ foldclosed(5), foldclosed(6), foldclosed(7), foldclosed(8)])
1038 " open one level of fold
1039 4foldopen
1040 mkview! Xview
1041 normal zE
1042 source Xview
1043 call assert_equal([-1, -1, 5, 5, -1, -1], [foldclosed(3), foldclosed(4),
1044 \ foldclosed(5), foldclosed(6), foldclosed(7), foldclosed(8)])
1045 " open all the folds
1046 %foldopen!
1047 mkview! Xview
1048 normal zE
1049 source Xview
1050 call assert_equal([-1, -1, -1, -1, -1, -1], [foldclosed(3), foldclosed(4),
1051 \ foldclosed(5), foldclosed(6), foldclosed(7), foldclosed(8)])
1052 call delete('Xfile')
1053 call delete('Xview')
1054 bw!
1055endfunc
1056
Bram Moolenaareca626f2016-12-01 18:47:38 +01001057" vim: shiftwidth=2 sts=2 expandtab