xref: /vim-8.2.3635/src/testdir/test_viminfo.vim (revision 5be4ceec)
1" Test for reading and writing .viminfo
2
3function Test_viminfo_read_and_write()
4  " First clear 'history', so that "hislen" is zero.  Then set it again,
5  " simulating Vim starting up.
6  set history=0
7  wviminfo Xviminfo
8  set history=1000
9
10  call histdel(':')
11  let lines = [
12	\ '# comment line',
13	\ '*encoding=utf-8',
14	\ '~MSle0~/asdf',
15	\ '|copied as-is',
16	\ '|and one more',
17	\ ]
18  call writefile(lines, 'Xviminfo')
19  rviminfo Xviminfo
20  call assert_equal('asdf', @/)
21
22  wviminfo Xviminfo
23  let lines = readfile('Xviminfo')
24  let done = 0
25  for line in lines
26    if line[0] == '|' && line !~ '^|[234],' && line !~ '^|<'
27      if done == 0
28	call assert_equal('|1,4', line)
29      elseif done == 1
30	call assert_equal('|copied as-is', line)
31      elseif done == 2
32	call assert_equal('|and one more', line)
33      endif
34      let done += 1
35    endif
36  endfor
37  call assert_equal(3, done)
38
39  call delete('Xviminfo')
40endfunc
41
42func Test_global_vars()
43  let test_dict = {'foo': 1, 'bar': 0, 'longvarible': 1000}
44  let g:MY_GLOBAL_DICT = test_dict
45  " store a really long list, so line wrapping will occur in viminfo file
46  let test_list = range(1,100)
47  let g:MY_GLOBAL_LIST = test_list
48  let test_blob = 0z00112233445566778899aabbccddeeff
49  let g:MY_GLOBAL_BLOB = test_blob
50  let test_false = v:false
51  let g:MY_GLOBAL_FALSE = test_false
52  let test_true = v:true
53  let g:MY_GLOBAL_TRUE = test_true
54  let test_null = v:null
55  let g:MY_GLOBAL_NULL = test_null
56  let test_none = v:none
57  let g:MY_GLOBAL_NONE = test_none
58
59  set viminfo='100,<50,s10,h,!,nviminfo
60  wv! Xviminfo
61
62  unlet g:MY_GLOBAL_DICT
63  unlet g:MY_GLOBAL_LIST
64  unlet g:MY_GLOBAL_BLOB
65  unlet g:MY_GLOBAL_FALSE
66  unlet g:MY_GLOBAL_TRUE
67  unlet g:MY_GLOBAL_NULL
68  unlet g:MY_GLOBAL_NONE
69
70  rv! Xviminfo
71  call assert_equal(test_dict, g:MY_GLOBAL_DICT)
72  call assert_equal(test_list, g:MY_GLOBAL_LIST)
73  call assert_equal(test_blob, g:MY_GLOBAL_BLOB)
74  call assert_equal(test_false, g:MY_GLOBAL_FALSE)
75  call assert_equal(test_true, g:MY_GLOBAL_TRUE)
76  call assert_equal(test_null, g:MY_GLOBAL_NULL)
77  call assert_equal(test_none, g:MY_GLOBAL_NONE)
78
79  call delete('Xviminfo')
80  set viminfo-=!
81endfunc
82
83func Test_cmdline_history()
84  call histdel(':')
85  call test_settime(11)
86  call histadd(':', "echo 'one'")
87  call test_settime(12)
88  " split into two lines
89  let long800 = repeat(" 'eight'", 100)
90  call histadd(':', "echo " . long800)
91  call test_settime(13)
92  " split into three lines
93  let long1400 = repeat(" 'fourteeeeen'", 100)
94  call histadd(':', "echo " . long1400)
95  wviminfo Xviminfo
96  let lines = readfile('Xviminfo')
97  let done_colon = 0
98  let done_bar = 0
99  let lnum = 0
100  while lnum < len(lines)
101    let line = lines[lnum] | let lnum += 1
102    if line[0] == ':'
103      if done_colon == 0
104	call assert_equal(":\x161408", line)
105	let line = lines[lnum] | let lnum += 1
106	call assert_equal('<echo ' . long1400, line)
107      elseif done_colon == 1
108	call assert_equal(":\x16808", line)
109	let line = lines[lnum] | let lnum += 1
110	call assert_equal("<echo " . long800, line)
111      elseif done_colon == 2
112	call assert_equal(":echo 'one'", line)
113      endif
114      let done_colon += 1
115    elseif line[0:4] == '|2,0,'
116      if done_bar == 0
117	call assert_equal("|2,0,13,,>1407", line)
118	let line = lines[lnum] | let lnum += 1
119	call assert_equal('|<"echo ' . long1400[0:484], line)
120	let line = lines[lnum] | let lnum += 1
121	call assert_equal('|<' . long1400[485:974], line)
122	let line = lines[lnum] | let lnum += 1
123	call assert_equal('|<' . long1400[975:] . '"', line)
124      elseif done_bar == 1
125	call assert_equal('|2,0,12,,>807', line)
126	let line = lines[lnum] | let lnum += 1
127	call assert_equal('|<"echo ' . long800[0:484], line)
128	let line = lines[lnum] | let lnum += 1
129	call assert_equal('|<' . long800[485:] . '"', line)
130      elseif done_bar == 2
131	call assert_equal("|2,0,11,,\"echo 'one'\"", line)
132      endif
133      let done_bar += 1
134    endif
135  endwhile
136  call assert_equal(3, done_colon)
137  call assert_equal(3, done_bar)
138
139  call histdel(':')
140  rviminfo Xviminfo
141  call assert_equal("echo " . long1400, histget(':', -1))
142  call assert_equal("echo " . long800, histget(':', -2))
143  call assert_equal("echo 'one'", histget(':', -3))
144
145  call delete('Xviminfo')
146endfunc
147
148func Test_cmdline_history_order()
149  call histdel(':')
150  call test_settime(11)
151  call histadd(':', "echo '11'")
152  call test_settime(22)
153  call histadd(':', "echo '22'")
154  call test_settime(33)
155  call histadd(':', "echo '33'")
156  wviminfo Xviminfo
157
158  call histdel(':')
159  " items go in between
160  call test_settime(15)
161  call histadd(':', "echo '15'")
162  call test_settime(27)
163  call histadd(':', "echo '27'")
164
165  rviminfo Xviminfo
166  call assert_equal("echo '33'", histget(':', -1))
167  call assert_equal("echo '27'", histget(':', -2))
168  call assert_equal("echo '22'", histget(':', -3))
169  call assert_equal("echo '15'", histget(':', -4))
170  call assert_equal("echo '11'", histget(':', -5))
171
172  call histdel(':')
173  " items go before and after
174  eval 8->test_settime()
175  call histadd(':', "echo '8'")
176  call test_settime(39)
177  call histadd(':', "echo '39'")
178
179  rviminfo Xviminfo
180  call assert_equal("echo '39'", histget(':', -1))
181  call assert_equal("echo '33'", histget(':', -2))
182  call assert_equal("echo '22'", histget(':', -3))
183  call assert_equal("echo '11'", histget(':', -4))
184  call assert_equal("echo '8'", histget(':', -5))
185
186  " Check sorting works when writing with merge.
187  call histdel(':')
188  call test_settime(8)
189  call histadd(':', "echo '8'")
190  call test_settime(15)
191  call histadd(':', "echo '15'")
192  call test_settime(27)
193  call histadd(':', "echo '27'")
194  call test_settime(39)
195  call histadd(':', "echo '39'")
196  wviminfo Xviminfo
197
198  call histdel(':')
199  rviminfo Xviminfo
200  call assert_equal("echo '39'", histget(':', -1))
201  call assert_equal("echo '33'", histget(':', -2))
202  call assert_equal("echo '27'", histget(':', -3))
203  call assert_equal("echo '22'", histget(':', -4))
204  call assert_equal("echo '15'", histget(':', -5))
205  call assert_equal("echo '11'", histget(':', -6))
206  call assert_equal("echo '8'", histget(':', -7))
207
208  call delete('Xviminfo')
209endfunc
210
211func Test_viminfo_registers()
212  call test_settime(8)
213  call setreg('a', "eight", 'c')
214  call test_settime(20)
215  call setreg('b', ["twenty", "again"], 'l')
216  call test_settime(40)
217  call setreg('c', ["four", "agai"], 'b4')
218  let l = []
219  set viminfo='100,<600,s10,h,!,nviminfo
220  for i in range(500)
221    call add(l, 'something')
222  endfor
223  call setreg('d', l, 'l')
224  wviminfo Xviminfo
225
226  call test_settime(10)
227  call setreg('a', '', 'b10')
228  call test_settime(15)
229  call setreg('b', 'drop')
230  call test_settime(50)
231  call setreg('c', 'keep', 'l')
232  call test_settime(30)
233  call setreg('d', 'drop', 'l')
234  rviminfo Xviminfo
235
236  call assert_equal("", getreg('a'))
237  call assert_equal("\<C-V>10", getregtype('a'))
238  call assert_equal("twenty\nagain\n", getreg('b'))
239  call assert_equal("V", getregtype('b'))
240  call assert_equal("keep\n", getreg('c'))
241  call assert_equal("V", getregtype('c'))
242  call assert_equal(l, getreg('d', 1, 1))
243  call assert_equal("V", getregtype('d'))
244
245  " Length around 440 switches to line continuation.
246  let len = 434
247  while len < 445
248    let s = repeat('a', len)
249    call setreg('"', s)
250    wviminfo Xviminfo
251    call setreg('"', '')
252    rviminfo Xviminfo
253    call assert_equal(s, getreg('"'), 'wrong register at length: ' . len)
254
255    let len += 1
256  endwhile
257
258  call delete('Xviminfo')
259endfunc
260
261func Test_viminfo_marks()
262  sp bufa
263  let bufa = bufnr('%')
264  sp bufb
265  let bufb = bufnr('%')
266
267  call test_settime(8)
268  call setpos("'A", [bufa, 1, 1, 0])
269  call test_settime(20)
270  call setpos("'B", [bufb, 9, 1, 0])
271  call setpos("'C", [bufa, 7, 1, 0])
272
273  delmark 0-9
274  call test_settime(25)
275  call setpos("'1", [bufb, 12, 1, 0])
276  call test_settime(35)
277  call setpos("'0", [bufa, 11, 1, 0])
278
279  call test_settime(45)
280  wviminfo Xviminfo
281
282  " Writing viminfo inserts the '0 mark.
283  call assert_equal([bufb, 1, 1, 0], getpos("'0"))
284  call assert_equal([bufa, 11, 1, 0], getpos("'1"))
285  call assert_equal([bufb, 12, 1, 0], getpos("'2"))
286
287  call test_settime(4)
288  call setpos("'A", [bufa, 9, 1, 0])
289  call test_settime(30)
290  call setpos("'B", [bufb, 2, 3, 0])
291  delmark C
292
293  delmark 0-9
294  call test_settime(30)
295  call setpos("'1", [bufb, 22, 1, 0])
296  call test_settime(55)
297  call setpos("'0", [bufa, 21, 1, 0])
298
299  rviminfo Xviminfo
300
301  call assert_equal([bufa, 1, 1, 0], getpos("'A"))
302  call assert_equal([bufb, 2, 3, 0], getpos("'B"))
303  call assert_equal([bufa, 7, 1, 0], getpos("'C"))
304
305  " numbered marks are merged
306  call assert_equal([bufa, 21, 1, 0], getpos("'0"))  " time 55
307  call assert_equal([bufb, 1, 1, 0], getpos("'1"))  " time 45
308  call assert_equal([bufa, 11, 1, 0], getpos("'2")) " time 35
309  call assert_equal([bufb, 22, 1, 0], getpos("'3")) " time 30
310  call assert_equal([bufb, 12, 1, 0], getpos("'4")) " time 25
311
312  call delete('Xviminfo')
313  exe 'bwipe ' . bufa
314  exe 'bwipe ' . bufb
315endfunc
316
317func Test_viminfo_jumplist()
318  split testbuf
319  clearjumps
320  call setline(1, ['time 05', 'time 10', 'time 15', 'time 20', 'time 30', 'last pos'])
321  call cursor(2, 1)
322  call test_settime(10)
323  exe "normal /20\r"
324  call test_settime(20)
325  exe "normal /30\r"
326  call test_settime(30)
327  exe "normal /last pos\r"
328  wviminfo Xviminfo
329
330  clearjumps
331  call cursor(1, 1)
332  call test_settime(5)
333  exe "normal /15\r"
334  call test_settime(15)
335  exe "normal /last pos\r"
336  call test_settime(40)
337  exe "normal ?30\r"
338  rviminfo Xviminfo
339
340  call assert_equal('time 30', getline('.'))
341  exe "normal \<C-O>"
342  call assert_equal('last pos', getline('.'))
343  exe "normal \<C-O>"
344  " duplicate for 'time 30' was removed
345  call assert_equal('time 20', getline('.'))
346  exe "normal \<C-O>"
347  call assert_equal('time 15', getline('.'))
348  exe "normal \<C-O>"
349  call assert_equal('time 10', getline('.'))
350  exe "normal \<C-O>"
351  call assert_equal('time 05', getline('.'))
352
353  clearjumps
354  call cursor(1, 1)
355  call test_settime(5)
356  exe "normal /15\r"
357  call test_settime(15)
358  exe "normal /last pos\r"
359  call test_settime(40)
360  exe "normal ?30\r"
361  " Test merge when writing
362  wviminfo Xviminfo
363  clearjumps
364  rviminfo Xviminfo
365
366  let last_line = line('.')
367  exe "normal \<C-O>"
368  call assert_equal('time 30', getline('.'))
369  exe "normal \<C-O>"
370  call assert_equal('last pos', getline('.'))
371  exe "normal \<C-O>"
372  " duplicate for 'time 30' was removed
373  call assert_equal('time 20', getline('.'))
374  exe "normal \<C-O>"
375  call assert_equal('time 15', getline('.'))
376  exe "normal \<C-O>"
377  call assert_equal('time 10', getline('.'))
378  exe "normal \<C-O>"
379  call assert_equal('time 05', getline('.'))
380
381  " Test with jumplist full.
382  clearjumps
383  call setline(1, repeat(['match here'], 101))
384  call cursor(1, 1)
385  call test_settime(10)
386  for i in range(100)
387    exe "normal /here\r"
388  endfor
389  rviminfo Xviminfo
390
391  " must be newest mark that comes from viminfo.
392  exe "normal \<C-O>"
393  call assert_equal(last_line, line('.'))
394
395  bwipe!
396  call delete('Xviminfo')
397endfunc
398
399func Test_viminfo_encoding()
400  set enc=latin1
401  call histdel(':')
402  call histadd(':', "echo '\xe9'")
403  wviminfo Xviminfo
404
405  set fencs=utf-8,latin1
406  set enc=utf-8
407  sp Xviminfo
408  call assert_equal('latin1', &fenc)
409  close
410
411  call histdel(':')
412  rviminfo Xviminfo
413  call assert_equal("echo 'é'", histget(':', -1))
414
415  call delete('Xviminfo')
416endfunc
417
418func Test_viminfo_bad_syntax()
419  let lines = []
420  call add(lines, '|<')  " empty continuation line
421  call add(lines, '|234234234234234324,nothing')
422  call add(lines, '|1+"no comma"')
423  call add(lines, '|1,2,3,4,5,6,7')  " too many items
424  call add(lines, '|1,"string version"')
425  call add(lines, '|1,>x') " bad continuation line
426  call add(lines, '|1,"x') " missing quote
427  call add(lines, '|1,"x\') " trailing backslash
428  call add(lines, '|1,,,,') "trailing comma
429  call add(lines, '|1,>234') " trailing continuation line
430  call writefile(lines, 'Xviminfo')
431  rviminfo Xviminfo
432
433  call delete('Xviminfo')
434endfunc
435
436func Test_viminfo_file_marks()
437  silent! bwipe test_viminfo.vim
438  silent! bwipe Xviminfo
439
440  call test_settime(10)
441  edit ten
442  call test_settime(25)
443  edit again
444  call test_settime(30)
445  edit thirty
446  wviminfo Xviminfo
447
448  call test_settime(20)
449  edit twenty
450  call test_settime(35)
451  edit again
452  call test_settime(40)
453  edit fourty
454  wviminfo Xviminfo
455
456  sp Xviminfo
457  1
458  for name in ['fourty', 'again', 'thirty', 'twenty', 'ten']
459    /^>
460    call assert_equal(name, substitute(getline('.'), '.*/', '', ''))
461  endfor
462  close
463
464  call delete('Xviminfo')
465endfunc
466
467func Test_viminfo_file_mark_tabclose()
468  tabnew Xtestfileintab
469  call setline(1, ['a','b','c','d','e'])
470  4
471  q!
472  wviminfo Xviminfo
473  sp Xviminfo
474  /^> .*Xtestfileintab
475  let lnum = line('.')
476  while 1
477    if lnum == line('$')
478      call assert_report('mark not found in Xtestfileintab')
479      break
480    endif
481    let lnum += 1
482    let line = getline(lnum)
483    if line == ''
484      call assert_report('mark not found in Xtestfileintab')
485      break
486    endif
487    if line =~ "^\t\""
488      call assert_equal('4', substitute(line, ".*\"\t\\(\\d\\).*", '\1', ''))
489      break
490    endif
491  endwhile
492
493  call delete('Xviminfo')
494  silent! bwipe Xtestfileintab
495endfunc
496
497func Test_viminfo_file_mark_zero_time()
498  let lines = [
499	\ '# Viminfo version',
500	\ '|1,4',
501	\ '',
502	\ '*encoding=utf-8',
503	\ '',
504	\ '# File marks:',
505	\ "'B  1  0  /tmp/nothing",
506	\ '|4,66,1,0,0,"/tmp/nothing"',
507	\ "",
508	\ ]
509  call writefile(lines, 'Xviminfo')
510  delmark B
511  rviminfo Xviminfo
512  call delete('Xviminfo')
513  call assert_equal(1, line("'B"))
514  delmark B
515endfunc
516
517func Test_viminfo_oldfiles()
518  let v:oldfiles = []
519  let lines = [
520	\ '# comment line',
521	\ '*encoding=utf-8',
522	\ '',
523	\ "> /tmp/file_one.txt",
524	\ "\t\"\t11\t0",
525	\ "",
526	\ "> /tmp/file_two.txt",
527	\ "\t\"\t11\t0",
528	\ "",
529	\ "> /tmp/another.txt",
530	\ "\t\"\t11\t0",
531	\ "",
532	\ ]
533  call writefile(lines, 'Xviminfo')
534  rviminfo! Xviminfo
535  call delete('Xviminfo')
536
537  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/'}))
538  call assert_equal(['1: /tmp/file_one.txt', '2: /tmp/file_two.txt'], filter(split(execute('filter file_ oldfiles'), "\n"), {i, v -> v =~ '/tmp/'}))
539  call assert_equal(['3: /tmp/another.txt'], filter(split(execute('filter /another/ oldfiles'), "\n"), {i, v -> v =~ '/tmp/'}))
540
541  new
542  call feedkeys("3\<CR>", 't')
543  browse oldfiles
544  call assert_equal("/tmp/another.txt", expand("%"))
545  bwipe
546endfunc
547