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_ref_job_partial_dict() 254 if has('job') 255 let g:ref_job = job_start('echo') 256 let d = {'a': 'b'} 257 call job_setoptions(g:ref_job, {'exit_cb': function('string', [], d)}) 258 endif 259endfunc 260 261func Test_auto_partial_rebind() 262 let dict1 = {'name': 'dict1'} 263 func! dict1.f1() 264 return self.name 265 endfunc 266 let dict1.f2 = function(dict1.f1, dict1) 267 268 call assert_equal('dict1', dict1.f1()) 269 call assert_equal('dict1', dict1['f1']()) 270 call assert_equal('dict1', dict1.f2()) 271 call assert_equal('dict1', dict1['f2']()) 272 273 let dict2 = {'name': 'dict2'} 274 let dict2.f1 = dict1.f1 275 let dict2.f2 = dict1.f2 276 277 call assert_equal('dict2', dict2.f1()) 278 call assert_equal('dict2', dict2['f1']()) 279 call assert_equal('dict1', dict2.f2()) 280 call assert_equal('dict1', dict2['f2']()) 281endfunc 282 283func Test_get_partial_items() 284 let dict = {'name': 'hello'} 285 let Cb = function('MyDictFunc', ["foo", "bar"], dict) 286 call assert_equal('MyDictFunc', get(Cb, 'func')) 287 call assert_equal(["foo", "bar"], get(Cb, 'args')) 288 call assert_equal(dict, get(Cb, 'dict')) 289 call assert_fails('call get(Cb, "xxx")', 'E475:') 290endfunc 291