blob: 30e1df99e7fbd6f2ebc52c05b3924f7635e5e247 [file] [log] [blame]
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001" 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
Bram Moolenaar790500a2016-03-15 11:05:45 +010012 return a:one > a:two ? 1 : -1
Bram Moolenaar1735bc92016-03-14 23:05:14 +010013 endif
Bram Moolenaar790500a2016-03-15 11:05:45 +010014 return a:one < a:two ? 1 : -1
Bram Moolenaar1735bc92016-03-14 23:05:14 +010015endfunc
16
17func Test_partial_args()
18 let Cb = function('MyFunc', ["foo", "bar"])
Bram Moolenaar65639032016-03-16 21:40:30 +010019
20 call Cb("zzz")
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021 call assert_equal("foo/bar/xxx", Cb("xxx"))
22 call assert_equal("foo/bar/yyy", call(Cb, ["yyy"]))
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010023 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())
Bram Moolenaar1735bc92016-03-14 23:05:14 +010027
Bram Moolenaar346418c2016-03-15 12:36:08 +010028 let Cb = function('MyFunc', [])
29 call assert_equal("a/b/c", Cb("a", "b", "c"))
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010030 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"))
Bram Moolenaar346418c2016-03-15 12:36:08 +010034
Bram Moolenaar1735bc92016-03-14 23:05:14 +010035 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:')
Bram Moolenaar346418c2016-03-15 12:36:08 +010050
Bram Moolenaar1735bc92016-03-14 23:05:14 +010051 let Cb = function('MyDictFunc', ["foo"], dict)
52 call assert_equal("hello/foo/xxx", Cb("xxx"))
53 call assert_fails('Cb()', 'E492:')
Bram Moolenaar346418c2016-03-15 12:36:08 +010054
55 let Cb = function('MyDictFunc', [], dict)
56 call assert_equal("hello/ttt/xxx", Cb("ttt", "xxx"))
57 call assert_fails('Cb("yyy")', 'E492:')
58
Bram Moolenaar1735bc92016-03-14 23:05:14 +010059 let Cb = function('MyDictFunc', dict)
60 call assert_equal("hello/xxx/yyy", Cb("xxx", "yyy"))
Bram Moolenaar346418c2016-03-15 12:36:08 +010061 call assert_fails('Cb("fff")', 'E492:')
Bram Moolenaar65639032016-03-16 21:40:30 +010062
63 let dict = {"tr": function('tr', ['hello', 'h', 'H'])}
64 call assert_equal("Hello", dict.tr())
Bram Moolenaar1735bc92016-03-14 23:05:14 +010065endfunc
Bram Moolenaarab1fa392016-03-15 19:33:34 +010066
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())
Bram Moolenaarab1fa392016-03-15 19:33:34 +010081endfunc
Bram Moolenaar7a5c46a2016-03-16 20:41:21 +010082
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
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010096function! 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
Bram Moolenaard22a1892016-03-17 20:50:47 +0100117
Bram Moolenaar9e63f612016-03-17 23:13:28 +0100118function! 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
Bram Moolenaard22a1892016-03-17 20:50:47 +0100148func 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
Bram Moolenaar5c291542016-03-19 20:05:45 +0100159
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
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +0100173
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
Bram Moolenaar24c77a12016-03-24 21:23:06 +0100183
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
Bram Moolenaarc5fbe8a2016-03-24 21:42:09 +0100196
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
Bram Moolenaar4c908612016-03-24 21:58:12 +0100209
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
Bram Moolenaarddecc252016-04-06 22:59:37 +0200223
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200224" This caused double free on exit if EXITFREE is defined.
Bram Moolenaarddecc252016-04-06 22:59:37 +0200225func 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
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200233" This caused double free on exit if EXITFREE is defined.
Bram Moolenaarddecc252016-04-06 22:59:37 +0200234func 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
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200241
242func Ignored(job1, job2, status)
243endfunc
244
245func Test_cycle_partial_job()
Bram Moolenaar9e404372016-04-08 17:25:19 +0200246 if has('job')
247 let job = job_start('echo')
248 call job_setoptions(job, {'exit_cb': function('Ignored', [job])})
249 unlet job
250 endif
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200251endfunc
252
253func Test_ref_job_partial_dict()
Bram Moolenaar9e404372016-04-08 17:25:19 +0200254 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
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200259endfunc
Bram Moolenaar1d429612016-05-24 15:44:17 +0200260
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
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +0200282
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