comparison src/testdir/test_lambda.vim @ 15406:63b02fcf1361 v8.1.0711

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