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