robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f907614088
master
1-- Test for user functions. 2-- Also test an <expr> mapping calling a function. 3-- Also test that a builtin function cannot be replaced. 4-- Also test for regression when calling arbitrary expression. 5 6local n =require ( 'test.functional.testnvim' )() 7local t =require ( 'test.testutil' ) 8 9local describe , it , setup =t . describe , t . it , t . setup 10local feed , insert , source =n . feed , n . insert , n . source 11local clear , feed_command , expect =n . clear , n . feed_command , n . expect 12 13describe ( 14'user functions, expr-mappings, overwrite protected builtin functions and regression on calling expressions' , 15function () 16setup ( clear ) 17 18it ( 'are working' , function () 19insert ( 'here' ) 20 21source ( [[ 22function Table(title, ...) 23let ret = a:title 24let idx = 1 25while idx <= a:0 26exe "let ret = ret . a:" . idx 27let idx = idx + 1 28endwhile 29return ret 30endfunction 31function Compute(n1, n2, divname) 32if a:n2 == 0 33return "fail" 34endif 35exe "let g:" . a:divname . " = ". a:n1 / a:n2 36return "ok" 37endfunction 38func Expr1() 39normal! v 40return "111" 41endfunc 42func Expr2() 43call search('XX', 'b') 44return "222" 45endfunc 46func ListItem() 47let g:counter += 1 48return g:counter . '. ' 49endfunc 50func ListReset() 51let g:counter = 0 52return '' 53endfunc 54func FuncWithRef(a) 55unlet g:FuncRef 56return a:a 57endfunc 58let g:FuncRef=function("FuncWithRef") 59let counter = 0 60inoremap <expr> ( ListItem() 61inoremap <expr> [ ListReset() 62imap <expr> + Expr1() 63imap <expr> * Expr2() 64let retval = "nop" 65/^here 66]] ) 67feed ( 'C<C-R>=Table("xxx", 4, "asdf")<cr>' ) 68feed ( ' <C-R>=Compute(45, 0, "retval")<cr>' ) 69feed ( ' <C-R>=retval<cr>' ) 70feed ( ' <C-R>=Compute(45, 5, "retval")<cr>' ) 71feed ( ' <C-R>=retval<cr>' ) 72feed ( ' <C-R>=g:FuncRef(333)<cr>' ) 73feed ( '<cr>' ) 74feed ( 'XX+-XX<cr>' ) 75feed ( '---*---<cr>' ) 76feed ( '(one<cr>' ) 77feed ( '(two<cr>' ) 78feed ( '[(one again<esc>' ) 79feed_command ( 'call append(line("$"), max([1, 2, 3]))' ) 80feed_command ( 'call extend(g:, {"max": function("min")})' ) 81feed_command ( 'call append(line("$"), max([1, 2, 3]))' ) 82feed_command ( 'try' ) 83-- Regression: the first line below used to throw "E110: Missing ')'" 84-- Second is here just to prove that this line is correct when not 85-- skipping rhs of &&. 86feed_command ( [[ $put =(0&&(function('tr'))(1, 2, 3))]] ) 87feed_command ( [[ $put =(1&&(function('tr'))(1, 2, 3))]] ) 88feed_command ( 'catch' ) 89feed_command ( [[ $put ='!!! Unexpected exception:']] ) 90feed_command ( ' $put =v:exception' ) 91feed_command ( 'endtry' ) 92 93-- Assert buffer contents. 94expect ( [[ 95xxx4asdf fail nop ok 9 333 96XX111-XX 97---222--- 981. one 992. two 1001. one again 1013 1023 1030 1041]] ) 105end ) 106end 107)