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