xref: /vim-8.2.3635/src/testdir/test_lambda.vim (revision bc93cebb)
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  if has('float')
220    call assert_equal(5.0, {x -> {y -> x / y}}(10)(2.0))
221  endif
222  call assert_equal(6, {x -> {y -> {z -> x + y + z}}}(1)(2)(3))
223
224  call assert_equal(6, {x -> {f -> f(x)}}(3)({x -> x * 2}))
225  call assert_equal(6, {f -> {x -> f(x)}}({x -> x * 2})(3))
226
227  " Z combinator
228  let Z = {f -> {x -> f({y -> x(x)(y)})}({x -> f({y -> x(x)(y)})})}
229  let Fact = {f -> {x -> x == 0 ? 1 : x * f(x - 1)}}
230  call assert_equal(120, Z(Fact)(5))
231endfunc
232
233func Test_closure_counter()
234  func! s:foo()
235    let x = 0
236    func! s:bar() closure
237      let x += 1
238      return x
239    endfunc
240    return function('s:bar')
241  endfunc
242
243  let l:F = s:foo()
244  call test_garbagecollect_now()
245  call assert_equal(1, l:F())
246  call assert_equal(2, l:F())
247  call assert_equal(3, l:F())
248  call assert_equal(4, l:F())
249endfunc
250
251func Test_closure_unlet()
252  func! s:foo()
253    let x = 1
254    func! s:bar() closure
255      unlet x
256    endfunc
257    call s:bar()
258    return l:
259  endfunc
260
261  call assert_false(has_key(s:foo(), 'x'))
262  call test_garbagecollect_now()
263endfunc
264
265func LambdaFoo()
266  let x = 0
267  func! LambdaBar() closure
268    let x += 1
269    return x
270  endfunc
271  return function('LambdaBar')
272endfunc
273
274func Test_closure_refcount()
275  let g:Count = LambdaFoo()
276  call test_garbagecollect_now()
277  call assert_equal(1, g:Count())
278  let g:Count2 = LambdaFoo()
279  call test_garbagecollect_now()
280  call assert_equal(1, g:Count2())
281  call assert_equal(2, g:Count())
282  call assert_equal(3, g:Count2())
283
284  delfunc LambdaFoo
285  delfunc LambdaBar
286endfunc
287
288func Test_named_function_closure()
289  func! Afoo()
290    let x = 14
291    func! s:Abar() closure
292      return x
293    endfunc
294    call assert_equal(14, s:Abar())
295  endfunc
296  call Afoo()
297  call assert_equal(14, s:Abar())
298  call test_garbagecollect_now()
299  call assert_equal(14, s:Abar())
300endfunc
301
302func Test_lambda_with_index()
303  let List = {x -> [x]}
304  let Extract = {-> function(List, ['foobar'])()[0]}
305  call assert_equal('foobar', Extract())
306endfunc
307
308func Test_lambda_error()
309  " This was causing a crash
310  call assert_fails('ec{@{->{d->()()', 'E15')
311endfunc
312