robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f907614088
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 eq =t . eq 7local eval =n . eval 8local exec =n . exec 9local exec_lua =n . exec_lua 10local expect =n . expect 11local feed =n . feed 12local fn =n . fn 13local api =n . api 14local source =n . source 15local command =n . command 16local exec_capture =n . exec_capture 17local pcall_err =t . pcall_err 18 19describe ( 'maparg()' , function () 20before_each ( clear ) 21 22local foo_bar_map_table ={ 23lhs ='foo' , 24lhsraw ='foo' , 25script =0 , 26silent =0 , 27rhs ='bar' , 28expr =0 , 29replace_keycodes =0 , 30sid =0 , 31scriptversion =1 , 32buffer =0 , 33nowait =0 , 34mode ='n' , 35mode_bits =0x01 , 36abbr =0 , 37noremap =1 , 38lnum =0 , 39} 40 41it ( 'returns a dict' , function () 42command ( 'nnoremap foo bar' ) 43eq ( 'bar' , fn . maparg ( 'foo' )) 44eq ( foo_bar_map_table , fn . maparg ( 'foo' , 'n' , false , true )) 45end ) 46 47it ( 'returns 1 for silent when <silent> is used' , function () 48command ( 'nnoremap <silent> foo bar' ) 49eq ( 1 , fn . maparg ( 'foo' , 'n' , false , true )[ 'silent' ]) 50 51command ( 'nnoremap baz bat' ) 52eq ( 0 , fn . maparg ( 'baz' , 'n' , false , true )[ 'silent' ]) 53end ) 54 55it ( 'returns an empty string when no map is present' , function () 56eq ( '' , fn . maparg ( 'not a mapping' )) 57end ) 58 59it ( 'returns an empty dict when no map is present and dict is requested' , function () 60eq ( {} , fn . maparg ( 'not a mapping' , 'n' , false , true )) 61end ) 62 63it ( 'returns the same value for noremap and <script>' , function () 64command ( 'inoremap <script> hello world' ) 65command ( 'inoremap this that' ) 66eq ( 67fn . maparg ( 'hello' , 'i' , false , true )[ 'noremap' ], 68fn . maparg ( 'this' , 'i' , false , true )[ 'noremap' ] 69) 70end ) 71 72it ( 'returns a boolean for buffer' , function () 73-- Open enough windows to know we aren't on buffer number 1 74command ( 'new' ) 75command ( 'new' ) 76command ( 'new' ) 77command ( 'cnoremap <buffer> this that' ) 78eq ( 1 , fn . maparg ( 'this' , 'c' , false , true )[ 'buffer' ]) 79 80-- Global will return 0 always 81command ( 'nnoremap other another' ) 82eq ( 0 , fn . maparg ( 'other' , 'n' , false , true )[ 'buffer' ]) 83end ) 84 85it ( 'returns script numbers' , function () 86source ( [[ 87function! s:maparg_test_function() abort 88return 'testing' 89endfunction 90 91nnoremap fizz :call <SID>maparg_test_function()<CR> 92]] ) 93eq ( 1 , fn . maparg ( 'fizz' , 'n' , false , true )[ 'sid' ]) 94eq ( 'testing' , api . nvim_call_function ( '<SNR>1_maparg_test_function' , {} )) 95end ) 96 97it ( 'works with <F12> and others' , function () 98source ( [[ 99let g:maparg_test_var = 0 100 101nnoremap <F12> :let g:maparg_test_var = 1<CR> 102]] ) 103eq ( 0 , eval ( 'g:maparg_test_var' )) 104source ( [[ 105call feedkeys("\<F12>") 106]] ) 107eq ( 1 , eval ( 'g:maparg_test_var' )) 108 109eq ( ':let g:maparg_test_var = 1<CR>' , fn . maparg ( '<F12>' , 'n' , false , true )[ 'rhs' ]) 110end ) 111 112it ( 'works with <expr>' , function () 113source ( [[ 114let counter = 0 115inoremap <expr> <C-L> ListItem() 116inoremap <expr> <C-R> ListReset() 117 118func ListItem() 119let g:counter += 1 120return g:counter . '. ' 121endfunc 122 123func ListReset() 124let g:counter = 0 125return '' 126endfunc 127 128call feedkeys("i\<C-L>") 129]] ) 130eq ( 1 , eval ( 'g:counter' )) 131 132local map_dict =fn . maparg ( '<C-L>' , 'i' , false , true ) 133eq ( 1 , map_dict [ 'expr' ]) 134eq ( 'i' , map_dict [ 'mode' ]) 135end ) 136 137it ( 'works with combining characters' , function () 138-- Using addacutes to make combining character better visible 139local function ac ( s ) 140local acute ='\204\129' -- U+0301 COMBINING ACUTE ACCENT 141local ret =s : gsub ( '`' , acute ) 142return ret 143end 144command ( ac ( [[ 145nnoremap a b` 146nnoremap c` d 147nnoremap e` f` 148]] )) 149eq ( ac ( 'b`' ), fn . maparg ( ac ( 'a' ))) 150eq ( ac ( '' ), fn . maparg ( ac ( 'c' ))) 151eq ( ac ( 'd' ), fn . maparg ( ac ( 'c`' ))) 152eq ( ac ( 'f`' ), fn . maparg ( ac ( 'e`' ))) 153 154local function acmap ( lhs , rhs ) 155return { 156lhs =ac ( lhs ), 157lhsraw =ac ( lhs ), 158rhs =ac ( rhs ), 159 160buffer =0 , 161expr =0 , 162replace_keycodes =0 , 163mode ='n' , 164mode_bits =0x01 , 165abbr =0 , 166noremap =1 , 167nowait =0 , 168script =0 , 169sid =0 , 170scriptversion =1 , 171silent =0 , 172lnum =0 , 173} 174end 175 176eq ( {} , fn . maparg ( ac ( 'c' ), 'n' , 0 , 1 )) 177eq ( acmap ( 'a' , 'b`' ), fn . maparg ( ac ( 'a' ), 'n' , 0 , 1 )) 178eq ( acmap ( 'c`' , 'd' ), fn . maparg ( ac ( 'c`' ), 'n' , 0 , 1 )) 179eq ( acmap ( 'e`' , 'f`' ), fn . maparg ( ac ( 'e`' ), 'n' , 0 , 1 )) 180end ) 181end ) 182 183describe ( 'mapset()' , function () 184before_each ( clear ) 185 186it ( 'can restore mapping with backslash in lhs' , function () 187api . nvim_set_keymap ( 'n' , '\\ab' , 'a' , {} ) 188eq ( '\nn \\ab a' , exec_capture ( 'nmap \\ab' )) 189local mapargs =fn . maparg ( '\\ab' , 'n' , false , true ) 190api . nvim_set_keymap ( 'n' , '\\ab' , 'b' , {} ) 191eq ( '\nn \\ab b' , exec_capture ( 'nmap \\ab' )) 192fn . mapset ( 'n' , false , mapargs ) 193eq ( '\nn \\ab a' , exec_capture ( 'nmap \\ab' )) 194end ) 195 196it ( 'can restore mapping description from the dict returned by maparg()' , function () 197api . nvim_set_keymap ( 'n' , 'lhs' , 'rhs' , { desc ='map description' } ) 198eq ( '\nn lhs rhs\n map description' , exec_capture ( 'nmap lhs' )) 199local mapargs =fn . maparg ( 'lhs' , 'n' , false , true ) 200api . nvim_set_keymap ( 'n' , 'lhs' , 'rhs' , { desc ='MAP DESCRIPTION' } ) 201eq ( '\nn lhs rhs\n MAP DESCRIPTION' , exec_capture ( 'nmap lhs' )) 202fn . mapset ( 'n' , false , mapargs ) 203eq ( '\nn lhs rhs\n map description' , exec_capture ( 'nmap lhs' )) 204end ) 205 206it ( 'can restore "replace_keycodes" from the dict returned by maparg()' , function () 207api . nvim_set_keymap ( 'i' , 'foo' , [['<l' .. 't>']] , { expr =true , replace_keycodes =true } ) 208feed ( 'Afoo' ) 209expect ( '<' ) 210local mapargs =fn . maparg ( 'foo' , 'i' , false , true ) 211api . nvim_set_keymap ( 'i' , 'foo' , [['<l' .. 't>']] , { expr =true } ) 212feed ( 'foo' ) 213expect ( '<<lt>' ) 214fn . mapset ( 'i' , false , mapargs ) 215feed ( 'foo' ) 216expect ( '<<lt><' ) 217end ) 218 219it ( 'replaces an abbreviation of the same lhs #20320' , function () 220command ( 'inoreabbr foo bar' ) 221eq ( '\ni foo * bar' , exec_capture ( 'iabbr foo' )) 222feed ( 'ifoo ' ) 223expect ( 'bar ' ) 224local mapargs =fn . maparg ( 'foo' , 'i' , true , true ) 225command ( 'inoreabbr foo BAR' ) 226eq ( '\ni foo * BAR' , exec_capture ( 'iabbr foo' )) 227feed ( 'foo ' ) 228expect ( 'bar BAR ' ) 229fn . mapset ( 'i' , true , mapargs ) 230eq ( '\ni foo * bar' , exec_capture ( 'iabbr foo' )) 231feed ( 'foo<Esc>' ) 232expect ( 'bar BAR bar' ) 233end ) 234 235it ( 'can restore Lua callback from the dict returned by maparg()' , function () 236eq ( 2370 , 238exec_lua ( [[ 239GlobalCount = 0 240vim.api.nvim_set_keymap('n', 'asdf', '', { 241callback = function() GlobalCount = GlobalCount + 1 end, 242}) 243return GlobalCount 244]] ) 245) 246feed ( 'asdf' ) 247eq ( 1 , exec_lua ( [[return GlobalCount]] )) 248 249exec_lua ( [[ 250_G.saved_asdf_map = vim.fn.maparg('asdf', 'n', false, true) 251vim.api.nvim_set_keymap('n', 'asdf', '', { 252callback = function() GlobalCount = GlobalCount + 10 end, 253}) 254]] ) 255feed ( 'asdf' ) 256eq ( 11 , exec_lua ( [[return GlobalCount]] )) 257 258exec_lua ( [[vim.fn.mapset('n', false, _G.saved_asdf_map)]] ) 259feed ( 'asdf' ) 260eq ( 12 , exec_lua ( [[return GlobalCount]] )) 261 262exec ( [[ 263let g:saved_asdf_map = maparg('asdf', 'n', v:false, v:true) 264lua << 265vim.api.nvim_set_keymap('n', 'asdf', '', { 266callback = function() GlobalCount = GlobalCount + 10 end, 267}) 268]] ) 269feed ( 'asdf' ) 270eq ( 22 , exec_lua ( [[return GlobalCount]] )) 271 272command ( [[call mapset('n', v:false, g:saved_asdf_map)]] ) 273feed ( 'asdf' ) 274eq ( 23 , exec_lua ( [[return GlobalCount]] )) 275end ) 276 277it ( 'does not leak memory if lhs is missing' , function () 278eq ( 279'Vim:E460: Entries missing in mapset() dict argument' , 280pcall_err ( exec_lua , [[vim.fn.mapset('n', false, {rhs = 'foo'})]] ) 281) 282eq ( 283'Vim:E460: Entries missing in mapset() dict argument' , 284pcall_err ( exec_lua , [[vim.fn.mapset('n', false, {callback = function() end})]] ) 285) 286end ) 287end )