robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
410be968d5
master
1" Test binding arguments to a Funcref. 2 3func MyFunc ( arg1 , arg2 , arg3 ) 4return a: arg1 . '/' . a: arg2 . '/' . a: arg3 5endfunc 6 7func MySort ( up , one , two ) 8if a: one == a: two 9return 0 10endif 11if a: up 12return a: one > a: two ? 1 : -1 13endif 14return a: one < a: two ? 1 : -1 15endfunc 16 17func MyMap ( sub , index , val ) 18return a: val - a: sub 19endfunc 20 21func MyFilter ( threshold , index , val ) 22return a: val > a: threshold 23endfunc 24 25func Test_partial_args () 26let Cb = function ( 'MyFunc' , [ "foo" , "bar" ]) 27 28call Cb ( "zzz" ) 29call assert_equal ( "foo/bar/xxx" , Cb ( "xxx" )) 30call assert_equal ( "foo/bar/yyy" , call ( Cb , [ "yyy" ])) 31let Cb2 = function ( Cb ) 32call assert_equal ( "foo/bar/zzz" , Cb2 ( "zzz" )) 33let Cb3 = function ( Cb , [ "www" ]) 34call assert_equal ( "foo/bar/www" , Cb3 ()) 35 36let Cb = function ( 'MyFunc' , []) 37call assert_equal ( "a/b/c" , Cb ( "a" , "b" , "c" )) 38let Cb2 = function ( Cb , []) 39call assert_equal ( "a/b/d" , Cb2 ( "a" , "b" , "d" )) 40let Cb3 = function ( Cb , [ "a" , "b" ]) 41call assert_equal ( "a/b/e" , Cb3 ( "e" )) 42 43let Sort = function ( 'MySort' , [ 1 ]) 44call assert_equal ([ 1 , 2 , 3 ], sort ([ 3 , 1 , 2 ], Sort )) 45let Sort = function ( 'MySort' , [ 0 ]) 46call assert_equal ([ 3 , 2 , 1 ], sort ([ 3 , 1 , 2 ], Sort )) 47 48let Map = function ( 'MyMap' , [ 2 ]) 49call assert_equal ([ -1 , 0 , 1 ], map ([ 1 , 2 , 3 ], Map )) 50let Map = function ( 'MyMap' , [ 3 ]) 51call assert_equal ([ -2 , -1 , 0 ], map ([ 1 , 2 , 3 ], Map )) 52 53let Filter = function ( 'MyFilter' , [ 1 ]) 54call assert_equal ([ 2 , 3 ], filter ([ 1 , 2 , 3 ], Filter )) 55let Filter = function ( 'MyFilter' , [ 2 ]) 56call assert_equal ([ 3 ], filter ([ 1 , 2 , 3 ], Filter )) 57endfunc 58 59func MyDictFunc ( arg1 , arg2 ) dict 60return self . name . '/' . a: arg1 . '/' . a: arg2 61endfunc 62 63func Test_partial_dict () 64let dict = { 'name' : 'hello' } 65let Cb = function ( 'MyDictFunc' , [ "foo" , "bar" ], dict ) 66call test_garbagecollect_now () 67call assert_equal ( "hello/foo/bar" , Cb ()) 68call assert_fails ( 'Cb("xxx")' , 'E492:' ) 69 70let Cb = function ( 'MyDictFunc' , [ "foo" ], dict ) 71call assert_equal ( "hello/foo/xxx" , Cb ( "xxx" )) 72call assert_fails ( 'Cb()' , 'E492:' ) 73 74let Cb = function ( 'MyDictFunc' , [], dict ) 75call assert_equal ( "hello/ttt/xxx" , Cb ( "ttt" , "xxx" )) 76call assert_fails ( 'Cb("yyy")' , 'E492:' ) 77 78let Cb = function ( 'MyDictFunc' , dict ) 79call assert_equal ( "hello/xxx/yyy" , Cb ( "xxx" , "yyy" )) 80call assert_fails ( 'Cb("fff")' , 'E492:' ) 81 82let Cb = function ( 'MyDictFunc' , dict ) 83call assert_equal ({ "foo" : "hello/foo/1" , "bar" : "hello/bar/2" }, map ({ "foo" : 1 , "bar" : 2 }, Cb )) 84 85let dict = { "tr" : function ( 'tr' , [ 'hello' , 'h' , 'H' ])} 86call assert_equal ( "Hello" , dict . tr ()) 87 88call assert_fails ( "let F=function('setloclist', 10)" , "E923:" ) 89call assert_fails ( "let F=function('setloclist', [], [])" , "E1206:" ) 90endfunc 91 92func Test_partial_implicit () 93let dict = { 'name' : 'foo' } 94func dict . MyFunc ( arg ) dict 95return self . name . '/' . a: arg 96endfunc 97 98call assert_equal ( 'foo/bar' , dict . MyFunc ( 'bar' )) 99 100call assert_fails ( 'let func = dict.MyFunc' , 'E704:' ) 101let Func = dict . MyFunc 102call assert_equal ( 'foo/aaa' , Func ( 'aaa' )) 103 104let Func = function ( dict . MyFunc , [ 'bbb' ]) 105call assert_equal ( 'foo/bbb' , Func ()) 106endfunc 107 108fun InnerCall ( funcref ) 109return a: funcref 110endfu 111 112fun OuterCall () 113let opt = { 'func' : function ( 'max' ) } 114call InnerCall ( opt . func ) 115endfu 116 117func Test_function_in_dict () 118call OuterCall () 119endfunc 120 121func s: cache_clear () dict 122return self . name 123endfunc 124 125func Test_script_function_in_dict () 126let s: obj = { 'name' : 'foo' } 127let s: obj2 = { 'name' : 'bar' } 128 129let s: obj [ 'clear' ] = function ( 's:cache_clear' ) 130 131call assert_equal ( 'foo' , s: obj . clear ()) 132let F = s: obj . clear 133call assert_equal ( 'foo' , F ()) 134call assert_equal ( 'foo' , call ( s: obj . clear , [], s: obj )) 135call assert_equal ( 'bar' , call ( s: obj . clear , [], s: obj2 )) 136 137let s: obj2 [ 'clear' ] = function ( 's:cache_clear' ) 138call assert_equal ( 'bar' , s: obj2 . clear ()) 139let B = s: obj2 . clear 140call assert_equal ( 'bar' , B ()) 141endfunc 142 143func s: cache_arg ( arg ) dict 144let s: result = self . name . '/' . a: arg 145return s: result 146endfunc 147 148func Test_script_function_in_dict_arg () 149let s: obj = { 'name' : 'foo' } 150let s: obj [ 'clear' ] = function ( 's:cache_arg' ) 151 152call assert_equal ( 'foo/bar' , s: obj . clear ( 'bar' )) 153let F = s: obj . clear 154let s: result = '' 155call assert_equal ( 'foo/bar' , F ( 'bar' )) 156call assert_equal ( 'foo/bar' , s: result ) 157 158let s: obj [ 'clear' ] = function ( 's:cache_arg' , [ 'bar' ]) 159call assert_equal ( 'foo/bar' , s: obj . clear ()) 160let s: result = '' 161call s: obj . clear () 162call assert_equal ( 'foo/bar' , s: result ) 163 164let F = s: obj . clear 165call assert_equal ( 'foo/bar' , F ()) 166let s: result = '' 167call F () 168call assert_equal ( 'foo/bar' , s: result ) 169 170call assert_equal ( 'foo/bar' , call ( s: obj . clear , [], s: obj )) 171endfunc 172 173func Test_partial_exists () 174let F = function ( 'MyFunc' ) 175call assert_true ( exists ( '*F' )) 176let lF = [ F ] 177call assert_true ( exists ( '*lF[0]' )) 178 179let F = function ( 'MyFunc' , [ 'arg' ]) 180call assert_true ( exists ( '*F' )) 181let lF = [ F ] 182call assert_true ( exists ( '*lF[0]' )) 183endfunc 184 185func Test_partial_string () 186let F = function ( 'MyFunc' ) 187call assert_equal ( "function('MyFunc')" , string ( F )) 188let F = function ( 'MyFunc' , [ 'foo' ]) 189call assert_equal ( "function('MyFunc', ['foo'])" , string ( F )) 190let F = function ( 'MyFunc' , [ 'foo' , 'bar' ]) 191call assert_equal ( "function('MyFunc', ['foo', 'bar'])" , string ( F )) 192let d = { 'one' : 1 } 193let F = function ( 'MyFunc' , d ) 194call assert_equal ( "function('MyFunc', {'one': 1})" , string ( F )) 195let F = function ( 'MyFunc' , [ 'foo' ], d ) 196call assert_equal ( "function('MyFunc', ['foo'], {'one': 1})" , string ( F )) 197" Nvim doesn't have null functions 198" call assert_equal("function('')", string(test_null_function())) 199" Nvim doesn't have null partials 200" call assert_equal("function('')", string(test_null_partial())) 201endfunc 202 203func Test_func_unref () 204let obj = {} 205function ! obj . func () abort 206endfunction 207let funcnumber = matchstr ( string ( obj . func ), '^function(''\zs.\{-}\ze''' ) 208call assert_true ( exists ( '*{' . funcnumber . '}' )) 209unlet obj 210call assert_false ( exists ( '*{' . funcnumber . '}' )) 211endfunc 212 213func Test_redefine_dict_func () 214let d = {} 215function d . test4 () 216endfunction 217let d . test4 = d . test4 218try 219function ! d . test4 ( name ) 220endfunction 221catch 222call assert_true ( v: errmsg , v: exception ) 223endtry 224endfunc 225 226" This caused double free on exit if EXITFREE is defined. 227func Test_cyclic_list_arg () 228let l = [] 229let Pt = function ( 'string' , [ l ]) 230call add ( l , Pt ) 231unlet l 232unlet Pt 233endfunc 234 235" This caused double free on exit if EXITFREE is defined. 236func Test_cyclic_dict_arg () 237let d = {} 238let Pt = function ( 'string' , [ d ]) 239let d . Pt = Pt 240unlet d 241unlet Pt 242endfunc 243 244func Ignored ( job1 , job2 , status ) 245endfunc 246 247func Test_cycle_partial_job () 248if has ( 'job' ) 249let job = job_start ( 'echo' ) 250call job_setoptions ( job , { 'exit_cb' : function ( 'Ignored' , [ job ])}) 251unlet job 252endif 253endfunc 254 255func Test_ref_job_partial_dict () 256if has ( 'job' ) 257let g: ref_job = job_start ( 'echo' ) 258let d = { 'a' : 'b' } 259call job_setoptions ( g: ref_job , { 'exit_cb' : function ( 'string' , [], d )}) 260call test_garbagecollect_now () 261endif 262endfunc 263 264func Test_auto_partial_rebind () 265let dict1 = { 'name' : 'dict1' } 266func ! dict1 . f1 () 267return self . name 268endfunc 269let dict1 . f2 = function ( dict1 . f1 , dict1 ) 270 271call assert_equal ( 'dict1' , dict1 . f1 ()) 272call assert_equal ( 'dict1' , dict1 [ 'f1' ]()) 273call assert_equal ( 'dict1' , dict1 . f2 ()) 274call assert_equal ( 'dict1' , dict1 [ 'f2' ]()) 275 276let dict2 = { 'name' : 'dict2' } 277let dict2 . f1 = dict1 . f1 278let dict2 . f2 = dict1 . f2 279 280call assert_equal ( 'dict2' , dict2 . f1 ()) 281call assert_equal ( 'dict2' , dict2 [ 'f1' ]()) 282call assert_equal ( 'dict1' , dict2 . f2 ()) 283call assert_equal ( 'dict1' , dict2 [ 'f2' ]()) 284endfunc 285 286func Test_get_partial_items () 287func s: Qux ( x , y , z = 3 , w = 1 , ...) 288endfunc 289func s: Qux1 ( x , y ) 290endfunc 291 292let dict = { 'name' : 'hello' } 293let args = [ "foo" , "bar" ] 294let Func = function ( 'MyDictFunc' ) 295let Cb = function ( 'MyDictFunc' , args , dict ) 296 297call assert_equal ( Func , get ( Cb , 'func' )) 298call assert_equal ( 'MyDictFunc' , get ( Cb , 'name' )) 299call assert_equal ( args , get ( Cb , 'args' )) 300call assert_equal ( dict , get ( Cb , 'dict' )) 301call assert_fails ( 'call get(Cb, "xxx")' , 'E475:' ) 302 303call assert_equal ( Func , get ( Func , 'func' )) 304call assert_equal ( 'MyDictFunc' , get ( Func , 'name' )) 305call assert_equal ([], get ( Func , 'args' )) 306call assert_true ( empty ( get ( Func , 'dict' ))) 307 308let P = function ( 'substitute' , [ 'hello there' , 'there' ]) 309let dict = { 'partial has' : 'no dict' } 310call assert_equal ( dict , get ( P , 'dict' , dict )) 311call assert_equal ( 0 , get ( l: P , 'dict' )) 312 313call assert_equal ({ 'required' : 2 , 'optional' : 2 , 'varargs' : v: true }, 314 \get ( funcref ( 's:Qux' , []), 'arity' )) 315call assert_equal ({ 'required' : 1 , 'optional' : 2 , 'varargs' : v: true }, 316 \get ( funcref ( 's:Qux' , [ 1 ]), 'arity' )) 317call assert_equal ({ 'required' : 0 , 'optional' : 2 , 'varargs' : v: true }, 318 \get ( funcref ( 's:Qux' , [ 1 , 2 ]), 'arity' )) 319call assert_equal ({ 'required' : 0 , 'optional' : 1 , 'varargs' : v: true }, 320 \get ( funcref ( 's:Qux' , [ 1 , 2 , 3 ]), 'arity' )) 321call assert_equal ({ 'required' : 0 , 'optional' : 0 , 'varargs' : v: true }, 322 \get ( funcref ( 's:Qux' , [ 1 , 2 , 3 , 4 ]), 'arity' )) 323" More args than expected is not an error 324call assert_equal ({ 'required' : 0 , 'optional' : 0 , 'varargs' : v: false }, 325 \get ( funcref ( 's:Qux1' , [ 1 , 2 , 3 , 4 ]), 'arity' )) 326 327delfunc s:Qux 328delfunc s:Qux1 329endfunc 330 331func Test_compare_partials () 332let d1 = {} 333let d2 = {} 334 335function d1 . f1 () dict 336endfunction 337 338function d1 . f2 () dict 339endfunction 340 341let F1 = get ( d1 , 'f1' ) 342let F2 = get ( d1 , 'f2' ) 343 344let F1d1 = function ( F1 , d1 ) 345let F2d1 = function ( F2 , d2 ) 346let F1d1a1 = function ( F1d1 , [ 1 ]) 347let F1d1a12 = function ( F1d1 , [ 1 , 2 ]) 348let F1a1 = function ( F1 , [ 1 ]) 349let F1a2 = function ( F1 , [ 2 ]) 350let F1d2 = function ( F1 , d2 ) 351let d3 = { 'f1' : F1 , 'f2' : F2 } 352let F1d3 = function ( F1 , d3 ) 353let F1ad1 = function ( F1 , [ d1 ]) 354let F1ad3 = function ( F1 , [ d3 ]) 355 356call assert_match ( '^function(''\d\+'')$' , string ( F1 )) " Not a partial 357call assert_match ( '^function(''\d\+'')$' , string ( F2 )) " Not a partial 358call assert_match ( '^function(''\d\+'', {.*})$' , string ( F1d1 )) " A partial 359call assert_match ( '^function(''\d\+'', {.*})$' , string ( F2d1 )) " A partial 360call assert_match ( '^function(''\d\+'', \[.*\])$' , string ( F1a1 )) " No dict 361 362" != 363let X = F1 364call assert_false ( F1 != X ) " same function 365let X = F1d1 366call assert_false ( F1d1 != X ) " same partial 367let X = F1d1a1 368call assert_false ( F1d1a1 != X ) " same partial 369let X = F1a1 370call assert_false ( F1a1 != X ) " same partial 371 372call assert_true ( F1 != F2 ) " Different functions 373call assert_true ( F1 != F1d1 ) " Partial /= non-partial 374call assert_true ( F1d1a1 != F1d1a12 ) " Different number of arguments 375call assert_true ( F1a1 != F1d1a12 ) " One has no dict 376call assert_true ( F1a1 != F1a2 ) " Different arguments 377call assert_true ( F1d2 != F1d1 ) " Different dictionaries 378call assert_false ( F1d1 != F1d3 ) " Equal dictionaries, even though d1 isnot d3 379 380" isnot, option 1 381call assert_true ( F1 isnot# F2 ) " Different functions 382call assert_true ( F1 isnot# F1d1 ) " Partial /= non-partial 383call assert_true ( F1d1 isnot# F1d3 ) " d1 isnot d3, even though d1 == d3 384call assert_true ( F1a1 isnot# F1d1a12 ) " One has no dict 385call assert_true ( F1a1 isnot# F1a2 ) " Different number of arguments 386call assert_true ( F1ad1 isnot# F1ad3 ) " In arguments d1 isnot d3 387 388" isnot, option 2 389call assert_true ( F1 isnot# F2 ) " Different functions 390call assert_true ( F1 isnot# F1d1 ) " Partial /= non-partial 391call assert_true ( d1 . f1 isnot# d1 . f1 ) " handle_subscript creates new partial each time 392 393" compare two null partials 394" Nvim doesn't have null partials 395" let N1 = test_null_partial() 396let N1 = function ( 'min' ) 397let N2 = N1 398call assert_true ( N1 is N2 ) 399call assert_true ( N1 == N2 ) 400 401" compare a partial and a null partial 402call assert_false ( N1 == F1 ) 403call assert_false ( F1 is N1 ) 404endfunc 405 406func Test_partial_method () 407func Foo ( x , y , z ) 408return x + y + z 409endfunc 410let d = { "Fn" : function ( 'Foo' , [ 10 , 20 ])} 411call assert_fails ( 'echo 30->d.Fn()' , 'E1265: Cannot use a partial here' ) 412delfunc Foo 413endfunc 414 415func Test_non_callable_type_as_method () 416let d = { "Fn" : 10 } 417call assert_fails ( 'echo 30->d.Fn()' , 'E1085: Not a callable type: d.Fn' ) 418endfunc 419 420" vim: shiftwidth=2 sts=2 expandtab