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