blob: a9dc8fd31a33a48669b00f318b498aa7c07b92a3 [file] [log] [blame]
Bram Moolenaarb20e3342016-01-18 23:29:01 +01001" Test for reading and writing .viminfo
2
Bram Moolenaar6bd1d772019-10-09 22:01:25 +02003source check.vim
4
Bram Moolenaar156919f2016-10-15 20:46:20 +02005function Test_viminfo_read_and_write()
Bram Moolenaar26b654a2019-07-22 20:50:17 +02006 " First clear 'history', so that "hislen" is zero. Then set it again,
7 " simulating Vim starting up.
8 set history=0
9 wviminfo Xviminfo
10 set history=1000
11
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020012 call histdel(':')
Bram Moolenaarb20e3342016-01-18 23:29:01 +010013 let lines = [
14 \ '# comment line',
15 \ '*encoding=utf-8',
16 \ '~MSle0~/asdf',
17 \ '|copied as-is',
18 \ '|and one more',
19 \ ]
20 call writefile(lines, 'Xviminfo')
21 rviminfo Xviminfo
22 call assert_equal('asdf', @/)
23
24 wviminfo Xviminfo
25 let lines = readfile('Xviminfo')
26 let done = 0
27 for line in lines
Bram Moolenaar156919f2016-10-15 20:46:20 +020028 if line[0] == '|' && line !~ '^|[234],' && line !~ '^|<'
Bram Moolenaarb20e3342016-01-18 23:29:01 +010029 if done == 0
Bram Moolenaar2d358992016-06-12 21:20:54 +020030 call assert_equal('|1,4', line)
Bram Moolenaarb20e3342016-01-18 23:29:01 +010031 elseif done == 1
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020032 call assert_equal('|copied as-is', line)
33 elseif done == 2
Bram Moolenaarb20e3342016-01-18 23:29:01 +010034 call assert_equal('|and one more', line)
35 endif
36 let done += 1
37 endif
38 endfor
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020039 call assert_equal(3, done)
Bram Moolenaarb20e3342016-01-18 23:29:01 +010040
41 call delete('Xviminfo')
42endfunc
43
44func Test_global_vars()
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +020045 let g:MY_GLOBAL_STRING = "Vim Editor"
46 let g:MY_GLOBAL_NUM = 345
47 let g:MY_GLOBAL_FLOAT = 3.14
Bram Moolenaarb20e3342016-01-18 23:29:01 +010048 let test_dict = {'foo': 1, 'bar': 0, 'longvarible': 1000}
49 let g:MY_GLOBAL_DICT = test_dict
50 " store a really long list, so line wrapping will occur in viminfo file
51 let test_list = range(1,100)
52 let g:MY_GLOBAL_LIST = test_list
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +010053 let test_blob = 0z00112233445566778899aabbccddeeff
54 let g:MY_GLOBAL_BLOB = test_blob
55 let test_false = v:false
56 let g:MY_GLOBAL_FALSE = test_false
57 let test_true = v:true
58 let g:MY_GLOBAL_TRUE = test_true
59 let test_null = v:null
60 let g:MY_GLOBAL_NULL = test_null
61 let test_none = v:none
62 let g:MY_GLOBAL_NONE = test_none
63
Bram Moolenaare9c07272016-03-30 20:50:46 +020064 set viminfo='100,<50,s10,h,!,nviminfo
Bram Moolenaarb20e3342016-01-18 23:29:01 +010065 wv! Xviminfo
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +010066
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +020067 unlet g:MY_GLOBAL_STRING
68 unlet g:MY_GLOBAL_NUM
69 unlet g:MY_GLOBAL_FLOAT
Bram Moolenaarb20e3342016-01-18 23:29:01 +010070 unlet g:MY_GLOBAL_DICT
71 unlet g:MY_GLOBAL_LIST
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +010072 unlet g:MY_GLOBAL_BLOB
73 unlet g:MY_GLOBAL_FALSE
74 unlet g:MY_GLOBAL_TRUE
75 unlet g:MY_GLOBAL_NULL
76 unlet g:MY_GLOBAL_NONE
Bram Moolenaarb20e3342016-01-18 23:29:01 +010077
78 rv! Xviminfo
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +020079 call assert_equal("Vim Editor", g:MY_GLOBAL_STRING)
80 call assert_equal(345, g:MY_GLOBAL_NUM)
81 call assert_equal(3.14, g:MY_GLOBAL_FLOAT)
Bram Moolenaarb20e3342016-01-18 23:29:01 +010082 call assert_equal(test_dict, g:MY_GLOBAL_DICT)
83 call assert_equal(test_list, g:MY_GLOBAL_LIST)
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +010084 call assert_equal(test_blob, g:MY_GLOBAL_BLOB)
85 call assert_equal(test_false, g:MY_GLOBAL_FALSE)
86 call assert_equal(test_true, g:MY_GLOBAL_TRUE)
87 call assert_equal(test_null, g:MY_GLOBAL_NULL)
88 call assert_equal(test_none, g:MY_GLOBAL_NONE)
Bram Moolenaarb20e3342016-01-18 23:29:01 +010089
90 call delete('Xviminfo')
91 set viminfo-=!
92endfunc
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020093
94func Test_cmdline_history()
95 call histdel(':')
96 call test_settime(11)
97 call histadd(':', "echo 'one'")
98 call test_settime(12)
99 " split into two lines
100 let long800 = repeat(" 'eight'", 100)
101 call histadd(':', "echo " . long800)
102 call test_settime(13)
103 " split into three lines
104 let long1400 = repeat(" 'fourteeeeen'", 100)
105 call histadd(':', "echo " . long1400)
106 wviminfo Xviminfo
107 let lines = readfile('Xviminfo')
108 let done_colon = 0
109 let done_bar = 0
110 let lnum = 0
111 while lnum < len(lines)
112 let line = lines[lnum] | let lnum += 1
113 if line[0] == ':'
114 if done_colon == 0
115 call assert_equal(":\x161408", line)
116 let line = lines[lnum] | let lnum += 1
117 call assert_equal('<echo ' . long1400, line)
118 elseif done_colon == 1
119 call assert_equal(":\x16808", line)
120 let line = lines[lnum] | let lnum += 1
121 call assert_equal("<echo " . long800, line)
122 elseif done_colon == 2
123 call assert_equal(":echo 'one'", line)
124 endif
125 let done_colon += 1
126 elseif line[0:4] == '|2,0,'
127 if done_bar == 0
128 call assert_equal("|2,0,13,,>1407", line)
129 let line = lines[lnum] | let lnum += 1
130 call assert_equal('|<"echo ' . long1400[0:484], line)
131 let line = lines[lnum] | let lnum += 1
132 call assert_equal('|<' . long1400[485:974], line)
133 let line = lines[lnum] | let lnum += 1
134 call assert_equal('|<' . long1400[975:] . '"', line)
135 elseif done_bar == 1
136 call assert_equal('|2,0,12,,>807', line)
137 let line = lines[lnum] | let lnum += 1
138 call assert_equal('|<"echo ' . long800[0:484], line)
139 let line = lines[lnum] | let lnum += 1
140 call assert_equal('|<' . long800[485:] . '"', line)
141 elseif done_bar == 2
142 call assert_equal("|2,0,11,,\"echo 'one'\"", line)
143 endif
144 let done_bar += 1
145 endif
146 endwhile
147 call assert_equal(3, done_colon)
148 call assert_equal(3, done_bar)
149
150 call histdel(':')
151 rviminfo Xviminfo
152 call assert_equal("echo " . long1400, histget(':', -1))
153 call assert_equal("echo " . long800, histget(':', -2))
154 call assert_equal("echo 'one'", histget(':', -3))
155
156 call delete('Xviminfo')
157endfunc
Bram Moolenaar1fd99c12016-06-09 20:24:28 +0200158
159func Test_cmdline_history_order()
160 call histdel(':')
161 call test_settime(11)
162 call histadd(':', "echo '11'")
163 call test_settime(22)
164 call histadd(':', "echo '22'")
165 call test_settime(33)
166 call histadd(':', "echo '33'")
167 wviminfo Xviminfo
168
169 call histdel(':')
170 " items go in between
171 call test_settime(15)
172 call histadd(':', "echo '15'")
173 call test_settime(27)
174 call histadd(':', "echo '27'")
175
176 rviminfo Xviminfo
177 call assert_equal("echo '33'", histget(':', -1))
178 call assert_equal("echo '27'", histget(':', -2))
179 call assert_equal("echo '22'", histget(':', -3))
180 call assert_equal("echo '15'", histget(':', -4))
181 call assert_equal("echo '11'", histget(':', -5))
182
183 call histdel(':')
184 " items go before and after
Bram Moolenaarce90e362019-09-08 18:58:44 +0200185 eval 8->test_settime()
Bram Moolenaar1fd99c12016-06-09 20:24:28 +0200186 call histadd(':', "echo '8'")
187 call test_settime(39)
188 call histadd(':', "echo '39'")
189
190 rviminfo Xviminfo
191 call assert_equal("echo '39'", histget(':', -1))
192 call assert_equal("echo '33'", histget(':', -2))
193 call assert_equal("echo '22'", histget(':', -3))
194 call assert_equal("echo '11'", histget(':', -4))
195 call assert_equal("echo '8'", histget(':', -5))
196
197 " Check sorting works when writing with merge.
198 call histdel(':')
199 call test_settime(8)
200 call histadd(':', "echo '8'")
201 call test_settime(15)
202 call histadd(':', "echo '15'")
203 call test_settime(27)
204 call histadd(':', "echo '27'")
205 call test_settime(39)
206 call histadd(':', "echo '39'")
207 wviminfo Xviminfo
208
209 call histdel(':')
210 rviminfo Xviminfo
211 call assert_equal("echo '39'", histget(':', -1))
212 call assert_equal("echo '33'", histget(':', -2))
213 call assert_equal("echo '27'", histget(':', -3))
214 call assert_equal("echo '22'", histget(':', -4))
215 call assert_equal("echo '15'", histget(':', -5))
216 call assert_equal("echo '11'", histget(':', -6))
217 call assert_equal("echo '8'", histget(':', -7))
218
219 call delete('Xviminfo')
220endfunc
Bram Moolenaar01227092016-06-11 14:47:40 +0200221
Bram Moolenaare80ff742016-06-11 21:14:18 +0200222func Test_viminfo_registers()
223 call test_settime(8)
224 call setreg('a', "eight", 'c')
225 call test_settime(20)
226 call setreg('b', ["twenty", "again"], 'l')
227 call test_settime(40)
228 call setreg('c', ["four", "agai"], 'b4')
229 let l = []
230 set viminfo='100,<600,s10,h,!,nviminfo
231 for i in range(500)
232 call add(l, 'something')
233 endfor
234 call setreg('d', l, 'l')
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200235 call setreg('e', "abc\<C-V>xyz")
Bram Moolenaare80ff742016-06-11 21:14:18 +0200236 wviminfo Xviminfo
237
238 call test_settime(10)
239 call setreg('a', '', 'b10')
240 call test_settime(15)
241 call setreg('b', 'drop')
242 call test_settime(50)
243 call setreg('c', 'keep', 'l')
244 call test_settime(30)
245 call setreg('d', 'drop', 'l')
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200246 call setreg('e', 'drop')
Bram Moolenaare80ff742016-06-11 21:14:18 +0200247 rviminfo Xviminfo
248
249 call assert_equal("", getreg('a'))
250 call assert_equal("\<C-V>10", getregtype('a'))
251 call assert_equal("twenty\nagain\n", getreg('b'))
252 call assert_equal("V", getregtype('b'))
253 call assert_equal("keep\n", getreg('c'))
254 call assert_equal("V", getregtype('c'))
255 call assert_equal(l, getreg('d', 1, 1))
256 call assert_equal("V", getregtype('d'))
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200257 call assert_equal("abc\<C-V>xyz", getreg('e'))
Bram Moolenaare80ff742016-06-11 21:14:18 +0200258
Bram Moolenaar25709572016-08-26 20:41:16 +0200259 " Length around 440 switches to line continuation.
260 let len = 434
261 while len < 445
262 let s = repeat('a', len)
263 call setreg('"', s)
264 wviminfo Xviminfo
265 call setreg('"', '')
266 rviminfo Xviminfo
267 call assert_equal(s, getreg('"'), 'wrong register at length: ' . len)
268
269 let len += 1
270 endwhile
271
Bram Moolenaare80ff742016-06-11 21:14:18 +0200272 call delete('Xviminfo')
273endfunc
274
Bram Moolenaar2d358992016-06-12 21:20:54 +0200275func Test_viminfo_marks()
276 sp bufa
277 let bufa = bufnr('%')
278 sp bufb
279 let bufb = bufnr('%')
280
281 call test_settime(8)
282 call setpos("'A", [bufa, 1, 1, 0])
283 call test_settime(20)
284 call setpos("'B", [bufb, 9, 1, 0])
285 call setpos("'C", [bufa, 7, 1, 0])
286
287 delmark 0-9
288 call test_settime(25)
289 call setpos("'1", [bufb, 12, 1, 0])
290 call test_settime(35)
291 call setpos("'0", [bufa, 11, 1, 0])
292
293 call test_settime(45)
294 wviminfo Xviminfo
295
296 " Writing viminfo inserts the '0 mark.
297 call assert_equal([bufb, 1, 1, 0], getpos("'0"))
298 call assert_equal([bufa, 11, 1, 0], getpos("'1"))
299 call assert_equal([bufb, 12, 1, 0], getpos("'2"))
300
301 call test_settime(4)
302 call setpos("'A", [bufa, 9, 1, 0])
303 call test_settime(30)
304 call setpos("'B", [bufb, 2, 3, 0])
305 delmark C
306
307 delmark 0-9
308 call test_settime(30)
309 call setpos("'1", [bufb, 22, 1, 0])
310 call test_settime(55)
311 call setpos("'0", [bufa, 21, 1, 0])
312
313 rviminfo Xviminfo
314
315 call assert_equal([bufa, 1, 1, 0], getpos("'A"))
316 call assert_equal([bufb, 2, 3, 0], getpos("'B"))
317 call assert_equal([bufa, 7, 1, 0], getpos("'C"))
318
319 " numbered marks are merged
320 call assert_equal([bufa, 21, 1, 0], getpos("'0")) " time 55
321 call assert_equal([bufb, 1, 1, 0], getpos("'1")) " time 45
322 call assert_equal([bufa, 11, 1, 0], getpos("'2")) " time 35
323 call assert_equal([bufb, 22, 1, 0], getpos("'3")) " time 30
324 call assert_equal([bufb, 12, 1, 0], getpos("'4")) " time 25
325
326 call delete('Xviminfo')
327 exe 'bwipe ' . bufa
328 exe 'bwipe ' . bufb
329endfunc
330
331func Test_viminfo_jumplist()
332 split testbuf
333 clearjumps
334 call setline(1, ['time 05', 'time 10', 'time 15', 'time 20', 'time 30', 'last pos'])
335 call cursor(2, 1)
336 call test_settime(10)
337 exe "normal /20\r"
338 call test_settime(20)
339 exe "normal /30\r"
340 call test_settime(30)
341 exe "normal /last pos\r"
342 wviminfo Xviminfo
343
344 clearjumps
345 call cursor(1, 1)
346 call test_settime(5)
347 exe "normal /15\r"
348 call test_settime(15)
349 exe "normal /last pos\r"
350 call test_settime(40)
351 exe "normal ?30\r"
352 rviminfo Xviminfo
353
354 call assert_equal('time 30', getline('.'))
355 exe "normal \<C-O>"
356 call assert_equal('last pos', getline('.'))
357 exe "normal \<C-O>"
358 " duplicate for 'time 30' was removed
359 call assert_equal('time 20', getline('.'))
360 exe "normal \<C-O>"
361 call assert_equal('time 15', getline('.'))
362 exe "normal \<C-O>"
363 call assert_equal('time 10', getline('.'))
364 exe "normal \<C-O>"
365 call assert_equal('time 05', getline('.'))
366
Bram Moolenaarece74ab2016-06-13 22:22:15 +0200367 clearjumps
368 call cursor(1, 1)
369 call test_settime(5)
370 exe "normal /15\r"
371 call test_settime(15)
372 exe "normal /last pos\r"
373 call test_settime(40)
374 exe "normal ?30\r"
375 " Test merge when writing
376 wviminfo Xviminfo
377 clearjumps
378 rviminfo Xviminfo
379
Bram Moolenaar28607ba2016-06-15 21:44:51 +0200380 let last_line = line('.')
Bram Moolenaarece74ab2016-06-13 22:22:15 +0200381 exe "normal \<C-O>"
382 call assert_equal('time 30', getline('.'))
383 exe "normal \<C-O>"
384 call assert_equal('last pos', getline('.'))
385 exe "normal \<C-O>"
386 " duplicate for 'time 30' was removed
387 call assert_equal('time 20', getline('.'))
388 exe "normal \<C-O>"
389 call assert_equal('time 15', getline('.'))
390 exe "normal \<C-O>"
391 call assert_equal('time 10', getline('.'))
392 exe "normal \<C-O>"
393 call assert_equal('time 05', getline('.'))
394
Bram Moolenaar28607ba2016-06-15 21:44:51 +0200395 " Test with jumplist full.
396 clearjumps
397 call setline(1, repeat(['match here'], 101))
398 call cursor(1, 1)
399 call test_settime(10)
400 for i in range(100)
401 exe "normal /here\r"
402 endfor
403 rviminfo Xviminfo
404
405 " must be newest mark that comes from viminfo.
406 exe "normal \<C-O>"
407 call assert_equal(last_line, line('.'))
408
Bram Moolenaar2d358992016-06-12 21:20:54 +0200409 bwipe!
410 call delete('Xviminfo')
411endfunc
412
Bram Moolenaar01227092016-06-11 14:47:40 +0200413func Test_viminfo_encoding()
Bram Moolenaar01227092016-06-11 14:47:40 +0200414 set enc=latin1
415 call histdel(':')
416 call histadd(':', "echo '\xe9'")
417 wviminfo Xviminfo
418
419 set fencs=utf-8,latin1
420 set enc=utf-8
421 sp Xviminfo
422 call assert_equal('latin1', &fenc)
423 close
424
425 call histdel(':')
426 rviminfo Xviminfo
427 call assert_equal("echo 'é'", histget(':', -1))
428
429 call delete('Xviminfo')
430endfunc
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +0200431
432func Test_viminfo_bad_syntax()
433 let lines = []
434 call add(lines, '|<') " empty continuation line
435 call add(lines, '|234234234234234324,nothing')
436 call add(lines, '|1+"no comma"')
437 call add(lines, '|1,2,3,4,5,6,7') " too many items
438 call add(lines, '|1,"string version"')
439 call add(lines, '|1,>x') " bad continuation line
440 call add(lines, '|1,"x') " missing quote
441 call add(lines, '|1,"x\') " trailing backslash
442 call add(lines, '|1,,,,') "trailing comma
443 call add(lines, '|1,>234') " trailing continuation line
444 call writefile(lines, 'Xviminfo')
Bram Moolenaare80ff742016-06-11 21:14:18 +0200445 rviminfo Xviminfo
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +0200446
447 call delete('Xviminfo')
448endfunc
449
Bram Moolenaarab9c89b2016-07-03 17:47:26 +0200450func Test_viminfo_file_marks()
451 silent! bwipe test_viminfo.vim
452 silent! bwipe Xviminfo
453
454 call test_settime(10)
455 edit ten
456 call test_settime(25)
457 edit again
458 call test_settime(30)
459 edit thirty
460 wviminfo Xviminfo
461
462 call test_settime(20)
463 edit twenty
464 call test_settime(35)
465 edit again
466 call test_settime(40)
467 edit fourty
468 wviminfo Xviminfo
469
470 sp Xviminfo
471 1
472 for name in ['fourty', 'again', 'thirty', 'twenty', 'ten']
473 /^>
474 call assert_equal(name, substitute(getline('.'), '.*/', '', ''))
475 endfor
476 close
477
478 call delete('Xviminfo')
479endfunc
Bram Moolenaare59215c2016-08-14 19:08:45 +0200480
481func Test_viminfo_file_mark_tabclose()
482 tabnew Xtestfileintab
483 call setline(1, ['a','b','c','d','e'])
484 4
485 q!
486 wviminfo Xviminfo
487 sp Xviminfo
488 /^> .*Xtestfileintab
489 let lnum = line('.')
490 while 1
491 if lnum == line('$')
Bram Moolenaar37175402017-03-18 20:18:45 +0100492 call assert_report('mark not found in Xtestfileintab')
Bram Moolenaare59215c2016-08-14 19:08:45 +0200493 break
494 endif
495 let lnum += 1
496 let line = getline(lnum)
497 if line == ''
Bram Moolenaar37175402017-03-18 20:18:45 +0100498 call assert_report('mark not found in Xtestfileintab')
Bram Moolenaare59215c2016-08-14 19:08:45 +0200499 break
500 endif
501 if line =~ "^\t\""
502 call assert_equal('4', substitute(line, ".*\"\t\\(\\d\\).*", '\1', ''))
503 break
504 endif
505 endwhile
506
507 call delete('Xviminfo')
508 silent! bwipe Xtestfileintab
509endfunc
Bram Moolenaare11d61a2016-08-20 18:36:54 +0200510
Bram Moolenaar156919f2016-10-15 20:46:20 +0200511func Test_viminfo_file_mark_zero_time()
512 let lines = [
513 \ '# Viminfo version',
514 \ '|1,4',
515 \ '',
516 \ '*encoding=utf-8',
517 \ '',
518 \ '# File marks:',
519 \ "'B 1 0 /tmp/nothing",
520 \ '|4,66,1,0,0,"/tmp/nothing"',
521 \ "",
522 \ ]
523 call writefile(lines, 'Xviminfo')
524 delmark B
525 rviminfo Xviminfo
526 call delete('Xviminfo')
527 call assert_equal(1, line("'B"))
528 delmark B
529endfunc
530
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200531" Test for saving and restoring file marks in unloaded buffers
532func Test_viminfo_file_mark_unloaded_buf()
533 let save_viminfo = &viminfo
534 set viminfo&vim
535 call writefile(repeat(['vim'], 10), 'Xfile1')
536 %bwipe
537 edit! Xfile1
538 call setpos("'u", [0, 3, 1, 0])
539 call setpos("'v", [0, 5, 1, 0])
540 enew
541 wviminfo Xviminfo
542 %bwipe
543 edit Xfile1
544 rviminfo! Xviminfo
545 call assert_equal([0, 3, 1, 0], getpos("'u"))
546 call assert_equal([0, 5, 1, 0], getpos("'v"))
547 %bwipe
548 call delete('Xfile1')
549 call delete('Xviminfo')
550 let &viminfo = save_viminfo
551endfunc
552
Bram Moolenaar156919f2016-10-15 20:46:20 +0200553func Test_viminfo_oldfiles()
Bram Moolenaare11d61a2016-08-20 18:36:54 +0200554 let v:oldfiles = []
555 let lines = [
556 \ '# comment line',
557 \ '*encoding=utf-8',
558 \ '',
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200559 \ ':h viminfo',
560 \ '?/session',
561 \ '=myvar',
562 \ '@123',
563 \ '',
564 \ "'E 2 0 /tmp/nothing",
565 \ '',
Bram Moolenaare11d61a2016-08-20 18:36:54 +0200566 \ "> /tmp/file_one.txt",
567 \ "\t\"\t11\t0",
568 \ "",
569 \ "> /tmp/file_two.txt",
570 \ "\t\"\t11\t0",
571 \ "",
572 \ "> /tmp/another.txt",
573 \ "\t\"\t11\t0",
574 \ "",
575 \ ]
576 call writefile(lines, 'Xviminfo')
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200577 delmark E
Bram Moolenaare11d61a2016-08-20 18:36:54 +0200578 rviminfo! Xviminfo
579 call delete('Xviminfo')
580
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200581 call assert_equal('h viminfo', histget(':'))
582 call assert_equal('session', histget('/'))
583 call assert_equal('myvar', histget('='))
584 call assert_equal('123', histget('@'))
585 call assert_equal(2, line("'E"))
Bram Moolenaar7b668e82016-08-23 23:51:21 +0200586 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/'}))
587 call assert_equal(['1: /tmp/file_one.txt', '2: /tmp/file_two.txt'], filter(split(execute('filter file_ oldfiles'), "\n"), {i, v -> v =~ '/tmp/'}))
588 call assert_equal(['3: /tmp/another.txt'], filter(split(execute('filter /another/ oldfiles'), "\n"), {i, v -> v =~ '/tmp/'}))
Bram Moolenaar5328cb82019-07-27 23:27:51 +0200589
590 new
591 call feedkeys("3\<CR>", 't')
592 browse oldfiles
593 call assert_equal("/tmp/another.txt", expand("%"))
594 bwipe
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200595 delmark E
596endfunc
597
598" Test for storing and restoring buffer list in 'viminfo'
599func Test_viminfo_bufferlist()
600 " If there are arguments, then :rviminfo doesn't read the buffer list.
601 " Need to delete all the arguments for :rviminfo to work.
602 %argdelete
603
604 edit Xfile1
605 edit Xfile2
606 set viminfo-=%
607 wviminfo Xviminfo
608 %bwipe
609 rviminfo Xviminfo
610 call assert_equal(1, len(getbufinfo()))
611
612 edit Xfile1
613 edit Xfile2
614 set viminfo^=%
615 wviminfo Xviminfo
616 %bwipe
617 rviminfo Xviminfo
618 let l = getbufinfo()
619 call assert_equal(3, len(l))
620 call assert_equal('Xfile1', bufname(l[1].bufnr))
621 call assert_equal('Xfile2', bufname(l[2].bufnr))
622
623 call delete('Xviminfo')
624 %bwipe
625 set viminfo-=%
626endfunc
627
628" Test for errors in a viminfo file
629func Test_viminfo_error()
630 " Non-existing viminfo files
631 call assert_fails('rviminfo xyz', 'E195:')
632
633 " Illegal starting character
634 call writefile(["a 123"], 'Xviminfo')
635 call assert_fails('rv Xviminfo', 'E575:')
636
637 " Illegal register name in the viminfo file
638 call writefile(['"@ LINE 0'], 'Xviminfo')
639 call assert_fails('rv Xviminfo', 'E577:')
640
641 " Invalid file mark line
642 call writefile(['>', '@'], 'Xviminfo')
643 call assert_fails('rv Xviminfo', 'E576:')
644
645 " Too many errors in viminfo file
646 call writefile(repeat(["a 123"], 15), 'Xviminfo')
647 call assert_fails('rv Xviminfo', 'E136:')
648
649 call writefile(['>'] + repeat(['@'], 10), 'Xviminfo')
650 call assert_fails('rv Xviminfo', 'E136:')
651
652 call writefile(repeat(['"@'], 15), 'Xviminfo')
653 call assert_fails('rv Xviminfo', 'E136:')
654
655 call delete('Xviminfo')
656endfunc
657
658" Test for saving and restoring last substitute string in viminfo
659func Test_viminfo_lastsub()
660 enew
661 call append(0, "blue blue blue")
662 call cursor(1, 1)
663 s/blue/green/
664 wviminfo Xviminfo
665 s/blue/yellow/
666 rviminfo! Xviminfo
667 &
668 call assert_equal("green yellow green", getline(1))
669 enew!
670 call delete('Xviminfo')
671endfunc
672
673" Test saving and restoring the register values using the older method
674func Test_viminfo_registers_old()
675 let lines = [
676 \ '# Viminfo version',
677 \ '|1,1',
678 \ '',
679 \ '*encoding=utf-8',
680 \ '',
681 \ '# Registers:',
682 \ '""0 CHAR 0',
683 \ ' Vim',
684 \ '"a CHAR 0',
685 \ ' red',
686 \ '"m@ CHAR 0',
687 \ " :echo 'Hello'\<CR>",
688 \ "",
689 \ ]
690 call writefile(lines, 'Xviminfo')
691 let @a = 'one'
692 let @b = 'two'
693 let @m = 'three'
694 let @" = 'four'
695 let @t = ":echo 'Unix'\<CR>"
696 silent! normal @t
697 rviminfo! Xviminfo
698 call assert_equal('red', getreg('a'))
699 call assert_equal('two', getreg('b'))
700 call assert_equal(":echo 'Hello'\<CR>", getreg('m'))
701 call assert_equal('Vim', getreg('"'))
702 call assert_equal("\nHello", execute('normal @@'))
703 call delete('Xviminfo')
704 let @" = ''
705endfunc
706
707" Test for saving and restoring large number of lines in a register
708func Test_viminfo_large_register()
709 let save_viminfo = &viminfo
710 set viminfo&vim
711 set viminfo-=<50
712 set viminfo+=<200
713 let lines = ['"r CHAR 0']
714 call extend(lines, repeat(["\tsun is rising"], 200))
715 call writefile(lines, 'Xviminfo')
716 let @r = ''
717 rviminfo! Xviminfo
718 call assert_equal(join(repeat(["sun is rising"], 200), "\n"), @r)
719 call delete('Xviminfo')
Bram Moolenaar6bd1d772019-10-09 22:01:25 +0200720 let @r = ''
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200721 let &viminfo = save_viminfo
722endfunc
723
724" Test for setting 'viminfofile' to NONE
725func Test_viminfofile_none()
Bram Moolenaar6bd1d772019-10-09 22:01:25 +0200726 let save_vif = &viminfofile
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200727 set viminfofile=NONE
728 wviminfo Xviminfo
729 call assert_false(filereadable('Xviminfo'))
730 call writefile([''], 'Xviminfo')
731 call assert_fails('rviminfo Xviminfo', 'E195:')
732 call delete('Xviminfo')
Bram Moolenaar6bd1d772019-10-09 22:01:25 +0200733 let &viminfofile = save_vif
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200734endfunc
735
Bram Moolenaar6bd1d772019-10-09 22:01:25 +0200736" Test for an unwritable and unreadble 'viminfo' file
737func Test_viminfo_perm()
738 CheckUnix
Bram Moolenaar07282f02019-10-10 16:46:17 +0200739 CheckNotRoot
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200740 call writefile([''], 'Xviminfo')
741 call setfperm('Xviminfo', 'r-x------')
742 call assert_fails('wviminfo Xviminfo', 'E137:')
Bram Moolenaar6bd1d772019-10-09 22:01:25 +0200743 call setfperm('Xviminfo', '--x------')
744 call assert_fails('rviminfo Xviminfo', 'E195:')
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200745 call delete('Xviminfo')
Bram Moolenaare11d61a2016-08-20 18:36:54 +0200746endfunc
Bram Moolenaar6bd1d772019-10-09 22:01:25 +0200747
748" Test for writing to an existing viminfo file merges the file marks
749func XTest_viminfo_marks_merge()
750 let save_viminfo = &viminfo
751 set viminfo&vim
752 set viminfo^=%
753 enew
754 %argdelete
755 %bwipe
756
757 call writefile(repeat(['editor'], 10), 'Xbufa')
758 call writefile(repeat(['Vim'], 10), 'Xbufb')
759
760 " set marks in buffers
761 call test_settime(10)
762 edit Xbufa
763 4mark a
764 wviminfo Xviminfo
765 edit Xbufb
766 4mark b
767 wviminfo Xviminfo
768 %bwipe
769
770 " set marks in buffers again
771 call test_settime(20)
772 edit Xbufb
773 6mark b
774 wviminfo Xviminfo
775 edit Xbufa
776 6mark a
777 wviminfo Xviminfo
778 %bwipe
779
780 " Load the buffer and check the marks
781 edit Xbufa
782 rviminfo! Xviminfo
783 call assert_equal(6, line("'a"))
784 edit Xbufb
785 rviminfo! Xviminfo
786 call assert_equal(6, line("'b"))
787
788 " cleanup
789 %bwipe
790 call delete('Xviminfo')
791 call delete('Xbufa')
792 call delete('Xbufb')
793 call test_settime(0)
794 let &viminfo=save_viminfo
795endfunc