1"
2" Tests for register operations
3"
4
5source check.vim
6
7" This test must be executed first to check for empty and unset registers.
8func Test_aaa_empty_reg_test()
9  call assert_fails('normal @@', 'E748:')
10  call assert_fails('normal @%', 'E354:')
11  call assert_fails('normal @#', 'E354:')
12  call assert_fails('normal @!', 'E354:')
13  call assert_fails('normal @:', 'E30:')
14  call assert_fails('normal @.', 'E29:')
15  call assert_fails('put /', 'E35:')
16  call assert_fails('put .', 'E29:')
17endfunc
18
19func Test_yank_shows_register()
20    enew
21    set report=0
22    call setline(1, ['foo', 'bar'])
23    " Line-wise
24    exe 'norm! yy'
25    call assert_equal('1 line yanked', v:statusmsg)
26    exe 'norm! "zyy'
27    call assert_equal('1 line yanked into "z', v:statusmsg)
28    exe 'norm! yj'
29    call assert_equal('2 lines yanked', v:statusmsg)
30    exe 'norm! "zyj'
31    call assert_equal('2 lines yanked into "z', v:statusmsg)
32
33    " Block-wise
34    exe "norm! \<C-V>y"
35    call assert_equal('block of 1 line yanked', v:statusmsg)
36    exe "norm! \<C-V>\"zy"
37    call assert_equal('block of 1 line yanked into "z', v:statusmsg)
38    exe "norm! \<C-V>jy"
39    call assert_equal('block of 2 lines yanked', v:statusmsg)
40    exe "norm! \<C-V>j\"zy"
41    call assert_equal('block of 2 lines yanked into "z', v:statusmsg)
42
43    bwipe!
44endfunc
45
46func Test_display_registers()
47    e file1
48    e file2
49    call setline(1, ['foo', 'bar'])
50    /bar
51    exe 'norm! y2l"axx'
52    call feedkeys("i\<C-R>=2*4\n\<esc>")
53    call feedkeys(":ls\n", 'xt')
54
55    let a = execute('display')
56    let b = execute('registers')
57
58    call assert_equal(a, b)
59    call assert_match('^\nType Name Content\n'
60          \ .         '  c  ""   a\n'
61          \ .         '  c  "0   ba\n'
62          \ .         '  c  "a   b\n'
63          \ .         '.*'
64          \ .         '  c  "-   a\n'
65          \ .         '.*'
66          \ .         '  c  ":   ls\n'
67          \ .         '  c  "%   file2\n'
68          \ .         '  c  "#   file1\n'
69          \ .         '  c  "/   bar\n'
70          \ .         '  c  "=   2\*4', a)
71
72    let a = execute('registers a')
73    call assert_match('^\nType Name Content\n'
74          \ .         '  c  "a   b', a)
75
76    let a = execute('registers :')
77    call assert_match('^\nType Name Content\n'
78          \ .         '  c  ":   ls', a)
79
80    bwipe!
81endfunc
82
83func Test_register_one()
84  " delete a line goes into register one
85  new
86  call setline(1, "one")
87  normal dd
88  call assert_equal("one\n", @1)
89
90  " delete a word does not change register one, does change "-
91  call setline(1, "two")
92  normal de
93  call assert_equal("one\n", @1)
94  call assert_equal("two", @-)
95
96  " delete a word with a register does not change register one
97  call setline(1, "three")
98  normal "ade
99  call assert_equal("three", @a)
100  call assert_equal("one\n", @1)
101
102  " delete a word with register DOES change register one with one of a list of
103  " operators
104  " %
105  call setline(1, ["(12)3"])
106  normal "ad%
107  call assert_equal("(12)", @a)
108  call assert_equal("(12)", @1)
109
110  " (
111  call setline(1, ["first second"])
112  normal $"ad(
113  call assert_equal("first secon", @a)
114  call assert_equal("first secon", @1)
115
116  " )
117  call setline(1, ["First Second."])
118  normal gg0"ad)
119  call assert_equal("First Second.", @a)
120  call assert_equal("First Second.", @1)
121
122  " `
123  call setline(1, ["start here."])
124  normal gg0fhmx0"ad`x
125  call assert_equal("start ", @a)
126  call assert_equal("start ", @1)
127
128  " /
129  call setline(1, ["searchX"])
130  exe "normal gg0\"ad/X\<CR>"
131  call assert_equal("search", @a)
132  call assert_equal("search", @1)
133
134  " ?
135  call setline(1, ["Ysearch"])
136  exe "normal gg$\"ad?Y\<CR>"
137  call assert_equal("Ysearc", @a)
138  call assert_equal("Ysearc", @1)
139
140  " n
141  call setline(1, ["Ynext"])
142  normal gg$"adn
143  call assert_equal("Ynex", @a)
144  call assert_equal("Ynex", @1)
145
146  " N
147  call setline(1, ["prevY"])
148  normal gg0"adN
149  call assert_equal("prev", @a)
150  call assert_equal("prev", @1)
151
152  " }
153  call setline(1, ["one", ""])
154  normal gg0"ad}
155  call assert_equal("one\n", @a)
156  call assert_equal("one\n", @1)
157
158  " {
159  call setline(1, ["", "two"])
160  normal 2G$"ad{
161  call assert_equal("\ntw", @a)
162  call assert_equal("\ntw", @1)
163
164  bwipe!
165endfunc
166
167" Check that replaying a typed sequence does not use an Esc and following
168" characters as an escape sequence.
169func Test_recording_esc_sequence()
170  new
171  try
172    let save_F2 = &t_F2
173  catch
174  endtry
175  let t_F2 = "\<Esc>OQ"
176  call feedkeys("qqiTest\<Esc>", "xt")
177  call feedkeys("OQuirk\<Esc>q", "xt")
178  call feedkeys("Go\<Esc>@q", "xt")
179  call assert_equal(['Quirk', 'Test', 'Quirk', 'Test'], getline(1, 4))
180  bwipe!
181  if exists('save_F2')
182    let &t_F2 = save_F2
183  else
184    set t_F2=
185  endif
186endfunc
187
188" Test for executing the last used register (@)
189func Test_last_used_exec_reg()
190  " Test for the @: command
191  let a = ''
192  call feedkeys(":let a ..= 'Vim'\<CR>", 'xt')
193  normal @:
194  call assert_equal('VimVim', a)
195
196  " Test for the @= command
197  let x = ''
198  let a = ":let x ..= 'Vim'\<CR>"
199  exe "normal @=a\<CR>"
200  normal @@
201  call assert_equal('VimVim', x)
202
203  " Test for the @. command
204  let a = ''
205  call feedkeys("i:let a ..= 'Edit'\<CR>", 'xt')
206  normal @.
207  normal @@
208  call assert_equal('EditEdit', a)
209
210  " Test for repeating the last command-line in visual mode
211  call append(0, 'register')
212  normal gg
213  let @r = ''
214  call feedkeys("v:yank R\<CR>", 'xt')
215  call feedkeys("v@:", 'xt')
216  call assert_equal("\nregister\nregister\n", @r)
217
218  enew!
219endfunc
220
221func Test_get_register()
222  enew
223  edit Xfile1
224  edit Xfile2
225  call assert_equal('Xfile2', getreg('%'))
226  call assert_equal('Xfile1', getreg('#'))
227
228  call feedkeys("iTwo\<Esc>", 'xt')
229  call assert_equal('Two', getreg('.'))
230  call assert_equal('', getreg('_'))
231  call assert_beeps('normal ":yy')
232  call assert_beeps('normal "%yy')
233  call assert_beeps('normal ".yy')
234
235  call assert_equal('', getreg("\<C-F>"))
236  call assert_equal('', getreg("\<C-W>"))
237  call assert_equal('', getreg("\<C-L>"))
238  " Change the last used register to '"' for the next test
239  normal! ""yy
240  let @" = 'happy'
241  call assert_equal('happy', getreg())
242  call assert_equal('happy', getreg(''))
243
244  call assert_equal('', getregtype('!'))
245  call assert_fails('echo getregtype([])', 'E730:')
246  call assert_equal('v', getregtype())
247  call assert_equal('v', getregtype(''))
248
249  " Test for inserting an invalid register content
250  call assert_beeps('exe "normal i\<C-R>!"')
251
252  " Test for inserting a register with multiple lines
253  call deletebufline('', 1, '$')
254  call setreg('r', ['a', 'b'])
255  exe "normal i\<C-R>r"
256  call assert_equal(['a', 'b', ''], getline(1, '$'))
257
258  " Test for inserting a multi-line register in the command line
259  call feedkeys(":\<C-R>r\<Esc>", 'xt')
260  call assert_equal("a\rb\r", histget(':', -1))
261
262  call assert_fails('let r = getreg("=", [])', 'E745:')
263  call assert_fails('let r = getreg("=", 1, [])', 'E745:')
264  enew!
265endfunc
266
267func Test_set_register()
268  call assert_fails("call setreg('#', 200)", 'E86:')
269
270  edit Xfile_alt_1
271  let b1 = bufnr('')
272  edit Xfile_alt_2
273  let b2 = bufnr('')
274  edit Xfile_alt_3
275  let b3 = bufnr('')
276  call setreg('#', 'alt_1')
277  call assert_equal('Xfile_alt_1', getreg('#'))
278  call setreg('#', b2)
279  call assert_equal('Xfile_alt_2', getreg('#'))
280
281  let ab = 'regwrite'
282  call setreg('=', '')
283  call setreg('=', 'a', 'a')
284  call setreg('=', 'b', 'a')
285  call assert_equal('regwrite', getreg('='))
286
287  " Test for setting a list of lines to special registers
288  call setreg('/', [])
289  call assert_equal('', @/)
290  call setreg('=', [])
291  call assert_equal('', @=)
292  call assert_fails("call setreg('/', ['a', 'b'])", 'E883:')
293  call assert_fails("call setreg('=', ['a', 'b'])", 'E883:')
294  call assert_equal(0, setreg('_', ['a', 'b']))
295
296  " Test for recording to a invalid register
297  call assert_beeps('normal q$')
298
299  " Appending to a register when recording
300  call append(0, "text for clipboard test")
301  normal gg
302  call feedkeys('qrllq', 'xt')
303  call feedkeys('qRhhq', 'xt')
304  call assert_equal('llhh', getreg('r'))
305
306  " Appending a list of characters to a register from different lines
307  let @r = ''
308  call append(0, ['abcdef', '123456'])
309  normal gg"ry3l
310  call cursor(2, 4)
311  normal "Ry3l
312  call assert_equal('abc456', @r)
313
314  " Test for gP with multiple lines selected using characterwise motion
315  %delete
316  call append(0, ['vim editor', 'vim editor'])
317  let @r = ''
318  exe "normal ggwy/vim /e\<CR>gP"
319  call assert_equal(['vim editor', 'vim editor', 'vim editor'], getline(1, 3))
320
321  " Test for gP with . register
322  %delete
323  normal iabc
324  normal ".gp
325  call assert_equal('abcabc', getline(1))
326  normal 0".gP
327  call assert_equal('abcabcabc', getline(1))
328
329  let @"=''
330  call setreg('', '1')
331  call assert_equal('1', @")
332  call setreg('@', '2')
333  call assert_equal('2', @")
334
335  enew!
336endfunc
337
338" Test for clipboard registers (* and +)
339func Test_clipboard_regs()
340  CheckNotGui
341  CheckFeature clipboard_working
342
343  new
344  call append(0, "text for clipboard test")
345  normal gg"*yiw
346  call assert_equal('text', getreg('*'))
347  normal gg2w"+yiw
348  call assert_equal('clipboard', getreg('+'))
349
350  " Test for replacing the clipboard register contents
351  set clipboard=unnamed
352  let @* = 'food'
353  normal ggviw"*p
354  call assert_equal('text', getreg('*'))
355  call assert_equal('food for clipboard test', getline(1))
356  normal ggviw"*p
357  call assert_equal('food', getreg('*'))
358  call assert_equal('text for clipboard test', getline(1))
359
360  " Test for replacing the selection register contents
361  set clipboard=unnamedplus
362  let @+ = 'food'
363  normal ggviw"+p
364  call assert_equal('text', getreg('+'))
365  call assert_equal('food for clipboard test', getline(1))
366  normal ggviw"+p
367  call assert_equal('food', getreg('+'))
368  call assert_equal('text for clipboard test', getline(1))
369
370  " Test for auto copying visually selected text to clipboard register
371  call setline(1, "text for clipboard test")
372  let @* = ''
373  set clipboard=autoselect
374  normal ggwwviwy
375  call assert_equal('clipboard', @*)
376
377  " Test for auto copying visually selected text to selection register
378  let @+ = ''
379  set clipboard=autoselectplus
380  normal ggwviwy
381  call assert_equal('for', @+)
382
383  set clipboard&vim
384  bwipe!
385endfunc
386
387" Test for restarting the current mode (insert or virtual replace) after
388" executing the contents of a register
389func Test_put_reg_restart_mode()
390  new
391  call append(0, 'editor')
392  normal gg
393  let @r = "ivim \<Esc>"
394  call feedkeys("i\<C-O>@r\<C-R>=mode()\<CR>", 'xt')
395  call assert_equal('vimi editor', getline(1))
396
397  call setline(1, 'editor')
398  normal gg
399  call feedkeys("gR\<C-O>@r\<C-R>=mode()\<CR>", 'xt')
400  call assert_equal('vimReditor', getline(1))
401
402  bwipe!
403endfunc
404
405" Test for executing a register using :@ command
406func Test_execute_register()
407  call setreg('r', [])
408  call assert_beeps('@r')
409  let i = 1
410  let @q = 'let i+= 1'
411  @q
412  @
413  call assert_equal(3, i)
414
415  " cannot execute a register in operator pending mode
416  call assert_beeps('normal! c@r')
417endfunc
418
419" Test for getting register info
420func Test_get_reginfo()
421  enew
422  call setline(1, ['foo', 'bar'])
423
424  exe 'norm! "zyy'
425  let info = getreginfo('"')
426  call assert_equal('z', info.points_to)
427  call setreg('y', 'baz')
428  call assert_equal('z', getreginfo('').points_to)
429  call setreg('y', { 'isunnamed': v:true })
430  call assert_equal('y', getreginfo('"').points_to)
431
432  exe '$put'
433  call assert_equal(getreg('y'), getline(3))
434  call setreg('', 'qux')
435  call assert_equal('0', getreginfo('').points_to)
436  call setreg('x', 'quux')
437  call assert_equal('0', getreginfo('').points_to)
438
439  let info = getreginfo('')
440  call assert_equal(getreg('', 1, 1), info.regcontents)
441  call assert_equal(getregtype(''), info.regtype)
442
443  exe "norm! 0\<c-v>e" .. '"zy'
444  let info = getreginfo('z')
445  call assert_equal(getreg('z', 1, 1), info.regcontents)
446  call assert_equal(getregtype('z'), info.regtype)
447  call assert_equal(1, +info.isunnamed)
448
449  let info = getreginfo('"')
450  call assert_equal('z', info.points_to)
451
452  bwipe!
453endfunc
454
455" Test for restoring register with dict from getreginfo
456func Test_set_register_dict()
457  enew!
458
459  call setreg('"', #{ regcontents: ['one', 'two'],
460        \ regtype: 'V', points_to: 'z' })
461  call assert_equal(['one', 'two'], getreg('"', 1, 1))
462  let info = getreginfo('"')
463  call assert_equal('z', info.points_to)
464  call assert_equal('V', info.regtype)
465  call assert_equal(1, +getreginfo('z').isunnamed)
466
467  call setreg('x', #{ regcontents: ['three', 'four'],
468        \ regtype: 'v', isunnamed: v:true })
469  call assert_equal(['three', 'four'], getreg('"', 1, 1))
470  let info = getreginfo('"')
471  call assert_equal('x', info.points_to)
472  call assert_equal('v', info.regtype)
473  call assert_equal(1, +getreginfo('x').isunnamed)
474
475  call setreg('y', #{ regcontents: 'five',
476        \ regtype: "\<c-v>", isunnamed: v:false })
477  call assert_equal("\<c-v>4", getreginfo('y').regtype)
478  call assert_equal(0, +getreginfo('y').isunnamed)
479  call assert_equal(['three', 'four'], getreg('"', 1, 1))
480  call assert_equal('x', getreginfo('"').points_to)
481
482  call setreg('"', #{ regcontents: 'six' })
483  call assert_equal('0', getreginfo('"').points_to)
484  call assert_equal(1, +getreginfo('0').isunnamed)
485  call assert_equal(['six'], getreginfo('0').regcontents)
486  call assert_equal(['six'], getreginfo('"').regcontents)
487
488  bwipe!
489endfunc
490
491" vim: shiftwidth=2 sts=2 expandtab
492