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