robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f6bf814378
master
1-- Test for Visual mode and operators. 2-- 3-- Tests for the two kinds of operations: Those executed with Visual mode 4-- followed by an operator and those executed via Operator-pending mode. Also 5-- part of the test are mappings, counts, and repetition with the . command. 6 7local n =require ( 'test.functional.testnvim' )() 8local t =require ( 'test.testutil' ) 9 10local describe , it , before_each =t . describe , t . it , t . before_each 11local feed , insert , source =n . feed , n . insert , n . source 12local clear , feed_command , expect =n . clear , n . feed_command , n . expect 13 14-- Vim script user functions needed for some of the test cases. 15local function source_user_functions () 16source ( [[ 17function MoveToCap() 18call search('\u', 'W') 19endfunction 20function SelectInCaps() 21let [line1, col1] = searchpos('\u', 'bcnW') 22let [line2, col2] = searchpos('.\u', 'nW') 23call setpos("'<", [0, line1, col1, 0]) 24call setpos("'>", [0, line2, col2, 0]) 25normal! gv 26endfunction 27]] ) 28end 29 30local function put_abc () 31source ( [[ 32$put ='a' 33$put ='b' 34$put ='c']] ) 35end 36 37local function put_aaabbbccc () 38source ( [[ 39$put ='aaa' 40$put ='bbb' 41$put ='ccc']] ) 42end 43 44local function define_select_mode_maps () 45source ( [[ 46snoremap <lt>End> <End> 47snoremap <lt>Down> <Down> 48snoremap <lt>Del> <Del>]] ) 49end 50 51describe ( 'Visual mode and operator' , function () 52before_each ( function () 53clear () 54source_user_functions () 55end ) 56 57it ( 'simple change in Visual mode' , function () 58-- Nvim: visual-repeat re-executes the visual operation, not a fixed-size reselect. 59 60-- Exercise characterwise Visual mode plus operator, with count and repeat. 61insert ( 'apple banana cherry' ) 62feed_command ( '/^apple' ) 63feed ( 'lvld.l3vd.' ) 64expect ( 'a ' ) 65 66-- Same in linewise Visual mode. 67feed_command ( '%delete _' ) 68insert ( [[ 69line 1 line 1 70line 2 line 2 71line 3 line 3 72line 4 line 4 73line 5 line 5 74line 6 line 6]] ) 75feed_command ( '/^line 1' ) 76feed ( 'Vcnewline<esc>j.j2Vd.' ) 77expect ( [[ 78newline 79newline]] ) 80 81-- Same in blockwise Visual mode. 82feed_command ( '%delete _' ) 83insert ( [[ 84xxxxxxxxxxxxx 85xxxxxxxxxxxxx 86xxxxxxxxxxxxx 87xxxxxxxxxxxxx]] ) 88feed_command ( '/^xxxx' ) 89feed ( '<c-v>jlc <esc>l.l2<c-v>c----<esc>l.' ) 90expect ( [[ 91-------- 92-------- 93xxxx-------- 94xxxx--------]] ) 95end ) 96 97it ( 'Visual mode mapping' , function () 98-- Nvim: visual-repeat re-executes the visual operation, not a fixed-size reselect. 99 100-- Set up Visual mode mappings. 101feed_command ( 'vnoremap W /\\u/s-1<CR>' ) 102feed_command ( 'vnoremap iW :<C-U>call SelectInCaps()<CR>' ) 103 104-- Do a simple change using the simple vmap, also with count and repeat. 105feed_command ( [[call setline(1, 'KiwiRaspberryDateWatermelonPeach')]] ) 106feed_command ( '/^Kiwi' ) 107feed ( 'vWcNo<esc>l.fD2vd.' ) 108expect ( 'NoNo' ) 109 110-- Same, using the vmap that maps to an Ex command. 111feed_command ( [[call setline(1, 'JambuRambutanBananaTangerineMango')]] ) 112feed_command ( '/^Jambu' ) 113feed ( 'llviWc-<esc>l.l2vdl.' ) 114expect ( '--a' ) 115end ) 116 117it ( 'Operator-pending mode mapping' , function () 118insert ( [[ 119PineappleQuinceLoganberryOrangeGrapefruitKiwiZ 120JuniperDurianZ 121LemonNectarineZ]] ) 122 123-- Set up Operator-pending mode mappings. 124feed_command ( 'onoremap W /\\u/<CR>' ) 125feed_command ( 'onoremap <Leader>W :<C-U>call MoveToCap()<CR>' ) 126feed_command ( 'onoremap iW :<C-U>call SelectInCaps()<CR>' ) 127 128-- Do a simple change using the simple omap, also with count and repeat. 129feed_command ( '/^Pineapple' ) 130feed ( 'cW-<esc>l.l2.l.' ) 131 132-- Same, using the omap that maps to an Ex command to move the cursor. 133feed_command ( '/^Juniper' ) 134feed ( 'g?\\WfD.' ) 135 136-- Same, using the omap that uses Ex and Visual mode (custom text object). 137feed_command ( '/^Lemon' ) 138feed ( 'yiWPlciWNew<esc>fr.' ) 139 140-- Assert buffer contents. 141expect ( [[ 142----Z 143WhavcreQhevnaZ 144LemonNewNewZ]] ) 145end ) 146 147-- Vim patch 7.3.879 addressed a bug where typing ":" (the start of an Ex 148-- command) in Operator-pending mode couldn't be aborted with Escape, the 149-- change operation implied by the operator was always executed. 150it ( 'patch 7.3.879' , function () 151insert ( [[ 152zzzz 153zzzz]] ) 154 155-- Start a change operation consisting of operator plus Ex command, like 156-- "dV:..." etc., then either 157-- - complete the operation by pressing Enter: as a result the buffer is 158-- changed, taking into account the v/V/<c-v> modifier given; or 159-- - abort the operation by pressing Escape: no change to the buffer is 160-- carried out. 161feed_command ( '/^zzzz' ) 162feed ( [[dV:<cr>dv:<cr>:set noma | let v:errmsg = ''<cr>]] ) 163feed ( [[d:<cr>:set ma | put = v:errmsg =~# '^E21' ? 'ok' : 'failed'<cr>]] ) 164feed ( [[dv:<esc>dV:<esc>:set noma | let v:errmsg = ''<cr>]] ) 165feed ( [[d:<esc>:set ma | put = v:errmsg =~# '^E21' ? 'failed' : 'ok'<cr>]] ) 166 167-- Assert buffer contents. 168expect ( [[ 169zzz 170ok 171ok]] ) 172end ) 173 174describe ( 'characterwise visual mode:' , function () 175it ( 'replace last line' , function () 176source ( [[ 177$put ='a' 178let @" = 'x']] ) 179feed ( 'v$p' ) 180 181expect ( [[ 182 183x]] ) 184end ) 185 186it ( 'delete middle line' , function () 187put_abc () 188feed ( 'kkv$d' ) 189 190expect ( [[ 191 192b 193c]] ) 194end ) 195 196it ( 'delete middle two line' , function () 197put_abc () 198feed ( 'kkvj$d' ) 199 200expect ( [[ 201 202c]] ) 203end ) 204 205it ( 'delete last line' , function () 206put_abc () 207feed ( 'v$d' ) 208 209expect ( [[ 210 211a 212b 213]] ) 214end ) 215 216it ( 'delete last two line' , function () 217put_abc () 218feed ( 'kvj$d' ) 219 220expect ( [[ 221 222a 223]] ) 224end ) 225end ) 226 227describe ( 'characterwise select mode:' , function () 228before_each ( function () 229define_select_mode_maps () 230end ) 231 232it ( 'delete middle line' , function () 233put_abc () 234feed ( 'kkgh<End><Del>' ) 235 236expect ( [[ 237 238b 239c]] ) 240end ) 241 242it ( 'delete middle two line' , function () 243put_abc () 244feed ( 'kkgh<Down><End><Del>' ) 245 246expect ( [[ 247 248c]] ) 249end ) 250 251it ( 'delete last line' , function () 252put_abc () 253feed ( 'gh<End><Del>' ) 254 255expect ( [[ 256 257a 258b 259]] ) 260end ) 261 262it ( 'delete last two line' , function () 263put_abc () 264feed ( 'kgh<Down><End><Del>' ) 265 266expect ( [[ 267 268a 269]] ) 270end ) 271end ) 272 273describe ( 'linewise select mode:' , function () 274before_each ( function () 275define_select_mode_maps () 276end ) 277 278it ( 'delete middle line' , function () 279put_abc () 280feed ( ' kkgH<Del> ' ) 281 282expect ( [[ 283 284b 285c]] ) 286end ) 287 288it ( 'delete middle two line' , function () 289put_abc () 290feed ( 'kkgH<Down><Del>' ) 291 292expect ( [[ 293 294c]] ) 295end ) 296 297it ( 'delete last line' , function () 298put_abc () 299feed ( 'gH<Del>' ) 300 301expect ( [[ 302 303a 304b]] ) 305end ) 306 307it ( 'delete last two line' , function () 308put_abc () 309feed ( 'kgH<Down><Del>' ) 310 311expect ( [[ 312 313a]] ) 314end ) 315end ) 316 317describe ( 'v_p:' , function () 318it ( 'replace last character with line register at middle line' , function () 319put_aaabbbccc () 320feed_command ( '-2yank' ) 321feed ( 'k$vp' ) 322 323expect ( [[ 324 325aaa 326bb 327aaa 328 329ccc]] ) 330end ) 331 332it ( 'replace last character with line register at middle line selecting newline' , function () 333put_aaabbbccc () 334feed_command ( '-2yank' ) 335feed ( 'k$v$p' ) 336 337expect ( [[ 338 339aaa 340bb 341aaa 342ccc]] ) 343end ) 344 345it ( 'replace last character with line register at last line' , function () 346put_aaabbbccc () 347feed_command ( '-2yank' ) 348feed ( '$vp' ) 349 350expect ( [[ 351 352aaa 353bbb 354cc 355aaa 356]] ) 357end ) 358 359it ( 'replace last character with line register at last line selecting newline' , function () 360put_aaabbbccc () 361feed_command ( '-2yank' ) 362feed ( '$v$p' ) 363 364expect ( [[ 365 366aaa 367bbb 368cc 369aaa 370]] ) 371end ) 372end ) 373 374-- luacheck: ignore 613 (Trailing whitespace in a string) 375it ( 'gv in exclusive select mode after operation' , function () 376source ( [[ 377$put ='zzz ' 378$put ='äà ' 379set selection=exclusive]] ) 380feed ( 'kv3lyjv3lpgvcxxx<Esc>' ) 381 382expect ( [[ 383 384zzz 385xxx ]] ) 386end ) 387 388it ( 'gv in exclusive select mode without operation' , function () 389source ( [[ 390$put ='zzz ' 391set selection=exclusive]] ) 392feed ( '0v3l<Esc>gvcxxx<Esc>' ) 393 394expect ( [[ 395 396xxx ]] ) 397end ) 398end )