blob: dac7c54731b4a5c45b2b83afa48a099a804c8928 [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 Moolenaar36ae89c2017-01-28 17:11:14 +0100134func Test_mksession_winheight()
135 new
Bram Moolenaar1c3c1042018-06-12 16:49:30 +0200136 set winheight=10
137 set winminheight=2
Bram Moolenaarc9b56b22017-01-29 14:14:09 +0100138 mksession! Xtest_mks.out
139 source Xtest_mks.out
Bram Moolenaar36ae89c2017-01-28 17:11:14 +0100140
Bram Moolenaarc9b56b22017-01-29 14:14:09 +0100141 call delete('Xtest_mks.out')
Bram Moolenaar36ae89c2017-01-28 17:11:14 +0100142endfunc
143
Bram Moolenaar1c3c1042018-06-12 16:49:30 +0200144func Test_mksession_large_winheight()
145 set winheight=999
146 mksession! Xtest_mks_winheight.out
147 set winheight&
148 source Xtest_mks_winheight.out
149 call delete('Xtest_mks_winheight.out')
150endfunc
151
Bram Moolenaared18f2c2019-01-24 20:30:52 +0100152func Test_mksession_rtp()
Bram Moolenaar681b6bc2019-01-24 22:23:58 +0100153 if has('win32')
154 " TODO: fix problem with backslashes
155 return
156 endif
Bram Moolenaared18f2c2019-01-24 20:30:52 +0100157 new
158 let _rtp=&rtp
159 " Make a real long (invalid) runtimepath value,
160 " that should exceed PATH_MAX (hopefully)
161 let newrtp=&rtp.',~'.repeat('/foobar', 1000)
162 let newrtp.=",".expand("$HOME")."/.vim"
163 let &rtp=newrtp
164
165 " determine expected value
166 let expected=split(&rtp, ',')
167 let expected = map(expected, '"set runtimepath+=".v:val')
168 let expected = ['set runtimepath='] + expected
169 let expected = map(expected, {v,w -> substitute(w, $HOME, "~", "g")})
170
171 mksession! Xtest_mks.out
172 let &rtp=_rtp
173 let li = filter(readfile('Xtest_mks.out'), 'v:val =~# "runtimepath"')
174 call assert_equal(expected, li)
175
176 call delete('Xtest_mks.out')
177endfunc
178
Bram Moolenaar79da5632017-02-01 22:52:44 +0100179func Test_mksession_arglist()
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200180 %argdel
Bram Moolenaar79da5632017-02-01 22:52:44 +0100181 next file1 file2 file3 file4
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200182 new
183 next | next
Bram Moolenaar79da5632017-02-01 22:52:44 +0100184 mksession! Xtest_mks.out
185 source Xtest_mks.out
186 call assert_equal(['file1', 'file2', 'file3', 'file4'], argv())
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200187 call assert_equal(2, argidx())
188 wincmd w
189 call assert_equal(0, argidx())
Bram Moolenaar79da5632017-02-01 22:52:44 +0100190
191 call delete('Xtest_mks.out')
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200192 enew | only
Bram Moolenaar79da5632017-02-01 22:52:44 +0100193 argdel *
194endfunc
195
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +0200196func Test_mksession_one_buffer_two_windows()
197 edit Xtest1
198 new Xtest2
199 split
200 mksession! Xtest_mks.out
201 let lines = readfile('Xtest_mks.out')
202 let count1 = 0
203 let count2 = 0
204 let count2buf = 0
205 for line in lines
206 if line =~ 'edit \f*Xtest1$'
207 let count1 += 1
208 endif
209 if line =~ 'edit \f\{-}Xtest2'
210 let count2 += 1
211 endif
212 if line =~ 'buffer \f\{-}Xtest2'
213 let count2buf += 1
214 endif
215 endfor
216 call assert_equal(1, count1, 'Xtest1 count')
217 call assert_equal(2, count2, 'Xtest2 count')
218 call assert_equal(2, count2buf, 'Xtest2 buffer count')
219
220 close
221 bwipe!
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +0200222 call delete('Xtest_mks.out')
223endfunc
224
Bram Moolenaar26d4b892018-07-04 22:26:28 +0200225func Test_mksession_lcd_multiple_tabs()
226 tabnew
227 tabnew
Bram Moolenaar81e2ac72018-07-04 22:44:08 +0200228 lcd .
Bram Moolenaar26d4b892018-07-04 22:26:28 +0200229 tabfirst
Bram Moolenaar81e2ac72018-07-04 22:44:08 +0200230 lcd .
Bram Moolenaar26d4b892018-07-04 22:26:28 +0200231 mksession! Xtest_mks.out
232 tabonly
233 source Xtest_mks.out
234 call assert_true(haslocaldir(), 'Tab 1 localdir')
235 tabnext 2
236 call assert_true(!haslocaldir(), 'Tab 2 localdir')
237 tabnext 3
238 call assert_true(haslocaldir(), 'Tab 3 localdir')
239 call delete('Xtest_mks.out')
240endfunc
241
Bram Moolenaar00aa0692019-04-27 20:37:57 +0200242" Test for tabpage-local directory
243func Test_mksession_tcd_multiple_tabs()
244 let save_cwd = getcwd()
245 call mkdir('Xtopdir')
246 cd Xtopdir
247 call mkdir('Xtabdir1')
248 call mkdir('Xtabdir2')
249 call mkdir('Xtabdir3')
250 call mkdir('Xwindir1')
251 call mkdir('Xwindir2')
252 call mkdir('Xwindir3')
253 tcd Xtabdir1
254 botright new
255 wincmd t
256 lcd ../Xwindir1
257 tabnew
258 tcd ../Xtabdir2
259 botright new
260 lcd ../Xwindir2
261 tabnew
262 tcd ../Xtabdir3
263 botright new
264 lcd ../Xwindir3
265 tabfirst
266 1wincmd w
267 mksession! Xtest_mks.out
268 only | tabonly
269 source Xtest_mks.out
270 call assert_equal('Xtabdir1', fnamemodify(getcwd(-1, 1), ':t'))
271 call assert_equal('Xwindir1', fnamemodify(getcwd(1, 1), ':t'))
272 call assert_equal('Xtabdir1', fnamemodify(getcwd(2, 1), ':t'))
273 call assert_equal('Xtabdir2', fnamemodify(getcwd(-1, 2), ':t'))
274 call assert_equal('Xtabdir2', fnamemodify(getcwd(1, 2), ':t'))
275 call assert_equal('Xwindir2', fnamemodify(getcwd(2, 2), ':t'))
276 call assert_equal('Xtabdir3', fnamemodify(getcwd(-1, 3), ':t'))
277 call assert_equal('Xtabdir3', fnamemodify(getcwd(1, 3), ':t'))
278 call assert_equal('Xwindir3', fnamemodify(getcwd(2, 3), ':t'))
Bram Moolenaar5bf46e92019-11-09 18:06:04 +0100279 %bwipe
280 call chdir(save_cwd)
Bram Moolenaar00aa0692019-04-27 20:37:57 +0200281 call delete("Xtopdir", "rf")
282endfunc
283
Bram Moolenaar26d4b892018-07-04 22:26:28 +0200284func Test_mksession_blank_tabs()
285 tabnew
286 tabnew
287 tabnew
288 tabnext 3
289 mksession! Xtest_mks.out
290 tabnew
291 tabnew
292 tabnext 2
293 source Xtest_mks.out
294 call assert_equal(4, tabpagenr('$'), 'session restore should restore number of tabs')
295 call assert_equal(3, tabpagenr(), 'session restore should restore the active tab')
296 call delete('Xtest_mks.out')
297endfunc
298
Bram Moolenaard39e2752019-01-26 20:07:38 +0100299func Test_mksession_buffer_count()
300 set hidden
301
302 " Edit exactly three files in the current session.
303 %bwipe!
304 e Xfoo | tabe Xbar | tabe Xbaz
305 tabdo write
306 mksession! Xtest_mks.out
307
308 " Verify that loading the session does not create additional buffers.
309 %bwipe!
310 source Xtest_mks.out
311 call assert_equal(3, len(getbufinfo()))
312
313 " Clean up.
314 call delete('Xfoo')
315 call delete('Xbar')
316 call delete('Xbaz')
317 call delete('Xtest_mks.out')
318 %bwipe!
319 set hidden&
320endfunc
321
Bram Moolenaare3c74d22019-01-12 16:29:30 +0100322if has('extra_search')
323
324func Test_mksession_hlsearch()
325 set hlsearch
326 mksession! Xtest_mks.out
327 nohlsearch
328 source Xtest_mks.out
329 call assert_equal(1, v:hlsearch, 'session should restore search highlighting state')
330 nohlsearch
331 mksession! Xtest_mks.out
332 source Xtest_mks.out
333 call assert_equal(0, v:hlsearch, 'session should restore search highlighting state')
334 call delete('Xtest_mks.out')
335endfunc
336
337endif
338
339
Bram Moolenaar26d4b892018-07-04 22:26:28 +0200340func Test_mksession_blank_windows()
341 split
342 split
343 split
344 3 wincmd w
345 mksession! Xtest_mks.out
346 split
347 split
348 2 wincmd w
349 source Xtest_mks.out
350 call assert_equal(4, winnr('$'), 'session restore should restore number of windows')
351 call assert_equal(3, winnr(), 'session restore should restore the active window')
352 call delete('Xtest_mks.out')
353endfunc
354
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100355if has('terminal')
356
357func Test_mksession_terminal_shell()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100358 CheckFeature quickfix
359
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100360 terminal
361 mksession! Xtest_mks.out
362 let lines = readfile('Xtest_mks.out')
363 let term_cmd = ''
364 for line in lines
365 if line =~ '^terminal'
366 let term_cmd = line
367 elseif line =~ 'badd.*' . &shell
368 call assert_report('unexpected shell line: ' . line)
369 endif
370 endfor
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100371 call assert_match('terminal ++curwin ++cols=\d\+ ++rows=\d\+\s*.*$', term_cmd)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100372
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200373 call StopShellInTerminal(bufnr('%'))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100374 call delete('Xtest_mks.out')
375endfunc
376
377func Test_mksession_terminal_no_restore_cmdarg()
378 terminal ++norestore
379 mksession! Xtest_mks.out
380 let lines = readfile('Xtest_mks.out')
381 let term_cmd = ''
382 for line in lines
383 if line =~ '^terminal'
384 call assert_report('session must not restore teminal')
385 endif
386 endfor
387
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200388 call StopShellInTerminal(bufnr('%'))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100389 call delete('Xtest_mks.out')
390endfunc
391
392func Test_mksession_terminal_no_restore_funcarg()
393 call term_start(&shell, {'norestore': 1})
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
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200403 call StopShellInTerminal(bufnr('%'))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100404 call delete('Xtest_mks.out')
405endfunc
406
407func Test_mksession_terminal_no_restore_func()
408 terminal
409 call term_setrestore(bufnr('%'), 'NONE')
410 mksession! Xtest_mks.out
411 let lines = readfile('Xtest_mks.out')
412 let term_cmd = ''
413 for line in lines
414 if line =~ '^terminal'
415 call assert_report('session must not restore teminal')
416 endif
417 endfor
418
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200419 call StopShellInTerminal(bufnr('%'))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100420 call delete('Xtest_mks.out')
421endfunc
422
423func Test_mksession_terminal_no_ssop()
424 terminal
425 set sessionoptions-=terminal
426 mksession! Xtest_mks.out
427 let lines = readfile('Xtest_mks.out')
428 let term_cmd = ''
429 for line in lines
430 if line =~ '^terminal'
431 call assert_report('session must not restore teminal')
432 endif
433 endfor
434
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200435 call StopShellInTerminal(bufnr('%'))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100436 call delete('Xtest_mks.out')
437 set sessionoptions&
438endfunc
439
440func Test_mksession_terminal_restore_other()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100441 CheckFeature quickfix
442
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100443 terminal
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200444 eval bufnr('%')->term_setrestore('other')
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100445 mksession! Xtest_mks.out
446 let lines = readfile('Xtest_mks.out')
447 let term_cmd = ''
448 for line in lines
449 if line =~ '^terminal'
450 let term_cmd = line
451 endif
452 endfor
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100453 call assert_match('terminal ++curwin ++cols=\d\+ ++rows=\d\+.*other', term_cmd)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100454
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200455 call StopShellInTerminal(bufnr('%'))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100456 call delete('Xtest_mks.out')
457endfunc
458
459endif " has('terminal')
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +0200460
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200461" Test :mkview with a file argument.
462func Test_mkview_file()
463 " Create a view with line number and a fold.
464 help :mkview
465 set number
Bram Moolenaar2bf4fe02018-08-30 14:04:25 +0200466 norm! V}zf0
Bram Moolenaardd5d18e2018-08-30 14:16:06 +0200467 let pos = getpos('.')
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200468 let linefoldclosed1 = foldclosed('.')
469 mkview! Xview
470 set nonumber
471 norm! zrj
472 " We can close the help window, as mkview with a file name should
473 " generate a command to edit the file.
474 helpclose
475
476 source Xview
477 call assert_equal(1, &number)
478 call assert_match('\*:mkview\*$', getline('.'))
Bram Moolenaardd5d18e2018-08-30 14:16:06 +0200479 call assert_equal(pos, getpos('.'))
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200480 call assert_equal(linefoldclosed1, foldclosed('.'))
481
482 " Creating a view again with the same file name should fail (file
483 " already exists). But with a !, the previous view should be
484 " overwritten without error.
485 help :loadview
486 call assert_fails('mkview Xview', 'E189:')
487 call assert_match('\*:loadview\*$', getline('.'))
488 mkview! Xview
489 call assert_match('\*:loadview\*$', getline('.'))
490
491 call delete('Xview')
492 bwipe
493endfunc
494
495" Test :mkview and :loadview with a custom 'viewdir'.
496func Test_mkview_loadview_with_viewdir()
497 set viewdir=Xviewdir
498
499 help :mkview
500 set number
501 norm! V}zf
Bram Moolenaardd5d18e2018-08-30 14:16:06 +0200502 let pos = getpos('.')
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200503 let linefoldclosed1 = foldclosed('.')
504 mkview 1
505 set nonumber
506 norm! zrj
507
508 loadview 1
509
510 " The directory Xviewdir/ should have been created and the view
511 " should be stored in that directory.
512 call assert_equal('Xviewdir/' .
513 \ substitute(
514 \ substitute(
515 \ expand('%:p'), '/', '=+', 'g'), ':', '=-', 'g') . '=1.vim',
516 \ glob('Xviewdir/*'))
517 call assert_equal(1, &number)
518 call assert_match('\*:mkview\*$', getline('.'))
Bram Moolenaardd5d18e2018-08-30 14:16:06 +0200519 call assert_equal(pos, getpos('.'))
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200520 call assert_equal(linefoldclosed1, foldclosed('.'))
521
522 call delete('Xviewdir', 'rf')
523 set viewdir&
524 helpclose
525endfunc
526
527func Test_mkview_no_file_name()
528 new
529 " :mkview or :mkview {nr} should fail in a unnamed buffer.
530 call assert_fails('mkview', 'E32:')
531 call assert_fails('mkview 1', 'E32:')
532
533 " :mkview {file} should succeed in a unnamed buffer.
534 mkview Xview
535 help
536 source Xview
537 call assert_equal('', bufname('%'))
538
539 call delete('Xview')
540 %bwipe
541endfunc
Bram Moolenaar79da5632017-02-01 22:52:44 +0100542
Bram Moolenaar555de4e2019-01-21 23:03:49 +0100543" A clean session (one empty buffer, one window, and one tab) should not
544" set any error messages when sourced because no commands should fail.
545func Test_mksession_no_errmsg()
546 let v:errmsg = ''
547 %bwipe!
548 mksession! Xtest_mks.out
549 source Xtest_mks.out
550 call assert_equal('', v:errmsg)
551 call delete('Xtest_mks.out')
552endfunc
553
Bram Moolenaarad36a352019-01-24 13:34:42 +0100554func Test_mksession_quote_in_filename()
Bram Moolenaar9e79ccb2019-01-24 15:57:30 +0100555 if !has('unix')
556 " only Unix can handle this weird filename
557 return
558 endif
Bram Moolenaarad36a352019-01-24 13:34:42 +0100559 let v:errmsg = ''
560 %bwipe!
561 split another
562 split x'y\"z
563 mksession! Xtest_mks_quoted.out
564 %bwipe!
565 source Xtest_mks_quoted.out
566 call assert_true(bufexists("x'y\"z"))
567
568 %bwipe!
569 call delete('Xtest_mks_quoted.out')
570endfunc
571
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200572" Test for storing global variables in a session file
573func Test_mksession_globals()
574 set sessionoptions+=globals
575
576 " create different global variables
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200577 let g:Global_string = "Sun is shining\r\n"
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200578 let g:Global_count = 100
579 let g:Global_pi = 3.14
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200580 let g:Global_neg_float = -2.68
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200581
582 mksession! Xtest_mks.out
583
584 unlet g:Global_string
585 unlet g:Global_count
586 unlet g:Global_pi
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200587 unlet g:Global_neg_float
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200588
589 source Xtest_mks.out
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200590 call assert_equal("Sun is shining\r\n", g:Global_string)
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200591 call assert_equal(100, g:Global_count)
592 call assert_equal(3.14, g:Global_pi)
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200593 call assert_equal(-2.68, g:Global_neg_float)
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200594
595 unlet g:Global_string
596 unlet g:Global_count
597 unlet g:Global_pi
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200598 unlet g:Global_neg_float
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200599 call delete('Xtest_mks.out')
600 set sessionoptions&
601endfunc
602
603" Test for changing backslash to forward slash in filenames
604func Test_mksession_slash()
Bram Moolenaar37ac4a52019-09-28 19:29:04 +0200605 if exists('+shellslash')
606 throw 'Skipped: cannot use backslash in file name'
607 endif
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200608 enew
609 %bwipe!
610 e a\\b\\c
611 mksession! Xtest_mks1.out
612 set sessionoptions+=slash
613 mksession! Xtest_mks2.out
614
615 %bwipe!
616 source Xtest_mks1.out
617 call assert_equal('a\b\c', bufname(''))
618 %bwipe!
619 source Xtest_mks2.out
620 call assert_equal('a/b/c', bufname(''))
621
622 %bwipe!
623 call delete('Xtest_mks1.out')
624 call delete('Xtest_mks2.out')
625 set sessionoptions&
626endfunc
627
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200628" Test for changing directory to the session file directory
629func Test_mksession_sesdir()
Bram Moolenaar5bf46e92019-11-09 18:06:04 +0100630 let save_cwd = getcwd()
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200631 call mkdir('Xproj')
632 mksession! Xproj/Xtest_mks1.out
633 set sessionoptions-=curdir
634 set sessionoptions+=sesdir
635 mksession! Xproj/Xtest_mks2.out
636
637 source Xproj/Xtest_mks1.out
638 call assert_equal('testdir', fnamemodify(getcwd(), ':t'))
639 source Xproj/Xtest_mks2.out
640 call assert_equal('Xproj', fnamemodify(getcwd(), ':t'))
Bram Moolenaar5bf46e92019-11-09 18:06:04 +0100641 call chdir(save_cwd)
642 %bwipe
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200643
644 set sessionoptions&
645 call delete('Xproj', 'rf')
646endfunc
647
648" Test for storing the 'lines' and 'columns' settings
649func Test_mksession_resize()
650 mksession! Xtest_mks1.out
651 set sessionoptions+=resize
652 mksession! Xtest_mks2.out
653
654 let lines = readfile('Xtest_mks1.out')
655 let found_resize = v:false
656 for line in lines
657 if line =~ '^set lines='
658 let found_resize = v:true
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200659 break
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200660 endif
661 endfor
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200662 call assert_false(found_resize)
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200663 let lines = readfile('Xtest_mks2.out')
664 let found_resize = v:false
665 for line in lines
666 if line =~ '^set lines='
667 let found_resize = v:true
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200668 break
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200669 endif
670 endfor
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200671 call assert_true(found_resize)
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200672
673 call delete('Xtest_mks1.out')
674 call delete('Xtest_mks2.out')
675 set sessionoptions&
676endfunc
677
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200678" Test for mksession with a named scratch buffer
679func Test_mksession_scratch()
680 enew | only
681 file Xscratch
682 set buftype=nofile
683 mksession! Xtest_mks.out
684 %bwipe
685 source Xtest_mks.out
686 call assert_equal('Xscratch', bufname(''))
687 call assert_equal('nofile', &buftype)
688 %bwipe
689 call delete('Xtest_mks.out')
690endfunc
691
692" Test for mksession with fold options
693func Test_mksession_foldopt()
694 set sessionoptions-=options
695 set sessionoptions+=folds
696 new
697 setlocal foldenable
698 setlocal foldmethod=expr
699 setlocal foldmarker=<<<,>>>
700 setlocal foldignore=%
701 setlocal foldlevel=2
702 setlocal foldminlines=10
703 setlocal foldnestmax=15
704 mksession! Xtest_mks.out
705 close
706 %bwipe
707
708 source Xtest_mks.out
709 call assert_true(&foldenable)
710 call assert_equal('expr', &foldmethod)
711 call assert_equal('<<<,>>>', &foldmarker)
712 call assert_equal('%', &foldignore)
713 call assert_equal(2, &foldlevel)
714 call assert_equal(10, &foldminlines)
715 call assert_equal(15, &foldnestmax)
716
717 close
718 %bwipe
719 set sessionoptions&
720endfunc
721
722" Test for mksession with window position
723func Test_mksession_winpos()
724 if !has('gui_running')
725 " Only applicable in GUI Vim
726 return
727 endif
728 set sessionoptions+=winpos
729 mksession! Xtest_mks.out
730 let found_winpos = v:false
731 let lines = readfile('Xtest_mks.out')
732 for line in lines
733 if line =~ '^winpos '
734 let found_winpos = v:true
735 break
736 endif
737 endfor
738 call assert_true(found_winpos)
739 call delete('Xtest_mks.out')
740 set sessionoptions&
741endfunc
742
743" Test for mksession with 'compatible' option
744func Test_mksession_compatible()
745 mksession! Xtest_mks1.out
746 set compatible
747 mksession! Xtest_mks2.out
748 set nocp
749
750 let test_success = v:false
751 let lines = readfile('Xtest_mks1.out')
752 for line in lines
753 if line =~ '^if &cp | set nocp | endif'
754 let test_success = v:true
755 break
756 endif
757 endfor
758 call assert_true(test_success)
759
760 let test_success = v:false
761 let lines = readfile('Xtest_mks2.out')
762 for line in lines
763 if line =~ '^if !&cp | set cp | endif'
764 let test_success = v:true
765 break
766 endif
767 endfor
768 call assert_true(test_success)
769
770 call delete('Xtest_mks1.out')
771 call delete('Xtest_mks2.out')
772 set compatible&
773 set sessionoptions&
774endfunc
775
Bram Moolenaar87500262019-08-01 23:05:49 +0200776func s:ClearMappings()
777 mapclear
778 omapclear
779 mapclear!
780 lmapclear
781 tmapclear
782endfunc
783
784func Test_mkvimrc()
785 let entries = [
786 \ ['', 'nothing', '<Nop>'],
787 \ ['n', 'normal', 'NORMAL'],
788 \ ['v', 'visual', 'VISUAL'],
789 \ ['s', 'select', 'SELECT'],
790 \ ['x', 'visualonly', 'VISUALONLY'],
791 \ ['o', 'operator', 'OPERATOR'],
792 \ ['i', 'insert', 'INSERT'],
793 \ ['l', 'lang', 'LANG'],
794 \ ['c', 'command', 'COMMAND'],
795 \ ['t', 'terminal', 'TERMINAL'],
796 \ ]
797 for entry in entries
798 exe entry[0] .. 'map ' .. entry[1] .. ' ' .. entry[2]
799 endfor
800
801 mkvimrc Xtestvimrc
802
803 call s:ClearMappings()
804 for entry in entries
805 call assert_equal('', maparg(entry[1], entry[0]))
806 endfor
807
808 source Xtestvimrc
809
810 for entry in entries
811 call assert_equal(entry[2], maparg(entry[1], entry[0]))
812 endfor
813
814 call s:ClearMappings()
815 call delete('Xtestvimrc')
816endfunc
817
Bram Moolenaareca626f2016-12-01 18:47:38 +0100818" vim: shiftwidth=2 sts=2 expandtab