blob: ed8b10fa03461c79c4910dc2a19459666a6d48e4 [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
201func Test_mksession_blank_windows()
202 split
203 split
204 split
205 3 wincmd w
206 mksession! Xtest_mks.out
207 split
208 split
209 2 wincmd w
210 source Xtest_mks.out
211 call assert_equal(4, winnr('$'), 'session restore should restore number of windows')
212 call assert_equal(3, winnr(), 'session restore should restore the active window')
213 call delete('Xtest_mks.out')
214endfunc
215
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100216if has('terminal')
217
218func Test_mksession_terminal_shell()
219 terminal
220 mksession! Xtest_mks.out
221 let lines = readfile('Xtest_mks.out')
222 let term_cmd = ''
223 for line in lines
224 if line =~ '^terminal'
225 let term_cmd = line
226 elseif line =~ 'badd.*' . &shell
227 call assert_report('unexpected shell line: ' . line)
228 endif
229 endfor
230 call assert_match('terminal ++curwin ++cols=\d\+ ++rows=\d\+\s*$', term_cmd)
231
232 call Stop_shell_in_terminal(bufnr('%'))
233 call delete('Xtest_mks.out')
234endfunc
235
236func Test_mksession_terminal_no_restore_cmdarg()
237 terminal ++norestore
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 call assert_report('session must not restore teminal')
244 endif
245 endfor
246
247 call Stop_shell_in_terminal(bufnr('%'))
248 call delete('Xtest_mks.out')
249endfunc
250
251func Test_mksession_terminal_no_restore_funcarg()
252 call term_start(&shell, {'norestore': 1})
253 mksession! Xtest_mks.out
254 let lines = readfile('Xtest_mks.out')
255 let term_cmd = ''
256 for line in lines
257 if line =~ '^terminal'
258 call assert_report('session must not restore teminal')
259 endif
260 endfor
261
262 call Stop_shell_in_terminal(bufnr('%'))
263 call delete('Xtest_mks.out')
264endfunc
265
266func Test_mksession_terminal_no_restore_func()
267 terminal
268 call term_setrestore(bufnr('%'), 'NONE')
269 mksession! Xtest_mks.out
270 let lines = readfile('Xtest_mks.out')
271 let term_cmd = ''
272 for line in lines
273 if line =~ '^terminal'
274 call assert_report('session must not restore teminal')
275 endif
276 endfor
277
278 call Stop_shell_in_terminal(bufnr('%'))
279 call delete('Xtest_mks.out')
280endfunc
281
282func Test_mksession_terminal_no_ssop()
283 terminal
284 set sessionoptions-=terminal
285 mksession! Xtest_mks.out
286 let lines = readfile('Xtest_mks.out')
287 let term_cmd = ''
288 for line in lines
289 if line =~ '^terminal'
290 call assert_report('session must not restore teminal')
291 endif
292 endfor
293
294 call Stop_shell_in_terminal(bufnr('%'))
295 call delete('Xtest_mks.out')
296 set sessionoptions&
297endfunc
298
299func Test_mksession_terminal_restore_other()
300 terminal
301 call term_setrestore(bufnr('%'), 'other')
302 mksession! Xtest_mks.out
303 let lines = readfile('Xtest_mks.out')
304 let term_cmd = ''
305 for line in lines
306 if line =~ '^terminal'
307 let term_cmd = line
308 endif
309 endfor
310 call assert_match('terminal ++curwin ++cols=\d\+ ++rows=\d\+ other', term_cmd)
311
312 call Stop_shell_in_terminal(bufnr('%'))
313 call delete('Xtest_mks.out')
314endfunc
315
316endif " has('terminal')
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +0200317
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200318" Test :mkview with a file argument.
319func Test_mkview_file()
320 " Create a view with line number and a fold.
321 help :mkview
322 set number
323 norm! V}zf
Bram Moolenaar92c1b692018-08-29 21:42:42 +0200324 let pos = getcurpos()
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200325 let linefoldclosed1 = foldclosed('.')
326 mkview! Xview
327 set nonumber
328 norm! zrj
329 " We can close the help window, as mkview with a file name should
330 " generate a command to edit the file.
331 helpclose
332
333 source Xview
334 call assert_equal(1, &number)
335 call assert_match('\*:mkview\*$', getline('.'))
Bram Moolenaar92c1b692018-08-29 21:42:42 +0200336 call assert_equal(pos, getcurpos())
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200337 call assert_equal(linefoldclosed1, foldclosed('.'))
338
339 " Creating a view again with the same file name should fail (file
340 " already exists). But with a !, the previous view should be
341 " overwritten without error.
342 help :loadview
343 call assert_fails('mkview Xview', 'E189:')
344 call assert_match('\*:loadview\*$', getline('.'))
345 mkview! Xview
346 call assert_match('\*:loadview\*$', getline('.'))
347
348 call delete('Xview')
349 bwipe
350endfunc
351
352" Test :mkview and :loadview with a custom 'viewdir'.
353func Test_mkview_loadview_with_viewdir()
354 set viewdir=Xviewdir
355
356 help :mkview
357 set number
358 norm! V}zf
Bram Moolenaar92c1b692018-08-29 21:42:42 +0200359 let pos = getcurpos()
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200360 let linefoldclosed1 = foldclosed('.')
361 mkview 1
362 set nonumber
363 norm! zrj
364
365 loadview 1
366
367 " The directory Xviewdir/ should have been created and the view
368 " should be stored in that directory.
369 call assert_equal('Xviewdir/' .
370 \ substitute(
371 \ substitute(
372 \ expand('%:p'), '/', '=+', 'g'), ':', '=-', 'g') . '=1.vim',
373 \ glob('Xviewdir/*'))
374 call assert_equal(1, &number)
375 call assert_match('\*:mkview\*$', getline('.'))
Bram Moolenaar92c1b692018-08-29 21:42:42 +0200376 call assert_equal(pos, getcurpos())
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200377 call assert_equal(linefoldclosed1, foldclosed('.'))
378
379 call delete('Xviewdir', 'rf')
380 set viewdir&
381 helpclose
382endfunc
383
384func Test_mkview_no_file_name()
385 new
386 " :mkview or :mkview {nr} should fail in a unnamed buffer.
387 call assert_fails('mkview', 'E32:')
388 call assert_fails('mkview 1', 'E32:')
389
390 " :mkview {file} should succeed in a unnamed buffer.
391 mkview Xview
392 help
393 source Xview
394 call assert_equal('', bufname('%'))
395
396 call delete('Xview')
397 %bwipe
398endfunc
Bram Moolenaar79da5632017-02-01 22:52:44 +0100399
Bram Moolenaareca626f2016-12-01 18:47:38 +0100400" vim: shiftwidth=2 sts=2 expandtab