xref: /vim-8.2.3635/src/testdir/test_viminfo.vim (revision 8ea05de6)
1" Test for reading and writing .viminfo
2
3source check.vim
4source term_util.vim
5source shared.vim
6
7function Test_viminfo_read_and_write()
8  " First clear 'history', so that "hislen" is zero.  Then set it again,
9  " simulating Vim starting up.
10  set history=0
11  wviminfo Xviminfo
12  set history=1000
13
14  call histdel(':')
15  let lines = [
16	\ '# comment line',
17	\ '*encoding=utf-8',
18	\ '~MSle0~/asdf',
19	\ '|copied as-is',
20	\ '|and one more',
21	\ ]
22  call writefile(lines, 'Xviminfo')
23  rviminfo Xviminfo
24  call assert_equal('asdf', @/)
25
26  wviminfo Xviminfo
27  let lines = readfile('Xviminfo')
28  let done = 0
29  for line in lines
30    if line[0] == '|' && line !~ '^|[234],' && line !~ '^|<'
31      if done == 0
32	call assert_equal('|1,4', line)
33      elseif done == 1
34	call assert_equal('|copied as-is', line)
35      elseif done == 2
36	call assert_equal('|and one more', line)
37      endif
38      let done += 1
39    endif
40  endfor
41  call assert_equal(3, done)
42
43  call delete('Xviminfo')
44endfunc
45
46func Test_global_vars()
47  let g:MY_GLOBAL_STRING = "Vim Editor"
48  let g:MY_GLOBAL_NUM = 345
49  let g:MY_GLOBAL_FLOAT = 3.14
50  let test_dict = {'foo': 1, 'bar': 0, 'longvarible': 1000}
51  let g:MY_GLOBAL_DICT = test_dict
52  " store a really long list, so line wrapping will occur in viminfo file
53  let test_list = range(1,100)
54  let g:MY_GLOBAL_LIST = test_list
55  let test_blob = 0z00112233445566778899aabbccddeeff
56  let g:MY_GLOBAL_BLOB = test_blob
57  let test_false = v:false
58  let g:MY_GLOBAL_FALSE = test_false
59  let test_true = v:true
60  let g:MY_GLOBAL_TRUE = test_true
61  let test_null = v:null
62  let g:MY_GLOBAL_NULL = test_null
63  let test_none = v:none
64  let g:MY_GLOBAL_NONE = test_none
65
66  set viminfo='100,<50,s10,h,!,nviminfo
67  wv! Xviminfo
68
69  unlet g:MY_GLOBAL_STRING
70  unlet g:MY_GLOBAL_NUM
71  unlet g:MY_GLOBAL_FLOAT
72  unlet g:MY_GLOBAL_DICT
73  unlet g:MY_GLOBAL_LIST
74  unlet g:MY_GLOBAL_BLOB
75  unlet g:MY_GLOBAL_FALSE
76  unlet g:MY_GLOBAL_TRUE
77  unlet g:MY_GLOBAL_NULL
78  unlet g:MY_GLOBAL_NONE
79
80  rv! Xviminfo
81  call assert_equal("Vim Editor", g:MY_GLOBAL_STRING)
82  call assert_equal(345, g:MY_GLOBAL_NUM)
83  call assert_equal(3.14, g:MY_GLOBAL_FLOAT)
84  call assert_equal(test_dict, g:MY_GLOBAL_DICT)
85  call assert_equal(test_list, g:MY_GLOBAL_LIST)
86  call assert_equal(test_blob, g:MY_GLOBAL_BLOB)
87  call assert_equal(test_false, g:MY_GLOBAL_FALSE)
88  call assert_equal(test_true, g:MY_GLOBAL_TRUE)
89  call assert_equal(test_null, g:MY_GLOBAL_NULL)
90  call assert_equal(test_none, g:MY_GLOBAL_NONE)
91
92  " Test for invalid values for a blob, list, dict in a viminfo file
93  call writefile([
94        \ "!GLOB_BLOB_1\tBLO\t123",
95        \ "!GLOB_BLOB_2\tBLO\t012",
96        \ "!GLOB_BLOB_3\tBLO\t0z1x",
97        \ "!GLOB_BLOB_4\tBLO\t0z12 ab",
98        \ "!GLOB_LIST_1\tLIS\t1 2",
99        \ "!GLOB_DICT_1\tDIC\t1 2"], 'Xviminfo')
100  call assert_fails('rv! Xviminfo', 'E15:')
101  call assert_equal('123', g:GLOB_BLOB_1)
102  call assert_equal(1, type(g:GLOB_BLOB_1))
103  call assert_equal('012', g:GLOB_BLOB_2)
104  call assert_equal(1, type(g:GLOB_BLOB_2))
105  call assert_equal('0z1x', g:GLOB_BLOB_3)
106  call assert_equal(1, type(g:GLOB_BLOB_3))
107  call assert_equal('0z12 ab', g:GLOB_BLOB_4)
108  call assert_equal(1, type(g:GLOB_BLOB_4))
109  call assert_equal('1 2', g:GLOB_LIST_1)
110  call assert_equal(1, type(g:GLOB_LIST_1))
111  call assert_equal('1 2', g:GLOB_DICT_1)
112  call assert_equal(1, type(g:GLOB_DICT_1))
113
114  call delete('Xviminfo')
115  set viminfo-=!
116endfunc
117
118func Test_global_vars_with_circular_reference()
119  let g:MY_GLOBAL_LIST = []
120  call add(g:MY_GLOBAL_LIST, g:MY_GLOBAL_LIST)
121  let g:MY_GLOBAL_DICT = {}
122  let g:MY_GLOBAL_DICT['self'] = g:MY_GLOBAL_DICT
123
124  set viminfo='100,<50,s10,h,!,nviminfo
125  wv! Xviminfo
126  call assert_equal(v:errmsg, '')
127
128  unlet g:MY_GLOBAL_LIST
129  unlet g:MY_GLOBAL_DICT
130
131  rv! Xviminfo
132  call assert_equal(v:errmsg, '')
133  call assert_true(!exists('g:MY_GLOBAL_LIST'))
134  call assert_true(!exists('g:MY_GLOBAL_DICT'))
135
136  call delete('Xviminfo')
137  set viminfo-=!
138endfunc
139
140func Test_cmdline_history()
141  call histdel(':')
142  call test_settime(11)
143  call histadd(':', "echo 'one'")
144  call test_settime(12)
145  " split into two lines
146  let long800 = repeat(" 'eight'", 100)
147  call histadd(':', "echo " . long800)
148  call test_settime(13)
149  " split into three lines
150  let long1400 = repeat(" 'fourteeeeen'", 100)
151  call histadd(':', "echo " . long1400)
152  wviminfo Xviminfo
153  let lines = readfile('Xviminfo')
154  let done_colon = 0
155  let done_bar = 0
156  let lnum = 0
157  while lnum < len(lines)
158    let line = lines[lnum] | let lnum += 1
159    if line[0] == ':'
160      if done_colon == 0
161	call assert_equal(":\x161408", line)
162	let line = lines[lnum] | let lnum += 1
163	call assert_equal('<echo ' . long1400, line)
164      elseif done_colon == 1
165	call assert_equal(":\x16808", line)
166	let line = lines[lnum] | let lnum += 1
167	call assert_equal("<echo " . long800, line)
168      elseif done_colon == 2
169	call assert_equal(":echo 'one'", line)
170      endif
171      let done_colon += 1
172    elseif line[0:4] == '|2,0,'
173      if done_bar == 0
174	call assert_equal("|2,0,13,,>1407", line)
175	let line = lines[lnum] | let lnum += 1
176	call assert_equal('|<"echo ' . long1400[0:484], line)
177	let line = lines[lnum] | let lnum += 1
178	call assert_equal('|<' . long1400[485:974], line)
179	let line = lines[lnum] | let lnum += 1
180	call assert_equal('|<' . long1400[975:] . '"', line)
181      elseif done_bar == 1
182	call assert_equal('|2,0,12,,>807', line)
183	let line = lines[lnum] | let lnum += 1
184	call assert_equal('|<"echo ' . long800[0:484], line)
185	let line = lines[lnum] | let lnum += 1
186	call assert_equal('|<' . long800[485:] . '"', line)
187      elseif done_bar == 2
188	call assert_equal("|2,0,11,,\"echo 'one'\"", line)
189      endif
190      let done_bar += 1
191    endif
192  endwhile
193  call assert_equal(3, done_colon)
194  call assert_equal(3, done_bar)
195
196  call histdel(':')
197  rviminfo Xviminfo
198  call assert_equal("echo " . long1400, histget(':', -1))
199  call assert_equal("echo " . long800, histget(':', -2))
200  call assert_equal("echo 'one'", histget(':', -3))
201
202  call delete('Xviminfo')
203endfunc
204
205func Test_cmdline_history_order()
206  call histdel(':')
207  call test_settime(11)
208  call histadd(':', "echo '11'")
209  call test_settime(22)
210  call histadd(':', "echo '22'")
211  call test_settime(33)
212  call histadd(':', "echo '33'")
213  wviminfo Xviminfo
214
215  call histdel(':')
216  " items go in between
217  call test_settime(15)
218  call histadd(':', "echo '15'")
219  call test_settime(27)
220  call histadd(':', "echo '27'")
221
222  rviminfo Xviminfo
223  call assert_equal("echo '33'", histget(':', -1))
224  call assert_equal("echo '27'", histget(':', -2))
225  call assert_equal("echo '22'", histget(':', -3))
226  call assert_equal("echo '15'", histget(':', -4))
227  call assert_equal("echo '11'", histget(':', -5))
228
229  call histdel(':')
230  " items go before and after
231  eval 8->test_settime()
232  call histadd(':', "echo '8'")
233  call test_settime(39)
234  call histadd(':', "echo '39'")
235
236  rviminfo Xviminfo
237  call assert_equal("echo '39'", histget(':', -1))
238  call assert_equal("echo '33'", histget(':', -2))
239  call assert_equal("echo '22'", histget(':', -3))
240  call assert_equal("echo '11'", histget(':', -4))
241  call assert_equal("echo '8'", histget(':', -5))
242
243  " Check sorting works when writing with merge.
244  call histdel(':')
245  call test_settime(8)
246  call histadd(':', "echo '8'")
247  call test_settime(15)
248  call histadd(':', "echo '15'")
249  call test_settime(27)
250  call histadd(':', "echo '27'")
251  call test_settime(39)
252  call histadd(':', "echo '39'")
253  wviminfo Xviminfo
254
255  call histdel(':')
256  rviminfo Xviminfo
257  call assert_equal("echo '39'", histget(':', -1))
258  call assert_equal("echo '33'", histget(':', -2))
259  call assert_equal("echo '27'", histget(':', -3))
260  call assert_equal("echo '22'", histget(':', -4))
261  call assert_equal("echo '15'", histget(':', -5))
262  call assert_equal("echo '11'", histget(':', -6))
263  call assert_equal("echo '8'", histget(':', -7))
264
265  call delete('Xviminfo')
266endfunc
267
268func Test_viminfo_registers()
269  call test_settime(8)
270  call setreg('a', "eight", 'c')
271  call test_settime(20)
272  call setreg('b', ["twenty", "again"], 'l')
273  call test_settime(40)
274  call setreg('c', ["four", "agai"], 'b4')
275  let l = []
276  set viminfo='100,<600,s10,h,!,nviminfo
277  for i in range(500)
278    call add(l, 'something')
279  endfor
280  call setreg('d', l, 'l')
281  call setreg('e', "abc\<C-V>xyz")
282  wviminfo Xviminfo
283
284  call test_settime(10)
285  call setreg('a', '', 'b10')
286  call test_settime(15)
287  call setreg('b', 'drop')
288  call test_settime(50)
289  call setreg('c', 'keep', 'l')
290  call test_settime(30)
291  call setreg('d', 'drop', 'l')
292  call setreg('e', 'drop')
293  rviminfo Xviminfo
294
295  call assert_equal("", getreg('a'))
296  call assert_equal("\<C-V>10", getregtype('a'))
297  call assert_equal("twenty\nagain\n", getreg('b'))
298  call assert_equal("V", getregtype('b'))
299  call assert_equal("keep\n", getreg('c'))
300  call assert_equal("V", getregtype('c'))
301  call assert_equal(l, getreg('d', 1, 1))
302  call assert_equal("V", getregtype('d'))
303  call assert_equal("abc\<C-V>xyz", getreg('e'))
304
305  " Length around 440 switches to line continuation.
306  let len = 434
307  while len < 445
308    let s = repeat('a', len)
309    call setreg('"', s)
310    wviminfo Xviminfo
311    call setreg('"', '')
312    rviminfo Xviminfo
313    call assert_equal(s, getreg('"'), 'wrong register at length: ' . len)
314
315    let len += 1
316  endwhile
317
318  call delete('Xviminfo')
319endfunc
320
321func Test_viminfo_marks()
322  sp bufa
323  let bufa = bufnr('%')
324  sp bufb
325  let bufb = bufnr('%')
326
327  call test_settime(8)
328  call setpos("'A", [bufa, 1, 1, 0])
329  call test_settime(20)
330  call setpos("'B", [bufb, 9, 1, 0])
331  call setpos("'C", [bufa, 7, 1, 0])
332
333  delmark 0-9
334  call test_settime(25)
335  call setpos("'1", [bufb, 12, 1, 0])
336  call test_settime(35)
337  call setpos("'0", [bufa, 11, 1, 0])
338
339  call test_settime(45)
340  wviminfo Xviminfo
341
342  " Writing viminfo inserts the '0 mark.
343  call assert_equal([bufb, 1, 1, 0], getpos("'0"))
344  call assert_equal([bufa, 11, 1, 0], getpos("'1"))
345  call assert_equal([bufb, 12, 1, 0], getpos("'2"))
346
347  call test_settime(4)
348  call setpos("'A", [bufa, 9, 1, 0])
349  call test_settime(30)
350  call setpos("'B", [bufb, 2, 3, 0])
351  delmark C
352
353  delmark 0-9
354  call test_settime(30)
355  call setpos("'1", [bufb, 22, 1, 0])
356  call test_settime(55)
357  call setpos("'0", [bufa, 21, 1, 0])
358
359  rviminfo Xviminfo
360
361  call assert_equal([bufa, 1, 1, 0], getpos("'A"))
362  call assert_equal([bufb, 2, 3, 0], getpos("'B"))
363  call assert_equal([bufa, 7, 1, 0], getpos("'C"))
364
365  " numbered marks are merged
366  call assert_equal([bufa, 21, 1, 0], getpos("'0"))  " time 55
367  call assert_equal([bufb, 1, 1, 0], getpos("'1"))  " time 45
368  call assert_equal([bufa, 11, 1, 0], getpos("'2")) " time 35
369  call assert_equal([bufb, 22, 1, 0], getpos("'3")) " time 30
370  call assert_equal([bufb, 12, 1, 0], getpos("'4")) " time 25
371
372  " deleted file marks are removed from viminfo
373  delmark C
374  wviminfo Xviminfo
375  rviminfo Xviminfo
376  call assert_equal([0, 0, 0, 0], getpos("'C"))
377
378  " deleted file marks stay in viminfo if defined in another vim later
379  call test_settime(70)
380  call setpos("'D", [bufb, 8, 1, 0])
381  wviminfo Xviminfo
382  call test_settime(65)
383  delmark D
384  call assert_equal([0, 0, 0, 0], getpos("'D"))
385  call test_settime(75)
386  rviminfo Xviminfo
387  call assert_equal([bufb, 8, 1, 0], getpos("'D"))
388
389  call delete('Xviminfo')
390  exe 'bwipe ' . bufa
391  exe 'bwipe ' . bufb
392endfunc
393
394func Test_viminfo_jumplist()
395  split testbuf
396  clearjumps
397  call setline(1, ['time 05', 'time 10', 'time 15', 'time 20', 'time 30', 'last pos'])
398  call cursor(2, 1)
399  call test_settime(10)
400  exe "normal /20\r"
401  call test_settime(20)
402  exe "normal /30\r"
403  call test_settime(30)
404  exe "normal /last pos\r"
405  wviminfo Xviminfo
406
407  clearjumps
408  call cursor(1, 1)
409  call test_settime(5)
410  exe "normal /15\r"
411  call test_settime(15)
412  exe "normal /last pos\r"
413  call test_settime(40)
414  exe "normal ?30\r"
415  rviminfo Xviminfo
416
417  call assert_equal('time 30', getline('.'))
418  exe "normal \<C-O>"
419  call assert_equal('last pos', getline('.'))
420  exe "normal \<C-O>"
421  " duplicate for 'time 30' was removed
422  call assert_equal('time 20', getline('.'))
423  exe "normal \<C-O>"
424  call assert_equal('time 15', getline('.'))
425  exe "normal \<C-O>"
426  call assert_equal('time 10', getline('.'))
427  exe "normal \<C-O>"
428  call assert_equal('time 05', getline('.'))
429
430  clearjumps
431  call cursor(1, 1)
432  call test_settime(5)
433  exe "normal /15\r"
434  call test_settime(15)
435  exe "normal /last pos\r"
436  call test_settime(40)
437  exe "normal ?30\r"
438  " Test merge when writing
439  wviminfo Xviminfo
440  clearjumps
441  rviminfo Xviminfo
442
443  let last_line = line('.')
444  exe "normal \<C-O>"
445  call assert_equal('time 30', getline('.'))
446  exe "normal \<C-O>"
447  call assert_equal('last pos', getline('.'))
448  exe "normal \<C-O>"
449  " duplicate for 'time 30' was removed
450  call assert_equal('time 20', getline('.'))
451  exe "normal \<C-O>"
452  call assert_equal('time 15', getline('.'))
453  exe "normal \<C-O>"
454  call assert_equal('time 10', getline('.'))
455  exe "normal \<C-O>"
456  call assert_equal('time 05', getline('.'))
457
458  " Test with jumplist full.
459  clearjumps
460  call setline(1, repeat(['match here'], 101))
461  call cursor(1, 1)
462  call test_settime(10)
463  for i in range(100)
464    exe "normal /here\r"
465  endfor
466  rviminfo Xviminfo
467
468  " must be newest mark that comes from viminfo.
469  exe "normal \<C-O>"
470  call assert_equal(last_line, line('.'))
471
472  bwipe!
473  call delete('Xviminfo')
474endfunc
475
476func Test_viminfo_encoding()
477  set enc=latin1
478  call histdel(':')
479  call histadd(':', "echo '\xe9'")
480  wviminfo Xviminfo
481
482  set fencs=utf-8,latin1
483  set enc=utf-8
484  sp Xviminfo
485  call assert_equal('latin1', &fenc)
486  close
487
488  call histdel(':')
489  rviminfo Xviminfo
490  call assert_equal("echo 'é'", histget(':', -1))
491
492  call delete('Xviminfo')
493endfunc
494
495func Test_viminfo_bad_syntax()
496  let lines = []
497  call add(lines, '|<')  " empty continuation line
498  call add(lines, '|234234234234234324,nothing')
499  call add(lines, '|1+"no comma"')
500  call add(lines, '|1,2,3,4,5,6,7')  " too many items
501  call add(lines, '|1,"string version"')
502  call add(lines, '|1,>x') " bad continuation line
503  call add(lines, '|1,"x') " missing quote
504  call add(lines, '|1,"x\') " trailing backslash
505  call add(lines, '|1,,,,') "trailing comma
506  call add(lines, '|1,>234') " trailing continuation line
507  call writefile(lines, 'Xviminfo')
508  rviminfo Xviminfo
509
510  call delete('Xviminfo')
511endfunc
512
513func Test_viminfo_file_marks()
514  silent! bwipe test_viminfo.vim
515  silent! bwipe Xviminfo
516
517  call test_settime(10)
518  edit ten
519  call test_settime(25)
520  edit again
521  call test_settime(30)
522  edit thirty
523  wviminfo Xviminfo
524
525  call test_settime(20)
526  edit twenty
527  call test_settime(35)
528  edit again
529  call test_settime(40)
530  edit fourty
531  wviminfo Xviminfo
532
533  sp Xviminfo
534  1
535  for name in ['fourty', 'again', 'thirty', 'twenty', 'ten']
536    /^>
537    call assert_equal(name, substitute(getline('.'), '.*/', '', ''))
538  endfor
539  close
540
541  call delete('Xviminfo')
542endfunc
543
544func Test_viminfo_file_mark_tabclose()
545  tabnew Xtestfileintab
546  call setline(1, ['a','b','c','d','e'])
547  4
548  q!
549  wviminfo Xviminfo
550  sp Xviminfo
551  /^> .*Xtestfileintab
552  let lnum = line('.')
553  while 1
554    if lnum == line('$')
555      call assert_report('mark not found in Xtestfileintab')
556      break
557    endif
558    let lnum += 1
559    let line = getline(lnum)
560    if line == ''
561      call assert_report('mark not found in Xtestfileintab')
562      break
563    endif
564    if line =~ "^\t\""
565      call assert_equal('4', substitute(line, ".*\"\t\\(\\d\\).*", '\1', ''))
566      break
567    endif
568  endwhile
569
570  call delete('Xviminfo')
571  silent! bwipe Xtestfileintab
572endfunc
573
574func Test_viminfo_file_mark_zero_time()
575  let lines = [
576	\ '# Viminfo version',
577	\ '|1,4',
578	\ '',
579	\ '*encoding=utf-8',
580	\ '',
581	\ '# File marks:',
582	\ "'B  1  0  /tmp/nothing",
583	\ '|4,66,1,0,0,"/tmp/nothing"',
584	\ "",
585	\ ]
586  call writefile(lines, 'Xviminfo')
587  delmark B
588  rviminfo Xviminfo
589  call delete('Xviminfo')
590  call assert_equal(1, line("'B"))
591  delmark B
592endfunc
593
594" Test for saving and restoring file marks in unloaded buffers
595func Test_viminfo_file_mark_unloaded_buf()
596  let save_viminfo = &viminfo
597  set viminfo&vim
598  call writefile(repeat(['vim'], 10), 'Xfile1')
599  %bwipe
600  edit! Xfile1
601  call setpos("'u", [0, 3, 1, 0])
602  call setpos("'v", [0, 5, 1, 0])
603  enew
604  wviminfo Xviminfo
605  %bwipe
606  edit Xfile1
607  rviminfo! Xviminfo
608  call assert_equal([0, 3, 1, 0], getpos("'u"))
609  call assert_equal([0, 5, 1, 0], getpos("'v"))
610  %bwipe
611  call delete('Xfile1')
612  call delete('Xviminfo')
613  let &viminfo = save_viminfo
614endfunc
615
616func Test_viminfo_oldfiles()
617  let v:oldfiles = []
618  let lines = [
619	\ '# comment line',
620	\ '*encoding=utf-8',
621	\ '',
622	\ ':h viminfo',
623	\ '?/session',
624	\ '=myvar',
625	\ '@123',
626	\ '',
627	\ "'E  2  0  /tmp/nothing",
628	\ '',
629	\ "> /tmp/file_one.txt",
630	\ "\t\"\t11\t0",
631	\ "",
632	\ "> /tmp/file_two.txt",
633	\ "\t\"\t11\t0",
634	\ "",
635	\ "> /tmp/another.txt",
636	\ "\t\"\t11\t0",
637	\ "",
638	\ ]
639  call writefile(lines, 'Xviminfo')
640  delmark E
641  rviminfo! Xviminfo
642  call delete('Xviminfo')
643
644  call assert_equal('h viminfo', histget(':'))
645  call assert_equal('session', histget('/'))
646  call assert_equal('myvar', histget('='))
647  call assert_equal('123', histget('@'))
648  call assert_equal(2, line("'E"))
649  call assert_equal(['1: /tmp/file_one.txt', '2: /tmp/file_two.txt', '3: /tmp/another.txt'], filter(split(execute('oldfiles'), "\n"), {i, v -> v =~ '/tmp/'}))
650  call assert_equal(['1: /tmp/file_one.txt', '2: /tmp/file_two.txt'], filter(split(execute('filter file_ oldfiles'), "\n"), {i, v -> v =~ '/tmp/'}))
651  call assert_equal(['3: /tmp/another.txt'], filter(split(execute('filter /another/ oldfiles'), "\n"), {i, v -> v =~ '/tmp/'}))
652
653  new
654  call feedkeys("3\<CR>", 't')
655  browse oldfiles
656  call assert_equal("/tmp/another.txt", expand("%"))
657  bwipe
658  delmark E
659endfunc
660
661" Test for storing and restoring buffer list in 'viminfo'
662func Test_viminfo_bufferlist()
663  " If there are arguments, then :rviminfo doesn't read the buffer list.
664  " Need to delete all the arguments for :rviminfo to work.
665  %argdelete
666
667  edit Xfile1
668  edit Xfile2
669  set viminfo-=%
670  wviminfo Xviminfo
671  %bwipe
672  rviminfo Xviminfo
673  call assert_equal(1, len(getbufinfo()))
674
675  edit Xfile1
676  edit Xfile2
677  set viminfo^=%
678  wviminfo Xviminfo
679  %bwipe
680  rviminfo Xviminfo
681  let l = getbufinfo()
682  call assert_equal(3, len(l))
683  call assert_equal('Xfile1', bufname(l[1].bufnr))
684  call assert_equal('Xfile2', bufname(l[2].bufnr))
685
686  call delete('Xviminfo')
687  %bwipe
688  set viminfo-=%
689endfunc
690
691" Test for errors in a viminfo file
692func Test_viminfo_error()
693  " Non-existing viminfo files
694  call assert_fails('rviminfo xyz', 'E195:')
695
696  " Illegal starting character
697  call writefile(["a 123"], 'Xviminfo')
698  call assert_fails('rv Xviminfo', 'E575:')
699
700  " Illegal register name in the viminfo file
701  call writefile(['"@	LINE	0'], 'Xviminfo')
702  call assert_fails('rv Xviminfo', 'E577:')
703
704  " Invalid file mark line
705  call writefile(['>', '@'], 'Xviminfo')
706  call assert_fails('rv Xviminfo', 'E576:')
707
708  " Too many errors in viminfo file
709  call writefile(repeat(["a 123"], 15), 'Xviminfo')
710  call assert_fails('rv Xviminfo', 'E575:')
711
712  call writefile(['>'] + repeat(['@'], 10), 'Xviminfo')
713  call assert_fails('rv Xviminfo', 'E576:')
714
715  call writefile(repeat(['"@'], 15), 'Xviminfo')
716  call assert_fails('rv Xviminfo', 'E577:')
717
718  call delete('Xviminfo')
719endfunc
720
721" Test for saving and restoring last substitute string in viminfo
722func Test_viminfo_lastsub()
723  enew
724  call append(0, "blue blue blue")
725  call cursor(1, 1)
726  s/blue/green/
727  wviminfo Xviminfo
728  s/blue/yellow/
729  rviminfo! Xviminfo
730  &
731  call assert_equal("green yellow green", getline(1))
732  enew!
733  call delete('Xviminfo')
734endfunc
735
736" Test saving and restoring the register values using the older method
737func Test_viminfo_registers_old()
738  let lines = [
739	\ '# Viminfo version',
740	\ '|1,1',
741	\ '',
742	\ '*encoding=utf-8',
743	\ '',
744	\ '# Registers:',
745	\ '""0 CHAR  0',
746	\ '	Vim',
747	\ '"a  CHAR  0',
748	\ '	red',
749	\ '"m@ CHAR  0',
750	\ "	:echo 'Hello'\<CR>",
751	\ "",
752	\ ]
753  call writefile(lines, 'Xviminfo')
754  let @a = 'one'
755  let @b = 'two'
756  let @m = 'three'
757  let @" = 'four'
758  let @t = ":echo 'Unix'\<CR>"
759  silent! normal @t
760  rviminfo! Xviminfo
761  call assert_equal('red', getreg('a'))
762  call assert_equal('two', getreg('b'))
763  call assert_equal(":echo 'Hello'\<CR>", getreg('m'))
764  call assert_equal('Vim', getreg('"'))
765  call assert_equal("\nHello", execute('normal @@'))
766  call delete('Xviminfo')
767  let @" = ''
768endfunc
769
770" Test for saving and restoring large number of lines in a register
771func Test_viminfo_large_register()
772  let save_viminfo = &viminfo
773  set viminfo&vim
774  set viminfo-=<50
775  set viminfo+=<200
776  let lines = ['"r	CHAR	0']
777  call extend(lines, repeat(["\tsun is rising"], 200))
778  call writefile(lines, 'Xviminfo')
779  let @r = ''
780  rviminfo! Xviminfo
781  call assert_equal(join(repeat(["sun is rising"], 200), "\n"), @r)
782  call delete('Xviminfo')
783  let @r = ''
784  let &viminfo = save_viminfo
785endfunc
786
787" Test for setting 'viminfofile' to NONE
788func Test_viminfofile_none()
789  let save_vif = &viminfofile
790  set viminfofile=NONE
791  wviminfo Xviminfo
792  call assert_false(filereadable('Xviminfo'))
793  call writefile([''], 'Xviminfo')
794  call assert_fails('rviminfo Xviminfo', 'E195:')
795  call delete('Xviminfo')
796  let &viminfofile = save_vif
797endfunc
798
799" Test for an unwritable and unreadble 'viminfo' file
800func Test_viminfo_perm()
801  CheckUnix
802  CheckNotRoot
803  call writefile([''], 'Xviminfo')
804  call setfperm('Xviminfo', 'r-x------')
805  call assert_fails('wviminfo Xviminfo', 'E137:')
806  call setfperm('Xviminfo', '--x------')
807  call assert_fails('rviminfo Xviminfo', 'E195:')
808  call delete('Xviminfo')
809
810  " Try to write the viminfo to a directory
811  call mkdir('Xdir')
812  call assert_fails('wviminfo Xdir', 'E137:')
813  call delete('Xdir', 'rf')
814endfunc
815
816" Test for writing to an existing viminfo file merges the file marks
817func XTest_viminfo_marks_merge()
818  let save_viminfo = &viminfo
819  set viminfo&vim
820  set viminfo^=%
821  enew
822  %argdelete
823  %bwipe
824
825  call writefile(repeat(['editor'], 10), 'Xbufa')
826  call writefile(repeat(['Vim'], 10), 'Xbufb')
827
828  " set marks in buffers
829  call test_settime(10)
830  edit Xbufa
831  4mark a
832  wviminfo Xviminfo
833  edit Xbufb
834  4mark b
835  wviminfo Xviminfo
836  %bwipe
837
838  " set marks in buffers again
839  call test_settime(20)
840  edit Xbufb
841  6mark b
842  wviminfo Xviminfo
843  edit Xbufa
844  6mark a
845  wviminfo Xviminfo
846  %bwipe
847
848  " Load the buffer and check the marks
849  edit Xbufa
850  rviminfo! Xviminfo
851  call assert_equal(6, line("'a"))
852  edit Xbufb
853  rviminfo! Xviminfo
854  call assert_equal(6, line("'b"))
855
856  " cleanup
857  %bwipe
858  call delete('Xviminfo')
859  call delete('Xbufa')
860  call delete('Xbufb')
861  call test_settime(0)
862  let &viminfo=save_viminfo
863endfunc
864
865" Test for errors in setting 'viminfo'
866func Test_viminfo_option_error()
867  " Missing number
868  call assert_fails('set viminfo=\"', 'E526:')
869  for c in split("'/:<@s", '\zs')
870    call assert_fails('set viminfo=' .. c, 'E526:')
871  endfor
872
873  " Missing comma
874  call assert_fails('set viminfo=%10!', 'E527:')
875  call assert_fails('set viminfo=!%10', 'E527:')
876  call assert_fails('set viminfo=h%10', 'E527:')
877  call assert_fails('set viminfo=c%10', 'E527:')
878  call assert_fails('set viminfo=:10%10', 'E527:')
879
880  " Missing ' setting
881  call assert_fails('set viminfo=%10', 'E528:')
882endfunc
883
884func Test_viminfo_oldfiles_newfile()
885  CheckRunVimInTerminal
886
887  let save_viminfo = &viminfo
888  let save_viminfofile = &viminfofile
889  set viminfo&vim
890  let v:oldfiles = []
891  let commands =<< trim [CODE]
892    set viminfofile=Xviminfofile
893    set viminfo&vim
894    w! Xnew-file.txt
895    qall
896  [CODE]
897  call writefile(commands, 'Xviminfotest')
898  let buf = RunVimInTerminal('-S Xviminfotest', #{wait_for_ruler: 0})
899  call WaitForAssert({-> assert_equal("finished", term_getstatus(buf))})
900
901  let &viminfofile = 'Xviminfofile'
902  rviminfo! Xviminfofile
903  call assert_match('Xnew-file.txt$', v:oldfiles[0])
904  call assert_equal(1, len(v:oldfiles))
905  call delete('Xviminfofile')
906  call delete('Xviminfotest')
907  call delete('Xnew-file.txt')
908  let &viminfo = save_viminfo
909  let &viminfofile = save_viminfofile
910endfunc
911
912" vim: shiftwidth=2 sts=2 expandtab
913