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