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