robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
813a6d3b11
master
1" Tests for put commands, e.g. ":put", "p", "gp", "P", "gP", etc. 2 3source check.vim 4source screendump.vim 5 6func Test_put_block () 7new 8call feedkeys ( "i\<C-V>u2500\<CR>x\<ESC>" , 'x' ) 9call feedkeys ( "\<C-V>y" , 'x' ) 10call feedkeys ( "gg0p" , 'x' ) 11call assert_equal ( "\u2500x" , getline ( 1 )) 12bwipe ! 13endfunc 14 15func Test_put_block_unicode () 16new 17call setreg ( 'a' , "À\nÀÀ\naaaaaaaaaaaa" , "\<C-V>" ) 18call setline ( 1 , [ ' 1' , ' 2' , ' 3' ]) 19exe "norm! \<C-V>jj\"ap" 20let expected = [ 'À 1' , 'ÀÀ 2' , 'aaaaaaaaaaaa3' ] 21call assert_equal ( expected , getline ( 1 , 3 )) 22bw ! 23endfunc 24 25func Test_put_char_block () 26new 27call setline ( 1 , [ 'Line 1' , 'Line 2' ]) 28f Xfile_put 29" visually select both lines and put the cursor at the top of the visual 30" selection and then put the buffer name over it 31exe "norm! G0\<c-v>ke\"%p" 32call assert_equal ([ 'Xfile_put 1' , 'Xfile_put 2' ], getline ( 1 , 2 )) 33bw ! 34endfunc 35 36func Test_put_char_block2 () 37new 38call setreg ( 'a' , ' one ' , 'v' ) 39call setline ( 1 , [ 'Line 1' , '' , 'Line 3' , '' ]) 40" visually select the first 3 lines and put register a over it 41exe "norm! ggl\<c-v>2j2l\"ap" 42call assert_equal ([ 'L one 1' , '' , 'L one 3' , '' ], getline ( 1 , 4 )) 43" clean up 44bw ! 45endfunc 46 47func Test_put_lines () 48new 49let a = [ getreg ( 'a' ), getregtype ( 'a' ) ] 50call setline ( 1 , [ 'Line 1' , 'Line2' , 'Line 3' , '' ]) 51exe 'norm! gg"add"AddG""p' 52call assert_equal ([ 'Line 3' , '' , 'Line 1' , 'Line2' ], getline ( 1 , '$' )) 53" clean up 54bw ! 55eval a [ 0 ] ->setreg ( 'a' , a [ 1 ]) 56endfunc 57 58func Test_put_expr () 59new 60call setline ( 1 , repeat ([ 'A' ], 6 )) 61exec "1norm! \"=line('.')\<cr>p" 62norm ! j0. 63norm ! j0. 64exec "4norm! \"=\<cr>P" 65norm ! j0. 66norm ! j0. 67call assert_equal ([ 'A1' , 'A2' , 'A3' , '4A' , '5A' , '6A' ], getline ( 1 , '$' )) 68bw ! 69endfunc 70 71func Test_put_fails_when_nomodifiable () 72new 73setlocal nomodifiable 74 75normal ! yy 76call assert_fails ( ':put' , 'E21:' ) 77call assert_fails ( ':put!' , 'E21:' ) 78call assert_fails ( ':iput' , 'E21:' ) 79call assert_fails ( ':iput!' , 'E21:' ) 80call assert_fails ( ':normal! p' , 'E21:' ) 81call assert_fails ( ':normal! gp' , 'E21:' ) 82call assert_fails ( ':normal! P' , 'E21:' ) 83call assert_fails ( ':normal! gP' , 'E21:' ) 84 85if has ( 'mouse' ) 86set mouse = n 87call assert_fails ( 'execute "normal! \<MiddleMouse>"' , 'E21:' ) 88set mouse & 89endif 90 91bwipeout ! 92endfunc 93 94" A bug was discovered where the Normal mode put commands (e.g., "p") would 95" output duplicate error messages when invoked in a non-modifiable buffer. 96func Test_put_p_errmsg_nodup () 97new 98setlocal nomodifiable 99 100normal ! yy 101 102func Capture_p_error () 103redir => s:p_err 104normal ! p 105redir END 106endfunc 107 108silent ! call Capture_p_error () 109 110" Error message output within a function should be three lines (the function 111" name, the line number, and the error message). 112call assert_equal ( 3 , count ( s: p_err , "\n" )) 113 114delfunction Capture_p_error 115bwipeout ! 116endfunc 117 118func Test_put_p_indent_visual () 119new 120call setline ( 1 , [ 'select this text' , 'select that text' ]) 121" yank "that" from the second line 122normal 2Gwvey 123" select "this" in the first line and put 124normal k0wve[p 125call assert_equal ( 'select that text' , getline ( 1 )) 126call assert_equal ( 'select that text' , getline ( 2 )) 127bwipe ! 128endfunc 129 130" Test for deleting all the contents of a buffer with a put 131func Test_put_visual_delete_all_lines () 132new 133call setline ( 1 , [ 'one' , 'two' , 'three' ]) 134let @r= '' 135normal ! VG"rgp 136call assert_equal ( 1 , line ( '$' )) 137bw ! 138endfunc 139 140func Test_gp_with_count_leaves_cursor_at_end () 141new 142call setline ( 1 , '<---->' ) 143call setreg ( '@' , "foo\nbar" , 'c' ) 144normal 1G3|3gp 145call assert_equal ([ 0 , 4 , 4 , 0 ], getpos ( "." )) 146call assert_equal ([ '<--foo' , 'barfoo' , 'barfoo' , 'bar-->' ], getline ( 1 , '$' )) 147call assert_equal ([ 0 , 4 , 3 , 0 ], getpos ( "']" )) 148 149bwipe ! 150endfunc 151 152func Test_p_with_count_leaves_mark_at_end () 153new 154call setline ( 1 , '<---->' ) 155call setreg ( '@' , "start\nend" , 'c' ) 156normal 1G3|3p 157call assert_equal ([ 0 , 1 , 4 , 0 ], getpos ( "." )) 158call assert_equal ([ '<--start' , 'endstart' , 'endstart' , 'end-->' ], getline ( 1 , '$' )) 159call assert_equal ([ 0 , 4 , 3 , 0 ], getpos ( "']" )) 160 161bwipe ! 162endfunc 163 164func Test_very_large_count () 165new 166" total put-length (21474837 * 100) brings 32 bit int overflow 167let @"= repeat ( 'x' , 100 ) 168call assert_fails ( 'norm 21474837p' , 'E1240:' ) 169bwipe ! 170endfunc 171 172func Test_very_large_count_64bit () 173new 174let @"= repeat ( 'x' , 100 ) 175call assert_fails ( 'norm 999999999p' , 'E1240:' ) 176bwipe ! 177endfunc 178 179func Test_very_large_count_block () 180new 181" total put-length (21474837 * 100) brings 32 bit int overflow 182call setline ( 1 , repeat ( 'x' , 100 )) 183exe "norm \<C-V>99ly" 184call assert_fails ( 'norm 21474837p' , 'E1240:' ) 185bwipe ! 186endfunc 187 188func Test_very_large_count_block_64bit () 189new 190call setline ( 1 , repeat ( 'x' , 100 )) 191exe "norm \<C-V>$y" 192call assert_fails ( 'norm 999999999p' , 'E1240:' ) 193bwipe ! 194endfunc 195 196func Test_put_above_first_line () 197new 198let @"= 'text' 199silent ! normal 0o00 2000 put 201call assert_equal ( 'text' , getline ( 1 )) 202bwipe ! 203endfunc 204 205func Test_multibyte_op_end_mark () 206new 207call setline ( 1 , 'тест' ) 208normal viwdp 209call assert_equal ([ 0 , 1 , 7 , 0 ], getpos ( "'>" )) 210call assert_equal ([ 0 , 1 , 7 , 0 ], getpos ( "']" )) 211 212normal Vyp 213call assert_equal ([ 0 , 1 , v: maxcol , 0 ], getpos ( "'>" )) 214call assert_equal ([ 0 , 2 , 7 , 0 ], getpos ( "']" )) 215bwipe ! 216endfunc 217 218" this was putting a mark before the start of a line 219func Test_put_empty_register () 220new 221norm yy 222norm [Pi00ggv)s0 223sil ! norm [P 224bwipe ! 225endfunc 226 227" this was putting the end mark after the end of the line 228func Test_put_visual_mode () 229edit ! SomeNewBuffer 230set selection = exclusive 231exe "norm o\t" 232m 0 233sil ! norm p p 234 235bwipe ! 236set selection & 237endfunc 238 239func Test_put_visual_block_mode () 240enew 241exe "norm 0R\<CR>\<C-C>V" 242sil exe "norm \<C-V>c \<MiddleDrag>" 243set ve = all 244sil norm vz=p 245 246bwipe ! 247set ve = 248endfunc 249 250func Test_put_other_window () 251CheckScreendump 252CheckRunVimInTerminal 253 254let lines =<< trim END 25540vsplit 2560put ='some text at the top' 257put =' one more text' 258put =' two more text' 259put =' three more text' 260put =' four more text' 261END 262call writefile ( lines , 'Xtest_put_other' , 'D' ) 263let buf = RunVimInTerminal ( '-S Xtest_put_other' , #{ rows : 10 }) 264 265call VerifyScreenDump ( buf , 'Test_put_other_window_1' , {}) 266 267call StopVimInTerminal ( buf ) 268endfunc 269 270func Test_put_in_last_displayed_line () 271CheckScreendump 272CheckRunVimInTerminal 273 274let lines =<< trim END 275vim9script 276autocmd CursorMoved * eval line('w$') 277@a = 'x'->repeat(&columns * 2 - 2) 278range(&lines)->setline(1) 279feedkeys('G"ap') 280END 281call writefile ( lines , 'Xtest_put_last_line' , 'D' ) 282let buf = RunVimInTerminal ( '-S Xtest_put_last_line' , #{ rows : 10 }) 283 284call VerifyScreenDump ( buf , 'Test_put_in_last_displayed_line_1' , {}) 285 286call StopVimInTerminal ( buf ) 287endfunc 288 289func Test_put_visual_replace_whole_fold () 290new 291let lines = repeat ([ '{{{1' , 'foo' , 'bar' , '' ], 2 ) 292call setline ( 1 , lines ) 293setlocal foldmethod = marker 294call setreg ( '"' , 'baz' ) 295call setreg ( '1' , '' ) 296normal ! Vp 297call assert_equal ( "{{{1\nfoo\nbar\n\n" , getreg ( '1' )) 298call assert_equal ([ 'baz' , '{{{1' , 'foo' , 'bar' , '' ], getline ( 1 , '$' )) 299 300bwipe ! 301endfunc 302 303func Test_put_visual_replace_fold_marker () 304new 305let lines = repeat ([ '{{{1' , 'foo' , 'bar' , '' ], 4 ) 306call setline ( 1 , lines ) 307setlocal foldmethod = marker 308normal ! Gkzo 309call setreg ( '"' , '{{{1' ) 310call setreg ( '1' , '' ) 311normal ! Vp 312call assert_equal ( "{{{1\n" , getreg ( '1' )) 313call assert_equal ( lines , getline ( 1 , '$' )) 314 315bwipe ! 316endfunc 317 318func Test_put_dict () 319new 320let d = #{ a: #{ b : 'abc' }, c: [ 1 , 2 ], d: 0 z10} 321put ! =d 322call assert_equal ([ "{'a': {'b': 'abc'}, 'c': [1, 2], 'd': 0z10}" , '' ], 323 \getline ( 1 , '$' )) 324bw ! 325endfunc 326 327func Test_put_list () 328new 329let l = [ 'a' , 'b' , 'c' ] 330put ! =l 331call assert_equal ([ 'a' , 'b' , 'c' , '' ], getline ( 1 , '$' )) 332bw ! 333endfunc 334 335func Test_iput_multiline () 336new 337setlocal noexpandtab 338call setline ( 1 , "\<Tab>foo" ) 339call setreg ( '0' , "bar\n\<Tab>bar2\nbar3" , 'l' ) 340iput 341call assert_equal ([ "\<Tab>bar" , "\<Tab>\<Tab>bar2" , "\<Tab>bar3" ], getline ( 2 , 4 )) 342setlocal expandtab tabstop = 8 shiftwidth = 8 noshiftround 343iput 344call assert_equal ([ repeat ( ' ' , 8 ) . "bar" , 345 \repeat ( ' ' , 16 ) . "bar2" , 346 \repeat ( ' ' , 8 ) . "bar3" ], getline ( 5 , 7 )) 347bw ! 348endfunc 349 350func Test_iput_beforeafter_tab () 351new 352setlocal noexpandtab 353call setline ( 1 , "\<Tab>foo" ) 354call setreg ( '0' , "bar" , 'l' ) 355iput 356call assert_equal ([ "\<Tab>bar" ], getline ( 2 , '$' )) 357call feedkeys ( "k" , 'x' ) 358iput ! 359call assert_equal ( "\<Tab>bar" , getline ( 1 )) 360call assert_equal ( "\<Tab>bar" , getline ( 3 )) 361bw ! 362endfunc 363 364func Test_iput_beforeafter_expandtab () 365new 366setlocal expandtab tabstop = 8 shiftwidth = 8 noshiftround 367call setline ( 1 , "\<Tab>foo" ) 368call setreg ( '0' , "bar" , 'l' ) 369iput 370call assert_equal ([ repeat ( ' ' , 8 ) . "bar" ], getline ( 2 , '$' )) 371: 1 iput ! 372call assert_equal ( repeat ( ' ' , 8 ) . "bar" , getline ( 1 )) 373bw ! 374endfunc 375 376func Test_iput_invalidrange () 377new 378call setreg ( '0' , "bar" , 'l' ) 379call assert_fails ( ':10iput' , 'E16:' ) 380bw ! 381endfunc 382 383func Test_iput_zero_range () 384new 385let _var = [ getreg ( 'a' ), getregtype ( 'a' )] 386call setreg ( 'a' , 'foobar' , 'l' ) 387call setline ( 1 , range ( 1 , 2 )) 388call cursor ( 1 , 1 ) 3890 iput a 390call assert_equal ([ 'foobar' , '1' , '2' ], getline ( 1 , '$' )) 391 %d 392call setline ( 1 , range ( 1 , 2 )) 393call cursor ( 1 , 1 ) 3940 iput ! a 395call assert_equal ([ 'foobar' , '1' , '2' ], getline ( 1 , '$' )) 396call setreg ( 'a' , _var [ 0 ], _var [ 1 ]) 397bw ! 398endfunc 399 400func Test_iput_not_put () 401new 402call setline ( 1 , "\<Tab>foo" ) 403call setreg ( '0' , "bar" , 'l' ) 404iput 405call assert_equal ( "\<Tab>bar" , getline ( 2 )) 406put 407call assert_equal ( "bar" , getline ( 3 )) 408bw ! 409endfunc 410 411" Test pasting the '.' register 412func Test_put_inserted () 413new 414 415for s in [ '' , '…' , '0' , '^' , '+0' , '+^' , '…0' , '…^' ] 416call setline ( 1 , 'foobar' ) 417exe $ "normal!A{s}\<Esc>" 418call assert_equal ( $ 'foobar{s}', getline(1)) 419normal ! ".p 420call assert_equal ( $ 'foobar{s}{s}', getline(1)) 421normal ! ".2p 422call assert_equal ( $ 'foobar{s}{s}{s}{s}', getline(1)) 423endfor 424 425for s in [ '0' , '^' , '+0' , '+^' , '…0' , '…^' ] 426call setline ( 1 , "\t\t\t\t\tfoobar" ) 427exe $ "normal!A\<C-D>{s}\<Esc>" 428call assert_equal ( $ "\t\t\t\tfoobar{s}", getline(1)) 429normal ! ".p 430call assert_equal ( $ "\t\t\tfoobar{s}{s}", getline(1)) 431normal ! ".2p 432call assert_equal ( $ "\tfoobar{s}{s}{s}{s}", getline(1)) 433endfor 434 435bwipe ! 436endfunc 437 438" vim: shiftwidth=2 sts=2 expandtab