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