blob: 352264981283bb914bb35b45776a547991b29ff9 [file] [log] [blame]
Bram Moolenaar71136db2019-11-30 19:48:46 +01001" Tests for register operations
Bram Moolenaar71136db2019-11-30 19:48:46 +01002
Bram Moolenaar54c8d222019-12-02 20:41:39 +01003source check.vim
Bram Moolenaar11a5b192020-07-10 21:17:51 +02004source view_util.vim
Bram Moolenaar54c8d222019-12-02 20:41:39 +01005
Bram Moolenaar71136db2019-11-30 19:48:46 +01006" 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:')
Bram Moolenaar6f1f0ca2019-12-01 18:16:18 +010014 call assert_fails('put /', 'E35:')
15 call assert_fails('put .', 'E29:')
Bram Moolenaar71136db2019-11-30 19:48:46 +010016endfunc
Bram Moolenaare45deb72017-07-16 17:56:16 +020017
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
Bram Moolenaar7ce551f2018-05-06 17:32:19 +020044
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)
Bram Moolenaar3691f1e2019-10-24 20:17:00 +020058 call assert_match('^\nType Name Content\n'
59 \ . ' c "" a\n'
60 \ . ' c "0 ba\n'
61 \ . ' c "a b\n'
Bram Moolenaar7ce551f2018-05-06 17:32:19 +020062 \ . '.*'
Bram Moolenaar3691f1e2019-10-24 20:17:00 +020063 \ . ' c "- a\n'
Bram Moolenaar7ce551f2018-05-06 17:32:19 +020064 \ . '.*'
Bram Moolenaar3691f1e2019-10-24 20:17:00 +020065 \ . ' c ": ls\n'
66 \ . ' c "% file2\n'
67 \ . ' c "# file1\n'
68 \ . ' c "/ bar\n'
69 \ . ' c "= 2\*4', a)
Bram Moolenaar7ce551f2018-05-06 17:32:19 +020070
71 let a = execute('registers a')
Bram Moolenaar3691f1e2019-10-24 20:17:00 +020072 call assert_match('^\nType Name Content\n'
73 \ . ' c "a b', a)
Bram Moolenaar7ce551f2018-05-06 17:32:19 +020074
75 let a = execute('registers :')
Bram Moolenaar3691f1e2019-10-24 20:17:00 +020076 call assert_match('^\nType Name Content\n'
77 \ . ' c ": ls', a)
Bram Moolenaar7ce551f2018-05-06 17:32:19 +020078
79 bwipe!
80endfunc
Bram Moolenaar9d7fdd42019-03-08 09:50:52 +010081
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
Bram Moolenaar6edbbd82019-03-10 09:41:51 +0100165
Bram Moolenaar11a5b192020-07-10 21:17:51 +0200166func 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
Bram Moolenaar6edbbd82019-03-10 09:41:51 +0100179" 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
Bram Moolenaar55d81cd2019-03-11 08:05:50 +0100183 try
184 let save_F2 = &t_F2
185 catch
186 endtry
Bram Moolenaar6edbbd82019-03-10 09:41:51 +0100187 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!
Bram Moolenaar55d81cd2019-03-11 08:05:50 +0100193 if exists('save_F2')
Bram Moolenaara5e44602019-05-25 21:52:30 +0200194 let &t_F2 = save_F2
Bram Moolenaar037c54f2019-04-20 23:47:46 +0200195 else
196 set t_F2=
Bram Moolenaar55d81cd2019-03-11 08:05:50 +0100197 endif
Bram Moolenaar6edbbd82019-03-10 09:41:51 +0100198endfunc
Bram Moolenaar71136db2019-11-30 19:48:46 +0100199
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000200func Test_recording_with_select_mode()
201 new
202 call feedkeys("qacc12345\<Esc>gH98765\<Esc>q", "tx")
203 call assert_equal("98765", getline(1))
204 call assert_equal("cc12345\<Esc>gH98765\<Esc>", @a)
205 call setline(1, 'asdf')
206 normal! @a
207 call assert_equal("98765", getline(1))
208 bwipe!
209endfunc
210
Bram Moolenaar71136db2019-11-30 19:48:46 +0100211" Test for executing the last used register (@)
212func Test_last_used_exec_reg()
213 " Test for the @: command
214 let a = ''
215 call feedkeys(":let a ..= 'Vim'\<CR>", 'xt')
216 normal @:
217 call assert_equal('VimVim', a)
218
219 " Test for the @= command
220 let x = ''
221 let a = ":let x ..= 'Vim'\<CR>"
222 exe "normal @=a\<CR>"
223 normal @@
224 call assert_equal('VimVim', x)
225
226 " Test for the @. command
227 let a = ''
228 call feedkeys("i:let a ..= 'Edit'\<CR>", 'xt')
229 normal @.
230 normal @@
231 call assert_equal('EditEdit', a)
232
Bram Moolenaar6f1f0ca2019-12-01 18:16:18 +0100233 " Test for repeating the last command-line in visual mode
234 call append(0, 'register')
235 normal gg
236 let @r = ''
237 call feedkeys("v:yank R\<CR>", 'xt')
238 call feedkeys("v@:", 'xt')
239 call assert_equal("\nregister\nregister\n", @r)
240
Bram Moolenaar71136db2019-11-30 19:48:46 +0100241 enew!
242endfunc
243
244func Test_get_register()
245 enew
246 edit Xfile1
247 edit Xfile2
248 call assert_equal('Xfile2', getreg('%'))
249 call assert_equal('Xfile1', getreg('#'))
250
251 call feedkeys("iTwo\<Esc>", 'xt')
252 call assert_equal('Two', getreg('.'))
253 call assert_equal('', getreg('_'))
254 call assert_beeps('normal ":yy')
255 call assert_beeps('normal "%yy')
256 call assert_beeps('normal ".yy')
257
258 call assert_equal('', getreg("\<C-F>"))
259 call assert_equal('', getreg("\<C-W>"))
260 call assert_equal('', getreg("\<C-L>"))
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100261 " Change the last used register to '"' for the next test
262 normal! ""yy
263 let @" = 'happy'
264 call assert_equal('happy', getreg())
265 call assert_equal('happy', getreg(''))
Bram Moolenaar71136db2019-11-30 19:48:46 +0100266
267 call assert_equal('', getregtype('!'))
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100268 call assert_fails('echo getregtype([])', 'E730:')
269 call assert_equal('v', getregtype())
270 call assert_equal('v', getregtype(''))
Bram Moolenaar71136db2019-11-30 19:48:46 +0100271
Bram Moolenaar6f1f0ca2019-12-01 18:16:18 +0100272 " Test for inserting an invalid register content
273 call assert_beeps('exe "normal i\<C-R>!"')
274
275 " Test for inserting a register with multiple lines
276 call deletebufline('', 1, '$')
277 call setreg('r', ['a', 'b'])
278 exe "normal i\<C-R>r"
279 call assert_equal(['a', 'b', ''], getline(1, '$'))
280
281 " Test for inserting a multi-line register in the command line
282 call feedkeys(":\<C-R>r\<Esc>", 'xt')
283 call assert_equal("a\rb\r", histget(':', -1))
284
Bram Moolenaar99fa7212020-04-26 15:59:55 +0200285 call assert_fails('let r = getreg("=", [])', 'E745:')
286 call assert_fails('let r = getreg("=", 1, [])', 'E745:')
Bram Moolenaar71136db2019-11-30 19:48:46 +0100287 enew!
Bram Moolenaard1ad99b2020-10-04 16:16:54 +0200288
289 " Using a register in operator-pending mode should fail
290 call assert_beeps('norm! c"')
Bram Moolenaar71136db2019-11-30 19:48:46 +0100291endfunc
292
293func Test_set_register()
294 call assert_fails("call setreg('#', 200)", 'E86:')
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +0200295 call assert_fails("call setreg('a', test_unknown())", 'E908:')
Bram Moolenaar71136db2019-11-30 19:48:46 +0100296
297 edit Xfile_alt_1
298 let b1 = bufnr('')
299 edit Xfile_alt_2
300 let b2 = bufnr('')
301 edit Xfile_alt_3
302 let b3 = bufnr('')
303 call setreg('#', 'alt_1')
304 call assert_equal('Xfile_alt_1', getreg('#'))
305 call setreg('#', b2)
306 call assert_equal('Xfile_alt_2', getreg('#'))
307
308 let ab = 'regwrite'
309 call setreg('=', '')
310 call setreg('=', 'a', 'a')
311 call setreg('=', 'b', 'a')
312 call assert_equal('regwrite', getreg('='))
313
Bram Moolenaar4b96df52020-01-26 22:00:26 +0100314 " Test for setting a list of lines to special registers
Bram Moolenaar6f1f0ca2019-12-01 18:16:18 +0100315 call setreg('/', [])
316 call assert_equal('', @/)
317 call setreg('=', [])
318 call assert_equal('', @=)
319 call assert_fails("call setreg('/', ['a', 'b'])", 'E883:')
320 call assert_fails("call setreg('=', ['a', 'b'])", 'E883:')
321 call assert_equal(0, setreg('_', ['a', 'b']))
322
323 " Test for recording to a invalid register
324 call assert_beeps('normal q$')
325
326 " Appending to a register when recording
327 call append(0, "text for clipboard test")
328 normal gg
329 call feedkeys('qrllq', 'xt')
330 call feedkeys('qRhhq', 'xt')
331 call assert_equal('llhh', getreg('r'))
332
Bram Moolenaar54c8d222019-12-02 20:41:39 +0100333 " Appending a list of characters to a register from different lines
334 let @r = ''
335 call append(0, ['abcdef', '123456'])
336 normal gg"ry3l
337 call cursor(2, 4)
338 normal "Ry3l
339 call assert_equal('abc456', @r)
340
341 " Test for gP with multiple lines selected using characterwise motion
342 %delete
343 call append(0, ['vim editor', 'vim editor'])
344 let @r = ''
345 exe "normal ggwy/vim /e\<CR>gP"
346 call assert_equal(['vim editor', 'vim editor', 'vim editor'], getline(1, 3))
347
348 " Test for gP with . register
349 %delete
350 normal iabc
351 normal ".gp
352 call assert_equal('abcabc', getline(1))
353 normal 0".gP
354 call assert_equal('abcabcabc', getline(1))
355
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100356 let @"=''
357 call setreg('', '1')
358 call assert_equal('1', @")
359 call setreg('@', '2')
360 call assert_equal('2', @")
361
Bram Moolenaar71136db2019-11-30 19:48:46 +0100362 enew!
363endfunc
364
Bram Moolenaar54c8d222019-12-02 20:41:39 +0100365" Test for clipboard registers (* and +)
366func Test_clipboard_regs()
367 CheckNotGui
368 CheckFeature clipboard_working
369
370 new
371 call append(0, "text for clipboard test")
372 normal gg"*yiw
373 call assert_equal('text', getreg('*'))
374 normal gg2w"+yiw
375 call assert_equal('clipboard', getreg('+'))
376
377 " Test for replacing the clipboard register contents
378 set clipboard=unnamed
379 let @* = 'food'
380 normal ggviw"*p
381 call assert_equal('text', getreg('*'))
382 call assert_equal('food for clipboard test', getline(1))
383 normal ggviw"*p
384 call assert_equal('food', getreg('*'))
385 call assert_equal('text for clipboard test', getline(1))
386
387 " Test for replacing the selection register contents
388 set clipboard=unnamedplus
389 let @+ = 'food'
390 normal ggviw"+p
391 call assert_equal('text', getreg('+'))
392 call assert_equal('food for clipboard test', getline(1))
393 normal ggviw"+p
394 call assert_equal('food', getreg('+'))
395 call assert_equal('text for clipboard test', getline(1))
396
397 " Test for auto copying visually selected text to clipboard register
398 call setline(1, "text for clipboard test")
399 let @* = ''
400 set clipboard=autoselect
401 normal ggwwviwy
402 call assert_equal('clipboard', @*)
403
404 " Test for auto copying visually selected text to selection register
405 let @+ = ''
406 set clipboard=autoselectplus
407 normal ggwviwy
408 call assert_equal('for', @+)
409
410 set clipboard&vim
411 bwipe!
412endfunc
413
414" Test for restarting the current mode (insert or virtual replace) after
415" executing the contents of a register
416func Test_put_reg_restart_mode()
417 new
418 call append(0, 'editor')
419 normal gg
420 let @r = "ivim \<Esc>"
421 call feedkeys("i\<C-O>@r\<C-R>=mode()\<CR>", 'xt')
422 call assert_equal('vimi editor', getline(1))
423
424 call setline(1, 'editor')
425 normal gg
426 call feedkeys("gR\<C-O>@r\<C-R>=mode()\<CR>", 'xt')
427 call assert_equal('vimReditor', getline(1))
428
429 bwipe!
430endfunc
431
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100432" Test for executing a register using :@ command
433func Test_execute_register()
434 call setreg('r', [])
435 call assert_beeps('@r')
436 let i = 1
437 let @q = 'let i+= 1'
438 @q
439 @
440 call assert_equal(3, i)
Bram Moolenaar1671f442020-03-10 07:48:13 +0100441
Bram Moolenaar8a9bc952020-10-02 18:48:07 +0200442 " try to execute expression register and use a backspace to cancel it
443 new
444 call feedkeys("@=\<BS>ax\<CR>y", 'xt')
445 call assert_equal(['x', 'y'], getline(1, '$'))
446 close!
447
Bram Moolenaar1671f442020-03-10 07:48:13 +0100448 " cannot execute a register in operator pending mode
449 call assert_beeps('normal! c@r')
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100450endfunc
451
Bram Moolenaarbb861e22020-06-07 18:16:36 +0200452" Test for getting register info
453func Test_get_reginfo()
454 enew
455 call setline(1, ['foo', 'bar'])
456
457 exe 'norm! "zyy'
458 let info = getreginfo('"')
459 call assert_equal('z', info.points_to)
460 call setreg('y', 'baz')
461 call assert_equal('z', getreginfo('').points_to)
462 call setreg('y', { 'isunnamed': v:true })
463 call assert_equal('y', getreginfo('"').points_to)
464
465 exe '$put'
466 call assert_equal(getreg('y'), getline(3))
467 call setreg('', 'qux')
468 call assert_equal('0', getreginfo('').points_to)
469 call setreg('x', 'quux')
470 call assert_equal('0', getreginfo('').points_to)
471
472 let info = getreginfo('')
473 call assert_equal(getreg('', 1, 1), info.regcontents)
474 call assert_equal(getregtype(''), info.regtype)
475
476 exe "norm! 0\<c-v>e" .. '"zy'
477 let info = getreginfo('z')
478 call assert_equal(getreg('z', 1, 1), info.regcontents)
479 call assert_equal(getregtype('z'), info.regtype)
480 call assert_equal(1, +info.isunnamed)
481
482 let info = getreginfo('"')
483 call assert_equal('z', info.points_to)
484
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +0200485 let @a="a1b2"
486 nnoremap <F2> <Cmd>let g:RegInfo = getreginfo()<CR>
487 exe "normal \"a\<F2>"
488 call assert_equal({'regcontents': ['a1b2'], 'isunnamed': v:false,
489 \ 'regtype': 'v'}, g:RegInfo)
490 nunmap <F2>
491 unlet g:RegInfo
492
Bram Moolenaarbb861e22020-06-07 18:16:36 +0200493 bwipe!
494endfunc
495
496" Test for restoring register with dict from getreginfo
497func Test_set_register_dict()
498 enew!
499
500 call setreg('"', #{ regcontents: ['one', 'two'],
501 \ regtype: 'V', points_to: 'z' })
502 call assert_equal(['one', 'two'], getreg('"', 1, 1))
503 let info = getreginfo('"')
504 call assert_equal('z', info.points_to)
505 call assert_equal('V', info.regtype)
506 call assert_equal(1, +getreginfo('z').isunnamed)
507
508 call setreg('x', #{ regcontents: ['three', 'four'],
509 \ regtype: 'v', isunnamed: v:true })
510 call assert_equal(['three', 'four'], getreg('"', 1, 1))
511 let info = getreginfo('"')
512 call assert_equal('x', info.points_to)
513 call assert_equal('v', info.regtype)
514 call assert_equal(1, +getreginfo('x').isunnamed)
515
516 call setreg('y', #{ regcontents: 'five',
517 \ regtype: "\<c-v>", isunnamed: v:false })
518 call assert_equal("\<c-v>4", getreginfo('y').regtype)
519 call assert_equal(0, +getreginfo('y').isunnamed)
520 call assert_equal(['three', 'four'], getreg('"', 1, 1))
521 call assert_equal('x', getreginfo('"').points_to)
522
523 call setreg('"', #{ regcontents: 'six' })
524 call assert_equal('0', getreginfo('"').points_to)
525 call assert_equal(1, +getreginfo('0').isunnamed)
526 call assert_equal(['six'], getreginfo('0').regcontents)
527 call assert_equal(['six'], getreginfo('"').regcontents)
528
Bram Moolenaar7633fe52020-06-22 19:10:56 +0200529 let @x = 'one'
530 call setreg('x', {})
531 call assert_equal(1, len(split(execute('reg x'), '\n')))
532
533 call assert_fails("call setreg('0', #{regtype: 'V'}, 'v')", 'E118:')
534 call assert_fails("call setreg('0', #{regtype: 'X'})", 'E475:')
535 call assert_fails("call setreg('0', #{regtype: 'vy'})", 'E475:')
536
Bram Moolenaarbb861e22020-06-07 18:16:36 +0200537 bwipe!
538endfunc
539
Bram Moolenaarcc613032020-06-07 21:31:18 +0200540func Test_v_register()
541 enew
542 call setline(1, 'nothing')
543
544 func s:Put()
545 let s:register = v:register
546 exec 'normal! "' .. v:register .. 'P'
547 endfunc
548 nnoremap <buffer> <plug>(test) :<c-u>call s:Put()<cr>
549 nmap <buffer> S <plug>(test)
550
551 let @z = "testz\n"
552 let @" = "test@\n"
553
554 let s:register = ''
555 call feedkeys('"_ddS', 'mx')
556 call assert_equal('test@', getline('.')) " fails before 8.2.0929
557 call assert_equal('"', s:register) " fails before 8.2.0929
558
559 let s:register = ''
560 call feedkeys('"zS', 'mx')
561 call assert_equal('z', s:register)
562
563 let s:register = ''
564 call feedkeys('"zSS', 'mx')
565 call assert_equal('"', s:register)
566
567 let s:register = ''
568 call feedkeys('"_S', 'mx')
569 call assert_equal('_', s:register)
570
571 let s:register = ''
572 normal "_ddS
573 call assert_equal('"', s:register) " fails before 8.2.0929
574 call assert_equal('test@', getline('.')) " fails before 8.2.0929
575
576 let s:register = ''
577 execute 'normal "z:call' "s:Put()\n"
578 call assert_equal('z', s:register)
579 call assert_equal('testz', getline('.'))
580
581 " Test operator and omap
582 let @b = 'testb'
583 func s:OpFunc(...)
584 let s:register2 = v:register
585 endfunc
586 set opfunc=s:OpFunc
587
588 normal "bg@l
589 normal S
590 call assert_equal('"', s:register) " fails before 8.2.0929
591 call assert_equal('b', s:register2)
592
593 func s:Motion()
594 let s:register1 = v:register
595 normal! l
596 endfunc
597 onoremap <buffer> Q :<c-u>call s:Motion()<cr>
598
599 normal "bg@Q
600 normal S
601 call assert_equal('"', s:register)
602 call assert_equal('b', s:register1)
603 call assert_equal('"', s:register2)
604
605 set opfunc&
606 bwipe!
607endfunc
608
Bram Moolenaar856c1112020-06-17 21:47:23 +0200609" Test for executing the contents of a register as an Ex command with line
610" continuation.
611func Test_execute_reg_as_ex_cmd()
612 " Line continuation with just two lines
613 let code =<< trim END
614 let l = [
615 \ 1]
616 END
617 let @r = code->join("\n")
618 let l = []
619 @r
620 call assert_equal([1], l)
621
622 " Line continuation with more than two lines
623 let code =<< trim END
624 let l = [
625 \ 1,
626 \ 2,
627 \ 3]
628 END
629 let @r = code->join("\n")
630 let l = []
631 @r
632 call assert_equal([1, 2, 3], l)
633
634 " use comments interspersed with code
635 let code =<< trim END
636 let l = [
637 "\ one
638 \ 1,
639 "\ two
640 \ 2,
641 "\ three
642 \ 3]
643 END
644 let @r = code->join("\n")
645 let l = []
646 @r
647 call assert_equal([1, 2, 3], l)
648
649 " use line continuation in the middle
650 let code =<< trim END
651 let a = "one"
652 let l = [
653 \ 1,
654 \ 2]
655 let b = "two"
656 END
657 let @r = code->join("\n")
658 let l = []
659 @r
660 call assert_equal([1, 2], l)
661 call assert_equal("one", a)
662 call assert_equal("two", b)
663
664 " only one line with a \
665 let @r = "\\let l = 1"
666 call assert_fails('@r', 'E10:')
667
668 " only one line with a "\
669 let @r = ' "\ let i = 1'
670 @r
671 call assert_false(exists('i'))
672
673 " first line also begins with a \
674 let @r = "\\let l = [\n\\ 1]"
675 call assert_fails('@r', 'E10:')
676
677 " Test with a large number of lines
678 let @r = "let str = \n"
679 let @r ..= repeat(" \\ 'abcdefghijklmnopqrstuvwxyz' ..\n", 312)
680 let @r ..= ' \ ""'
681 @r
682 call assert_equal(repeat('abcdefghijklmnopqrstuvwxyz', 312), str)
683endfunc
684
Bram Moolenaar25fd2672020-06-22 20:30:27 +0200685" Test for clipboard registers with ASCII NUL
686func Test_clipboard_nul()
687 CheckFeature clipboard_working
688 new
689
690 " Test for putting ASCII NUL into the clipboard
691 set clipboard=unnamed
692 call append(0, "\ntest")
693 normal ggyyp
694 call assert_equal("^@test^@", strtrans(getreg('*')))
695 call assert_equal(getline(1), getline(2))
696 let b = split(execute(":reg *"), "\n")
697 call assert_match('"\*\s*\^@test\^J',b[1])
698
699 set clipboard&vim
700 bwipe!
701endfunc
702
Bram Moolenaaref85a9b2020-07-10 20:24:07 +0200703func Test_ve_blockpaste()
704 new
705 set ve=all
706 0put =['QWERTZ','ASDFGH']
707 call cursor(1,1)
708 exe ":norm! \<C-V>3ljdP"
709 call assert_equal(1, col('.'))
710 call assert_equal(getline(1, 2), ['QWERTZ', 'ASDFGH'])
711 call cursor(1,1)
712 exe ":norm! \<C-V>3ljd"
713 call cursor(1,1)
714 norm! $3lP
715 call assert_equal(5, col('.'))
716 call assert_equal(getline(1, 2), ['TZ QWER', 'GH ASDF'])
717 set ve&vim
718 bwipe!
719endfunc
720
Bram Moolenaar032a2d02020-12-22 17:59:35 +0100721func Test_insert_small_delete()
722 new
723 call setline(1, ['foo foobar bar'])
724 call cursor(1,1)
725 exe ":norm! ciw'\<C-R>-'"
Bram Moolenaar9f63a652020-12-23 12:50:20 +0100726 call assert_equal("'foo' foobar bar", getline(1))
Bram Moolenaar032a2d02020-12-22 17:59:35 +0100727 exe ":norm! w.w."
Bram Moolenaar9f63a652020-12-23 12:50:20 +0100728 call assert_equal("'foo' 'foobar' 'bar'", getline(1))
Bram Moolenaar032a2d02020-12-22 17:59:35 +0100729 bwipe!
730endfunc
731
Bram Moolenaarf4fcedc2021-03-15 18:36:20 +0100732" Record in insert mode using CTRL-O
733func Test_record_in_insert_mode()
734 new
735 let @r = ''
736 call setline(1, ['foo'])
737 call feedkeys("i\<C-O>qrbaz\<C-O>q", 'xt')
738 call assert_equal('baz', @r)
739 bwipe!
740endfunc
741
Bram Moolenaara4bc2dd2022-01-27 19:27:16 +0000742func Test_record_in_select_mode()
743 new
744 call setline(1, 'text')
745 sil norm q00
746 sil norm q
747 call assert_equal('0ext', getline(1))
zeertzjqfbf4f1c2022-01-28 12:50:43 +0000748
749 %delete
750 let @r = ''
751 call setline(1, ['abc', 'abc', 'abc'])
752 smap <F2> <Right><Right>,
753 call feedkeys("qrgh\<F2>Dk\<Esc>q", 'xt')
754 call assert_equal("gh\<F2>Dk\<Esc>", @r)
755 norm j0@rj0@@
756 call assert_equal([',Dk', ',Dk', ',Dk'], getline(1, 3))
757 sunmap <F2>
758
Bram Moolenaara4bc2dd2022-01-27 19:27:16 +0000759 bwipe!
760endfunc
761
zeertzjq81b46a62022-04-09 17:58:49 +0100762" mapping that ends macro recording should be removed from recorded macro
763func Test_end_record_using_mapping()
764 call setline(1, 'aaa')
765 nnoremap s q
766 call feedkeys('safas', 'tx')
767 call assert_equal('fa', @a)
768 nunmap s
769
770 nnoremap xx q
771 call feedkeys('0xxafaxx', 'tx')
772 call assert_equal('fa', @a)
773 nunmap xx
774
775 nnoremap xsx q
776 call feedkeys('0qafaxsx', 'tx')
777 call assert_equal('fa', @a)
778 nunmap xsx
779
780 bwipe!
781endfunc
782
zeertzjq6d4e7252022-04-07 13:58:04 +0100783func Test_end_reg_executing()
784 nnoremap s <Nop>
785 let @a = 's'
786 call feedkeys("@aqaq\<Esc>", 'tx')
787 call assert_equal('', @a)
788 call assert_equal('', getline(1))
789
790 call setline(1, 'aaa')
791 nnoremap s qa
792 let @a = 'fa'
793 call feedkeys("@asq\<Esc>", 'tx')
794 call assert_equal('', @a)
795 call assert_equal('aaa', getline(1))
796
797 nunmap s
798 bwipe!
799endfunc
800
Christian Brabandt78eb9cc2021-09-14 18:55:51 +0200801" Make sure that y_append is correctly reset
802" and the previous register is working as expected
803func Test_register_y_append_reset()
804 new
805 call setline(1, ['1',
806 \ '2 ----------------------------------------------------',
807 \ '3',
808 \ '4',
809 \ '5 ----------------------------------------------------',
810 \ '6',
811 \ '7',
812 \ '8 ----------------------------------------------------',
813 \ '9',
814 \ '10 aaaaaaa 4.',
815 \ '11 Game Dbl-Figures Leaders:',
816 \ '12 Player Pts FG% 3P% FT% RB AS BL ST TO PF EFF',
817 \ '13 bbbbbbbbb 12 (50 /0 /67 )/ 7/ 3/ 0/ 2/ 3/ 4/+15',
818 \ '14 cccccc 12 (57 /67 /100)/ 2/ 1/ 1/ 0/ 1/ 3/+12',
819 \ '15 ddddddd 10 (63 /0 /0 )/ 1/ 3/ 0/ 3/ 5/ 3/ +9',
820 \ '16 4 5-15 0-3 2-2 5-12 1-1 3-4 33.3 0.0 100 41.7 100 75 12 14',
821 \ '17 F 23-55 2-10 9-11 23-52 3-13 26-29 41.8 20 81.8 44.2 23.1 89.7 57 75',
822 \ '18 4 3 6 3 2 3 3 4 3 3 7 3 1 4 6 -1 -1 +2 -1 -2',
823 \ '19 F 13 19 5 10 4 17 22 9 14 32 13 4 20 17 -1 -13 -4 -3 -3 +5'])
824 11
825 exe "norm! \"a5dd"
826 norm! j
827 exe "norm! \"bY"
828 norm! 2j
829 exe "norm! \"BY"
830 norm! 4k
831 norm! 5dd
832 norm! 3k
833 " The next put should put the content of the unnamed register, not of
834 " register b!
835 norm! p
836 call assert_equal(['1',
837 \ '2 ----------------------------------------------------',
838 \ '3',
839 \ '4',
840 \ '5 ----------------------------------------------------',
841 \ '6',
842 \ '10 aaaaaaa 4.',
843 \ '16 4 5-15 0-3 2-2 5-12 1-1 3-4 33.3 0.0 100 41.7 100 75 12 14',
844 \ '17 F 23-55 2-10 9-11 23-52 3-13 26-29 41.8 20 81.8 44.2 23.1 89.7 57 75',
845 \ '18 4 3 6 3 2 3 3 4 3 3 7 3 1 4 6 -1 -1 +2 -1 -2',
846 \ '19 F 13 19 5 10 4 17 22 9 14 32 13 4 20 17 -1 -13 -4 -3 -3 +5',
847 \ '7',
848 \ '8 ----------------------------------------------------',
849 \ '9'], getline(1,'$'))
850 bwipe!
851endfunc
852
Bram Moolenaar71136db2019-11-30 19:48:46 +0100853" vim: shiftwidth=2 sts=2 expandtab