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