1" 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
10source shared.vim
11
12func 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',
25    \   'A���  three mulTibyte characters'
26    \ ])
27  let tmpfile = 'Xtemp'
28  exec 'w! ' . tmpfile
29  /^start:
30  set wrap
31  vsplit
32  norm! j16|
33  split
34  norm! j16|
35  split
36  norm! j16|
37  split
38  norm! j8|
39  split
40  norm! j8|
41  split
42  norm! j16|
43  split
44  norm! j16|
45  split
46  norm! j16|
47  wincmd l
48
49  set nowrap
50  /^start:
51  norm! j16|3zl
52  split
53  norm! j016|3zl
54  split
55  norm! j016|3zl
56  split
57  norm! j08|3zl
58  split
59  norm! j08|3zl
60  split
61  norm! j016|3zl
62  split
63  norm! j016|3zl
64  split
65  norm! j016|3zl
66  split
67  call wincol()
68  mksession! Xtest_mks.out
69  let li = filter(readfile('Xtest_mks.out'), 'v:val =~# "\\(^ *normal! 0\\|^ *exe ''normal!\\)"')
70  let expected = [
71    \   'normal! 016|',
72    \   'normal! 016|',
73    \   'normal! 016|',
74    \   'normal! 08|',
75    \   'normal! 08|',
76    \   'normal! 016|',
77    \   'normal! 016|',
78    \   'normal! 016|',
79    \   "  exe 'normal! ' . s:c . '|zs' . 16 . '|'",
80    \   "  normal! 016|",
81    \   "  exe 'normal! ' . s:c . '|zs' . 16 . '|'",
82    \   "  normal! 016|",
83    \   "  exe 'normal! ' . s:c . '|zs' . 16 . '|'",
84    \   "  normal! 016|",
85    \   "  exe 'normal! ' . s:c . '|zs' . 8 . '|'",
86    \   "  normal! 08|",
87    \   "  exe 'normal! ' . s:c . '|zs' . 8 . '|'",
88    \   "  normal! 08|",
89    \   "  exe 'normal! ' . s:c . '|zs' . 16 . '|'",
90    \   "  normal! 016|",
91    \   "  exe 'normal! ' . s:c . '|zs' . 16 . '|'",
92    \   "  normal! 016|",
93    \   "  exe 'normal! ' . s:c . '|zs' . 16 . '|'",
94    \   "  normal! 016|",
95    \   "  exe 'normal! ' . s:c . '|zs' . 16 . '|'",
96    \   "  normal! 016|"
97    \ ]
98  call assert_equal(expected, li)
99  tabclose!
100
101  call delete('Xtest_mks.out')
102  call delete(tmpfile)
103  let &wrap = wrap_save
104  set sessionoptions&
105endfunc
106
107func Test_mksession_winheight()
108  new
109  set winheight=10
110  set winminheight=2
111  mksession! Xtest_mks.out
112  source Xtest_mks.out
113
114  call delete('Xtest_mks.out')
115endfunc
116
117func Test_mksession_large_winheight()
118  set winheight=999
119  mksession! Xtest_mks_winheight.out
120  set winheight&
121  source Xtest_mks_winheight.out
122  call delete('Xtest_mks_winheight.out')
123endfunc
124
125func Test_mksession_arglist()
126  argdel *
127  next file1 file2 file3 file4
128  mksession! Xtest_mks.out
129  source Xtest_mks.out
130  call assert_equal(['file1', 'file2', 'file3', 'file4'], argv())
131
132  call delete('Xtest_mks.out')
133  argdel *
134endfunc
135
136func Test_mksession_one_buffer_two_windows()
137  edit Xtest1
138  new Xtest2
139  split
140  mksession! Xtest_mks.out
141  let lines = readfile('Xtest_mks.out')
142  let count1 = 0
143  let count2 = 0
144  let count2buf = 0
145  for line in lines
146    if line =~ 'edit \f*Xtest1$'
147      let count1 += 1
148    endif
149    if line =~ 'edit \f\{-}Xtest2'
150      let count2 += 1
151    endif
152    if line =~ 'buffer \f\{-}Xtest2'
153      let count2buf += 1
154    endif
155  endfor
156  call assert_equal(1, count1, 'Xtest1 count')
157  call assert_equal(2, count2, 'Xtest2 count')
158  call assert_equal(2, count2buf, 'Xtest2 buffer count')
159
160  close
161  bwipe!
162  call delete('Xtest_mks.out')
163endfunc
164
165func Test_mksession_lcd_multiple_tabs()
166  tabnew
167  tabnew
168  lcd .
169  tabfirst
170  lcd .
171  mksession! Xtest_mks.out
172  tabonly
173  source Xtest_mks.out
174  call assert_true(haslocaldir(), 'Tab 1 localdir')
175  tabnext 2
176  call assert_true(!haslocaldir(), 'Tab 2 localdir')
177  tabnext 3
178  call assert_true(haslocaldir(), 'Tab 3 localdir')
179  call delete('Xtest_mks.out')
180endfunc
181
182func Test_mksession_blank_tabs()
183  tabnew
184  tabnew
185  tabnew
186  tabnext 3
187  mksession! Xtest_mks.out
188  tabnew
189  tabnew
190  tabnext 2
191  source Xtest_mks.out
192  call assert_equal(4, tabpagenr('$'), 'session restore should restore number of tabs')
193  call assert_equal(3, tabpagenr(), 'session restore should restore the active tab')
194  call delete('Xtest_mks.out')
195endfunc
196
197func Test_mksession_blank_windows()
198  split
199  split
200  split
201  3 wincmd w
202  mksession! Xtest_mks.out
203  split
204  split
205  2 wincmd w
206  source Xtest_mks.out
207  call assert_equal(4, winnr('$'), 'session restore should restore number of windows')
208  call assert_equal(3, winnr(), 'session restore should restore the active window')
209  call delete('Xtest_mks.out')
210endfunc
211
212if has('terminal')
213
214func Test_mksession_terminal_shell()
215  terminal
216  mksession! Xtest_mks.out
217  let lines = readfile('Xtest_mks.out')
218  let term_cmd = ''
219  for line in lines
220    if line =~ '^terminal'
221      let term_cmd = line
222    elseif line =~ 'badd.*' . &shell
223      call assert_report('unexpected shell line: ' . line)
224    endif
225  endfor
226  call assert_match('terminal ++curwin ++cols=\d\+ ++rows=\d\+\s*$', term_cmd)
227
228  call Stop_shell_in_terminal(bufnr('%'))
229  call delete('Xtest_mks.out')
230endfunc
231
232func Test_mksession_terminal_no_restore_cmdarg()
233  terminal ++norestore
234  mksession! Xtest_mks.out
235  let lines = readfile('Xtest_mks.out')
236  let term_cmd = ''
237  for line in lines
238    if line =~ '^terminal'
239      call assert_report('session must not restore teminal')
240    endif
241  endfor
242
243  call Stop_shell_in_terminal(bufnr('%'))
244  call delete('Xtest_mks.out')
245endfunc
246
247func Test_mksession_terminal_no_restore_funcarg()
248  call term_start(&shell, {'norestore': 1})
249  mksession! Xtest_mks.out
250  let lines = readfile('Xtest_mks.out')
251  let term_cmd = ''
252  for line in lines
253    if line =~ '^terminal'
254      call assert_report('session must not restore teminal')
255    endif
256  endfor
257
258  call Stop_shell_in_terminal(bufnr('%'))
259  call delete('Xtest_mks.out')
260endfunc
261
262func Test_mksession_terminal_no_restore_func()
263  terminal
264  call term_setrestore(bufnr('%'), 'NONE')
265  mksession! Xtest_mks.out
266  let lines = readfile('Xtest_mks.out')
267  let term_cmd = ''
268  for line in lines
269    if line =~ '^terminal'
270      call assert_report('session must not restore teminal')
271    endif
272  endfor
273
274  call Stop_shell_in_terminal(bufnr('%'))
275  call delete('Xtest_mks.out')
276endfunc
277
278func Test_mksession_terminal_no_ssop()
279  terminal
280  set sessionoptions-=terminal
281  mksession! Xtest_mks.out
282  let lines = readfile('Xtest_mks.out')
283  let term_cmd = ''
284  for line in lines
285    if line =~ '^terminal'
286      call assert_report('session must not restore teminal')
287    endif
288  endfor
289
290  call Stop_shell_in_terminal(bufnr('%'))
291  call delete('Xtest_mks.out')
292  set sessionoptions&
293endfunc
294
295func Test_mksession_terminal_restore_other()
296  terminal
297  call term_setrestore(bufnr('%'), 'other')
298  mksession! Xtest_mks.out
299  let lines = readfile('Xtest_mks.out')
300  let term_cmd = ''
301  for line in lines
302    if line =~ '^terminal'
303      let term_cmd = line
304    endif
305  endfor
306  call assert_match('terminal ++curwin ++cols=\d\+ ++rows=\d\+ other', term_cmd)
307
308  call Stop_shell_in_terminal(bufnr('%'))
309  call delete('Xtest_mks.out')
310endfunc
311
312endif " has('terminal')
313
314" Test :mkview with a file argument.
315func Test_mkview_file()
316  " Create a view with line number and a fold.
317  help :mkview
318  set number
319  norm! V}zf
320  let pos = getpos('.')
321  let linefoldclosed1 = foldclosed('.')
322  mkview! Xview
323  set nonumber
324  norm! zrj
325  " We can close the help window, as mkview with a file name should
326  " generate a command to edit the file.
327  helpclose
328
329  source Xview
330  call assert_equal(1, &number)
331  call assert_match('\*:mkview\*$', getline('.'))
332  call assert_equal(pos, getpos('.'))
333  call assert_equal(linefoldclosed1, foldclosed('.'))
334
335  " Creating a view again with the same file name should fail (file
336  " already exists). But with a !, the previous view should be
337  " overwritten without error.
338  help :loadview
339  call assert_fails('mkview Xview', 'E189:')
340  call assert_match('\*:loadview\*$', getline('.'))
341  mkview! Xview
342  call assert_match('\*:loadview\*$', getline('.'))
343
344  call delete('Xview')
345  bwipe
346endfunc
347
348" Test :mkview and :loadview with a custom 'viewdir'.
349func Test_mkview_loadview_with_viewdir()
350  set viewdir=Xviewdir
351
352  help :mkview
353  set number
354  norm! V}zf
355  let pos = getpos('.')
356  let linefoldclosed1 = foldclosed('.')
357  mkview 1
358  set nonumber
359  norm! zrj
360
361  loadview 1
362
363  " The directory Xviewdir/ should have been created and the view
364  " should be stored in that directory.
365  call assert_equal('Xviewdir/' .
366        \           substitute(
367        \             substitute(
368        \               expand('%:p'), '/', '=+', 'g'), ':', '=-', 'g') . '=1.vim',
369        \           glob('Xviewdir/*'))
370  call assert_equal(1, &number)
371  call assert_match('\*:mkview\*$', getline('.'))
372  call assert_equal(pos, getpos('.'))
373  call assert_equal(linefoldclosed1, foldclosed('.'))
374
375  call delete('Xviewdir', 'rf')
376  set viewdir&
377  helpclose
378endfunc
379
380func Test_mkview_no_file_name()
381  new
382  " :mkview or :mkview {nr} should fail in a unnamed buffer.
383  call assert_fails('mkview', 'E32:')
384  call assert_fails('mkview 1', 'E32:')
385
386  " :mkview {file} should succeed in a unnamed buffer.
387  mkview Xview
388  help
389  source Xview
390  call assert_equal('', bufname('%'))
391
392  call delete('Xview')
393  %bwipe
394endfunc
395
396" vim: shiftwidth=2 sts=2 expandtab
397