blob: 3fc5c435ca4a40935568bc97a16a5b6d3b49ad2d [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 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()
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200157 %argdel
Bram Moolenaar79da5632017-02-01 22:52:44 +0100158 next file1 file2 file3 file4
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200159 new
160 next | next
Bram Moolenaar79da5632017-02-01 22:52:44 +0100161 mksession! Xtest_mks.out
162 source Xtest_mks.out
163 call assert_equal(['file1', 'file2', 'file3', 'file4'], argv())
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200164 call assert_equal(2, argidx())
165 wincmd w
166 call assert_equal(0, argidx())
Bram Moolenaar79da5632017-02-01 22:52:44 +0100167
168 call delete('Xtest_mks.out')
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200169 enew | only
Bram Moolenaar79da5632017-02-01 22:52:44 +0100170 argdel *
171endfunc
172
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +0200173func Test_mksession_one_buffer_two_windows()
174 edit Xtest1
175 new Xtest2
176 split
177 mksession! Xtest_mks.out
178 let lines = readfile('Xtest_mks.out')
179 let count1 = 0
180 let count2 = 0
181 let count2buf = 0
182 for line in lines
183 if line =~ 'edit \f*Xtest1$'
184 let count1 += 1
185 endif
186 if line =~ 'edit \f\{-}Xtest2'
187 let count2 += 1
188 endif
189 if line =~ 'buffer \f\{-}Xtest2'
190 let count2buf += 1
191 endif
192 endfor
193 call assert_equal(1, count1, 'Xtest1 count')
194 call assert_equal(2, count2, 'Xtest2 count')
195 call assert_equal(2, count2buf, 'Xtest2 buffer count')
196
197 close
198 bwipe!
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +0200199 call delete('Xtest_mks.out')
200endfunc
201
Bram Moolenaar26d4b892018-07-04 22:26:28 +0200202func Test_mksession_lcd_multiple_tabs()
203 tabnew
204 tabnew
Bram Moolenaar81e2ac72018-07-04 22:44:08 +0200205 lcd .
Bram Moolenaar26d4b892018-07-04 22:26:28 +0200206 tabfirst
Bram Moolenaar81e2ac72018-07-04 22:44:08 +0200207 lcd .
Bram Moolenaar26d4b892018-07-04 22:26:28 +0200208 mksession! Xtest_mks.out
209 tabonly
210 source Xtest_mks.out
211 call assert_true(haslocaldir(), 'Tab 1 localdir')
212 tabnext 2
213 call assert_true(!haslocaldir(), 'Tab 2 localdir')
214 tabnext 3
215 call assert_true(haslocaldir(), 'Tab 3 localdir')
216 call delete('Xtest_mks.out')
217endfunc
218
Bram Moolenaar00aa0692019-04-27 20:37:57 +0200219" Test for tabpage-local directory
220func Test_mksession_tcd_multiple_tabs()
221 let save_cwd = getcwd()
222 call mkdir('Xtopdir')
223 cd Xtopdir
224 call mkdir('Xtabdir1')
225 call mkdir('Xtabdir2')
226 call mkdir('Xtabdir3')
227 call mkdir('Xwindir1')
228 call mkdir('Xwindir2')
229 call mkdir('Xwindir3')
230 tcd Xtabdir1
231 botright new
232 wincmd t
233 lcd ../Xwindir1
234 tabnew
235 tcd ../Xtabdir2
236 botright new
237 lcd ../Xwindir2
238 tabnew
239 tcd ../Xtabdir3
240 botright new
241 lcd ../Xwindir3
242 tabfirst
243 1wincmd w
244 mksession! Xtest_mks.out
245 only | tabonly
246 source Xtest_mks.out
247 call assert_equal('Xtabdir1', fnamemodify(getcwd(-1, 1), ':t'))
248 call assert_equal('Xwindir1', fnamemodify(getcwd(1, 1), ':t'))
249 call assert_equal('Xtabdir1', fnamemodify(getcwd(2, 1), ':t'))
250 call assert_equal('Xtabdir2', fnamemodify(getcwd(-1, 2), ':t'))
251 call assert_equal('Xtabdir2', fnamemodify(getcwd(1, 2), ':t'))
252 call assert_equal('Xwindir2', fnamemodify(getcwd(2, 2), ':t'))
253 call assert_equal('Xtabdir3', fnamemodify(getcwd(-1, 3), ':t'))
254 call assert_equal('Xtabdir3', fnamemodify(getcwd(1, 3), ':t'))
255 call assert_equal('Xwindir3', fnamemodify(getcwd(2, 3), ':t'))
256 only | tabonly
257 exe 'cd ' . save_cwd
258 call delete("Xtopdir", "rf")
259endfunc
260
Bram Moolenaar26d4b892018-07-04 22:26:28 +0200261func Test_mksession_blank_tabs()
262 tabnew
263 tabnew
264 tabnew
265 tabnext 3
266 mksession! Xtest_mks.out
267 tabnew
268 tabnew
269 tabnext 2
270 source Xtest_mks.out
271 call assert_equal(4, tabpagenr('$'), 'session restore should restore number of tabs')
272 call assert_equal(3, tabpagenr(), 'session restore should restore the active tab')
273 call delete('Xtest_mks.out')
274endfunc
275
Bram Moolenaard39e2752019-01-26 20:07:38 +0100276func Test_mksession_buffer_count()
277 set hidden
278
279 " Edit exactly three files in the current session.
280 %bwipe!
281 e Xfoo | tabe Xbar | tabe Xbaz
282 tabdo write
283 mksession! Xtest_mks.out
284
285 " Verify that loading the session does not create additional buffers.
286 %bwipe!
287 source Xtest_mks.out
288 call assert_equal(3, len(getbufinfo()))
289
290 " Clean up.
291 call delete('Xfoo')
292 call delete('Xbar')
293 call delete('Xbaz')
294 call delete('Xtest_mks.out')
295 %bwipe!
296 set hidden&
297endfunc
298
Bram Moolenaare3c74d22019-01-12 16:29:30 +0100299if has('extra_search')
300
301func Test_mksession_hlsearch()
302 set hlsearch
303 mksession! Xtest_mks.out
304 nohlsearch
305 source Xtest_mks.out
306 call assert_equal(1, v:hlsearch, 'session should restore search highlighting state')
307 nohlsearch
308 mksession! Xtest_mks.out
309 source Xtest_mks.out
310 call assert_equal(0, v:hlsearch, 'session should restore search highlighting state')
311 call delete('Xtest_mks.out')
312endfunc
313
314endif
315
316
Bram Moolenaar26d4b892018-07-04 22:26:28 +0200317func Test_mksession_blank_windows()
318 split
319 split
320 split
321 3 wincmd w
322 mksession! Xtest_mks.out
323 split
324 split
325 2 wincmd w
326 source Xtest_mks.out
327 call assert_equal(4, winnr('$'), 'session restore should restore number of windows')
328 call assert_equal(3, winnr(), 'session restore should restore the active window')
329 call delete('Xtest_mks.out')
330endfunc
331
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100332if has('terminal')
333
334func Test_mksession_terminal_shell()
335 terminal
336 mksession! Xtest_mks.out
337 let lines = readfile('Xtest_mks.out')
338 let term_cmd = ''
339 for line in lines
340 if line =~ '^terminal'
341 let term_cmd = line
342 elseif line =~ 'badd.*' . &shell
343 call assert_report('unexpected shell line: ' . line)
344 endif
345 endfor
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100346 call assert_match('terminal ++curwin ++cols=\d\+ ++rows=\d\+\s*.*$', term_cmd)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100347
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200348 call StopShellInTerminal(bufnr('%'))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100349 call delete('Xtest_mks.out')
350endfunc
351
352func Test_mksession_terminal_no_restore_cmdarg()
353 terminal ++norestore
354 mksession! Xtest_mks.out
355 let lines = readfile('Xtest_mks.out')
356 let term_cmd = ''
357 for line in lines
358 if line =~ '^terminal'
359 call assert_report('session must not restore teminal')
360 endif
361 endfor
362
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200363 call StopShellInTerminal(bufnr('%'))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100364 call delete('Xtest_mks.out')
365endfunc
366
367func Test_mksession_terminal_no_restore_funcarg()
368 call term_start(&shell, {'norestore': 1})
369 mksession! Xtest_mks.out
370 let lines = readfile('Xtest_mks.out')
371 let term_cmd = ''
372 for line in lines
373 if line =~ '^terminal'
374 call assert_report('session must not restore teminal')
375 endif
376 endfor
377
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200378 call StopShellInTerminal(bufnr('%'))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100379 call delete('Xtest_mks.out')
380endfunc
381
382func Test_mksession_terminal_no_restore_func()
383 terminal
384 call term_setrestore(bufnr('%'), 'NONE')
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 call assert_report('session must not restore teminal')
391 endif
392 endfor
393
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200394 call StopShellInTerminal(bufnr('%'))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100395 call delete('Xtest_mks.out')
396endfunc
397
398func Test_mksession_terminal_no_ssop()
399 terminal
400 set sessionoptions-=terminal
401 mksession! Xtest_mks.out
402 let lines = readfile('Xtest_mks.out')
403 let term_cmd = ''
404 for line in lines
405 if line =~ '^terminal'
406 call assert_report('session must not restore teminal')
407 endif
408 endfor
409
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200410 call StopShellInTerminal(bufnr('%'))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100411 call delete('Xtest_mks.out')
412 set sessionoptions&
413endfunc
414
415func Test_mksession_terminal_restore_other()
416 terminal
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200417 eval bufnr('%')->term_setrestore('other')
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100418 mksession! Xtest_mks.out
419 let lines = readfile('Xtest_mks.out')
420 let term_cmd = ''
421 for line in lines
422 if line =~ '^terminal'
423 let term_cmd = line
424 endif
425 endfor
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100426 call assert_match('terminal ++curwin ++cols=\d\+ ++rows=\d\+.*other', term_cmd)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100427
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200428 call StopShellInTerminal(bufnr('%'))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100429 call delete('Xtest_mks.out')
430endfunc
431
432endif " has('terminal')
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +0200433
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200434" Test :mkview with a file argument.
435func Test_mkview_file()
436 " Create a view with line number and a fold.
437 help :mkview
438 set number
Bram Moolenaar2bf4fe02018-08-30 14:04:25 +0200439 norm! V}zf0
Bram Moolenaardd5d18e2018-08-30 14:16:06 +0200440 let pos = getpos('.')
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200441 let linefoldclosed1 = foldclosed('.')
442 mkview! Xview
443 set nonumber
444 norm! zrj
445 " We can close the help window, as mkview with a file name should
446 " generate a command to edit the file.
447 helpclose
448
449 source Xview
450 call assert_equal(1, &number)
451 call assert_match('\*:mkview\*$', getline('.'))
Bram Moolenaardd5d18e2018-08-30 14:16:06 +0200452 call assert_equal(pos, getpos('.'))
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200453 call assert_equal(linefoldclosed1, foldclosed('.'))
454
455 " Creating a view again with the same file name should fail (file
456 " already exists). But with a !, the previous view should be
457 " overwritten without error.
458 help :loadview
459 call assert_fails('mkview Xview', 'E189:')
460 call assert_match('\*:loadview\*$', getline('.'))
461 mkview! Xview
462 call assert_match('\*:loadview\*$', getline('.'))
463
464 call delete('Xview')
465 bwipe
466endfunc
467
468" Test :mkview and :loadview with a custom 'viewdir'.
469func Test_mkview_loadview_with_viewdir()
470 set viewdir=Xviewdir
471
472 help :mkview
473 set number
474 norm! V}zf
Bram Moolenaardd5d18e2018-08-30 14:16:06 +0200475 let pos = getpos('.')
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200476 let linefoldclosed1 = foldclosed('.')
477 mkview 1
478 set nonumber
479 norm! zrj
480
481 loadview 1
482
483 " The directory Xviewdir/ should have been created and the view
484 " should be stored in that directory.
485 call assert_equal('Xviewdir/' .
486 \ substitute(
487 \ substitute(
488 \ expand('%:p'), '/', '=+', 'g'), ':', '=-', 'g') . '=1.vim',
489 \ glob('Xviewdir/*'))
490 call assert_equal(1, &number)
491 call assert_match('\*:mkview\*$', getline('.'))
Bram Moolenaardd5d18e2018-08-30 14:16:06 +0200492 call assert_equal(pos, getpos('.'))
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200493 call assert_equal(linefoldclosed1, foldclosed('.'))
494
495 call delete('Xviewdir', 'rf')
496 set viewdir&
497 helpclose
498endfunc
499
500func Test_mkview_no_file_name()
501 new
502 " :mkview or :mkview {nr} should fail in a unnamed buffer.
503 call assert_fails('mkview', 'E32:')
504 call assert_fails('mkview 1', 'E32:')
505
506 " :mkview {file} should succeed in a unnamed buffer.
507 mkview Xview
508 help
509 source Xview
510 call assert_equal('', bufname('%'))
511
512 call delete('Xview')
513 %bwipe
514endfunc
Bram Moolenaar79da5632017-02-01 22:52:44 +0100515
Bram Moolenaar555de4e2019-01-21 23:03:49 +0100516" A clean session (one empty buffer, one window, and one tab) should not
517" set any error messages when sourced because no commands should fail.
518func Test_mksession_no_errmsg()
519 let v:errmsg = ''
520 %bwipe!
521 mksession! Xtest_mks.out
522 source Xtest_mks.out
523 call assert_equal('', v:errmsg)
524 call delete('Xtest_mks.out')
525endfunc
526
Bram Moolenaarad36a352019-01-24 13:34:42 +0100527func Test_mksession_quote_in_filename()
Bram Moolenaar9e79ccb2019-01-24 15:57:30 +0100528 if !has('unix')
529 " only Unix can handle this weird filename
530 return
531 endif
Bram Moolenaarad36a352019-01-24 13:34:42 +0100532 let v:errmsg = ''
533 %bwipe!
534 split another
535 split x'y\"z
536 mksession! Xtest_mks_quoted.out
537 %bwipe!
538 source Xtest_mks_quoted.out
539 call assert_true(bufexists("x'y\"z"))
540
541 %bwipe!
542 call delete('Xtest_mks_quoted.out')
543endfunc
544
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200545" Test for storing global variables in a session file
546func Test_mksession_globals()
547 set sessionoptions+=globals
548
549 " create different global variables
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200550 let g:Global_string = "Sun is shining\r\n"
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200551 let g:Global_count = 100
552 let g:Global_pi = 3.14
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200553 let g:Global_neg_float = -2.68
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200554
555 mksession! Xtest_mks.out
556
557 unlet g:Global_string
558 unlet g:Global_count
559 unlet g:Global_pi
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200560 unlet g:Global_neg_float
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200561
562 source Xtest_mks.out
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200563 call assert_equal("Sun is shining\r\n", g:Global_string)
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200564 call assert_equal(100, g:Global_count)
565 call assert_equal(3.14, g:Global_pi)
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200566 call assert_equal(-2.68, g:Global_neg_float)
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200567
568 unlet g:Global_string
569 unlet g:Global_count
570 unlet g:Global_pi
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200571 unlet g:Global_neg_float
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200572 call delete('Xtest_mks.out')
573 set sessionoptions&
574endfunc
575
576" Test for changing backslash to forward slash in filenames
577func Test_mksession_slash()
Bram Moolenaar37ac4a52019-09-28 19:29:04 +0200578 if exists('+shellslash')
579 throw 'Skipped: cannot use backslash in file name'
580 endif
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200581 enew
582 %bwipe!
583 e a\\b\\c
584 mksession! Xtest_mks1.out
585 set sessionoptions+=slash
586 mksession! Xtest_mks2.out
587
588 %bwipe!
589 source Xtest_mks1.out
590 call assert_equal('a\b\c', bufname(''))
591 %bwipe!
592 source Xtest_mks2.out
593 call assert_equal('a/b/c', bufname(''))
594
595 %bwipe!
596 call delete('Xtest_mks1.out')
597 call delete('Xtest_mks2.out')
598 set sessionoptions&
599endfunc
600
601" Test for storing global and local argument list in a session file
602func Test_mkseesion_arglocal()
603 enew | only
604 n a b c
605 new
606 arglocal
607 mksession! Xtest_mks.out
608
609 %bwipe!
610 %argdelete
611 argglobal
612 source Xtest_mks.out
613 call assert_equal(2, winnr('$'))
614 call assert_equal(2, arglistid(1))
615 call assert_equal(0, arglistid(2))
616
617 %bwipe!
618 %argdelete
619 argglobal
620 call delete('Xtest_mks.out')
621endfunc
622
623" Test for changing directory to the session file directory
624func Test_mksession_sesdir()
625 call mkdir('Xproj')
626 mksession! Xproj/Xtest_mks1.out
627 set sessionoptions-=curdir
628 set sessionoptions+=sesdir
629 mksession! Xproj/Xtest_mks2.out
630
631 source Xproj/Xtest_mks1.out
632 call assert_equal('testdir', fnamemodify(getcwd(), ':t'))
633 source Xproj/Xtest_mks2.out
634 call assert_equal('Xproj', fnamemodify(getcwd(), ':t'))
635 cd ..
636
637 set sessionoptions&
638 call delete('Xproj', 'rf')
639endfunc
640
641" Test for storing the 'lines' and 'columns' settings
642func Test_mksession_resize()
643 mksession! Xtest_mks1.out
644 set sessionoptions+=resize
645 mksession! Xtest_mks2.out
646
647 let lines = readfile('Xtest_mks1.out')
648 let found_resize = v:false
649 for line in lines
650 if line =~ '^set lines='
651 let found_resize = v:true
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200652 break
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200653 endif
654 endfor
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200655 call assert_false(found_resize)
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200656 let lines = readfile('Xtest_mks2.out')
657 let found_resize = v:false
658 for line in lines
659 if line =~ '^set lines='
660 let found_resize = v:true
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200661 break
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200662 endif
663 endfor
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200664 call assert_true(found_resize)
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200665
666 call delete('Xtest_mks1.out')
667 call delete('Xtest_mks2.out')
668 set sessionoptions&
669endfunc
670
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200671" Test for mksession with a named scratch buffer
672func Test_mksession_scratch()
673 enew | only
674 file Xscratch
675 set buftype=nofile
676 mksession! Xtest_mks.out
677 %bwipe
678 source Xtest_mks.out
679 call assert_equal('Xscratch', bufname(''))
680 call assert_equal('nofile', &buftype)
681 %bwipe
682 call delete('Xtest_mks.out')
683endfunc
684
685" Test for mksession with fold options
686func Test_mksession_foldopt()
687 set sessionoptions-=options
688 set sessionoptions+=folds
689 new
690 setlocal foldenable
691 setlocal foldmethod=expr
692 setlocal foldmarker=<<<,>>>
693 setlocal foldignore=%
694 setlocal foldlevel=2
695 setlocal foldminlines=10
696 setlocal foldnestmax=15
697 mksession! Xtest_mks.out
698 close
699 %bwipe
700
701 source Xtest_mks.out
702 call assert_true(&foldenable)
703 call assert_equal('expr', &foldmethod)
704 call assert_equal('<<<,>>>', &foldmarker)
705 call assert_equal('%', &foldignore)
706 call assert_equal(2, &foldlevel)
707 call assert_equal(10, &foldminlines)
708 call assert_equal(15, &foldnestmax)
709
710 close
711 %bwipe
712 set sessionoptions&
713endfunc
714
715" Test for mksession with window position
716func Test_mksession_winpos()
717 if !has('gui_running')
718 " Only applicable in GUI Vim
719 return
720 endif
721 set sessionoptions+=winpos
722 mksession! Xtest_mks.out
723 let found_winpos = v:false
724 let lines = readfile('Xtest_mks.out')
725 for line in lines
726 if line =~ '^winpos '
727 let found_winpos = v:true
728 break
729 endif
730 endfor
731 call assert_true(found_winpos)
732 call delete('Xtest_mks.out')
733 set sessionoptions&
734endfunc
735
736" Test for mksession with 'compatible' option
737func Test_mksession_compatible()
738 mksession! Xtest_mks1.out
739 set compatible
740 mksession! Xtest_mks2.out
741 set nocp
742
743 let test_success = v:false
744 let lines = readfile('Xtest_mks1.out')
745 for line in lines
746 if line =~ '^if &cp | set nocp | endif'
747 let test_success = v:true
748 break
749 endif
750 endfor
751 call assert_true(test_success)
752
753 let test_success = v:false
754 let lines = readfile('Xtest_mks2.out')
755 for line in lines
756 if line =~ '^if !&cp | set cp | endif'
757 let test_success = v:true
758 break
759 endif
760 endfor
761 call assert_true(test_success)
762
763 call delete('Xtest_mks1.out')
764 call delete('Xtest_mks2.out')
765 set compatible&
766 set sessionoptions&
767endfunc
768
Bram Moolenaar87500262019-08-01 23:05:49 +0200769func s:ClearMappings()
770 mapclear
771 omapclear
772 mapclear!
773 lmapclear
774 tmapclear
775endfunc
776
777func Test_mkvimrc()
778 let entries = [
779 \ ['', 'nothing', '<Nop>'],
780 \ ['n', 'normal', 'NORMAL'],
781 \ ['v', 'visual', 'VISUAL'],
782 \ ['s', 'select', 'SELECT'],
783 \ ['x', 'visualonly', 'VISUALONLY'],
784 \ ['o', 'operator', 'OPERATOR'],
785 \ ['i', 'insert', 'INSERT'],
786 \ ['l', 'lang', 'LANG'],
787 \ ['c', 'command', 'COMMAND'],
788 \ ['t', 'terminal', 'TERMINAL'],
789 \ ]
790 for entry in entries
791 exe entry[0] .. 'map ' .. entry[1] .. ' ' .. entry[2]
792 endfor
793
794 mkvimrc Xtestvimrc
795
796 call s:ClearMappings()
797 for entry in entries
798 call assert_equal('', maparg(entry[1], entry[0]))
799 endfor
800
801 source Xtestvimrc
802
803 for entry in entries
804 call assert_equal(entry[2], maparg(entry[1], entry[0]))
805 endfor
806
807 call s:ClearMappings()
808 call delete('Xtestvimrc')
809endfunc
810
Bram Moolenaareca626f2016-12-01 18:47:38 +0100811" vim: shiftwidth=2 sts=2 expandtab