blob: 145c91cba5f7a2dcc375f1225bd6b11aac43a700 [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
6if !has('multi_byte') || !has('mksession')
7 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 Moolenaar79da5632017-02-01 22:52:44 +0100129func Test_mksession_arglist()
130 argdel *
131 next file1 file2 file3 file4
132 mksession! Xtest_mks.out
133 source Xtest_mks.out
134 call assert_equal(['file1', 'file2', 'file3', 'file4'], argv())
135
136 call delete('Xtest_mks.out')
137 argdel *
138endfunc
139
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +0200140func Test_mksession_one_buffer_two_windows()
141 edit Xtest1
142 new Xtest2
143 split
144 mksession! Xtest_mks.out
145 let lines = readfile('Xtest_mks.out')
146 let count1 = 0
147 let count2 = 0
148 let count2buf = 0
149 for line in lines
150 if line =~ 'edit \f*Xtest1$'
151 let count1 += 1
152 endif
153 if line =~ 'edit \f\{-}Xtest2'
154 let count2 += 1
155 endif
156 if line =~ 'buffer \f\{-}Xtest2'
157 let count2buf += 1
158 endif
159 endfor
160 call assert_equal(1, count1, 'Xtest1 count')
161 call assert_equal(2, count2, 'Xtest2 count')
162 call assert_equal(2, count2buf, 'Xtest2 buffer count')
163
164 close
165 bwipe!
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +0200166 call delete('Xtest_mks.out')
167endfunc
168
Bram Moolenaar26d4b892018-07-04 22:26:28 +0200169func Test_mksession_lcd_multiple_tabs()
170 tabnew
171 tabnew
Bram Moolenaar81e2ac72018-07-04 22:44:08 +0200172 lcd .
Bram Moolenaar26d4b892018-07-04 22:26:28 +0200173 tabfirst
Bram Moolenaar81e2ac72018-07-04 22:44:08 +0200174 lcd .
Bram Moolenaar26d4b892018-07-04 22:26:28 +0200175 mksession! Xtest_mks.out
176 tabonly
177 source Xtest_mks.out
178 call assert_true(haslocaldir(), 'Tab 1 localdir')
179 tabnext 2
180 call assert_true(!haslocaldir(), 'Tab 2 localdir')
181 tabnext 3
182 call assert_true(haslocaldir(), 'Tab 3 localdir')
183 call delete('Xtest_mks.out')
184endfunc
185
186func Test_mksession_blank_tabs()
187 tabnew
188 tabnew
189 tabnew
190 tabnext 3
191 mksession! Xtest_mks.out
192 tabnew
193 tabnew
194 tabnext 2
195 source Xtest_mks.out
196 call assert_equal(4, tabpagenr('$'), 'session restore should restore number of tabs')
197 call assert_equal(3, tabpagenr(), 'session restore should restore the active tab')
198 call delete('Xtest_mks.out')
199endfunc
200
Bram Moolenaare3c74d22019-01-12 16:29:30 +0100201if has('extra_search')
202
203func Test_mksession_hlsearch()
204 set hlsearch
205 mksession! Xtest_mks.out
206 nohlsearch
207 source Xtest_mks.out
208 call assert_equal(1, v:hlsearch, 'session should restore search highlighting state')
209 nohlsearch
210 mksession! Xtest_mks.out
211 source Xtest_mks.out
212 call assert_equal(0, v:hlsearch, 'session should restore search highlighting state')
213 call delete('Xtest_mks.out')
214endfunc
215
216endif
217
218
Bram Moolenaar26d4b892018-07-04 22:26:28 +0200219func Test_mksession_blank_windows()
220 split
221 split
222 split
223 3 wincmd w
224 mksession! Xtest_mks.out
225 split
226 split
227 2 wincmd w
228 source Xtest_mks.out
229 call assert_equal(4, winnr('$'), 'session restore should restore number of windows')
230 call assert_equal(3, winnr(), 'session restore should restore the active window')
231 call delete('Xtest_mks.out')
232endfunc
233
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100234if has('terminal')
235
236func Test_mksession_terminal_shell()
237 terminal
238 mksession! Xtest_mks.out
239 let lines = readfile('Xtest_mks.out')
240 let term_cmd = ''
241 for line in lines
242 if line =~ '^terminal'
243 let term_cmd = line
244 elseif line =~ 'badd.*' . &shell
245 call assert_report('unexpected shell line: ' . line)
246 endif
247 endfor
248 call assert_match('terminal ++curwin ++cols=\d\+ ++rows=\d\+\s*$', term_cmd)
249
250 call Stop_shell_in_terminal(bufnr('%'))
251 call delete('Xtest_mks.out')
252endfunc
253
254func Test_mksession_terminal_no_restore_cmdarg()
255 terminal ++norestore
256 mksession! Xtest_mks.out
257 let lines = readfile('Xtest_mks.out')
258 let term_cmd = ''
259 for line in lines
260 if line =~ '^terminal'
261 call assert_report('session must not restore teminal')
262 endif
263 endfor
264
265 call Stop_shell_in_terminal(bufnr('%'))
266 call delete('Xtest_mks.out')
267endfunc
268
269func Test_mksession_terminal_no_restore_funcarg()
270 call term_start(&shell, {'norestore': 1})
271 mksession! Xtest_mks.out
272 let lines = readfile('Xtest_mks.out')
273 let term_cmd = ''
274 for line in lines
275 if line =~ '^terminal'
276 call assert_report('session must not restore teminal')
277 endif
278 endfor
279
280 call Stop_shell_in_terminal(bufnr('%'))
281 call delete('Xtest_mks.out')
282endfunc
283
284func Test_mksession_terminal_no_restore_func()
285 terminal
286 call term_setrestore(bufnr('%'), 'NONE')
287 mksession! Xtest_mks.out
288 let lines = readfile('Xtest_mks.out')
289 let term_cmd = ''
290 for line in lines
291 if line =~ '^terminal'
292 call assert_report('session must not restore teminal')
293 endif
294 endfor
295
296 call Stop_shell_in_terminal(bufnr('%'))
297 call delete('Xtest_mks.out')
298endfunc
299
300func Test_mksession_terminal_no_ssop()
301 terminal
302 set sessionoptions-=terminal
303 mksession! Xtest_mks.out
304 let lines = readfile('Xtest_mks.out')
305 let term_cmd = ''
306 for line in lines
307 if line =~ '^terminal'
308 call assert_report('session must not restore teminal')
309 endif
310 endfor
311
312 call Stop_shell_in_terminal(bufnr('%'))
313 call delete('Xtest_mks.out')
314 set sessionoptions&
315endfunc
316
317func Test_mksession_terminal_restore_other()
318 terminal
319 call term_setrestore(bufnr('%'), 'other')
320 mksession! Xtest_mks.out
321 let lines = readfile('Xtest_mks.out')
322 let term_cmd = ''
323 for line in lines
324 if line =~ '^terminal'
325 let term_cmd = line
326 endif
327 endfor
328 call assert_match('terminal ++curwin ++cols=\d\+ ++rows=\d\+ other', term_cmd)
329
330 call Stop_shell_in_terminal(bufnr('%'))
331 call delete('Xtest_mks.out')
332endfunc
333
334endif " has('terminal')
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +0200335
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200336" Test :mkview with a file argument.
337func Test_mkview_file()
338 " Create a view with line number and a fold.
339 help :mkview
340 set number
Bram Moolenaar2bf4fe02018-08-30 14:04:25 +0200341 norm! V}zf0
Bram Moolenaardd5d18e2018-08-30 14:16:06 +0200342 let pos = getpos('.')
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200343 let linefoldclosed1 = foldclosed('.')
344 mkview! Xview
345 set nonumber
346 norm! zrj
347 " We can close the help window, as mkview with a file name should
348 " generate a command to edit the file.
349 helpclose
350
351 source Xview
352 call assert_equal(1, &number)
353 call assert_match('\*:mkview\*$', getline('.'))
Bram Moolenaardd5d18e2018-08-30 14:16:06 +0200354 call assert_equal(pos, getpos('.'))
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200355 call assert_equal(linefoldclosed1, foldclosed('.'))
356
357 " Creating a view again with the same file name should fail (file
358 " already exists). But with a !, the previous view should be
359 " overwritten without error.
360 help :loadview
361 call assert_fails('mkview Xview', 'E189:')
362 call assert_match('\*:loadview\*$', getline('.'))
363 mkview! Xview
364 call assert_match('\*:loadview\*$', getline('.'))
365
366 call delete('Xview')
367 bwipe
368endfunc
369
370" Test :mkview and :loadview with a custom 'viewdir'.
371func Test_mkview_loadview_with_viewdir()
372 set viewdir=Xviewdir
373
374 help :mkview
375 set number
376 norm! V}zf
Bram Moolenaardd5d18e2018-08-30 14:16:06 +0200377 let pos = getpos('.')
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200378 let linefoldclosed1 = foldclosed('.')
379 mkview 1
380 set nonumber
381 norm! zrj
382
383 loadview 1
384
385 " The directory Xviewdir/ should have been created and the view
386 " should be stored in that directory.
387 call assert_equal('Xviewdir/' .
388 \ substitute(
389 \ substitute(
390 \ expand('%:p'), '/', '=+', 'g'), ':', '=-', 'g') . '=1.vim',
391 \ glob('Xviewdir/*'))
392 call assert_equal(1, &number)
393 call assert_match('\*:mkview\*$', getline('.'))
Bram Moolenaardd5d18e2018-08-30 14:16:06 +0200394 call assert_equal(pos, getpos('.'))
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200395 call assert_equal(linefoldclosed1, foldclosed('.'))
396
397 call delete('Xviewdir', 'rf')
398 set viewdir&
399 helpclose
400endfunc
401
402func Test_mkview_no_file_name()
403 new
404 " :mkview or :mkview {nr} should fail in a unnamed buffer.
405 call assert_fails('mkview', 'E32:')
406 call assert_fails('mkview 1', 'E32:')
407
408 " :mkview {file} should succeed in a unnamed buffer.
409 mkview Xview
410 help
411 source Xview
412 call assert_equal('', bufname('%'))
413
414 call delete('Xview')
415 %bwipe
416endfunc
Bram Moolenaar79da5632017-02-01 22:52:44 +0100417
Bram Moolenaar555de4e2019-01-21 23:03:49 +0100418" A clean session (one empty buffer, one window, and one tab) should not
419" set any error messages when sourced because no commands should fail.
420func Test_mksession_no_errmsg()
421 let v:errmsg = ''
422 %bwipe!
423 mksession! Xtest_mks.out
424 source Xtest_mks.out
425 call assert_equal('', v:errmsg)
426 call delete('Xtest_mks.out')
427endfunc
428
Bram Moolenaarad36a352019-01-24 13:34:42 +0100429func Test_mksession_quote_in_filename()
430 let v:errmsg = ''
431 %bwipe!
432 split another
433 split x'y\"z
434 mksession! Xtest_mks_quoted.out
435 %bwipe!
436 source Xtest_mks_quoted.out
437 call assert_true(bufexists("x'y\"z"))
438
439 %bwipe!
440 call delete('Xtest_mks_quoted.out')
441endfunc
442
Bram Moolenaar555de4e2019-01-21 23:03:49 +0100443
Bram Moolenaareca626f2016-12-01 18:47:38 +0100444" vim: shiftwidth=2 sts=2 expandtab