robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
3b00ddb601
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3 4local describe , it , before_each =t . describe , t . it , t . before_each 5local clear =n . clear 6local insert =n . insert 7local feed =n . feed 8local expect =n . expect 9local eq =t . eq 10local map =vim . tbl_map 11local filter =vim . tbl_filter 12local command =n . command 13local curbuf_contents =n . curbuf_contents 14local fn =n . fn 15local dedent =t . dedent 16 17local function reset () 18command ( 'bwipe! | new' ) 19insert ( [[ 20Line of words 1 21Line of words 2]] ) 22command ( 'goto 1' ) 23feed ( 'itest_string.<esc>u' ) 24fn . setreg ( 'a' , 'test_stringa' , 'V' ) 25fn . setreg ( 'b' , 'test_stringb\ntest_stringb\ntest_stringb' , 'b' ) 26fn . setreg ( '"' , 'test_string"' , 'v' ) 27end 28 29-- We check the last inserted register ". in each of these tests because it is 30-- implemented completely differently in do_put(). 31-- It is implemented differently so that control characters and imap'ped 32-- characters work in the same manner when pasted as when inserted. 33describe ( 'put command' , function () 34clear () 35before_each ( reset ) 36 37local function visual_marks_zero () 38for _ , v in pairs ( fn . getpos ( "'<" )) do 39if v ~= 0 then 40return false 41end 42end 43for _ , v in pairs ( fn . getpos ( "'>" )) do 44if v ~= 0 then 45return false 46end 47end 48return true 49end 50 51-- {{{ Where test definitions are run 52local function run_test_variations ( test_variations , extra_setup ) 53reset () 54if extra_setup then 55extra_setup () 56end 57local init_contents =curbuf_contents () 58local init_cursorpos =fn . getcurpos () 59local assert_no_change =function () 60expect ( init_contents ) 61eq ( init_cursorpos , fn . getcurpos ()) 62end 63 64for _ , test in pairs ( test_variations ) do 65it ( test . description , function () 66if extra_setup then 67extra_setup () 68end 69local orig_dotstr =fn . getreg ( '.' ) 70t . ok ( visual_marks_zero ()) 71-- Make sure every test starts from the same conditions 72assert_no_change () 73local was_cli =test . test_action () 74test . test_assertions ( test . exception_table , false ) 75-- Undo puts the cursor and text back to before the change. 76feed ( 'u' ) 77assert_no_change () 78 79-- Should not have changed the ". register 80-- If we paste the ". register with a count we can't avoid 81-- changing this register, hence avoid this check. 82if not test . exception_table . dot_reg_changed then 83eq ( orig_dotstr , fn . getreg ( '.' )) 84end 85 86-- Doing something, undoing it, and then redoing it should 87-- leave us in the same state as just doing it once. 88-- For :ex actions we want '@:', for normal actions we want '.' 89 90-- The '.' redo doesn't work for visual put so just exit if 91-- it was tested. 92-- We check that visual put was used by checking if the '< and 93-- '> marks were changed. 94if not visual_marks_zero () then 95return 96end 97 98if was_cli then 99feed ( '@:' ) 100else 101feed ( '.' ) 102end 103 104test . test_assertions ( test . exception_table , true ) 105end ) 106end 107end -- run_test_variations() 108-- }}} 109 110local function create_test_defs ( 111test_defs , 112command_base , 113command_creator , -- {{{ 114expect_base , 115expect_creator 116) 117local rettab ={} 118local exceptions 119for _ , v in pairs ( test_defs ) do 120if v [ 4 ] then 121exceptions =v [ 4 ] 122else 123exceptions ={} 124end 125table . insert ( rettab , { 126test_action =command_creator ( command_base , v [ 1 ]), 127test_assertions =expect_creator ( expect_base , v [ 2 ]), 128description =v [ 3 ], 129exception_table =exceptions , 130} ) 131end 132return rettab 133end -- create_test_defs() }}} 134 135local function find_cursor_position ( expect_string ) -- {{{ 136-- There must only be one occurrence of the character 'x' in 137-- expect_string. 138-- This function removes that occurrence, and returns the position that 139-- it was in. 140-- This returns the cursor position that would leave the 'x' in that 141-- place if we feed 'ix<esc>' and the string existed before it. 142for linenum , line in pairs ( fn . split ( expect_string , '\n' , 1 )) do 143local column =line : find ( 'x' ) 144if column then 145return { linenum , column } , expect_string : gsub ( 'x' , '' ) 146end 147end 148end -- find_cursor_position() }}} 149 150-- Action function creators {{{ 151local function create_p_action ( test_map , substitution ) 152local temp_val =test_map : gsub ( 'p' , substitution ) 153return function () 154feed ( temp_val ) 155return false 156end 157end 158 159local function create_put_action ( command_base , substitution ) 160local temp_val =command_base : gsub ( 'put' , substitution ) 161return function () 162feed ( ':' .. temp_val .. '<CR>' ) 163return true 164end 165end 166-- }}} 167 168-- Expect function creator {{{ 169local function expect_creator ( conversion_function , expect_base , conversion_table ) 170local temp_expect_string =conversion_function ( expect_base , conversion_table ) 171local cursor_position , expect_string =find_cursor_position ( temp_expect_string ) 172return function ( exception_table , after_redo ) 173expect ( expect_string ) 174 175-- Have to use getcurpos() instead of api.nvim_win_get_cursor(0) in 176-- order to account for virtualedit. 177-- We always want the curswant element in getcurpos(), which is 178-- sometimes different to the column element in 179-- api.nvim_win_get_cursor(0). 180-- NOTE: The ".gp command leaves the cursor after the pasted text 181-- when running, but does not when the command is redone with the 182-- '.' command. 183if not ( exception_table . redo_position and after_redo ) then 184local actual_position =fn . getcurpos () 185eq ( cursor_position , { actual_position [ 2 ], actual_position [ 5 ] } ) 186end 187end 188end -- expect_creator() }}} 189 190-- Test definitions {{{ 191local function copy_def ( def ) 192local rettab ={ '' , {} , '' , nil } 193rettab [ 1 ] =def [ 1 ] 194for k , v in pairs ( def [ 2 ]) do 195rettab [ 2 ][ k ] =v 196end 197rettab [ 3 ] =def [ 3 ] 198if def [ 4 ] then 199rettab [ 4 ] ={} 200for k , v in pairs ( def [ 4 ]) do 201rettab [ 4 ][ k ] =v 202end 203end 204return rettab 205end 206 207local normal_command_defs ={ 208{ 209'p' , 210{ cursor_after =false , put_backwards =false , dot_register =false } , 211'pastes after cursor with p' , 212} , 213{ 214'gp' , 215{ cursor_after =true , put_backwards =false , dot_register =false } , 216'leaves cursor after text with gp' , 217} , 218{ 219'".p' , 220{ cursor_after =false , put_backwards =false , dot_register =true } , 221'works with the ". register' , 222} , 223{ 224'".gp' , 225{ cursor_after =true , put_backwards =false , dot_register =true } , 226'gp works with the ". register' , 227{ redo_position =true } , 228} , 229{ 230'P' , 231{ cursor_after =false , put_backwards =true , dot_register =false } , 232'pastes before cursor with P' , 233} , 234{ 235'gP' , 236{ cursor_after =true , put_backwards =true , dot_register =false } , 237'gP pastes before cursor and leaves cursor after text' , 238} , 239{ 240'".P' , 241{ cursor_after =false , put_backwards =true , dot_register =true } , 242'P works with ". register' , 243} , 244{ 245'".gP' , 246{ cursor_after =true , put_backwards =true , dot_register =true } , 247'gP works with ". register' , 248{ redo_position =true } , 249} , 250} 251 252-- Add a definition applying a count for each definition above. 253-- Could do this for each transformation (p -> P, p -> gp etc), but I think 254-- it's neater this way (balance between being explicit and too verbose). 255for i =1 , # normal_command_defs do 256local cur =normal_command_defs [ i ] 257 258-- Make modified copy of current definition that includes a count. 259local newdef =copy_def ( cur ) 260newdef [ 2 ]. count =2 261cur [ 2 ]. count =1 262newdef [ 1 ] ='2' .. newdef [ 1 ] 263newdef [ 3 ] ='double ' .. newdef [ 3 ] 264 265if cur [ 2 ]. dot_register then 266if not cur [ 4 ] then 267newdef [ 4 ] ={} 268end 269newdef [ 4 ]. dot_reg_changed =true 270end 271 272normal_command_defs [ # normal_command_defs + 1 ] =newdef 273end 274 275local ex_command_defs ={ 276{ 277'put' , 278{ put_backwards =false , dot_register =false } , 279'pastes linewise forwards with :put' , 280} , 281{ 282'put!' , 283{ put_backwards =true , dot_register =false } , 284'pastes linewise backwards with :put!' , 285} , 286{ 287'put .' , 288{ put_backwards =false , dot_register =true } , 289'pastes linewise with the dot register' , 290} , 291{ 292'put! .' , 293{ put_backwards =true , dot_register =true } , 294'pastes linewise backwards with the dot register' , 295} , 296} 297 298local function non_dotdefs ( def_table ) 299return filter ( function ( d ) 300return not d [ 2 ]. dot_register 301end , def_table ) 302end 303 304-- }}} 305 306-- Conversion functions {{{ 307local function convert_charwise ( expect_base , conversion_table , virtualedit_end , visual_put ) 308expect_base =dedent ( expect_base ) 309-- There is no difference between 'P' and 'p' when VIsual_active 310if not visual_put then 311if conversion_table . put_backwards then 312-- Special case for virtualedit at the end of a line. 313local replace_string 314if not virtualedit_end then 315replace_string ='test_stringx"%1' 316else 317replace_string ='test_stringx"' 318end 319expect_base =expect_base : gsub ( '(.)test_stringx"' , replace_string ) 320end 321end 322if conversion_table . count > 1 then 323local rep_string ='test_string"' 324local extra_puts =rep_string : rep ( conversion_table . count - 1 ) 325expect_base =expect_base : gsub ( 'test_stringx"' , extra_puts .. 'test_stringx"' ) 326end 327if conversion_table . cursor_after then 328expect_base =expect_base : gsub ( 'test_stringx"' , 'test_string"x' ) 329end 330if conversion_table . dot_register then 331expect_base =expect_base : gsub ( '(test_stringx?)"' , '%1.' ) 332end 333return expect_base 334end -- convert_charwise() 335 336local function make_back ( string ) 337local prev_line 338local rettab ={} 339local string_found =false 340for _ , line in pairs ( fn . split ( string , '\n' , 1 )) do 341if line : find ( 'test_string' ) then 342string_found =true 343table . insert ( rettab , line ) 344else 345if string_found then 346if prev_line then 347table . insert ( rettab , prev_line ) 348prev_line =nil 349end 350table . insert ( rettab , line ) 351else 352table . insert ( rettab , prev_line ) 353prev_line =line 354end 355end 356end 357-- In case there are no lines after the text that was put. 358if prev_line and string_found then 359table . insert ( rettab , prev_line ) 360end 361return table . concat ( rettab , '\n' ) 362end -- make_back() 363 364local function convert_linewise ( expect_base , conversion_table , _ , use_a , indent ) 365expect_base =dedent ( expect_base ) 366if conversion_table . put_backwards then 367expect_base =make_back ( expect_base ) 368end 369local p_str ='test_string"' 370if use_a then 371p_str ='test_stringa' 372end 373 374if conversion_table . dot_register then 375expect_base =expect_base : gsub ( 'x' .. p_str , 'xtest_string.' ) 376p_str ='test_string.' 377end 378 379if conversion_table . cursor_after then 380expect_base =expect_base : gsub ( 'x' .. p_str .. '\n' , p_str .. '\nx' ) 381end 382 383-- The 'indent' argument is only used here because a single put with an 384-- indent doesn't require special handling. It doesn't require special 385-- handling because the cursor is never put before the indent, hence 386-- the modification of 'test_stringx"' gives the same overall answer as 387-- modifying ' test_stringx"'. 388 389-- Only happens when using normal mode command actions. 390if conversion_table . count and conversion_table . count > 1 then 391if not indent then 392indent ='' 393end 394local rep_string =indent .. p_str .. '\n' 395local extra_puts =rep_string : rep ( conversion_table . count - 1 ) 396local orig_string , new_string 397if conversion_table . cursor_after then 398orig_string =indent .. p_str .. '\nx' 399new_string =extra_puts .. orig_string 400else 401orig_string =indent .. 'x' .. p_str .. '\n' 402new_string =orig_string .. extra_puts 403end 404expect_base =expect_base : gsub ( orig_string , new_string ) 405end 406return expect_base 407end 408 409local function put_x_last ( orig_line , p_str ) 410local prev_end , cur_end , cur_start =0 , 0 , 0 411while cur_start do 412prev_end =cur_end 413cur_start , cur_end =orig_line : find ( p_str , prev_end ) 414end 415-- Assume (because that is the only way I call it) that p_str matches 416-- the pattern 'test_string.' 417return orig_line : sub ( 1 , prev_end - 1 ) .. 'x' .. orig_line : sub ( prev_end ) 418end 419 420local function convert_blockwise ( 421expect_base , 422conversion_table , 423visual , 424use_b , 425trailing_whitespace 426) 427expect_base =dedent ( expect_base ) 428local p_str ='test_string"' 429if use_b then 430p_str ='test_stringb' 431end 432 433if conversion_table . dot_register then 434expect_base =expect_base : gsub ( '(x?)' .. p_str , '%1test_string.' ) 435-- Looks strange, but the dot is a special character in the pattern 436-- and a literal character in the replacement. 437expect_base =expect_base : gsub ( 'test_stringx.' , 'test_stringx.' ) 438p_str ='test_string.' 439end 440 441-- No difference between 'p' and 'P' in visual mode. 442if not visual then 443if conversion_table . put_backwards then 444-- One for the line where the cursor is left, one for all other 445-- lines. 446expect_base =expect_base : gsub ( '([^x])' .. p_str , p_str .. '%1' ) 447expect_base =expect_base : gsub ( '([^x])x' .. p_str , 'x' .. p_str .. '%1' ) 448if not trailing_whitespace then 449expect_base =expect_base : gsub ( ' \n' , '\n' ) 450expect_base =expect_base : gsub ( ' $' , '' ) 451end 452end 453end 454 455if conversion_table . count and conversion_table . count > 1 then 456local p_pattern =p_str : gsub ( '%.' , '%%.' ) 457expect_base =expect_base : gsub ( p_pattern , p_str : rep ( conversion_table . count )) 458expect_base = 459expect_base : gsub ( 'test_stringx([b".])' , p_str : rep ( conversion_table . count - 1 ) .. '%0' ) 460end 461 462if conversion_table . cursor_after then 463if not visual then 464local prev_line 465local rettab ={} 466local prev_in_block =false 467for _ , line in pairs ( fn . split ( expect_base , '\n' , 1 )) do 468if line : find ( 'test_string' ) then 469if prev_line then 470prev_line =prev_line : gsub ( 'x' , '' ) 471table . insert ( rettab , prev_line ) 472end 473prev_line =line 474prev_in_block =true 475else 476if prev_in_block then 477prev_line =put_x_last ( prev_line , p_str ) 478table . insert ( rettab , prev_line ) 479prev_in_block =false 480end 481table . insert ( rettab , line ) 482end 483end 484if prev_line and prev_in_block then 485table . insert ( rettab , put_x_last ( prev_line , p_str )) 486end 487 488expect_base =table . concat ( rettab , '\n' ) 489else 490expect_base =expect_base : gsub ( 'x(.)' , '%1x' ) 491end 492end 493 494return expect_base 495end 496-- }}} 497 498-- Convenience functions {{{ 499local function run_normal_mode_tests ( 500test_string , 501base_map , 502extra_setup , 503virtualedit_end , 504selection_string 505) 506local function convert_closure ( e , c ) 507return convert_charwise ( e , c , virtualedit_end , selection_string ) 508end 509local function expect_normal_creator ( expect_base , conversion_table ) 510local test_expect =expect_creator ( convert_closure , expect_base , conversion_table ) 511return function ( exception_table , after_redo ) 512test_expect ( exception_table , after_redo ) 513if selection_string then 514if not conversion_table . put_backwards then 515eq ( selection_string , fn . getreg ( '"' )) 516end 517else 518eq ( 'test_string"' , fn . getreg ( '"' )) 519end 520end 521end 522run_test_variations ( 523create_test_defs ( 524normal_command_defs , 525base_map , 526create_p_action , 527test_string , 528expect_normal_creator 529), 530extra_setup 531) 532end -- run_normal_mode_tests() 533 534local function convert_linewiseer ( expect_base , conversion_table ) 535return expect_creator ( convert_linewise , expect_base , conversion_table ) 536end 537 538local function run_linewise_tests ( expect_base , base_command , extra_setup ) 539local linewise_test_defs =create_test_defs ( 540ex_command_defs , 541base_command , 542create_put_action , 543expect_base , 544convert_linewiseer 545) 546run_test_variations ( linewise_test_defs , extra_setup ) 547end -- run_linewise_tests() 548-- }}} 549 550-- Actual tests 551describe ( 'default pasting' , function () 552local expect_string =[[ 553Ltest_stringx"ine of words 1 554Line of words 2]] 555run_normal_mode_tests ( expect_string , 'p' ) 556 557run_linewise_tests ( 558[[ 559Line of words 1 560xtest_string" 561Line of words 2]] , 562'put' 563) 564end ) 565 566describe ( 'linewise register' , function () 567-- put with 'p' 568local local_ex_command_defs =non_dotdefs ( normal_command_defs ) 569local base_expect_string =[[ 570Line of words 1 571xtest_stringa 572Line of words 2]] 573local function local_convert_linewise ( expect_base , conversion_table ) 574return convert_linewise ( expect_base , conversion_table , nil , true ) 575end 576local function expect_lineput ( expect_base , conversion_table ) 577return expect_creator ( local_convert_linewise , expect_base , conversion_table ) 578end 579run_test_variations ( 580create_test_defs ( 581local_ex_command_defs , 582'"ap' , 583create_p_action , 584base_expect_string , 585expect_lineput 586) 587) 588 589-- put with :put 590local linewise_put_defs =non_dotdefs ( ex_command_defs ) 591base_expect_string =[[ 592Line of words 1 593xtest_stringa 594Line of words 2]] 595run_test_variations ( 596create_test_defs ( 597linewise_put_defs , 598'put a' , 599create_put_action , 600base_expect_string , 601convert_linewiseer 602) 603) 604end ) 605 606describe ( 'blockwise register' , function () 607local blockwise_put_defs =non_dotdefs ( normal_command_defs ) 608local test_base =[[ 609Lxtest_stringbine of words 1 610Ltest_stringbine of words 2 611test_stringb]] 612 613local function expect_block_creator ( expect_base , conversion_table ) 614return expect_creator ( function ( e , c ) 615return convert_blockwise ( e , c , nil , true ) 616end , expect_base , conversion_table ) 617end 618 619run_test_variations ( 620create_test_defs ( blockwise_put_defs , '"bp' , create_p_action , test_base , expect_block_creator ) 621) 622end ) 623 624it ( 'adds correct indentation when put with [p and ]p' , function () 625feed ( 'G>>"a]pix<esc>' ) 626-- luacheck: ignore 627expect ( [[ 628Line of words 1 629Line of words 2 630xtest_stringa]] ) 631feed ( 'uu"a[pix<esc>' ) 632-- luacheck: ignore 633expect ( [[ 634Line of words 1 635xtest_stringa 636Line of words 2]] ) 637end ) 638 639describe ( 'linewise paste with autoindent' , function () 640-- luacheck: ignore 641run_linewise_tests ( 642[[ 643Line of words 1 644Line of words 2 645xtest_string"]] , 646'put' , 647function () 648fn . setline ( '$' , ' Line of words 2' ) 649-- Set curswant to '8' to be at the end of the tab character 650-- This is where the cursor is put back after the 'u' command. 651fn . setpos ( '.' , { 0 , 2 , 1 , 0 , 8 } ) 652command ( 'set autoindent' ) 653end 654) 655end ) 656 657describe ( 'put inside tabs with virtualedit' , function () 658local test_string =[[ 659Line of words 1 660test_stringx" Line of words 2]] 661run_normal_mode_tests ( test_string , 'p' , function () 662fn . setline ( '$' , ' Line of words 2' ) 663command ( 'setlocal virtualedit=all' ) 664fn . setpos ( '.' , { 0 , 2 , 1 , 2 , 3 } ) 665end ) 666end ) 667 668describe ( 'put after the line with virtualedit' , function () 669-- luacheck: ignore 621 670local test_string =[[ 671Line of words 1 test_stringx" 672Line of words 2]] 673run_normal_mode_tests ( test_string , 'p' , function () 674fn . setline ( '$' , ' Line of words 2' ) 675command ( 'setlocal virtualedit=all' ) 676fn . setpos ( '.' , { 0 , 1 , 16 , 1 , 17 } ) 677end , true ) 678end ) 679 680describe ( 'Visual put' , function () 681describe ( 'basic put' , function () 682local test_string =[[ 683test_stringx" words 1 684Line of words 2]] 685run_normal_mode_tests ( test_string , 'v2ep' , nil , nil , 'Line of' ) 686end ) 687describe ( 'over trailing newline' , function () 688local test_string ='Line of test_stringx"Line of words 2' 689run_normal_mode_tests ( test_string , 'v$p' , function () 690fn . setpos ( '.' , { 0 , 1 , 9 , 0 , 9 } ) 691end , nil , 'words 1\n' ) 692end ) 693describe ( 'linewise mode' , function () 694local test_string =[[ 695xtest_string" 696Line of words 2]] 697local function expect_vis_linewise ( expect_base , conversion_table ) 698return expect_creator ( function ( e , c ) 699return convert_linewise ( e , c , nil , nil ) 700end , expect_base , conversion_table ) 701end 702run_test_variations ( 703create_test_defs ( 704normal_command_defs , 705'Vp' , 706create_p_action , 707test_string , 708expect_vis_linewise 709), 710function () 711fn . setpos ( '.' , { 0 , 1 , 1 , 0 , 1 } ) 712end 713) 714 715describe ( 'with whitespace at bol' , function () 716local function expect_vis_lineindented ( expect_base , conversion_table ) 717local test_expect =expect_creator ( function ( e , c ) 718return convert_linewise ( e , c , nil , nil , ' ' ) 719end , expect_base , conversion_table ) 720return function ( exception_table , after_redo ) 721test_expect ( exception_table , after_redo ) 722if not conversion_table . put_backwards then 723eq ( 'Line of words 1\n' , fn . getreg ( '"' )) 724end 725end 726end 727local base_expect_string =[[ 728xtest_string" 729Line of words 2]] 730run_test_variations ( 731create_test_defs ( 732normal_command_defs , 733'Vp' , 734create_p_action , 735base_expect_string , 736expect_vis_lineindented 737), 738function () 739feed ( 'i test_string.<esc>u' ) 740fn . setreg ( '"' , ' test_string"' , 'v' ) 741end 742) 743end ) 744end ) 745 746describe ( 'blockwise visual mode' , function () 747local test_base =[[ 748test_stringx"e of words 1 749test_string"e of words 2]] 750 751local function expect_block_creator ( expect_base , conversion_table ) 752local test_expect =expect_creator ( function ( e , c ) 753return convert_blockwise ( e , c , true ) 754end , expect_base , conversion_table ) 755return function ( e , c ) 756test_expect ( e , c ) 757if not conversion_table . put_backwards then 758eq ( 'Lin\nLin' , fn . getreg ( '"' )) 759end 760end 761end 762 763local select_down_test_defs =create_test_defs ( 764normal_command_defs , 765'<C-v>jllp' , 766create_p_action , 767test_base , 768expect_block_creator 769) 770run_test_variations ( select_down_test_defs ) 771 772-- "." repeat of an upward visual block put applies at the restored cursor, so the put text 773-- lands elsewhere: skip the position check after redo. 774local undo_redo_no =map ( function ( table ) 775local rettab =copy_def ( table ) 776if not rettab [ 4 ] then 777rettab [ 4 ] ={} 778end 779rettab [ 4 ]. redo_position =true 780return rettab 781end , normal_command_defs ) 782 783-- Selection direction doesn't matter 784run_test_variations ( 785create_test_defs ( 786undo_redo_no , 787'<C-v>kllp' , 788create_p_action , 789test_base , 790expect_block_creator 791), 792function () 793fn . setpos ( '.' , { 0 , 2 , 1 , 0 , 1 } ) 794end 795) 796 797describe ( 'blockwise cursor after undo' , function () 798-- Undo and CTRL-R restore the pre-change cursor position, even for an upward selection 799-- (the harness only covers "." redo, not CTRL-R). 800local function assertion_creator ( _ , _ ) 801return function ( _ , _ ) 802feed ( 'u' ) 803-- Have to use feed('u') here to set curswant, because 804-- ex_undo() doesn't do that. 805eq ( { 0 , 2 , 1 , 0 , 1 } , fn . getcurpos ()) 806feed ( '<C-r>' ) 807eq ( { 0 , 2 , 1 , 0 , 1 } , fn . getcurpos ()) 808end 809end 810 811run_test_variations ( 812create_test_defs ( undo_redo_no , '<C-v>kllp' , create_p_action , test_base , assertion_creator ), 813function () 814fn . setpos ( '.' , { 0 , 2 , 1 , 0 , 1 } ) 815end 816) 817end ) 818end ) 819 820describe ( "with 'virtualedit'" , function () 821describe ( 'splitting a tab character' , function () 822local base_expect_string =[[ 823Line of words 1 824test_stringx" Line of words 2]] 825run_normal_mode_tests ( base_expect_string , 'vp' , function () 826fn . setline ( '$' , ' Line of words 2' ) 827command ( 'setlocal virtualedit=all' ) 828fn . setpos ( '.' , { 0 , 2 , 1 , 2 , 3 } ) 829end , nil , ' ' ) 830end ) 831describe ( 'after end of line' , function () 832local base_expect_string =[[ 833Line of words 1 test_stringx" 834Line of words 2]] 835run_normal_mode_tests ( base_expect_string , 'vp' , function () 836command ( 'setlocal virtualedit=all' ) 837fn . setpos ( '.' , { 0 , 1 , 16 , 2 , 18 } ) 838end , true , ' ' ) 839end ) 840end ) 841end ) 842 843describe ( '. register special tests' , function () 844-- luacheck: ignore 621 845before_each ( reset ) 846it ( 'applies control character actions' , function () 847feed ( 'i<C-t><esc>u' ) 848expect ( [[ 849Line of words 1 850Line of words 2]] ) 851feed ( '".p' ) 852expect ( [[ 853Line of words 1 854Line of words 2]] ) 855feed ( 'u1go<C-v>j".p' ) 856eq ( 857[[ 858ine of words 1 859ine of words 2]] , 860curbuf_contents () 861) 862end ) 863 864-- Asserts whether `keys` makes Nvim beep. 865local function bell_test ( keys , should_ring ) 866local cmd ='normal! ' .. keys 867if should_ring then 868eq ( 0 , fn . assert_beeps ( cmd )) 869else 870eq ( 0 , fn . assert_nobeep ( cmd )) 871end 872end 873 874it ( 'should not ring the bell with gp at end of line' , function () 875bell_test ( '$".gp' ) 876 877-- Even if the last character is a multibyte character. 878reset () 879fn . setline ( 1 , 'helloม' ) 880bell_test ( '$".gp' ) 881end ) 882 883it ( 'should not ring the bell with gp and end of file' , function () 884fn . setpos ( '.' , { 0 , 2 , 1 , 0 } ) 885bell_test ( '$vl".gp' ) 886end ) 887 888it ( 'should ring the bell when deleting if not appropriate' , function () 889t . skip ( t . is_os ( 'bsd' ), 'crashes on freebsd' ) 890 891command ( 'goto 2' ) 892feed ( 'i<bs><esc>' ) 893expect ( [[ 894ine of words 1 895Line of words 2]] ) 896bell_test ( '".P' , true ) 897end ) 898 899it ( 'should restore cursor position after undo of ".p' , function () 900local origpos =fn . getcurpos () 901feed ( '".pu' ) 902eq ( origpos , fn . getcurpos ()) 903end ) 904 905it ( "should be unaffected by 'autoindent' with V\".2p" , function () 906command ( 'set autoindent' ) 907feed ( 'i test_string.<esc>u' ) 908feed ( 'V".2p' ) 909expect ( [[ 910test_string. 911test_string. 912Line of words 2]] ) 913end ) 914end ) 915end )