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 assert_equal("hello/foo/bar", Cb()) 67 call assert_fails('Cb("xxx")', 'E492:') 68 69 let Cb = function('MyDictFunc', ["foo"], dict) 70 call assert_equal("hello/foo/xxx", Cb("xxx")) 71 call assert_fails('Cb()', 'E492:') 72 73 let Cb = function('MyDictFunc', [], dict) 74 call assert_equal("hello/ttt/xxx", Cb("ttt", "xxx")) 75 call assert_fails('Cb("yyy")', 'E492:') 76 77 let Cb = function('MyDictFunc', dict) 78 call assert_equal("hello/xxx/yyy", Cb("xxx", "yyy")) 79 call assert_fails('Cb("fff")', 'E492:') 80 81 let Cb = function('MyDictFunc', dict) 82 call assert_equal({"foo": "hello/foo/1", "bar": "hello/bar/2"}, map({"foo": 1, "bar": 2}, Cb)) 83 84 let dict = {"tr": function('tr', ['hello', 'h', 'H'])} 85 call assert_equal("Hello", dict.tr()) 86endfunc 87 88func Test_partial_implicit() 89 let dict = {'name': 'foo'} 90 func dict.MyFunc(arg) dict 91 return self.name . '/' . a:arg 92 endfunc 93 94 call assert_equal('foo/bar', dict.MyFunc('bar')) 95 96 call assert_fails('let func = dict.MyFunc', 'E704:') 97 let Func = dict.MyFunc 98 call assert_equal('foo/aaa', Func('aaa')) 99 100 let Func = function(dict.MyFunc, ['bbb']) 101 call assert_equal('foo/bbb', Func()) 102endfunc 103 104fun InnerCall(funcref) 105 return a:funcref 106endfu 107 108fun OuterCall() 109 let opt = { 'func' : function('sin') } 110 call InnerCall(opt.func) 111endfu 112 113func Test_function_in_dict() 114 call OuterCall() 115endfunc 116 117function! s:cache_clear() dict 118 return self.name 119endfunction 120 121func Test_script_function_in_dict() 122 let s:obj = {'name': 'foo'} 123 let s:obj2 = {'name': 'bar'} 124 125 let s:obj['clear'] = function('s:cache_clear') 126 127 call assert_equal('foo', s:obj.clear()) 128 let F = s:obj.clear 129 call assert_equal('foo', F()) 130 call assert_equal('foo', call(s:obj.clear, [], s:obj)) 131 call assert_equal('bar', call(s:obj.clear, [], s:obj2)) 132 133 let s:obj2['clear'] = function('s:cache_clear') 134 call assert_equal('bar', s:obj2.clear()) 135 let B = s:obj2.clear 136 call assert_equal('bar', B()) 137endfunc 138 139function! s:cache_arg(arg) dict 140 let s:result = self.name . '/' . a:arg 141 return s:result 142endfunction 143 144func Test_script_function_in_dict_arg() 145 let s:obj = {'name': 'foo'} 146 let s:obj['clear'] = function('s:cache_arg') 147 148 call assert_equal('foo/bar', s:obj.clear('bar')) 149 let F = s:obj.clear 150 let s:result = '' 151 call assert_equal('foo/bar', F('bar')) 152 call assert_equal('foo/bar', s:result) 153 154 let s:obj['clear'] = function('s:cache_arg', ['bar']) 155 call assert_equal('foo/bar', s:obj.clear()) 156 let s:result = '' 157 call s:obj.clear() 158 call assert_equal('foo/bar', s:result) 159 160 let F = s:obj.clear 161 call assert_equal('foo/bar', F()) 162 let s:result = '' 163 call F() 164 call assert_equal('foo/bar', s:result) 165 166 call assert_equal('foo/bar', call(s:obj.clear, [], s:obj)) 167endfunc 168 169func Test_partial_exists() 170 let F = function('MyFunc') 171 call assert_true(exists('*F')) 172 let lF = [F] 173 call assert_true(exists('*lF[0]')) 174 175 let F = function('MyFunc', ['arg']) 176 call assert_true(exists('*F')) 177 let lF = [F] 178 call assert_true(exists('*lF[0]')) 179endfunc 180 181func Test_partial_string() 182 let F = function('MyFunc') 183 call assert_equal("function('MyFunc')", string(F)) 184 let F = function('MyFunc', ['foo']) 185 call assert_equal("function('MyFunc', ['foo'])", string(F)) 186 let F = function('MyFunc', ['foo', 'bar']) 187 call assert_equal("function('MyFunc', ['foo', 'bar'])", string(F)) 188 let d = {'one': 1} 189 let F = function('MyFunc', d) 190 call assert_equal("function('MyFunc', {'one': 1})", string(F)) 191 let F = function('MyFunc', ['foo'], d) 192 call assert_equal("function('MyFunc', ['foo'], {'one': 1})", string(F)) 193endfunc 194 195func Test_func_unref() 196 let obj = {} 197 function! obj.func() abort 198 endfunction 199 let funcnumber = matchstr(string(obj.func), '^function(''\zs.\{-}\ze''') 200 call assert_true(exists('*{' . funcnumber . '}')) 201 unlet obj 202 call assert_false(exists('*{' . funcnumber . '}')) 203endfunc 204 205func Test_tostring() 206 let d = {} 207 let d.d = d 208 function d.test3() 209 echo 42 210 endfunction 211 try 212 call string(d.test3) 213 catch 214 call assert_true(v:false, v:exception) 215 endtry 216endfunc 217 218func Test_redefine_dict_func() 219 let d = {} 220 function d.test4() 221 endfunction 222 let d.test4 = d.test4 223 try 224 function! d.test4(name) 225 endfunction 226 catch 227 call assert_true(v:errmsg, v:exception) 228 endtry 229endfunc 230 231func Test_bind_in_python() 232 if has('python') 233 let g:d = {} 234 function g:d.test2() 235 endfunction 236 python import vim 237 try 238 call assert_equal(pyeval('vim.bindeval("g:d.test2")'), g:d.test2) 239 catch 240 call assert_true(v:false, v:exception) 241 endtry 242 endif 243endfunc 244 245" This caused double free on exit if EXITFREE is defined. 246func Test_cyclic_list_arg() 247 let l = [] 248 let Pt = function('string', [l]) 249 call add(l, Pt) 250 unlet l 251 unlet Pt 252endfunc 253 254" This caused double free on exit if EXITFREE is defined. 255func Test_cyclic_dict_arg() 256 let d = {} 257 let Pt = function('string', [d]) 258 let d.Pt = Pt 259 unlet d 260 unlet Pt 261endfunc 262 263func Ignored3(job1, job2, status) 264endfunc 265 266func Test_cycle_partial_job() 267 if has('job') 268 let job = job_start('echo') 269 call job_setoptions(job, {'exit_cb': function('Ignored3', [job])}) 270 unlet job 271 endif 272endfunc 273 274func Ignored2(job, status) 275endfunc 276 277func Test_ref_job_partial_dict() 278 if has('job') 279 let g:ref_job = job_start('echo') 280 let d = {'a': 'b'} 281 call job_setoptions(g:ref_job, {'exit_cb': function('Ignored2', [], d)}) 282 endif 283endfunc 284 285func Test_auto_partial_rebind() 286 let dict1 = {'name': 'dict1'} 287 func! dict1.f1() 288 return self.name 289 endfunc 290 let dict1.f2 = function(dict1.f1, dict1) 291 292 call assert_equal('dict1', dict1.f1()) 293 call assert_equal('dict1', dict1['f1']()) 294 call assert_equal('dict1', dict1.f2()) 295 call assert_equal('dict1', dict1['f2']()) 296 297 let dict2 = {'name': 'dict2'} 298 let dict2.f1 = dict1.f1 299 let dict2.f2 = dict1.f2 300 301 call assert_equal('dict2', dict2.f1()) 302 call assert_equal('dict2', dict2['f1']()) 303 call assert_equal('dict1', dict2.f2()) 304 call assert_equal('dict1', dict2['f2']()) 305endfunc 306 307func Test_get_partial_items() 308 let dict = {'name': 'hello'} 309 let args = ["foo", "bar"] 310 let Func = function('MyDictFunc') 311 let Cb = function('MyDictFunc', args, dict) 312 313 call assert_equal(Func, get(Cb, 'func')) 314 call assert_equal('MyDictFunc', get(Cb, 'name')) 315 call assert_equal(args, get(Cb, 'args')) 316 call assert_equal(dict, get(Cb, 'dict')) 317 call assert_fails('call get(Cb, "xxx")', 'E475:') 318 319 call assert_equal(Func, get(Func, 'func')) 320 call assert_equal('MyDictFunc', get(Func, 'name')) 321 call assert_equal([], get(Func, 'args')) 322 call assert_true(empty( get(Func, 'dict'))) 323endfunc 324 325func Test_compare_partials() 326 let d1 = {} 327 let d2 = {} 328 329 function d1.f1() dict 330 endfunction 331 332 function d1.f2() dict 333 endfunction 334 335 let F1 = get(d1, 'f1') 336 let F2 = get(d1, 'f2') 337 338 let F1d1 = function(F1, d1) 339 let F2d1 = function(F2, d2) 340 let F1d1a1 = function(F1d1, [1]) 341 let F1d1a12 = function(F1d1, [1, 2]) 342 let F1a1 = function(F1, [1]) 343 let F1a2 = function(F1, [2]) 344 let F1d2 = function(F1, d2) 345 let d3 = {'f1': F1, 'f2': F2} 346 let F1d3 = function(F1, d3) 347 let F1ad1 = function(F1, [d1]) 348 let F1ad3 = function(F1, [d3]) 349 350 call assert_match('^function(''\d\+'')$', string(F1)) " Not a partial 351 call assert_match('^function(''\d\+'')$', string(F2)) " Not a partial 352 call assert_match('^function(''\d\+'', {.*})$', string(F1d1)) " A partial 353 call assert_match('^function(''\d\+'', {.*})$', string(F2d1)) " A partial 354 call assert_match('^function(''\d\+'', \[.*\])$', string(F1a1)) " No dict 355 356 " != 357 let X = F1 358 call assert_false(F1 != X) " same function 359 let X = F1d1 360 call assert_false(F1d1 != X) " same partial 361 let X = F1d1a1 362 call assert_false(F1d1a1 != X) " same partial 363 let X = F1a1 364 call assert_false(F1a1 != X) " same partial 365 366 call assert_true(F1 != F2) " Different functions 367 call assert_true(F1 != F1d1) " Partial /= non-partial 368 call assert_true(F1d1a1 != F1d1a12) " Different number of arguments 369 call assert_true(F1a1 != F1d1a12) " One has no dict 370 call assert_true(F1a1 != F1a2) " Different arguments 371 call assert_true(F1d2 != F1d1) " Different dictionaries 372 call assert_false(F1d1 != F1d3) " Equal dictionaries, even though d1 isnot d3 373 374 " isnot, option 1 375 call assert_true(F1 isnot# F2) " Different functions 376 call assert_true(F1 isnot# F1d1) " Partial /= non-partial 377 call assert_true(F1d1 isnot# F1d3) " d1 isnot d3, even though d1 == d3 378 call assert_true(F1a1 isnot# F1d1a12) " One has no dict 379 call assert_true(F1a1 isnot# F1a2) " Different number of arguments 380 call assert_true(F1ad1 isnot# F1ad3) " In arguments d1 isnot d3 381 382 " isnot, option 2 383 call assert_true(F1 isnot# F2) " Different functions 384 call assert_true(F1 isnot# F1d1) " Partial /= non-partial 385 call assert_true(d1.f1 isnot# d1.f1) " handle_subscript creates new partial each time 386endfunc 387