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  enew!
263endfunc
264
265func Test_set_register()
266  call assert_fails("call setreg('#', 200)", 'E86:')
267
268  edit Xfile_alt_1
269  let b1 = bufnr('')
270  edit Xfile_alt_2
271  let b2 = bufnr('')
272  edit Xfile_alt_3
273  let b3 = bufnr('')
274  call setreg('#', 'alt_1')
275  call assert_equal('Xfile_alt_1', getreg('#'))
276  call setreg('#', b2)
277  call assert_equal('Xfile_alt_2', getreg('#'))
278
279  let ab = 'regwrite'
280  call setreg('=', '')
281  call setreg('=', 'a', 'a')
282  call setreg('=', 'b', 'a')
283  call assert_equal('regwrite', getreg('='))
284
285  " Test for setting a list of lines to special registers
286  call setreg('/', [])
287  call assert_equal('', @/)
288  call setreg('=', [])
289  call assert_equal('', @=)
290  call assert_fails("call setreg('/', ['a', 'b'])", 'E883:')
291  call assert_fails("call setreg('=', ['a', 'b'])", 'E883:')
292  call assert_equal(0, setreg('_', ['a', 'b']))
293
294  " Test for recording to a invalid register
295  call assert_beeps('normal q$')
296
297  " Appending to a register when recording
298  call append(0, "text for clipboard test")
299  normal gg
300  call feedkeys('qrllq', 'xt')
301  call feedkeys('qRhhq', 'xt')
302  call assert_equal('llhh', getreg('r'))
303
304  " Appending a list of characters to a register from different lines
305  let @r = ''
306  call append(0, ['abcdef', '123456'])
307  normal gg"ry3l
308  call cursor(2, 4)
309  normal "Ry3l
310  call assert_equal('abc456', @r)
311
312  " Test for gP with multiple lines selected using characterwise motion
313  %delete
314  call append(0, ['vim editor', 'vim editor'])
315  let @r = ''
316  exe "normal ggwy/vim /e\<CR>gP"
317  call assert_equal(['vim editor', 'vim editor', 'vim editor'], getline(1, 3))
318
319  " Test for gP with . register
320  %delete
321  normal iabc
322  normal ".gp
323  call assert_equal('abcabc', getline(1))
324  normal 0".gP
325  call assert_equal('abcabcabc', getline(1))
326
327  let @"=''
328  call setreg('', '1')
329  call assert_equal('1', @")
330  call setreg('@', '2')
331  call assert_equal('2', @")
332
333  enew!
334endfunc
335
336" Test for clipboard registers (* and +)
337func Test_clipboard_regs()
338  CheckNotGui
339  CheckFeature clipboard_working
340
341  new
342  call append(0, "text for clipboard test")
343  normal gg"*yiw
344  call assert_equal('text', getreg('*'))
345  normal gg2w"+yiw
346  call assert_equal('clipboard', getreg('+'))
347
348  " Test for replacing the clipboard register contents
349  set clipboard=unnamed
350  let @* = 'food'
351  normal ggviw"*p
352  call assert_equal('text', getreg('*'))
353  call assert_equal('food for clipboard test', getline(1))
354  normal ggviw"*p
355  call assert_equal('food', getreg('*'))
356  call assert_equal('text for clipboard test', getline(1))
357
358  " Test for replacing the selection register contents
359  set clipboard=unnamedplus
360  let @+ = 'food'
361  normal ggviw"+p
362  call assert_equal('text', getreg('+'))
363  call assert_equal('food for clipboard test', getline(1))
364  normal ggviw"+p
365  call assert_equal('food', getreg('+'))
366  call assert_equal('text for clipboard test', getline(1))
367
368  " Test for auto copying visually selected text to clipboard register
369  call setline(1, "text for clipboard test")
370  let @* = ''
371  set clipboard=autoselect
372  normal ggwwviwy
373  call assert_equal('clipboard', @*)
374
375  " Test for auto copying visually selected text to selection register
376  let @+ = ''
377  set clipboard=autoselectplus
378  normal ggwviwy
379  call assert_equal('for', @+)
380
381  set clipboard&vim
382  bwipe!
383endfunc
384
385" Test for restarting the current mode (insert or virtual replace) after
386" executing the contents of a register
387func Test_put_reg_restart_mode()
388  new
389  call append(0, 'editor')
390  normal gg
391  let @r = "ivim \<Esc>"
392  call feedkeys("i\<C-O>@r\<C-R>=mode()\<CR>", 'xt')
393  call assert_equal('vimi editor', getline(1))
394
395  call setline(1, 'editor')
396  normal gg
397  call feedkeys("gR\<C-O>@r\<C-R>=mode()\<CR>", 'xt')
398  call assert_equal('vimReditor', getline(1))
399
400  bwipe!
401endfunc
402
403" Test for executing a register using :@ command
404func Test_execute_register()
405  call setreg('r', [])
406  call assert_beeps('@r')
407  let i = 1
408  let @q = 'let i+= 1'
409  @q
410  @
411  call assert_equal(3, i)
412
413  " cannot execute a register in operator pending mode
414  call assert_beeps('normal! c@r')
415endfunc
416
417" vim: shiftwidth=2 sts=2 expandtab
418