xref: /vim-8.2.3635/src/testdir/test_lambda.vim (revision 73f4439c)
1" Test for lambda and closure
2
3function! Test_lambda_feature()
4  call assert_equal(1, has('lambda'))
5endfunction
6
7function! Test_lambda_with_filter()
8  let s:x = 2
9  call assert_equal([2, 3], filter([1, 2, 3], {i, v -> v >= s:x}))
10endfunction
11
12function! Test_lambda_with_map()
13  let s:x = 1
14  call assert_equal([2, 3, 4], map([1, 2, 3], {i, v -> v + s:x}))
15endfunction
16
17function! Test_lambda_with_sort()
18  call assert_equal([1, 2, 3, 4, 7], sort([3,7,2,1,4], {a, b -> a - b}))
19endfunction
20
21function! Test_lambda_with_timer()
22  if !has('timers')
23    return
24  endif
25
26  let s:n = 0
27  let s:timer_id = 0
28  function! s:Foo()
29    "let n = 0
30    let s:timer_id = timer_start(50, {-> execute("let s:n += 1 | echo s:n", "")}, {"repeat": -1})
31  endfunction
32
33  call s:Foo()
34  sleep 200ms
35  " do not collect lambda
36  call test_garbagecollect_now()
37  let m = s:n
38  sleep 200ms
39  call timer_stop(s:timer_id)
40  call assert_true(m > 1)
41  call assert_true(s:n > m + 1)
42  call assert_true(s:n < 9)
43endfunction
44
45function! Test_lambda_with_partial()
46  let l:Cb = function({... -> ['zero', a:1, a:2, a:3]}, ['one', 'two'])
47  call assert_equal(['zero', 'one', 'two', 'three'], l:Cb('three'))
48endfunction
49
50function Test_lambda_fails()
51  call assert_equal(3, {a, b -> a + b}(1, 2))
52  call assert_fails('echo {a, a -> a + a}(1, 2)', 'E15:')
53  call assert_fails('echo {a, b -> a + b)}(1, 2)', 'E15:')
54endfunc
55
56func Test_not_lamda()
57  let x = {'>' : 'foo'}
58  call assert_equal('foo', x['>'])
59endfunc
60
61function! Test_lambda_capture_by_reference()
62  let v = 1
63  let l:F = {x -> x + v}
64  let v = 2
65  call assert_equal(12, l:F(10))
66endfunction
67
68function! Test_lambda_side_effect()
69  function! s:update_and_return(arr)
70    let a:arr[1] = 5
71    return a:arr
72  endfunction
73
74  function! s:foo(arr)
75    return {-> s:update_and_return(a:arr)}
76  endfunction
77
78  let arr = [3,2,1]
79  call assert_equal([3, 5, 1], s:foo(arr)())
80endfunction
81
82function! Test_lambda_refer_local_variable_from_other_scope()
83  function! s:foo(X)
84    return a:X() " refer l:x in s:bar()
85  endfunction
86
87  function! s:bar()
88    let x = 123
89    return s:foo({-> x})
90  endfunction
91
92  call assert_equal(123, s:bar())
93endfunction
94
95function! Test_lambda_do_not_share_local_variable()
96  function! s:define_funcs()
97    let l:One = {-> split(execute("let a = 'abc' | echo a"))[0]}
98    let l:Two = {-> exists("a") ? a : "no"}
99    return [l:One, l:Two]
100  endfunction
101
102  let l:F = s:define_funcs()
103
104  call assert_equal('no', l:F[1]())
105  call assert_equal('abc', l:F[0]())
106  call assert_equal('no', l:F[1]())
107endfunction
108
109function! Test_lambda_closure_counter()
110  function! s:foo()
111    let x = 0
112    return {-> [execute("let x += 1"), x][-1]}
113  endfunction
114
115  let l:F = s:foo()
116  call test_garbagecollect_now()
117  call assert_equal(1, l:F())
118  call assert_equal(2, l:F())
119  call assert_equal(3, l:F())
120  call assert_equal(4, l:F())
121endfunction
122
123function! Test_lambda_with_a_var()
124  function! s:foo()
125    let x = 2
126    return {... -> a:000 + [x]}
127  endfunction
128  function! s:bar()
129    return s:foo()(1)
130  endfunction
131
132  call assert_equal([1, 2], s:bar())
133endfunction
134
135function! Test_lambda_call_lambda_from_lambda()
136  function! s:foo(x)
137    let l:F1 = {-> {-> a:x}}
138    return {-> l:F1()}
139  endfunction
140
141  let l:F = s:foo(1)
142  call assert_equal(1, l:F()())
143endfunction
144
145function! Test_lambda_delfunc()
146  function! s:gen()
147    let pl = l:
148    let l:Foo = {-> get(pl, "Foo", get(pl, "Bar", {-> 0}))}
149    let l:Bar = l:Foo
150    delfunction l:Foo
151    return l:Bar
152  endfunction
153
154  let l:F = s:gen()
155  call assert_fails(':call l:F()', 'E933:')
156endfunction
157
158function! Test_lambda_scope()
159  function! s:NewCounter()
160    let c = 0
161    return {-> [execute('let c += 1'), c][-1]}
162  endfunction
163
164  function! s:NewCounter2()
165    return {-> [execute('let c += 100'), c][-1]}
166  endfunction
167
168  let l:C = s:NewCounter()
169  let l:D = s:NewCounter2()
170
171  call assert_equal(1, l:C())
172  call assert_fails(':call l:D()', 'E15:') " E121: then E15:
173  call assert_equal(2, l:C())
174endfunction
175
176function! Test_lambda_share_scope()
177  function! s:New()
178    let c = 0
179    let l:Inc0 = {-> [execute('let c += 1'), c][-1]}
180    let l:Dec0 = {-> [execute('let c -= 1'), c][-1]}
181    return [l:Inc0, l:Dec0]
182  endfunction
183
184  let [l:Inc, l:Dec] = s:New()
185
186  call assert_equal(1, l:Inc())
187  call assert_equal(2, l:Inc())
188  call assert_equal(1, l:Dec())
189endfunction
190
191function! Test_lambda_circular_reference()
192  function! s:Foo()
193    let d = {}
194    let d.f = {-> d}
195    return d.f
196  endfunction
197
198  call s:Foo()
199  call test_garbagecollect_now()
200  let i = 0 | while i < 10000 | call s:Foo() | let i+= 1 | endwhile
201  call test_garbagecollect_now()
202endfunction
203
204function! Test_lambda_combination()
205  call assert_equal(2, {x -> {x -> x}}(1)(2))
206  call assert_equal(10, {y -> {x -> x(y)(10)}({y -> y})}({z -> z}))
207  call assert_equal(5.0, {x -> {y -> x / y}}(10)(2.0))
208  call assert_equal(6, {x -> {y -> {z -> x + y + z}}}(1)(2)(3))
209
210  call assert_equal(6, {x -> {f -> f(x)}}(3)({x -> x * 2}))
211  call assert_equal(6, {f -> {x -> f(x)}}({x -> x * 2})(3))
212
213  " Z combinator
214  let Z = {f -> {x -> f({y -> x(x)(y)})}({x -> f({y -> x(x)(y)})})}
215  let Fact = {f -> {x -> x == 0 ? 1 : x * f(x - 1)}}
216  call assert_equal(120, Z(Fact)(5))
217endfunction
218
219function! Test_closure_counter()
220  function! s:foo()
221    let x = 0
222    function! s:bar() closure
223      let x += 1
224      return x
225    endfunction
226    return function('s:bar')
227  endfunction
228
229  let l:F = s:foo()
230  call test_garbagecollect_now()
231  call assert_equal(1, l:F())
232  call assert_equal(2, l:F())
233  call assert_equal(3, l:F())
234  call assert_equal(4, l:F())
235endfunction
236
237function! Test_closure_unlet()
238  function! s:foo()
239    let x = 1
240    function! s:bar() closure
241      unlet x
242    endfunction
243    call s:bar()
244    return l:
245  endfunction
246
247  call assert_false(has_key(s:foo(), 'x'))
248  call test_garbagecollect_now()
249endfunction
250
251function! LambdaFoo()
252  let x = 0
253  function! LambdaBar() closure
254    let x += 1
255    return x
256  endfunction
257  return function('LambdaBar')
258endfunction
259
260func Test_closure_refcount()
261  let g:Count = LambdaFoo()
262  call test_garbagecollect_now()
263  call assert_equal(1, g:Count())
264  let g:Count2 = LambdaFoo()
265  call test_garbagecollect_now()
266  call assert_equal(1, g:Count2())
267  call assert_equal(2, g:Count())
268  call assert_equal(3, g:Count2())
269
270  delfunc LambdaFoo
271  delfunc LambdaBar
272endfunc
273
274func Test_named_function_closure()
275  func! Afoo()
276    let x = 14
277    func! s:Abar() closure
278      return x
279    endfunc
280    call assert_equal(14, s:Abar())
281  endfunc
282  call Afoo()
283  call assert_equal(14, s:Abar())
284  call test_garbagecollect_now()
285  call assert_equal(14, s:Abar())
286endfunc
287