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 eq =t . eq 6local exec_lua =n . exec_lua 7local clear =n . clear 8 9before_each ( clear ) 10 11describe ( 'vim.keycode()' , function () 12local function parse ( key , simplify ) 13local parsed =exec_lua ( [[return select(2,vim.keycode(...,true))]] , key ) 14return vim . tbl_map ( function ( tbl ) 15if simplify == 1 then 16return { tbl . mod , tbl . key } 17elseif simplify == 2 then 18return { tbl . mod , tbl . key , tbl . keys } 19end 20return tbl 21end , parsed ) 22end 23 24it ( 'gets structured keychord info' , function () 25eq ( { { mod ={ 'M' } , key ='f' , keys ='<M-f>' } } , parse ( '<A-f>' )) 26eq ( { { { 'M' } , 'Home' } } , parse ( '<A-Home>' , 1 )) 27eq ( { { { 'M' } , 'f' } , { {} , 'b' } , { {} , 'c' } } , parse ( '<A-f>b<char-99>' , 1 )) 28 29eq ( { { { 'M' } , '>' } } , parse ( '<A->>' , 1 )) 30eq ( { { { 'M' } , 't_>>' } } , parse ( '<A-t_>>>' , 1 )) 31 32-- Multiple modifiers, normalized to canonical order (mod_mask_table): M, T, C, S, … 33eq ( { { mod ={ 'C' , 'S' } , key ='Home' , keys ='<C-S-Home>' } } , parse ( '<C-S-Home>' )) 34eq ( { { { 'M' , 'C' } , 'x' , '<M-C-X>' } } , parse ( '<M-C-x>' , 2 )) 35eq ( { { { 'M' , 'C' , 'S' } , 'F1' , '<M-C-S-F1>' } } , parse ( '<C-A-S-F1>' , 2 )) 36end ) 37 38it ( 'normalizes' , function () 39-- stylua: ignore start 40eq ( { { {} , '\\' , '<Bslash>' } } , parse ( '\\' , 2 )) 41eq ( { { { 'M' } , '\\' , '<M-Bslash>' } } , parse ( '<A-\\>' , 2 )) 42eq ( { { { 'C' } , '\\' , '<C-\\>' } } , parse ( '<C-\\>' , 2 )) 43eq ( { { {} , '|' , '<Bar>' } } , parse ( '|' , 2 )) 44eq ( { { { 'M' } , '|' , '<M-Bar>' } } , parse ( '<A-|>' , 2 )) 45eq ( { { { 'C' } , '|' , '<C-Bar>' } } , parse ( '<C-|>' , 2 )) 46eq ( { { {} , '\127' , '\127' } } , parse ( '\127' , 2 )) 47eq ( { { { 'M' } , '\127' , '<M-^?>' } } , parse ( '<A-\127>' , 2 )) 48eq ( { { { 'C' } , '\127' , '<C-^?>' } } , parse ( '<C-\127>' , 2 )) 49 50eq ( { { {} , '<' , '<lt>' } } , parse ( '<' , 2 )) 51eq ( { { {} , '\t' , '<Tab>' } } , parse ( '\t' , 2 )) 52eq ( { { {} , '\r' , '<CR>' } } , parse ( '\r' , 2 )) 53eq ( { { {} , '\n' , '<NL>' } } , parse ( '\n' , 2 )) 54eq ( { { {} , '\27' , '<Esc>' } } , parse ( '\27' , 2 )) 55eq ( { { {} , ' ' , '<Space>' } } , parse ( ' ' , 2 )) 56 57eq ( { { { 'S' } , 'a' , 'A' } } , parse ( 'A' , 2 )) 58eq ( { { { 'C' } , 'a' , '<C-A>' } } , parse ( '<C-a>' , 2 )) 59-- stylua: ignore end 60 61eq ( { { mod ={} , key ='<' , keys ='<lt>' , key_alt ='lt' } } , parse ( '<' )) 62end ) 63 64it ( 'utf8' , function () 65eq ( { { {} , 'ö' } , { { 'M' } , 'ó' } , { {} , 'ú' } , { {} , 'ü' } } , parse ( 'ö<A-ó>úü' , 1 )) 66eq ( { { {} , 'a' } , { {} , '\226\129\129' } , { {} , 'b' } } , parse ( 'a\226\129\129b' , 1 )) 67eq ( { { {} , 'a' } , { {} , '\242\129\129\129' } , { {} , 'b' } } , parse ( 'a\242\129\129\129b' , 1 )) 68end ) 69 70-- Canonical notation: concatenating each chord's `keys` must equal keytrans(vim.keycode(…)). 71it ( 'keys concatenate to keytrans()' , function () 72for _ , s in ipairs ( { 73'|' , 74'\\' , 75'<' , 76'<A-|>' , 77'<C-\\>' , 78'A' , 79'<C-a>' , 80'<C-S-Home>ab' , 81' \t' , 82'ö<A-ó>' , 83} ) do 84local concat , keytrans =exec_lua ( function ( k ) 85local enc , chords =vim . keycode ( k , true ) 86local parts =vim . tbl_map ( function ( c ) 87return c . keys 88end , chords ) 89return table . concat ( parts ), vim . fn . keytrans ( enc ) 90end , s ) 91eq ( keytrans , concat ) 92end 93end ) 94end ) 95 96describe ( 'nvim_replace_termcodes' , function () 97it ( 'escapes K_SPECIAL as K_SPECIAL KS_SPECIAL KE_FILLER' , function () 98eq ( '\128\254X' , n . api . nvim_replace_termcodes ( '\128' , true , true , true )) 99end ) 100 101it ( 'leaves non-K_SPECIAL string unchanged' , function () 102eq ( 'abc' , n . api . nvim_replace_termcodes ( 'abc' , true , true , true )) 103end ) 104 105it ( 'converts <expressions>' , function () 106eq ( '\\' , n . api . nvim_replace_termcodes ( '<Leader>' , true , true , true )) 107end ) 108 109it ( 'converts <LeftMouse> to K_SPECIAL KS_EXTRA KE_LEFTMOUSE' , function () 110-- K_SPECIAL KS_EXTRA KE_LEFTMOUSE 111-- 0x80 0xfd 0x2c 112-- 128 253 44 113eq ( '\128\253\44' , n . api . nvim_replace_termcodes ( '<LeftMouse>' , true , true , true )) 114end ) 115 116it ( 'converts keycodes' , function () 117eq ( '\nx\27x\rx<x' , n . api . nvim_replace_termcodes ( '<NL>x<Esc>x<CR>x<lt>x' , true , true , true )) 118end ) 119 120it ( 'does not convert keycodes if special=false' , function () 121eq ( 122'<NL>x<Esc>x<CR>x<lt>x' , 123n . api . nvim_replace_termcodes ( '<NL>x<Esc>x<CR>x<lt>x' , true , true , false ) 124) 125end ) 126 127it ( 'does not crash when transforming an empty string' , function () 128-- Actually does not test anything, because current code will use NULL for 129-- an empty string. 130-- 131-- Problem here is that if String argument has .data in allocated memory 132-- then `return str` in vim_replace_termcodes body will make Neovim free 133-- `str.data` twice: once when freeing arguments, then when freeing return 134-- value. 135eq ( '' , n . api . nvim_replace_termcodes ( '' , true , true , true )) 136end ) 137 138-- Not exactly the case, as nvim_replace_termcodes() escapes K_SPECIAL in Unicode 139it ( 'translates the result of keytrans() on string with 0x80 byte back' , function () 140local s ='ff\128\253\097tt' 141eq ( s , n . api . nvim_replace_termcodes ( n . fn . keytrans ( s ), true , true , true )) 142end ) 143end )