1" Tests for register operations
2
3source check.vim
4source view_util.vim
5
6" This test must be executed first to check for empty and unset registers.
7func Test_aaa_empty_reg_test()
8  call assert_fails('normal @@', 'E748:')
9  call assert_fails('normal @%', 'E354:')
10  call assert_fails('normal @#', 'E354:')
11  call assert_fails('normal @!', 'E354:')
12  call assert_fails('normal @:', 'E30:')
13  call assert_fails('normal @.', 'E29:')
14  call assert_fails('put /', 'E35:')
15  call assert_fails('put .', 'E29:')
16endfunc
17
18func Test_yank_shows_register()
19    enew
20    set report=0
21    call setline(1, ['foo', 'bar'])
22    " Line-wise
23    exe 'norm! yy'
24    call assert_equal('1 line yanked', v:statusmsg)
25    exe 'norm! "zyy'
26    call assert_equal('1 line yanked into "z', v:statusmsg)
27    exe 'norm! yj'
28    call assert_equal('2 lines yanked', v:statusmsg)
29    exe 'norm! "zyj'
30    call assert_equal('2 lines yanked into "z', v:statusmsg)
31
32    " Block-wise
33    exe "norm! \<C-V>y"
34    call assert_equal('block of 1 line yanked', v:statusmsg)
35    exe "norm! \<C-V>\"zy"
36    call assert_equal('block of 1 line yanked into "z', v:statusmsg)
37    exe "norm! \<C-V>jy"
38    call assert_equal('block of 2 lines yanked', v:statusmsg)
39    exe "norm! \<C-V>j\"zy"
40    call assert_equal('block of 2 lines yanked into "z', v:statusmsg)
41
42    bwipe!
43endfunc
44
45func Test_display_registers()
46    e file1
47    e file2
48    call setline(1, ['foo', 'bar'])
49    /bar
50    exe 'norm! y2l"axx'
51    call feedkeys("i\<C-R>=2*4\n\<esc>")
52    call feedkeys(":ls\n", 'xt')
53
54    let a = execute('display')
55    let b = execute('registers')
56
57    call assert_equal(a, b)
58    call assert_match('^\nType Name Content\n'
59          \ .         '  c  ""   a\n'
60          \ .         '  c  "0   ba\n'
61          \ .         '  c  "a   b\n'
62          \ .         '.*'
63          \ .         '  c  "-   a\n'
64          \ .         '.*'
65          \ .         '  c  ":   ls\n'
66          \ .         '  c  "%   file2\n'
67          \ .         '  c  "#   file1\n'
68          \ .         '  c  "/   bar\n'
69          \ .         '  c  "=   2\*4', a)
70
71    let a = execute('registers a')
72    call assert_match('^\nType Name Content\n'
73          \ .         '  c  "a   b', a)
74
75    let a = execute('registers :')
76    call assert_match('^\nType Name Content\n'
77          \ .         '  c  ":   ls', a)
78
79    bwipe!
80endfunc
81
82func Test_register_one()
83  " delete a line goes into register one
84  new
85  call setline(1, "one")
86  normal dd
87  call assert_equal("one\n", @1)
88
89  " delete a word does not change register one, does change "-
90  call setline(1, "two")
91  normal de
92  call assert_equal("one\n", @1)
93  call assert_equal("two", @-)
94
95  " delete a word with a register does not change register one
96  call setline(1, "three")
97  normal "ade
98  call assert_equal("three", @a)
99  call assert_equal("one\n", @1)
100
101  " delete a word with register DOES change register one with one of a list of
102  " operators
103  " %
104  call setline(1, ["(12)3"])
105  normal "ad%
106  call assert_equal("(12)", @a)
107  call assert_equal("(12)", @1)
108
109  " (
110  call setline(1, ["first second"])
111  normal $"ad(
112  call assert_equal("first secon", @a)
113  call assert_equal("first secon", @1)
114
115  " )
116  call setline(1, ["First Second."])
117  normal gg0"ad)
118  call assert_equal("First Second.", @a)
119  call assert_equal("First Second.", @1)
120
121  " `
122  call setline(1, ["start here."])
123  normal gg0fhmx0"ad`x
124  call assert_equal("start ", @a)
125  call assert_equal("start ", @1)
126
127  " /
128  call setline(1, ["searchX"])
129  exe "normal gg0\"ad/X\<CR>"
130  call assert_equal("search", @a)
131  call assert_equal("search", @1)
132
133  " ?
134  call setline(1, ["Ysearch"])
135  exe "normal gg$\"ad?Y\<CR>"
136  call assert_equal("Ysearc", @a)
137  call assert_equal("Ysearc", @1)
138
139  " n
140  call setline(1, ["Ynext"])
141  normal gg$"adn
142  call assert_equal("Ynex", @a)
143  call assert_equal("Ynex", @1)
144
145  " N
146  call setline(1, ["prevY"])
147  normal gg0"adN
148  call assert_equal("prev", @a)
149  call assert_equal("prev", @1)
150
151  " }
152  call setline(1, ["one", ""])
153  normal gg0"ad}
154  call assert_equal("one\n", @a)
155  call assert_equal("one\n", @1)
156
157  " {
158  call setline(1, ["", "two"])
159  normal 2G$"ad{
160  call assert_equal("\ntw", @a)
161  call assert_equal("\ntw", @1)
162
163  bwipe!
164endfunc
165
166func Test_recording_status_in_ex_line()
167  norm qx
168  redraw!
169  call assert_equal('recording @x', Screenline(&lines))
170  set shortmess=q
171  redraw!
172  call assert_equal('recording', Screenline(&lines))
173  set shortmess&
174  norm q
175  redraw!
176  call assert_equal('', Screenline(&lines))
177endfunc
178
179" Check that replaying a typed sequence does not use an Esc and following
180" characters as an escape sequence.
181func Test_recording_esc_sequence()
182  new
183  try
184    let save_F2 = &t_F2
185  catch
186  endtry
187  let t_F2 = "\<Esc>OQ"
188  call feedkeys("qqiTest\<Esc>", "xt")
189  call feedkeys("OQuirk\<Esc>q", "xt")
190  call feedkeys("Go\<Esc>@q", "xt")
191  call assert_equal(['Quirk', 'Test', 'Quirk', 'Test'], getline(1, 4))
192  bwipe!
193  if exists('save_F2')
194    let &t_F2 = save_F2
195  else
196    set t_F2=
197  endif
198endfunc
199
200" Test for executing the last used register (@)
201func Test_last_used_exec_reg()
202  " Test for the @: command
203  let a = ''
204  call feedkeys(":let a ..= 'Vim'\<CR>", 'xt')
205  normal @:
206  call assert_equal('VimVim', a)
207
208  " Test for the @= command
209  let x = ''
210  let a = ":let x ..= 'Vim'\<CR>"
211  exe "normal @=a\<CR>"
212  normal @@
213  call assert_equal('VimVim', x)
214
215  " Test for the @. command
216  let a = ''
217  call feedkeys("i:let a ..= 'Edit'\<CR>", 'xt')
218  normal @.
219  normal @@
220  call assert_equal('EditEdit', a)
221
222  " Test for repeating the last command-line in visual mode
223  call append(0, 'register')
224  normal gg
225  let @r = ''
226  call feedkeys("v:yank R\<CR>", 'xt')
227  call feedkeys("v@:", 'xt')
228  call assert_equal("\nregister\nregister\n", @r)
229
230  enew!
231endfunc
232
233func Test_get_register()
234  enew
235  edit Xfile1
236  edit Xfile2
237  call assert_equal('Xfile2', getreg('%'))
238  call assert_equal('Xfile1', getreg('#'))
239
240  call feedkeys("iTwo\<Esc>", 'xt')
241  call assert_equal('Two', getreg('.'))
242  call assert_equal('', getreg('_'))
243  call assert_beeps('normal ":yy')
244  call assert_beeps('normal "%yy')
245  call assert_beeps('normal ".yy')
246
247  call assert_equal('', getreg("\<C-F>"))
248  call assert_equal('', getreg("\<C-W>"))
249  call assert_equal('', getreg("\<C-L>"))
250  " Change the last used register to '"' for the next test
251  normal! ""yy
252  let @" = 'happy'
253  call assert_equal('happy', getreg())
254  call assert_equal('happy', getreg(''))
255
256  call assert_equal('', getregtype('!'))
257  call assert_fails('echo getregtype([])', 'E730:')
258  call assert_equal('v', getregtype())
259  call assert_equal('v', getregtype(''))
260
261  " Test for inserting an invalid register content
262  call assert_beeps('exe "normal i\<C-R>!"')
263
264  " Test for inserting a register with multiple lines
265  call deletebufline('', 1, '$')
266  call setreg('r', ['a', 'b'])
267  exe "normal i\<C-R>r"
268  call assert_equal(['a', 'b', ''], getline(1, '$'))
269
270  " Test for inserting a multi-line register in the command line
271  call feedkeys(":\<C-R>r\<Esc>", 'xt')
272  call assert_equal("a\rb\r", histget(':', -1))
273
274  call assert_fails('let r = getreg("=", [])', 'E745:')
275  call assert_fails('let r = getreg("=", 1, [])', 'E745:')
276  enew!
277
278  " Using a register in operator-pending mode should fail
279  call assert_beeps('norm! c"')
280endfunc
281
282func Test_set_register()
283  call assert_fails("call setreg('#', 200)", 'E86:')
284
285  edit Xfile_alt_1
286  let b1 = bufnr('')
287  edit Xfile_alt_2
288  let b2 = bufnr('')
289  edit Xfile_alt_3
290  let b3 = bufnr('')
291  call setreg('#', 'alt_1')
292  call assert_equal('Xfile_alt_1', getreg('#'))
293  call setreg('#', b2)
294  call assert_equal('Xfile_alt_2', getreg('#'))
295
296  let ab = 'regwrite'
297  call setreg('=', '')
298  call setreg('=', 'a', 'a')
299  call setreg('=', 'b', 'a')
300  call assert_equal('regwrite', getreg('='))
301
302  " Test for setting a list of lines to special registers
303  call setreg('/', [])
304  call assert_equal('', @/)
305  call setreg('=', [])
306  call assert_equal('', @=)
307  call assert_fails("call setreg('/', ['a', 'b'])", 'E883:')
308  call assert_fails("call setreg('=', ['a', 'b'])", 'E883:')
309  call assert_equal(0, setreg('_', ['a', 'b']))
310
311  " Test for recording to a invalid register
312  call assert_beeps('normal q$')
313
314  " Appending to a register when recording
315  call append(0, "text for clipboard test")
316  normal gg
317  call feedkeys('qrllq', 'xt')
318  call feedkeys('qRhhq', 'xt')
319  call assert_equal('llhh', getreg('r'))
320
321  " Appending a list of characters to a register from different lines
322  let @r = ''
323  call append(0, ['abcdef', '123456'])
324  normal gg"ry3l
325  call cursor(2, 4)
326  normal "Ry3l
327  call assert_equal('abc456', @r)
328
329  " Test for gP with multiple lines selected using characterwise motion
330  %delete
331  call append(0, ['vim editor', 'vim editor'])
332  let @r = ''
333  exe "normal ggwy/vim /e\<CR>gP"
334  call assert_equal(['vim editor', 'vim editor', 'vim editor'], getline(1, 3))
335
336  " Test for gP with . register
337  %delete
338  normal iabc
339  normal ".gp
340  call assert_equal('abcabc', getline(1))
341  normal 0".gP
342  call assert_equal('abcabcabc', getline(1))
343
344  let @"=''
345  call setreg('', '1')
346  call assert_equal('1', @")
347  call setreg('@', '2')
348  call assert_equal('2', @")
349
350  enew!
351endfunc
352
353" Test for clipboard registers (* and +)
354func Test_clipboard_regs()
355  CheckNotGui
356  CheckFeature clipboard_working
357
358  new
359  call append(0, "text for clipboard test")
360  normal gg"*yiw
361  call assert_equal('text', getreg('*'))
362  normal gg2w"+yiw
363  call assert_equal('clipboard', getreg('+'))
364
365  " Test for replacing the clipboard register contents
366  set clipboard=unnamed
367  let @* = 'food'
368  normal ggviw"*p
369  call assert_equal('text', getreg('*'))
370  call assert_equal('food for clipboard test', getline(1))
371  normal ggviw"*p
372  call assert_equal('food', getreg('*'))
373  call assert_equal('text for clipboard test', getline(1))
374
375  " Test for replacing the selection register contents
376  set clipboard=unnamedplus
377  let @+ = 'food'
378  normal ggviw"+p
379  call assert_equal('text', getreg('+'))
380  call assert_equal('food for clipboard test', getline(1))
381  normal ggviw"+p
382  call assert_equal('food', getreg('+'))
383  call assert_equal('text for clipboard test', getline(1))
384
385  " Test for auto copying visually selected text to clipboard register
386  call setline(1, "text for clipboard test")
387  let @* = ''
388  set clipboard=autoselect
389  normal ggwwviwy
390  call assert_equal('clipboard', @*)
391
392  " Test for auto copying visually selected text to selection register
393  let @+ = ''
394  set clipboard=autoselectplus
395  normal ggwviwy
396  call assert_equal('for', @+)
397
398  set clipboard&vim
399  bwipe!
400endfunc
401
402" Test for restarting the current mode (insert or virtual replace) after
403" executing the contents of a register
404func Test_put_reg_restart_mode()
405  new
406  call append(0, 'editor')
407  normal gg
408  let @r = "ivim \<Esc>"
409  call feedkeys("i\<C-O>@r\<C-R>=mode()\<CR>", 'xt')
410  call assert_equal('vimi editor', getline(1))
411
412  call setline(1, 'editor')
413  normal gg
414  call feedkeys("gR\<C-O>@r\<C-R>=mode()\<CR>", 'xt')
415  call assert_equal('vimReditor', getline(1))
416
417  bwipe!
418endfunc
419
420" Test for executing a register using :@ command
421func Test_execute_register()
422  call setreg('r', [])
423  call assert_beeps('@r')
424  let i = 1
425  let @q = 'let i+= 1'
426  @q
427  @
428  call assert_equal(3, i)
429
430  " try to execute expression register and use a backspace to cancel it
431  new
432  call feedkeys("@=\<BS>ax\<CR>y", 'xt')
433  call assert_equal(['x', 'y'], getline(1, '$'))
434  close!
435
436  " cannot execute a register in operator pending mode
437  call assert_beeps('normal! c@r')
438endfunc
439
440" Test for getting register info
441func Test_get_reginfo()
442  enew
443  call setline(1, ['foo', 'bar'])
444
445  exe 'norm! "zyy'
446  let info = getreginfo('"')
447  call assert_equal('z', info.points_to)
448  call setreg('y', 'baz')
449  call assert_equal('z', getreginfo('').points_to)
450  call setreg('y', { 'isunnamed': v:true })
451  call assert_equal('y', getreginfo('"').points_to)
452
453  exe '$put'
454  call assert_equal(getreg('y'), getline(3))
455  call setreg('', 'qux')
456  call assert_equal('0', getreginfo('').points_to)
457  call setreg('x', 'quux')
458  call assert_equal('0', getreginfo('').points_to)
459
460  let info = getreginfo('')
461  call assert_equal(getreg('', 1, 1), info.regcontents)
462  call assert_equal(getregtype(''), info.regtype)
463
464  exe "norm! 0\<c-v>e" .. '"zy'
465  let info = getreginfo('z')
466  call assert_equal(getreg('z', 1, 1), info.regcontents)
467  call assert_equal(getregtype('z'), info.regtype)
468  call assert_equal(1, +info.isunnamed)
469
470  let info = getreginfo('"')
471  call assert_equal('z', info.points_to)
472
473  bwipe!
474endfunc
475
476" Test for restoring register with dict from getreginfo
477func Test_set_register_dict()
478  enew!
479
480  call setreg('"', #{ regcontents: ['one', 'two'],
481        \ regtype: 'V', points_to: 'z' })
482  call assert_equal(['one', 'two'], getreg('"', 1, 1))
483  let info = getreginfo('"')
484  call assert_equal('z', info.points_to)
485  call assert_equal('V', info.regtype)
486  call assert_equal(1, +getreginfo('z').isunnamed)
487
488  call setreg('x', #{ regcontents: ['three', 'four'],
489        \ regtype: 'v', isunnamed: v:true })
490  call assert_equal(['three', 'four'], getreg('"', 1, 1))
491  let info = getreginfo('"')
492  call assert_equal('x', info.points_to)
493  call assert_equal('v', info.regtype)
494  call assert_equal(1, +getreginfo('x').isunnamed)
495
496  call setreg('y', #{ regcontents: 'five',
497        \ regtype: "\<c-v>", isunnamed: v:false })
498  call assert_equal("\<c-v>4", getreginfo('y').regtype)
499  call assert_equal(0, +getreginfo('y').isunnamed)
500  call assert_equal(['three', 'four'], getreg('"', 1, 1))
501  call assert_equal('x', getreginfo('"').points_to)
502
503  call setreg('"', #{ regcontents: 'six' })
504  call assert_equal('0', getreginfo('"').points_to)
505  call assert_equal(1, +getreginfo('0').isunnamed)
506  call assert_equal(['six'], getreginfo('0').regcontents)
507  call assert_equal(['six'], getreginfo('"').regcontents)
508
509  let @x = 'one'
510  call setreg('x', {})
511  call assert_equal(1, len(split(execute('reg x'), '\n')))
512
513  call assert_fails("call setreg('0', #{regtype: 'V'}, 'v')", 'E118:')
514  call assert_fails("call setreg('0', #{regtype: 'X'})", 'E475:')
515  call assert_fails("call setreg('0', #{regtype: 'vy'})", 'E475:')
516
517  bwipe!
518endfunc
519
520func Test_v_register()
521  enew
522  call setline(1, 'nothing')
523
524  func s:Put()
525    let s:register = v:register
526    exec 'normal! "' .. v:register .. 'P'
527  endfunc
528  nnoremap <buffer> <plug>(test) :<c-u>call s:Put()<cr>
529  nmap <buffer> S <plug>(test)
530
531  let @z = "testz\n"
532  let @" = "test@\n"
533
534  let s:register = ''
535  call feedkeys('"_ddS', 'mx')
536  call assert_equal('test@', getline('.'))  " fails before 8.2.0929
537  call assert_equal('"', s:register)        " fails before 8.2.0929
538
539  let s:register = ''
540  call feedkeys('"zS', 'mx')
541  call assert_equal('z', s:register)
542
543  let s:register = ''
544  call feedkeys('"zSS', 'mx')
545  call assert_equal('"', s:register)
546
547  let s:register = ''
548  call feedkeys('"_S', 'mx')
549  call assert_equal('_', s:register)
550
551  let s:register = ''
552  normal "_ddS
553  call assert_equal('"', s:register)        " fails before 8.2.0929
554  call assert_equal('test@', getline('.'))  " fails before 8.2.0929
555
556  let s:register = ''
557  execute 'normal "z:call' "s:Put()\n"
558  call assert_equal('z', s:register)
559  call assert_equal('testz', getline('.'))
560
561  " Test operator and omap
562  let @b = 'testb'
563  func s:OpFunc(...)
564    let s:register2 = v:register
565  endfunc
566  set opfunc=s:OpFunc
567
568  normal "bg@l
569  normal S
570  call assert_equal('"', s:register)        " fails before 8.2.0929
571  call assert_equal('b', s:register2)
572
573  func s:Motion()
574    let s:register1 = v:register
575    normal! l
576  endfunc
577  onoremap <buffer> Q :<c-u>call s:Motion()<cr>
578
579  normal "bg@Q
580  normal S
581  call assert_equal('"', s:register)
582  call assert_equal('b', s:register1)
583  call assert_equal('"', s:register2)
584
585  set opfunc&
586  bwipe!
587endfunc
588
589" Test for executing the contents of a register as an Ex command with line
590" continuation.
591func Test_execute_reg_as_ex_cmd()
592  " Line continuation with just two lines
593  let code =<< trim END
594    let l = [
595      \ 1]
596  END
597  let @r = code->join("\n")
598  let l = []
599  @r
600  call assert_equal([1], l)
601
602  " Line continuation with more than two lines
603  let code =<< trim END
604    let l = [
605      \ 1,
606      \ 2,
607      \ 3]
608  END
609  let @r = code->join("\n")
610  let l = []
611  @r
612  call assert_equal([1, 2, 3], l)
613
614  " use comments interspersed with code
615  let code =<< trim END
616    let l = [
617      "\ one
618      \ 1,
619      "\ two
620      \ 2,
621      "\ three
622      \ 3]
623  END
624  let @r = code->join("\n")
625  let l = []
626  @r
627  call assert_equal([1, 2, 3], l)
628
629  " use line continuation in the middle
630  let code =<< trim END
631    let a = "one"
632    let l = [
633      \ 1,
634      \ 2]
635    let b = "two"
636  END
637  let @r = code->join("\n")
638  let l = []
639  @r
640  call assert_equal([1, 2], l)
641  call assert_equal("one", a)
642  call assert_equal("two", b)
643
644  " only one line with a \
645  let @r = "\\let l = 1"
646  call assert_fails('@r', 'E10:')
647
648  " only one line with a "\
649  let @r = '   "\ let i = 1'
650  @r
651  call assert_false(exists('i'))
652
653  " first line also begins with a \
654  let @r = "\\let l = [\n\\ 1]"
655  call assert_fails('@r', 'E10:')
656
657  " Test with a large number of lines
658  let @r = "let str = \n"
659  let @r ..= repeat("  \\ 'abcdefghijklmnopqrstuvwxyz' ..\n", 312)
660  let @r ..= '  \ ""'
661  @r
662  call assert_equal(repeat('abcdefghijklmnopqrstuvwxyz', 312), str)
663endfunc
664
665" Test for clipboard registers with ASCII NUL
666func Test_clipboard_nul()
667  CheckFeature clipboard_working
668  new
669
670  " Test for putting ASCII NUL into the clipboard
671  set clipboard=unnamed
672  call append(0, "\ntest")
673  normal ggyyp
674  call assert_equal("^@test^@", strtrans(getreg('*')))
675  call assert_equal(getline(1), getline(2))
676  let b = split(execute(":reg *"), "\n")
677  call assert_match('"\*\s*\^@test\^J',b[1])
678
679  set clipboard&vim
680  bwipe!
681endfunc
682
683func Test_ve_blockpaste()
684  new
685  set ve=all
686  0put =['QWERTZ','ASDFGH']
687  call cursor(1,1)
688  exe ":norm! \<C-V>3ljdP"
689  call assert_equal(1, col('.'))
690  call assert_equal(getline(1, 2), ['QWERTZ', 'ASDFGH'])
691  call cursor(1,1)
692  exe ":norm! \<C-V>3ljd"
693  call cursor(1,1)
694  norm! $3lP
695  call assert_equal(5, col('.'))
696  call assert_equal(getline(1, 2), ['TZ  QWER', 'GH  ASDF'])
697  set ve&vim
698  bwipe!
699endfunc
700
701func Test_insert_small_delete()
702  new
703  call setline(1, ['foo foobar bar'])
704  call cursor(1,1)
705  exe ":norm! ciw'\<C-R>-'"
706  call assert_equal(getline(1), "'foo' foobar bar")
707  exe ":norm! w.w."
708  call assert_equal(getline(1), "'foo' 'foobar' 'bar'")
709  bwipe!
710endfunc
711
712" vim: shiftwidth=2 sts=2 expandtab
713