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