robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
dd6ed20272
master
1" Tests for maparg(), mapcheck(), mapset(), maplist() 2" Also test utf8 map with a 0x80 byte. 3 4source shared.vim 5 6func s: SID () 7return str2nr ( matchstr ( expand ( '<sfile>' ), '<SNR>\zs\d\+\ze_SID$' )) 8endfunc 9 10func Test_maparg () 11new 12set cpo -= < 13set encoding = utf8 14let sid = s: SID () 15 16let lnum = expand ( '<sflnum>' ) 17map foo< C-V > is< F4 > foo 18vnoremap <script> <buffer> <expr> <silent> bar is bar 19call assert_equal ( "is<F4>foo" , maparg ( 'foo<C-V>' )) " maparg() string result 20call assert_equal ({ 'mode' : ' ' , 'mode_bits' : 0x47 , 'abbr' : 0 , 'buffer' : 0 , 21 \'noremap' : 0 , 'script' : 0 , 'expr' : 0 , 'nowait' : 0 , 'silent' : 0 , 22"\ Nvim-specific field "replace_keycodes" 23 \'replace_keycodes' : 0 , 24 \'lhs' : 'foo<C-V>' , 'lhsraw' : "foo\x80\xfc\x04V" , 'rhs' : 'is<F4>foo' , 25 \'lhsrawalt' : "foo\x16" , 26 \'sid' : sid , 'scriptversion' : 1 , 'lnum' : lnum + 1 }, 27 \maparg ( 'foo<C-V>' , '' , 0 , 1 )) 28call assert_equal ({ 'mode' : 'v' , 'mode_bits' : 0x42 , 'abbr' : 0 , 'buffer' : 1 , 29 \'noremap' : 1 , 'script' : 1 , 'expr' : 1 , 'nowait' : 0 , 'silent' : 1 , 30"\ Nvim-specific field "replace_keycodes" 31 \'replace_keycodes' : 0 , 32 \'lhs' : 'bar' , 'lhsraw' : 'bar' , 'rhs' : 'isbar' , 33 \'sid' : sid , 'scriptversion' : 1 , 'lnum' : lnum + 2 }, 34 \'bar' ->maparg ( '' , 0 , 1 )) 35unmap foo<C-V> 36vunmap <buffer> bar 37 38let lnum = expand ( '<sflnum>' ) 39map <buffer> <nowait> foo bar 40call assert_equal ({ 'mode' : ' ' , 'mode_bits' : 0x47 , 'abbr' : 0 , 'buffer' : 1 , 41 \'noremap' : 0 , 'script' : 0 , 'expr' : 0 , 'nowait' : 1 , 'silent' : 0 , 42"\ Nvim-specific field "replace_keycodes" 43 \'replace_keycodes' : 0 , 44 \'lhs' : 'foo' , 'lhsraw' : 'foo' , 'rhs' : 'bar' , 45 \'sid' : sid , 'scriptversion' : 1 , 'lnum' : lnum + 1 }, 46 \maparg ( 'foo' , '' , 0 , 1 )) 47unmap <buffer> foo 48 49let lnum = expand ( '<sflnum>' ) 50tmap baz foo 51call assert_equal ({ 'mode' : 't' , 'mode_bits' : 0x80 , 'abbr' : 0 , 'buffer' : 0 , 52 \'noremap' : 0 , 'script' : 0 , 'expr' : 0 , 'nowait' : 0 , 'silent' : 0 , 53"\ Nvim-specific field "replace_keycodes" 54 \'replace_keycodes' : 0 , 55 \'lhs' : 'baz' , 'lhsraw' : 'baz' , 'rhs' : 'foo' , 56 \'sid' : sid , 'scriptversion' : 1 , 'lnum' : lnum + 1 }, 57 \maparg ( 'baz' , 't' , 0 , 1 )) 58tunmap baz 59 60let lnum = expand ( '<sflnum>' ) 61iab A B 62call assert_equal ({ 'mode' : 'i' , 'mode_bits' : 0x10 , 'abbr' : 1 , 'buffer' : 0 , 63 \'noremap' : 0 , 'script' : 0 , 'expr' : 0 , 'nowait' : 0 , 'silent' : 0 , 64"\ Nvim-specific field "replace_keycodes" 65 \'replace_keycodes' : 0 , 66 \'lhs' : 'A' , 'lhsraw' : 'A' , 'rhs' : 'B' , 67 \'sid' : sid , 'scriptversion' : 1 , 'lnum' : lnum + 1 }, 68 \maparg ( 'A' , 'i' , 1 , 1 )) 69iuna A 70 71map abc x< char-114 > x 72call assert_equal ( "xrx" , maparg ( 'abc' )) 73map abc y< S-char-114 > y 74call assert_equal ( "yRy" , maparg ( 'abc' )) 75 76" character with K_SPECIAL byte 77nmap abc … 78call assert_equal ( '…' , maparg ( 'abc' )) 79 80" modified character with K_SPECIAL byte 81nmap abc< M-… > 82call assert_equal ( '<M-…>' , maparg ( 'abc' )) 83 84" illegal bytes 85let str = ":\x7f:\x80:\x90:\xd0:" 86exe 'nmap abc ' .. str 87call assert_equal ( str , maparg ( 'abc' )) 88unlet str 89 90omap { w 91let d = maparg ( '{' , 'o' , 0 , 1 ) 92call assert_equal ([ '{' , 'w' , 'o' ], [ d . lhs , d . rhs , d . mode ]) 93ounmap { 94 95lmap { w 96let d = maparg ( '{' , 'l' , 0 , 1 ) 97call assert_equal ([ '{' , 'w' , 'l' ], [ d . lhs , d . rhs , d . mode ]) 98lunmap { 99 100nmap { w 101let d = maparg ( '{' , 'n' , 0 , 1 ) 102call assert_equal ([ '{' , 'w' , 'n' ], [ d . lhs , d . rhs , d . mode ]) 103nunmap { 104 105xmap { w 106let d = maparg ( '{' , 'x' , 0 , 1 ) 107call assert_equal ([ '{' , 'w' , 'x' ], [ d . lhs , d . rhs , d . mode ]) 108xunmap { 109 110smap { w 111let d = maparg ( '{' , 's' , 0 , 1 ) 112call assert_equal ([ '{' , 'w' , 's' ], [ d . lhs , d . rhs , d . mode ]) 113sunmap { 114 115map < C-I > foo 116unmap <Tab> 117" This used to cause a segfault 118call maparg ( '<C-I>' , '' , 0 , 1 ) 119unmap <C-I> 120 121map abc< Nop > 122call assert_equal ( "<Nop>" , maparg ( 'abc' )) 123unmap abc 124 125call feedkeys ( ":abbr esc \<C-V>\<C-V>\<C-V>\<C-V>\<C-V>\<Esc>\<CR>" , "xt" ) 126let d = maparg ( 'esc' , 'i' , 1 , 1 ) 127call assert_equal ([ 'esc' , "\<C-V>\<C-V>\<Esc>" , '!' ], [ d . lhs , d . rhs , d . mode ]) 128abclear 129unlet d 130endfunc 131 132" def Test_vim9_maparg() 133" nmap { w 134" var one: string = maparg('{') 135" assert_equal('w', one) 136" var two: string = maparg('{', 'n') 137" assert_equal('w', two) 138" var three: string = maparg('{', 'n', 0) 139" assert_equal('w', three) 140" var four: dict<any> = maparg('{', 'n', 0, 1) 141" assert_equal(['{', 'w', 'n'], [four.lhs, four.rhs, four.mode]) 142" nunmap { 143" enddef 144 145func Test_mapcheck () 146call assert_equal ( '' , mapcheck ( 'a' )) 147call assert_equal ( '' , mapcheck ( 'abc' )) 148call assert_equal ( '' , mapcheck ( 'ax' )) 149call assert_equal ( '' , mapcheck ( 'b' )) 150 151map a something 152call assert_equal ( 'something' , mapcheck ( 'a' )) 153call assert_equal ( 'something' , mapcheck ( 'a' , 'n' )) 154call assert_equal ( '' , mapcheck ( 'a' , 'c' )) 155call assert_equal ( '' , mapcheck ( 'a' , 'i' )) 156call assert_equal ( 'something' , 'abc' ->mapcheck ()) 157call assert_equal ( 'something' , 'ax' ->mapcheck ()) 158call assert_equal ( '' , mapcheck ( 'b' )) 159unmap a 160 161map ab foobar 162call assert_equal ( 'foobar' , mapcheck ( 'a' )) 163call assert_equal ( 'foobar' , mapcheck ( 'abc' )) 164call assert_equal ( '' , mapcheck ( 'ax' )) 165call assert_equal ( '' , mapcheck ( 'b' )) 166unmap ab 167 168map abc barfoo 169call assert_equal ( 'barfoo' , mapcheck ( 'a' )) 170call assert_equal ( 'barfoo' , mapcheck ( 'a' , 'n' , 0 )) 171call assert_equal ( '' , mapcheck ( 'a' , 'n' , 1 )) 172call assert_equal ( 'barfoo' , mapcheck ( 'abc' )) 173call assert_equal ( '' , mapcheck ( 'ax' )) 174call assert_equal ( '' , mapcheck ( 'b' )) 175unmap abc 176 177abbr ab abbrev 178call assert_equal ( 'abbrev' , mapcheck ( 'a' , 'i' , 1 )) 179call assert_equal ( '' , mapcheck ( 'a' , 'n' , 1 )) 180call assert_equal ( '' , mapcheck ( 'a' , 'i' , 0 )) 181unabbr ab 182endfunc 183 184func Test_range_map () 185new 186" Outside of the range, minimum 187inoremap < Char-0x1040 > a 188execute "normal a\u1040\<Esc>" 189" Inside of the range, minimum 190inoremap < Char-0x103f > b 191execute "normal a\u103f\<Esc>" 192" Inside of the range, maximum 193inoremap < Char-0xf03f > c 194execute "normal a\uf03f\<Esc>" 195" Outside of the range, maximum 196inoremap < Char-0xf040 > d 197execute "normal a\uf040\<Esc>" 198call assert_equal ( "abcd" , getline ( 1 )) 199endfunc 200 201func One_mapset_test ( keys , rhs ) 202exe 'nnoremap ' .. a: keys .. ' ' .. a: rhs 203let orig = maparg ( a: keys , 'n' , 0 , 1 ) 204call assert_equal ( a: keys , orig . lhs ) 205call assert_equal ( a: rhs , orig . rhs ) 206call assert_equal ( 'n' , orig . mode ) 207 208exe 'nunmap ' .. a: keys 209let d = maparg ( a: keys , 'n' , 0 , 1 ) 210call assert_equal ({}, d ) 211 212call mapset ( 'n' , 0 , orig ) 213let d = maparg ( a: keys , 'n' , 0 , 1 ) 214call assert_equal ( a: keys , d . lhs ) 215call assert_equal ( a: rhs , d . rhs ) 216call assert_equal ( 'n' , d . mode ) 217 218exe 'nunmap ' .. a: keys 219endfunc 220 221func Test_mapset () 222call One_mapset_test ( 'K' , 'original<CR>' ) 223call One_mapset_test ( '<F3>' , 'original<CR>' ) 224call One_mapset_test ( '<F3>' , '<lt>Nop>' ) 225 226" Check <> key conversion 227new 228inoremap K one< Left > x 229call feedkeys ( "iK\<Esc>" , 'xt' ) 230call assert_equal ( 'onxe' , getline ( 1 )) 231 232let orig = maparg ( 'K' , 'i' , 0 , 1 ) 233call assert_equal ( 'K' , orig . lhs ) 234call assert_equal ( 'one<Left>x' , orig . rhs ) 235call assert_equal ( 'i' , orig . mode ) 236 237iunmap K 238let d = maparg ( 'K' , 'i' , 0 , 1 ) 239call assert_equal ({}, d ) 240 241call mapset ( 'i' , 0 , orig ) 242call feedkeys ( "SK\<Esc>" , 'xt' ) 243call assert_equal ( 'onxe' , getline ( 1 )) 244 245iunmap K 246 247" Test that <Nop> is restored properly 248inoremap K< Nop > 249call feedkeys ( "SK\<Esc>" , 'xt' ) 250call assert_equal ( '' , getline ( 1 )) 251 252let orig = maparg ( 'K' , 'i' , 0 , 1 ) 253call assert_equal ( 'K' , orig . lhs ) 254call assert_equal ( '<Nop>' , orig . rhs ) 255call assert_equal ( 'i' , orig . mode ) 256 257inoremap K foo 258call feedkeys ( "SK\<Esc>" , 'xt' ) 259call assert_equal ( 'foo' , getline ( 1 )) 260 261call mapset ( 'i' , 0 , orig ) 262call feedkeys ( "SK\<Esc>" , 'xt' ) 263call assert_equal ( '' , getline ( 1 )) 264 265iunmap K 266 267" Test literal <CR> using a backslash 268let cpo_save = & cpo 269set cpo -= B 270inoremap K one\<CR>two 271call feedkeys ( "SK\<Esc>" , 'xt' ) 272call assert_equal ( 'one<CR>two' , getline ( 1 )) 273 274let orig = maparg ( 'K' , 'i' , 0 , 1 ) 275call assert_equal ( 'K' , orig . lhs ) 276call assert_equal ( 'one\<CR>two' , orig . rhs ) 277call assert_equal ( 'i' , orig . mode ) 278 279iunmap K 280let d = maparg ( 'K' , 'i' , 0 , 1 ) 281call assert_equal ({}, d ) 282 283call mapset ( 'i' , 0 , orig ) 284call feedkeys ( "SK\<Esc>" , 'xt' ) 285call assert_equal ( 'one<CR>two' , getline ( 1 )) 286 287iunmap K 288 289" Test literal <CR> using CTRL-V 290inoremap K one< CR > two 291call feedkeys ( "SK\<Esc>" , 'xt' ) 292call assert_equal ( 'one<CR>two' , getline ( 1 )) 293 294let orig = maparg ( 'K' , 'i' , 0 , 1 ) 295call assert_equal ( 'K' , orig . lhs ) 296call assert_equal ( "one\x16<CR>two" , orig . rhs ) 297call assert_equal ( 'i' , orig . mode ) 298 299iunmap K 300let d = maparg ( 'K' , 'i' , 0 , 1 ) 301call assert_equal ({}, d ) 302 303call mapset ( 'i' , 0 , orig ) 304call feedkeys ( "SK\<Esc>" , 'xt' ) 305call assert_equal ( 'one<CR>two' , getline ( 1 )) 306 307iunmap K 308let & cpo = cpo_save 309bwipe ! 310 311call assert_fails ( 'call mapset([], v:false, {})' , 'E730:' ) 312call assert_fails ( 'call mapset("i", 0, "")' , 'E1206:' ) 313call assert_fails ( 'call mapset("i", 0, {})' , 'E460:' ) 314endfunc 315 316func Test_mapset_arg1_dir () 317" This test is mostly about get_map_mode_string. 318" Once the code gets past that, it's common with the 3 arg mapset. 319 320" GetModes() return list of modes for 'XZ' lhs using maplist. 321" There is one list item per mapping 322func s: GetModes ( abbr = v: false ) 323return maplist ( a: abbr ) ->filter ({ _ , m ->m . lhs == 'XZ' }) 324 \ ->mapnew ({ _ , m ->m . mode }) 325endfunc 326 327func s: UnmapAll ( lhs ) 328const unmap_cmds = [ 'unmap' , 'unmap!' , 'tunmap' , 'lunmap' ] 329for cmd in unmap_cmds 330try |call execute ( cmd .. ' ' .. a: lhs ) |catch /E31/ |endtry 331endfor 332endfunc 333 334let tmap = {} 335 336" some mapset(mode, abbr, dict) tests using get_map_mode_str 337map XZ x 338let tmap = maplist () ->filter ({ _ , m ->m . lhs == 'XZ' })[ 0 ] ->copy () 339" this splits the mapping into 2 mappings 340call mapset ( 'ox' , v: false , tmap ) 341call assert_equal ( 2 , len ( s: GetModes ())) 342call mapset ( 'o' , v: false , tmap ) 343call assert_equal ( 3 , len ( s: GetModes ())) 344" test that '' acts like ' ', and that the 3 mappings become 1 345call mapset ( '' , v: false , tmap ) 346call assert_equal ([ ' ' ], s: GetModes ()) 347" dict's mode/abbr are ignored 348call s: UnmapAll ( 'XZ' ) 349let tmap . mode = '!' 350let tmap . abbr = v: true 351call mapset ( 'o' , v: false , tmap ) 352call assert_equal ([ 'o' ], s: GetModes ()) 353 354" test the 3 arg version handles bad mode string, dict not used 355call assert_fails ( "call mapset('vi', v:false, {})" , 'E1276:' ) 356 357 358" get the abbreviations out of the way 359abbreviate XZ ZX 360let tmap = maplist ( v: true ) ->filter ({ _ , m ->m . lhs == 'XZ' })[ 0 ] ->copy () 361 362abclear 363" 'ic' is the default ab command, shows up as '!' 364let tmap . mode = 'ic' 365call mapset ( tmap ) 366call assert_equal ([ '!' ], s: GetModes ( v: true )) 367 368abclear 369let tmap . mode = 'i' 370call mapset ( tmap ) 371call assert_equal ([ 'i' ], s: GetModes ( v: true )) 372 373abclear 374let tmap . mode = 'c' 375call mapset ( tmap ) 376call assert_equal ([ 'c' ], s: GetModes ( v: true )) 377 378abclear 379let tmap . mode = '!' 380call mapset ( tmap ) 381call assert_equal ([ '!' ], s: GetModes ( v: true )) 382 383call assert_fails ( "call mapset(#{mode: ' !', abbr: 1})" , 'E1276:' ) 384call assert_fails ( "call mapset(#{mode: 'cl', abbr: 1})" , 'E1276:' ) 385call assert_fails ( "call mapset(#{mode: 'in', abbr: 1})" , 'E1276:' ) 386 387" the map commands 388map XZ x 389let tmap = maplist () ->filter ({ _ , m ->m . lhs == 'XZ' })[ 0 ] ->copy () 390 391" try the combos 392call s: UnmapAll ( 'XZ' ) 393" 'nxso' is ' ', the unadorned :map 394let tmap . mode = 'nxso' 395call mapset ( tmap ) 396call assert_equal ([ ' ' ], s: GetModes ()) 397 398cal s: UnmapAll ( 'XZ' ) 399" 'ic' is '!' 400let tmap . mode = 'ic' 401call mapset ( tmap ) 402call assert_equal ([ '!' ], s: GetModes ()) 403 404call s: UnmapAll ( 'XZ' ) 405" 'xs' is really 'v' 406let tmap . mode = 'xs' 407call mapset ( tmap ) 408call assert_equal ([ 'v' ], s: GetModes ()) 409 410" try the individual modes 411call s: UnmapAll ( 'XZ' ) 412let tmap . mode = 'n' 413call mapset ( tmap ) 414call assert_equal ([ 'n' ], s: GetModes ()) 415 416call s: UnmapAll ( 'XZ' ) 417let tmap . mode = 'x' 418call mapset ( tmap ) 419call assert_equal ([ 'x' ], s: GetModes ()) 420 421call s: UnmapAll ( 'XZ' ) 422let tmap . mode = 's' 423call mapset ( tmap ) 424call assert_equal ([ 's' ], s: GetModes ()) 425 426call s: UnmapAll ( 'XZ' ) 427let tmap . mode = 'o' 428call mapset ( tmap ) 429call assert_equal ([ 'o' ], s: GetModes ()) 430 431call s: UnmapAll ( 'XZ' ) 432let tmap . mode = 'i' 433call mapset ( tmap ) 434call assert_equal ([ 'i' ], s: GetModes ()) 435 436call s: UnmapAll ( 'XZ' ) 437let tmap . mode = 'c' 438call mapset ( tmap ) 439call assert_equal ([ 'c' ], s: GetModes ()) 440 441call s: UnmapAll ( 'XZ' ) 442let tmap . mode = 't' 443call mapset ( tmap ) 444call assert_equal ([ 't' ], s: GetModes ()) 445 446call s: UnmapAll ( 'XZ' ) 447let tmap . mode = 'l' 448call mapset ( tmap ) 449call assert_equal ([ 'l' ], s: GetModes ()) 450 451call s: UnmapAll ( 'XZ' ) 452 453" get errors for modes that can't be in one mapping 454call assert_fails ( "call mapset(#{mode: 'nxsoi', abbr: 0})" , 'E1276:' ) 455call assert_fails ( "call mapset(#{mode: ' !', abbr: 0})" , 'E1276:' ) 456call assert_fails ( "call mapset(#{mode: 'ix', abbr: 0})" , 'E1276:' ) 457call assert_fails ( "call mapset(#{mode: 'tl', abbr: 0})" , 'E1276:' ) 458call assert_fails ( "call mapset(#{mode: ' l', abbr: 0})" , 'E1276:' ) 459call assert_fails ( "call mapset(#{mode: ' t', abbr: 0})" , 'E1276:' ) 460endfunc 461 462func Check_ctrlb_map ( d , check_alt ) 463call assert_equal ( '<C-B>' , a: d . lhs ) 464if a: check_alt 465call assert_equal ( "\x80\xfc\x04B" , a: d . lhsraw ) 466call assert_equal ( "\x02" , a: d . lhsrawalt ) 467else 468call assert_equal ( "\x02" , a: d . lhsraw ) 469endif 470endfunc 471 472func Test_map_local () 473nmap a global 474nmap <buffer> a local 475 476let prev_map_list = split ( execute ( 'nmap a' ), "\n" ) 477call assert_match ( 'n\s*a\s*@local' , prev_map_list [ 0 ]) 478call assert_match ( 'n\s*a\s*global' , prev_map_list [ 1 ]) 479 480let mapping = maparg ( 'a' , 'n' , 0 , 1 ) 481call assert_equal ( 1 , mapping . buffer ) 482let mapping . rhs = 'new_local' 483call mapset ( 'n' , 0 , mapping ) 484 485" Check that the global mapping is left untouched. 486let map_list = split ( execute ( 'nmap a' ), "\n" ) 487call assert_match ( 'n\s*a\s*@new_local' , map_list [ 0 ]) 488call assert_match ( 'n\s*a\s*global' , map_list [ 1 ]) 489 490nunmap a 491endfunc 492 493func Test_map_restore () 494" Test restoring map with alternate keycode 495nmap < C-B > back 496let d = maparg ( '<C-B>' , 'n' , 0 , 1 ) 497call Check_ctrlb_map ( d , 1 ) 498let dsimp = maparg ( "\x02" , 'n' , 0 , 1 ) 499call Check_ctrlb_map ( dsimp , 0 ) 500nunmap <C-B> 501call mapset ( 'n' , 0 , d ) 502let d = maparg ( '<C-B>' , 'n' , 0 , 1 ) 503call Check_ctrlb_map ( d , 1 ) 504let dsimp = maparg ( "\x02" , 'n' , 0 , 1 ) 505call Check_ctrlb_map ( dsimp , 0 ) 506 507nunmap <C-B> 508endfunc 509 510" Test restoring an <SID> mapping 511func Test_map_restore_sid () 512func RestoreMap () 513const d = maparg ( '<CR>' , 'i' , v: false , v: true ) 514iunmap <buffer> <CR> 515call mapset ( 'i' , v: false , d ) 516endfunc 517 518let mapscript =<< trim [CODE] 519inoremap <silent><buffer> <SID>Return <C-R>=42<CR> 520inoremap <script><buffer> <CR> <CR><SID>Return 521[CODE] 522call writefile ( mapscript , 'Xmapscript' , 'D' ) 523 524new 525source Xmapscript 526inoremap <buffer> < C-B > <Cmd> call RestoreMap () <CR> 527call feedkeys ( "i\<CR>\<*C-B>\<CR>" , 'xt' ) 528call assert_equal ([ '' , '42' , '42' ], getline ( 1 , '$' )) 529 530bwipe ! 531delfunc RestoreMap 532endfunc 533 534" Test restoring a mapping with a negative script ID 535func Test_map_restore_negative_sid () 536let after =<< trim [CODE] 537call assert_equal("\tLast set from --cmd argument", 538\ execute('verbose nmap ,n')->trim()->split("\n")[-1]) 539let d = maparg(',n', 'n', 0, 1) 540nunmap ,n 541call assert_equal('No mapping found', 542\ execute('verbose nmap ,n')->trim()->split("\n")[-1]) 543call mapset('n', 0, d) 544call assert_equal("\tLast set from --cmd argument", 545\ execute('verbose nmap ,n')->trim()->split("\n")[-1]) 546call writefile(v:errors, 'Xresult') 547qall! 548[CODE] 549 550if RunVim ([], after , '--clean --cmd "nmap ,n <Nop>"' ) 551call assert_equal ([], readfile ( 'Xresult' )) 552endif 553call delete ( 'Xresult' ) 554endfunc 555 556" Check that restoring a mapping doesn't remove a mapping whose {rhs} matches 557" the restored mapping's {lhs}. 558func Test_map_restore_with_rhs_match_lhs () 559nnoremap < F2 > < F3 > 560nnoremap < F3 > < F4 > 561call assert_equal ( '<F3>' , maparg ( '<F2>' , 'n' )) 562call assert_equal ( '<F4>' , maparg ( '<F3>' , 'n' )) 563let d = maparg ( '<F3>' , 'n' , v: false , v: true ) 564nunmap <F3> 565call assert_equal ( '<F3>' , maparg ( '<F2>' , 'n' )) 566call assert_equal ( '' , maparg ( '<F3>' , 'n' )) 567call mapset ( d ) 568call assert_equal ( '<F3>' , maparg ( '<F2>' , 'n' )) 569call assert_equal ( '<F4>' , maparg ( '<F3>' , 'n' )) 570 571nunmap <F2> 572nunmap <F3> 573endfunc 574 575func Test_maplist () 576new 577func s: ClearMappingsAbbreviations () 578mapclear |nmapclear |vmapclear |xmapclear |smapclear |omapclear 579mapclear ! |imapclear |lmapclear |cmapclear |tmapclear 580mapclear <buffer> |nmapclear <buffer> |vmapclear <buffer> 581xmapclear <buffer> |smapclear <buffer> |omapclear <buffer> 582mapclear ! <buffer> |imapclear <buffer> |lmapclear <buffer> 583cmapclear <buffer> |tmapclear <buffer> 584abclear |abclear <buffer> 585endfunc 586 587func s: AddMaps ( new , accum ) 588if len ( a: new ) > 0 && a: new [ 0 ] != "No mapping found" 589eval a: accum ->extend ( a: new ) 590endif 591endfunc 592 593call s: ClearMappingsAbbreviations () 594call assert_equal ( 0 , len ( maplist ())) 595call assert_equal ( 0 , len ( maplist ( v: true ))) 596 597" Set up some mappings. 598map dup bar 599map <buffer> dup bufbar 600map foo< C-V > is< F4 > foo 601vnoremap <script> <buffer> <expr> <silent> bar is bar 602tmap baz foo 603omap h w 604lmap i w 605nmap j w 606xmap k w 607smap l w 608map abc< Nop > 609nmap < M-j > x 610nmap < M-Space > y 611" And abbreviations 612abbreviate xy he 613abbreviate xx she 614abbreviate <buffer> x they 615 616" Get a list of the mappings with the ':map' commands. 617" Check maplist() return a list of the same size. 618call assert_equal ( 13 , len ( maplist ())) 619call assert_equal ( 3 , len ( maplist ( v: true ))) 620call assert_equal ( 13 , len ( maplist ( v: false ))) 621 622" collect all the current maps using :map commands 623let maps_command = [] 624call s: AddMaps ( split ( execute ( 'map' ), '\n' ), maps_command ) 625call s: AddMaps ( split ( execute ( 'map!' ), '\n' ), maps_command ) 626call s: AddMaps ( split ( execute ( 'tmap' ), '\n' ), maps_command ) 627call s: AddMaps ( split ( execute ( 'lmap' ), '\n' ), maps_command ) 628 629" Use maplist to get all the maps 630let maps_maplist = maplist () 631call assert_equal ( len ( maps_command ), len ( maps_maplist )) 632 633" make sure all the mode-lhs are unique, no duplicates 634let map_set = {} 635for d in maps_maplist 636let map_set [ d . mode .. "-" .. d . lhs .. "-" .. d . buffer ] = 0 637endfor 638call assert_equal ( len ( maps_maplist ), len ( map_set )) 639 640" For everything returned by maplist, should be the same as from maparg. 641" Except for "map dup", because maparg returns the <buffer> version 642for d in maps_maplist 643if d . lhs == 'dup' && d . buffer == 0 644continue 645endif 646let d_maparg = maparg ( d . lhs , d . mode , v: false , v: true ) 647call assert_equal ( d_maparg , d ) 648endfor 649 650" Check abbr matches maparg 651for d in maplist ( v: true ) 652" Note, d.mode is '!', but can't use that with maparg 653let d_maparg = maparg ( d . lhs , 'i' , v: true , v: true ) 654call assert_equal ( d_maparg , d ) 655endfor 656 657call s: ClearMappingsAbbreviations () 658call assert_equal ( 0 , len ( maplist ())) 659call assert_equal ( 0 , len ( maplist ( v: true ))) 660endfunc 661 662 663" vim: shiftwidth=2 sts=2 expandtab