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 , pending =t . describe , t . it , t . before_each , t . pending 5local eq , neq , call =t . eq , t . neq , n . call 6local eval , feed , clear =n . eval , n . feed , n . clear 7local command , insert , expect =n . command , n . insert , n . expect 8local feed_command =n . feed_command 9local curwin =n . api . nvim_get_current_win 10 11describe ( "'langmap'" , function () 12before_each ( function () 13clear () 14insert ( 'iii www' ) 15command ( 'set langmap=iw,wi' ) 16feed ( 'gg0' ) 17end ) 18 19it ( 'converts keys in normal mode' , function () 20feed ( 'ix' ) 21expect ( 'iii ww' ) 22feed ( 'whello<esc>' ) 23expect ( 'iii helloww' ) 24end ) 25it ( 'gives characters that are mapped by :nmap.' , function () 26command ( 'map i 0x' ) 27feed ( 'w' ) 28expect ( 'ii www' ) 29end ) 30describe ( "'langnoremap' option." , function () 31before_each ( function () 32command ( 'nmapclear' ) 33end ) 34it ( "'langnoremap' is by default ON" , function () 35eq ( 1 , eval ( '&langnoremap' )) 36end ) 37it ( "Results of maps are not converted when 'langnoremap' ON." , function () 38command ( 'nmap x i' ) 39feed ( 'xdl<esc>' ) 40expect ( 'dliii www' ) 41end ) 42it ( 'applies when deciding whether to map recursively' , function () 43command ( 'nmap l i' ) 44command ( 'nmap w j' ) 45feed ( 'll' ) 46expect ( 'liii www' ) 47end ) 48it ( "does not stop applying 'langmap' on first character of a mapping" , function () 49command ( '1t1' ) 50command ( '1t1' ) 51command ( 'goto 1' ) 52command ( 'nmap w j' ) 53feed ( 'iiahello' ) 54expect ( [[ 55iii www 56iii www 57ihelloii www]] ) 58end ) 59it ( "Results of maps are converted when 'langnoremap' OFF." , function () 60command ( 'set nolangnoremap' ) 61command ( 'nmap x i' ) 62feed ( 'xdl<esc>' ) 63expect ( 'iii ww' ) 64end ) 65end ) 66-- e.g. CTRL-W_j , mj , 'j and "jp 67it ( 'conversions are applied to keys in middle of command' , function () 68-- Works in middle of window command 69feed ( '<C-w>s' ) 70local origwin =curwin () 71feed ( '<C-w>i' ) 72neq ( origwin , curwin ()) 73-- Works when setting a mark 74feed ( 'yy3p3gg0mwgg0mi' ) 75eq ( { 0 , 3 , 1 , 0 } , call ( 'getpos' , "'i" )) 76eq ( { 0 , 1 , 1 , 0 } , call ( 'getpos' , "'w" )) 77feed ( '3dd' ) 78-- Works when moving to a mark 79feed ( "'i" ) 80eq ( { 0 , 1 , 1 , 0 } , call ( 'getpos' , '.' )) 81-- Works when selecting a register 82feed ( 'qillqqwhhq' ) 83eq ( 'hh' , eval ( '@i' )) 84eq ( 'll' , eval ( '@w' )) 85feed ( 'a<C-r>i<esc>' ) 86expect ( 'illii www' ) 87feed ( '"ip' ) 88expect ( 'illllii www' ) 89-- Works with i_CTRL-O 90feed ( '0a<C-O>ihi<esc>' ) 91expect ( 'illllii hiwww' ) 92end ) 93 94describe ( 'exceptions' , function () 95-- All "command characters" that 'langmap' does not apply to. 96-- These tests consist of those places where some subset of ASCII 97-- characters define certain commands, yet 'langmap' is not applied to 98-- them. 99-- n.b. I think these shouldn't be exceptions. 100it ( ':s///c confirmation' , function () 101command ( 'set langmap=yn,ny' ) 102feed ( 'qa' ) 103feed_command ( 's/i/w/gc' ) 104feed ( 'yynq' ) 105expect ( 'wwi www' ) 106feed ( 'u@a' ) 107expect ( 'wwi www' ) 108eq ( ':s/i/w/gc\ryyn' , eval ( '@a' )) 109end ) 110it ( 'insert-mode CTRL-G' , function () 111command ( 'set langmap=jk,kj' ) 112command ( 'd' ) 113insert ( [[ 114hello 115hello 116hello]] ) 117expect ( [[ 118hello 119hello 120hello]] ) 121feed ( 'qa' ) 122feed ( 'gg3|ahello<C-G>jx<esc>' ) 123feed ( 'q' ) 124expect ( [[ 125helhellolo 126helxlo 127hello]] ) 128eq ( 'gg3|ahellojx' , eval ( '@a' )) 129end ) 130it ( 'command-line CTRL-\\' , function () 131command ( 'set langmap=en,ne' ) 132feed ( ':<C-\\>e\'hello\'\r<C-B>put ="<C-E>"<CR>' ) 133expect ( [[ 134iii www 135hello]] ) 136end ) 137it ( 'command-line CTRL-R' , function () 138n . source ( [[ 139let i_value = 0 140let j_value = 0 141call setreg('i', 'i_value') 142call setreg('j', 'j_value') 143set langmap=ij,ji 144]] ) 145feed ( ':let <C-R>i=1<CR>' ) 146eq ( 1 , eval ( 'i_value' )) 147eq ( 0 , eval ( 'j_value' )) 148end ) 149-- it('-- More -- prompt', function() 150-- -- The 'b' 'j' 'd' 'f' commands at the -- More -- prompt 151-- end) 152it ( 'ask yes/no after backwards range' , function () 153command ( 'set langmap=yn,ny' ) 154feed ( 'dd' ) 155insert ( [[ 156hello 157there 158these 159are 160some 161lines 162]] ) 163feed_command ( '4,2d' ) 164feed ( 'n' ) 165expect ( [[ 166hello 167there 168these 169are 170some 171lines 172]] ) 173end ) 174it ( 'prompt for number' , function () 175command ( 'set langmap=12,21' ) 176n . source ( [[ 177let gotten_one = 0 178function Map() 179let answer = inputlist(['a', '1.', '2.', '3.']) 180if answer == 1 181let g:gotten_one = 1 182endif 183endfunction 184nnoremap x :call Map()<CR> 185]] ) 186feed ( 'x1<CR>' ) 187eq ( 1 , eval ( 'gotten_one' )) 188command ( 'let g:gotten_one = 0' ) 189feed_command ( 'call Map()' ) 190feed ( '1<CR>' ) 191eq ( 1 , eval ( 'gotten_one' )) 192end ) 193end ) 194it ( 'conversions are not applied during setreg()' , function () 195call ( 'setreg' , 'i' , 'ww' ) 196eq ( 'ww' , eval ( '@i' )) 197end ) 198it ( 'conversions not applied in insert mode' , function () 199feed ( 'aiiiwww' ) 200expect ( 'iiiiwwwii www' ) 201end ) 202it ( 'conversions not applied in search mode' , function () 203feed ( '/iii<cr>x' ) 204expect ( 'ii www' ) 205end ) 206it ( 'conversions not applied in cmdline mode' , function () 207feed ( ':call append(1, "iii")<cr>' ) 208expect ( [[ 209iii www 210iii]] ) 211end ) 212 213local function testrecording ( command_string , expect_string , setup_function , expect_macro ) 214if setup_function then 215setup_function () 216end 217feed ( 'qa' .. command_string .. 'q' ) 218expect ( expect_string ) 219eq ( expect_macro or n . fn . nvim_replace_termcodes ( command_string , true , true , true ), eval ( '@a' )) 220if setup_function then 221setup_function () 222end 223-- n.b. may need nvim_replace_termcodes() here. 224feed ( '@a' ) 225expect ( expect_string ) 226end 227 228local function local_setup () 229-- Can't use `insert` as it uses `i` and we've swapped the meaning of that 230-- with the `langmap` setting. 231command ( '%d' ) 232command ( "put ='hello'" ) 233command ( '1d' ) 234end 235 236it ( 'does not affect recording special keys' , function () 237testrecording ( 'A<BS><esc>' , 'hell' , local_setup ) 238testrecording ( '>><lt><lt>' , 'hello' , local_setup ) 239command ( 'nnoremap \\ x' ) 240testrecording ( '\\' , 'ello' , local_setup ) 241testrecording ( 'A<C-V><BS><esc>' , 'hello<BS>' , local_setup ) 242end ) 243pending ( 'Translates modified keys correctly' , function () 244command ( 'nnoremap <M-i> x' ) 245command ( 'nnoremap <M-w> l' ) 246testrecording ( '<M-w>' , 'ello' , local_setup ) 247testrecording ( '<M-i>x' , 'hllo' , local_setup ) 248end ) 249it ( 'handles multibyte characters' , function () 250command ( 'set langmap=ïx,δd,ςw,λl' ) 251feed ( 'ïλδς' ) 252expect ( 'iwww' ) 253end ) 254it ( 'handles multibyte mappings' , function () 255command ( 'set langmap=ςw,λl,…ö,ν],μä' ) 256command ( 'nnoremap äö x' ) 257command ( 'nnoremap ää i…μpäike<esc>' ) 258command ( 'nnoremap ]] r' ) 259command ( 'nnoremap ï ie<esc>' ) 260feed ( 'λμ…' ) 261expect ( 'ii www' ) 262feed ( 'μμ' ) 263expect ( 'i…μpäikei www' ) 264feed ( 'lννn' ) 265expect ( 'i…μpäiken www' ) 266feed ( 'lï' ) 267expect ( 'i…μpäikene www' ) 268end ) 269pending ( 'handles multibyte characters in a macro' , function () 270command ( 'set langmap=ïx' ) 271testrecording ( 'ï' , 'ello' , local_setup ) 272-- The test below checks that what's recorded is correct. 273-- It doesn't check the behaviour, as in order to cause some behaviour we 274-- need to map the multi-byte character, and there is a known bug 275-- preventing this from working (see the test below). 276command ( 'set langmap=xï' ) 277testrecording ( 'x' , 'hello' , local_setup ) 278end ) 279pending ( 'handles multibyte mappings in a macro' , function () 280-- See this vim issue for the problem, may as well add a test. 281-- https://github.com/vim/vim/issues/297 282command ( 'set langmap=ïx' ) 283command ( 'nnoremap x diw' ) 284testrecording ( 'ï' , '' , local_setup ) 285command ( 'set nolangnoremap' ) 286command ( 'set langmap=xï' ) 287command ( 'nnoremap ï ix<esc>' ) 288testrecording ( 'x' , 'xhello' , local_setup ) 289end ) 290it ( 'does not crash when mapping char < 256 to char >= 256' , function () 291-- Note: this warning should be removed when the pending tests above are fixed. 292local wmsg =table . concat ( { 293"'langmap': Mapping from e to ε will not work properly" , 294"'langmap': Mapping from ü to μ will not work properly" , 295} , '\n' ) 296-- ä ë ï ö ü ÿ < 256, β γ ε μ >= 256 297command ( 'set langmap=iw,wi,äë,ëä,ïx,xï,βγ,γβ,eε,εe,üμ,μü,ÿy,yÿ' ) 298eq ( wmsg , n . exec_capture ( 'messages' )) 299command ( 'messages clear | set langmap=iwäëïxβγeεüμÿy;wiëäxïγβεeμüyÿ' ) 300eq ( wmsg , n . exec_capture ( 'messages' )) 301end ) 302-- This test is to ensure the behaviour doesn't change from what's already 303-- around. I (hardenedapple) personally think this behaviour should be 304-- changed. 305it ( 'treats control modified keys as characters' , function () 306command ( 'nnoremap <C-w> iw<esc>' ) 307command ( 'nnoremap <C-i> ii<esc>' ) 308testrecording ( '<C-w>' , 'whello' , local_setup , eval ( [["\<*C-w>"]] )) 309testrecording ( '<C-i>' , 'ihello' , local_setup , eval ( [["\<*C-i>"]] )) 310end ) 311end )