robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

zeertzjqMerge pull request #29924 from zeertzjq/vim-9.1.0411410be968d5

master
11.9 KiB420 linesraw
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', [], [])", "E1206:")
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))
197  " Nvim doesn't have null functions
198  " call assert_equal("function('')", string(test_null_function()))
199  " Nvim doesn't have null partials
200  " call assert_equal("function('')", string(test_null_partial()))
201endfunc
202
203func Test_func_unref()
204  let obj = {}
205  function! obj.func() abort
206  endfunction
207  let funcnumber = matchstr(string(obj.func), '^function(''\zs.\{-}\ze''')
208  call assert_true(exists('*{' . funcnumber . '}'))
209  unlet obj
210  call assert_false(exists('*{' . funcnumber . '}'))
211endfunc
212
213func Test_redefine_dict_func()
214  let d = {}
215  function d.test4()
216  endfunction
217  let d.test4 = d.test4
218  try
219    function! d.test4(name)
220    endfunction
221  catch
222    call assert_true(v:errmsg, v:exception)
223  endtry
224endfunc
225
226" This caused double free on exit if EXITFREE is defined.
227func Test_cyclic_list_arg()
228  let l = []
229  let Pt = function('string', [l])
230  call add(l, Pt)
231  unlet l
232  unlet Pt
233endfunc
234
235" This caused double free on exit if EXITFREE is defined.
236func Test_cyclic_dict_arg()
237  let d = {}
238  let Pt = function('string', [d])
239  let d.Pt = Pt
240  unlet d
241  unlet Pt
242endfunc
243
244func Ignored(job1, job2, status)
245endfunc
246
247func Test_cycle_partial_job()
248  if has('job')
249    let job = job_start('echo')
250    call job_setoptions(job, {'exit_cb': function('Ignored', [job])})
251    unlet job
252  endif
253endfunc
254
255func Test_ref_job_partial_dict()
256  if has('job')
257    let g:ref_job = job_start('echo')
258    let d = {'a': 'b'}
259    call job_setoptions(g:ref_job, {'exit_cb': function('string', [], d)})
260    call test_garbagecollect_now()
261  endif
262endfunc
263
264func Test_auto_partial_rebind()
265  let dict1 = {'name': 'dict1'}
266  func! dict1.f1()
267    return self.name
268  endfunc
269  let dict1.f2 = function(dict1.f1, dict1)
270
271  call assert_equal('dict1', dict1.f1())
272  call assert_equal('dict1', dict1['f1']())
273  call assert_equal('dict1', dict1.f2())
274  call assert_equal('dict1', dict1['f2']())
275
276  let dict2 = {'name': 'dict2'}
277  let dict2.f1 = dict1.f1
278  let dict2.f2 = dict1.f2
279
280  call assert_equal('dict2', dict2.f1())
281  call assert_equal('dict2', dict2['f1']())
282  call assert_equal('dict1', dict2.f2())
283  call assert_equal('dict1', dict2['f2']())
284endfunc
285
286func Test_get_partial_items()
287  func s:Qux(x, y, z=3, w=1, ...)
288  endfunc
289  func s:Qux1(x, y)
290  endfunc
291
292  let dict = {'name': 'hello'}
293  let args = ["foo", "bar"]
294  let Func = function('MyDictFunc')
295  let Cb = function('MyDictFunc', args, dict)
296
297  call assert_equal(Func, get(Cb, 'func'))
298  call assert_equal('MyDictFunc', get(Cb, 'name'))
299  call assert_equal(args, get(Cb, 'args'))
300  call assert_equal(dict, get(Cb, 'dict'))
301  call assert_fails('call get(Cb, "xxx")', 'E475:')
302
303  call assert_equal(Func, get(Func, 'func'))
304  call assert_equal('MyDictFunc', get(Func, 'name'))
305  call assert_equal([], get(Func, 'args'))
306  call assert_true(empty( get(Func, 'dict')))
307
308  let P = function('substitute', ['hello there', 'there'])
309  let dict = {'partial has': 'no dict'}
310  call assert_equal(dict, get(P, 'dict', dict))
311  call assert_equal(0, get(l:P, 'dict'))
312
313  call assert_equal({'required': 2, 'optional': 2, 'varargs': v:true},
314      \ get(funcref('s:Qux', []), 'arity'))
315  call assert_equal({'required': 1, 'optional': 2, 'varargs': v:true},
316      \ get(funcref('s:Qux', [1]), 'arity'))
317  call assert_equal({'required': 0, 'optional': 2, 'varargs': v:true},
318      \ get(funcref('s:Qux', [1, 2]), 'arity'))
319  call assert_equal({'required': 0, 'optional': 1, 'varargs': v:true},
320      \ get(funcref('s:Qux', [1, 2, 3]), 'arity'))
321  call assert_equal({'required': 0, 'optional': 0, 'varargs': v:true},
322      \ get(funcref('s:Qux', [1, 2, 3, 4]), 'arity'))
323  " More args than expected is not an error
324  call assert_equal({'required': 0, 'optional': 0, 'varargs': v:false},
325      \ get(funcref('s:Qux1', [1, 2, 3, 4]), 'arity'))
326
327  delfunc s:Qux
328  delfunc s:Qux1
329endfunc
330
331func Test_compare_partials()
332  let d1 = {}
333  let d2 = {}
334
335  function d1.f1() dict
336  endfunction
337
338  function d1.f2() dict
339  endfunction
340
341  let F1 = get(d1, 'f1')
342  let F2 = get(d1, 'f2')
343
344  let F1d1 = function(F1, d1)
345  let F2d1 = function(F2, d2)
346  let F1d1a1 = function(F1d1, [1])
347  let F1d1a12 = function(F1d1, [1, 2])
348  let F1a1 = function(F1, [1])
349  let F1a2 = function(F1, [2])
350  let F1d2 = function(F1, d2)
351  let d3 = {'f1': F1, 'f2': F2}
352  let F1d3 = function(F1, d3)
353  let F1ad1 = function(F1, [d1])
354  let F1ad3 = function(F1, [d3])
355
356  call assert_match('^function(''\d\+'')$', string(F1))  " Not a partial
357  call assert_match('^function(''\d\+'')$', string(F2))  " Not a partial
358  call assert_match('^function(''\d\+'', {.*})$', string(F1d1))  " A partial
359  call assert_match('^function(''\d\+'', {.*})$', string(F2d1))  " A partial
360  call assert_match('^function(''\d\+'', \[.*\])$', string(F1a1))  " No dict
361
362  " !=
363  let X = F1
364  call assert_false(F1 != X)  " same function
365  let X = F1d1
366  call assert_false(F1d1 != X)  " same partial
367  let X = F1d1a1
368  call assert_false(F1d1a1 != X)  " same partial
369  let X = F1a1
370  call assert_false(F1a1 != X)  " same partial
371
372  call assert_true(F1 != F2)  " Different functions
373  call assert_true(F1 != F1d1)  " Partial /= non-partial
374  call assert_true(F1d1a1 != F1d1a12)  " Different number of arguments
375  call assert_true(F1a1 != F1d1a12)  " One has no dict
376  call assert_true(F1a1 != F1a2)  " Different arguments
377  call assert_true(F1d2 != F1d1)  " Different dictionaries
378  call assert_false(F1d1 != F1d3)  " Equal dictionaries, even though d1 isnot d3
379
380  " isnot, option 1
381  call assert_true(F1 isnot# F2)  " Different functions
382  call assert_true(F1 isnot# F1d1)  " Partial /= non-partial
383  call assert_true(F1d1 isnot# F1d3)  " d1 isnot d3, even though d1 == d3
384  call assert_true(F1a1 isnot# F1d1a12)  " One has no dict
385  call assert_true(F1a1 isnot# F1a2)  " Different number of arguments
386  call assert_true(F1ad1 isnot# F1ad3)  " In arguments d1 isnot d3
387
388  " isnot, option 2
389  call assert_true(F1 isnot# F2)  " Different functions
390  call assert_true(F1 isnot# F1d1)  " Partial /= non-partial
391  call assert_true(d1.f1 isnot# d1.f1)  " handle_subscript creates new partial each time
392
393  " compare two null partials
394  " Nvim doesn't have null partials
395  " let N1 = test_null_partial()
396  let N1 = function('min')
397  let N2 = N1
398  call assert_true(N1 is N2)
399  call assert_true(N1 == N2)
400
401  " compare a partial and a null partial
402  call assert_false(N1 == F1)
403  call assert_false(F1 is N1)
404endfunc
405
406func Test_partial_method()
407  func Foo(x, y, z)
408    return x + y + z
409  endfunc
410  let d = {"Fn": function('Foo', [10, 20])}
411  call assert_fails('echo 30->d.Fn()', 'E1265: Cannot use a partial here')
412  delfunc Foo
413endfunc
414
415func Test_non_callable_type_as_method()
416  let d = {"Fn": 10}
417  call assert_fails('echo 30->d.Fn()', 'E1085: Not a callable type: d.Fn')
418endfunc
419
420" vim: shiftwidth=2 sts=2 expandtab