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!
277endfunc
278
279func Test_set_register()
280  call assert_fails("call setreg('#', 200)", 'E86:')
281
282  edit Xfile_alt_1
283  let b1 = bufnr('')
284  edit Xfile_alt_2
285  let b2 = bufnr('')
286  edit Xfile_alt_3
287  let b3 = bufnr('')
288  call setreg('#', 'alt_1')
289  call assert_equal('Xfile_alt_1', getreg('#'))
290  call setreg('#', b2)
291  call assert_equal('Xfile_alt_2', getreg('#'))
292
293  let ab = 'regwrite'
294  call setreg('=', '')
295  call setreg('=', 'a', 'a')
296  call setreg('=', 'b', 'a')
297  call assert_equal('regwrite', getreg('='))
298
299  " Test for setting a list of lines to special registers
300  call setreg('/', [])
301  call assert_equal('', @/)
302  call setreg('=', [])
303  call assert_equal('', @=)
304  call assert_fails("call setreg('/', ['a', 'b'])", 'E883:')
305  call assert_fails("call setreg('=', ['a', 'b'])", 'E883:')
306  call assert_equal(0, setreg('_', ['a', 'b']))
307
308  " Test for recording to a invalid register
309  call assert_beeps('normal q$')
310
311  " Appending to a register when recording
312  call append(0, "text for clipboard test")
313  normal gg
314  call feedkeys('qrllq', 'xt')
315  call feedkeys('qRhhq', 'xt')
316  call assert_equal('llhh', getreg('r'))
317
318  " Appending a list of characters to a register from different lines
319  let @r = ''
320  call append(0, ['abcdef', '123456'])
321  normal gg"ry3l
322  call cursor(2, 4)
323  normal "Ry3l
324  call assert_equal('abc456', @r)
325
326  " Test for gP with multiple lines selected using characterwise motion
327  %delete
328  call append(0, ['vim editor', 'vim editor'])
329  let @r = ''
330  exe "normal ggwy/vim /e\<CR>gP"
331  call assert_equal(['vim editor', 'vim editor', 'vim editor'], getline(1, 3))
332
333  " Test for gP with . register
334  %delete
335  normal iabc
336  normal ".gp
337  call assert_equal('abcabc', getline(1))
338  normal 0".gP
339  call assert_equal('abcabcabc', getline(1))
340
341  let @"=''
342  call setreg('', '1')
343  call assert_equal('1', @")
344  call setreg('@', '2')
345  call assert_equal('2', @")
346
347  enew!
348endfunc
349
350" Test for clipboard registers (* and +)
351func Test_clipboard_regs()
352  CheckNotGui
353  CheckFeature clipboard_working
354
355  new
356  call append(0, "text for clipboard test")
357  normal gg"*yiw
358  call assert_equal('text', getreg('*'))
359  normal gg2w"+yiw
360  call assert_equal('clipboard', getreg('+'))
361
362  " Test for replacing the clipboard register contents
363  set clipboard=unnamed
364  let @* = 'food'
365  normal ggviw"*p
366  call assert_equal('text', getreg('*'))
367  call assert_equal('food for clipboard test', getline(1))
368  normal ggviw"*p
369  call assert_equal('food', getreg('*'))
370  call assert_equal('text for clipboard test', getline(1))
371
372  " Test for replacing the selection register contents
373  set clipboard=unnamedplus
374  let @+ = 'food'
375  normal ggviw"+p
376  call assert_equal('text', getreg('+'))
377  call assert_equal('food for clipboard test', getline(1))
378  normal ggviw"+p
379  call assert_equal('food', getreg('+'))
380  call assert_equal('text for clipboard test', getline(1))
381
382  " Test for auto copying visually selected text to clipboard register
383  call setline(1, "text for clipboard test")
384  let @* = ''
385  set clipboard=autoselect
386  normal ggwwviwy
387  call assert_equal('clipboard', @*)
388
389  " Test for auto copying visually selected text to selection register
390  let @+ = ''
391  set clipboard=autoselectplus
392  normal ggwviwy
393  call assert_equal('for', @+)
394
395  set clipboard&vim
396  bwipe!
397endfunc
398
399" Test for restarting the current mode (insert or virtual replace) after
400" executing the contents of a register
401func Test_put_reg_restart_mode()
402  new
403  call append(0, 'editor')
404  normal gg
405  let @r = "ivim \<Esc>"
406  call feedkeys("i\<C-O>@r\<C-R>=mode()\<CR>", 'xt')
407  call assert_equal('vimi editor', getline(1))
408
409  call setline(1, 'editor')
410  normal gg
411  call feedkeys("gR\<C-O>@r\<C-R>=mode()\<CR>", 'xt')
412  call assert_equal('vimReditor', getline(1))
413
414  bwipe!
415endfunc
416
417" Test for executing a register using :@ command
418func Test_execute_register()
419  call setreg('r', [])
420  call assert_beeps('@r')
421  let i = 1
422  let @q = 'let i+= 1'
423  @q
424  @
425  call assert_equal(3, i)
426
427  " try to execute expression register and use a backspace to cancel it
428  new
429  call feedkeys("@=\<BS>ax\<CR>y", 'xt')
430  call assert_equal(['x', 'y'], getline(1, '$'))
431  close!
432
433  " cannot execute a register in operator pending mode
434  call assert_beeps('normal! c@r')
435endfunc
436
437" Test for getting register info
438func Test_get_reginfo()
439  enew
440  call setline(1, ['foo', 'bar'])
441
442  exe 'norm! "zyy'
443  let info = getreginfo('"')
444  call assert_equal('z', info.points_to)
445  call setreg('y', 'baz')
446  call assert_equal('z', getreginfo('').points_to)
447  call setreg('y', { 'isunnamed': v:true })
448  call assert_equal('y', getreginfo('"').points_to)
449
450  exe '$put'
451  call assert_equal(getreg('y'), getline(3))
452  call setreg('', 'qux')
453  call assert_equal('0', getreginfo('').points_to)
454  call setreg('x', 'quux')
455  call assert_equal('0', getreginfo('').points_to)
456
457  let info = getreginfo('')
458  call assert_equal(getreg('', 1, 1), info.regcontents)
459  call assert_equal(getregtype(''), info.regtype)
460
461  exe "norm! 0\<c-v>e" .. '"zy'
462  let info = getreginfo('z')
463  call assert_equal(getreg('z', 1, 1), info.regcontents)
464  call assert_equal(getregtype('z'), info.regtype)
465  call assert_equal(1, +info.isunnamed)
466
467  let info = getreginfo('"')
468  call assert_equal('z', info.points_to)
469
470  bwipe!
471endfunc
472
473" Test for restoring register with dict from getreginfo
474func Test_set_register_dict()
475  enew!
476
477  call setreg('"', #{ regcontents: ['one', 'two'],
478        \ regtype: 'V', points_to: 'z' })
479  call assert_equal(['one', 'two'], getreg('"', 1, 1))
480  let info = getreginfo('"')
481  call assert_equal('z', info.points_to)
482  call assert_equal('V', info.regtype)
483  call assert_equal(1, +getreginfo('z').isunnamed)
484
485  call setreg('x', #{ regcontents: ['three', 'four'],
486        \ regtype: 'v', isunnamed: v:true })
487  call assert_equal(['three', 'four'], getreg('"', 1, 1))
488  let info = getreginfo('"')
489  call assert_equal('x', info.points_to)
490  call assert_equal('v', info.regtype)
491  call assert_equal(1, +getreginfo('x').isunnamed)
492
493  call setreg('y', #{ regcontents: 'five',
494        \ regtype: "\<c-v>", isunnamed: v:false })
495  call assert_equal("\<c-v>4", getreginfo('y').regtype)
496  call assert_equal(0, +getreginfo('y').isunnamed)
497  call assert_equal(['three', 'four'], getreg('"', 1, 1))
498  call assert_equal('x', getreginfo('"').points_to)
499
500  call setreg('"', #{ regcontents: 'six' })
501  call assert_equal('0', getreginfo('"').points_to)
502  call assert_equal(1, +getreginfo('0').isunnamed)
503  call assert_equal(['six'], getreginfo('0').regcontents)
504  call assert_equal(['six'], getreginfo('"').regcontents)
505
506  let @x = 'one'
507  call setreg('x', {})
508  call assert_equal(1, len(split(execute('reg x'), '\n')))
509
510  call assert_fails("call setreg('0', #{regtype: 'V'}, 'v')", 'E118:')
511  call assert_fails("call setreg('0', #{regtype: 'X'})", 'E475:')
512  call assert_fails("call setreg('0', #{regtype: 'vy'})", 'E475:')
513
514  bwipe!
515endfunc
516
517func Test_v_register()
518  enew
519  call setline(1, 'nothing')
520
521  func s:Put()
522    let s:register = v:register
523    exec 'normal! "' .. v:register .. 'P'
524  endfunc
525  nnoremap <buffer> <plug>(test) :<c-u>call s:Put()<cr>
526  nmap <buffer> S <plug>(test)
527
528  let @z = "testz\n"
529  let @" = "test@\n"
530
531  let s:register = ''
532  call feedkeys('"_ddS', 'mx')
533  call assert_equal('test@', getline('.'))  " fails before 8.2.0929
534  call assert_equal('"', s:register)        " fails before 8.2.0929
535
536  let s:register = ''
537  call feedkeys('"zS', 'mx')
538  call assert_equal('z', s:register)
539
540  let s:register = ''
541  call feedkeys('"zSS', 'mx')
542  call assert_equal('"', s:register)
543
544  let s:register = ''
545  call feedkeys('"_S', 'mx')
546  call assert_equal('_', s:register)
547
548  let s:register = ''
549  normal "_ddS
550  call assert_equal('"', s:register)        " fails before 8.2.0929
551  call assert_equal('test@', getline('.'))  " fails before 8.2.0929
552
553  let s:register = ''
554  execute 'normal "z:call' "s:Put()\n"
555  call assert_equal('z', s:register)
556  call assert_equal('testz', getline('.'))
557
558  " Test operator and omap
559  let @b = 'testb'
560  func s:OpFunc(...)
561    let s:register2 = v:register
562  endfunc
563  set opfunc=s:OpFunc
564
565  normal "bg@l
566  normal S
567  call assert_equal('"', s:register)        " fails before 8.2.0929
568  call assert_equal('b', s:register2)
569
570  func s:Motion()
571    let s:register1 = v:register
572    normal! l
573  endfunc
574  onoremap <buffer> Q :<c-u>call s:Motion()<cr>
575
576  normal "bg@Q
577  normal S
578  call assert_equal('"', s:register)
579  call assert_equal('b', s:register1)
580  call assert_equal('"', s:register2)
581
582  set opfunc&
583  bwipe!
584endfunc
585
586" Test for executing the contents of a register as an Ex command with line
587" continuation.
588func Test_execute_reg_as_ex_cmd()
589  " Line continuation with just two lines
590  let code =<< trim END
591    let l = [
592      \ 1]
593  END
594  let @r = code->join("\n")
595  let l = []
596  @r
597  call assert_equal([1], l)
598
599  " Line continuation with more than two lines
600  let code =<< trim END
601    let l = [
602      \ 1,
603      \ 2,
604      \ 3]
605  END
606  let @r = code->join("\n")
607  let l = []
608  @r
609  call assert_equal([1, 2, 3], l)
610
611  " use comments interspersed with code
612  let code =<< trim END
613    let l = [
614      "\ one
615      \ 1,
616      "\ two
617      \ 2,
618      "\ three
619      \ 3]
620  END
621  let @r = code->join("\n")
622  let l = []
623  @r
624  call assert_equal([1, 2, 3], l)
625
626  " use line continuation in the middle
627  let code =<< trim END
628    let a = "one"
629    let l = [
630      \ 1,
631      \ 2]
632    let b = "two"
633  END
634  let @r = code->join("\n")
635  let l = []
636  @r
637  call assert_equal([1, 2], l)
638  call assert_equal("one", a)
639  call assert_equal("two", b)
640
641  " only one line with a \
642  let @r = "\\let l = 1"
643  call assert_fails('@r', 'E10:')
644
645  " only one line with a "\
646  let @r = '   "\ let i = 1'
647  @r
648  call assert_false(exists('i'))
649
650  " first line also begins with a \
651  let @r = "\\let l = [\n\\ 1]"
652  call assert_fails('@r', 'E10:')
653
654  " Test with a large number of lines
655  let @r = "let str = \n"
656  let @r ..= repeat("  \\ 'abcdefghijklmnopqrstuvwxyz' ..\n", 312)
657  let @r ..= '  \ ""'
658  @r
659  call assert_equal(repeat('abcdefghijklmnopqrstuvwxyz', 312), str)
660endfunc
661
662" Test for clipboard registers with ASCII NUL
663func Test_clipboard_nul()
664  CheckFeature clipboard_working
665  new
666
667  " Test for putting ASCII NUL into the clipboard
668  set clipboard=unnamed
669  call append(0, "\ntest")
670  normal ggyyp
671  call assert_equal("^@test^@", strtrans(getreg('*')))
672  call assert_equal(getline(1), getline(2))
673  let b = split(execute(":reg *"), "\n")
674  call assert_match('"\*\s*\^@test\^J',b[1])
675
676  set clipboard&vim
677  bwipe!
678endfunc
679
680func Test_ve_blockpaste()
681  new
682  set ve=all
683  0put =['QWERTZ','ASDFGH']
684  call cursor(1,1)
685  exe ":norm! \<C-V>3ljdP"
686  call assert_equal(1, col('.'))
687  call assert_equal(getline(1, 2), ['QWERTZ', 'ASDFGH'])
688  call cursor(1,1)
689  exe ":norm! \<C-V>3ljd"
690  call cursor(1,1)
691  norm! $3lP
692  call assert_equal(5, col('.'))
693  call assert_equal(getline(1, 2), ['TZ  QWER', 'GH  ASDF'])
694  set ve&vim
695  bwipe!
696endfunc
697
698" vim: shiftwidth=2 sts=2 expandtab
699