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