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 n = 0 30 let s:timer_id = timer_start(50, {-> execute("let s:n += 1 | echo s:n", "")}, {"repeat": -1}) 31 endfunc 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) 43endfunc 44 45func 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')) 48endfunc 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)', 'E853:') 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 61func 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)) 66endfunc 67 68func Test_lambda_side_effect() 69 func! s:update_and_return(arr) 70 let a:arr[1] = 5 71 return a:arr 72 endfunc 73 74 func! s:foo(arr) 75 return {-> s:update_and_return(a:arr)} 76 endfunc 77 78 let arr = [3,2,1] 79 call assert_equal([3, 5, 1], s:foo(arr)()) 80endfunc 81 82func Test_lambda_refer_local_variable_from_other_scope() 83 func! s:foo(X) 84 return a:X() " refer l:x in s:bar() 85 endfunc 86 87 func! s:bar() 88 let x = 123 89 return s:foo({-> x}) 90 endfunc 91 92 call assert_equal(123, s:bar()) 93endfunc 94 95func Test_lambda_do_not_share_local_variable() 96 func! 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 endfunc 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]()) 107endfunc 108 109func Test_lambda_closure_counter() 110 func! s:foo() 111 let x = 0 112 return {-> [execute("let x += 1"), x][-1]} 113 endfunc 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()) 121endfunc 122 123func Test_lambda_with_a_var() 124 func! s:foo() 125 let x = 2 126 return {... -> a:000 + [x]} 127 endfunc 128 func! s:bar() 129 return s:foo()(1) 130 endfunc 131 132 call assert_equal([1, 2], s:bar()) 133endfunc 134 135func Test_lambda_call_lambda_from_lambda() 136 func! s:foo(x) 137 let l:F1 = {-> {-> a:x}} 138 return {-> l:F1()} 139 endfunc 140 141 let l:F = s:foo(1) 142 call assert_equal(1, l:F()()) 143endfunc 144 145func Test_lambda_delfunc() 146 func! 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 endfunc 153 154 let l:F = s:gen() 155 call assert_fails(':call l:F()', 'E933:') 156endfunc 157 158func Test_lambda_scope() 159 func! s:NewCounter() 160 let c = 0 161 return {-> [execute('let c += 1'), c][-1]} 162 endfunc 163 164 func! s:NewCounter2() 165 return {-> [execute('let c += 100'), c][-1]} 166 endfunc 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()', 'E121:') 173 call assert_equal(2, l:C()) 174endfunc 175 176func Test_lambda_share_scope() 177 func! 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 endfunc 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()) 189endfunc 190 191func Test_lambda_circular_reference() 192 func! s:Foo() 193 let d = {} 194 let d.f = {-> d} 195 return d.f 196 endfunc 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() 202endfunc 203 204func 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)) 217endfunc 218 219func Test_closure_counter() 220 func! s:foo() 221 let x = 0 222 func! s:bar() closure 223 let x += 1 224 return x 225 endfunc 226 return function('s:bar') 227 endfunc 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()) 235endfunc 236 237func Test_closure_unlet() 238 func! s:foo() 239 let x = 1 240 func! s:bar() closure 241 unlet x 242 endfunc 243 call s:bar() 244 return l: 245 endfunc 246 247 call assert_false(has_key(s:foo(), 'x')) 248 call test_garbagecollect_now() 249endfunc 250 251func LambdaFoo() 252 let x = 0 253 func! LambdaBar() closure 254 let x += 1 255 return x 256 endfunc 257 return function('LambdaBar') 258endfunc 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 288func Test_lambda_with_index() 289 let List = {x -> [x]} 290 let Extract = {-> function(List, ['foobar'])()[0]} 291 call assert_equal('foobar', Extract()) 292endfunc 293