annotate src/testdir/test_lambda.vim @ 9735:8037eb704e93 v7.4.2143

commit https://github.com/vim/vim/commit/bc7ce675b2d1c9fb58c067eff3edd59abc30aba4 Author: Bram Moolenaar <Bram@vim.org> Date: Mon Aug 1 22:49:22 2016 +0200 patch 7.4.2143 Problem: A funccal is garbage collected while it can still be used. Solution: Set copyID in all referenced functions. Do not list lambda functions with ":function".
author Christian Brabandt <cb@256bit.org>
date Mon, 01 Aug 2016 23:00:07 +0200
parents 8436bb5134f5
children ade79e3e3b88
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9688
2ea935bdd1a1 commit https://github.com/vim/vim/commit/10ce39a0d52272a3dfff2feb8c631529f29e6740
Christian Brabandt <cb@256bit.org>
parents: 9686
diff changeset
1 " Test for lambda and closure
2ea935bdd1a1 commit https://github.com/vim/vim/commit/10ce39a0d52272a3dfff2feb8c631529f29e6740
Christian Brabandt <cb@256bit.org>
parents: 9686
diff changeset
2
9690
4a7a88404076 commit https://github.com/vim/vim/commit/9532fe7fbe1b14531931e83bd9f8054efdcf7509
Christian Brabandt <cb@256bit.org>
parents: 9688
diff changeset
3 function! Test_lambda_feature()
4a7a88404076 commit https://github.com/vim/vim/commit/9532fe7fbe1b14531931e83bd9f8054efdcf7509
Christian Brabandt <cb@256bit.org>
parents: 9688
diff changeset
4 call assert_equal(1, has('lambda'))
4a7a88404076 commit https://github.com/vim/vim/commit/9532fe7fbe1b14531931e83bd9f8054efdcf7509
Christian Brabandt <cb@256bit.org>
parents: 9688
diff changeset
5 endfunction
4a7a88404076 commit https://github.com/vim/vim/commit/9532fe7fbe1b14531931e83bd9f8054efdcf7509
Christian Brabandt <cb@256bit.org>
parents: 9688
diff changeset
6
9527
e8b3db8e2d30 commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
7 function! Test_lambda_with_filter()
e8b3db8e2d30 commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
8 let s:x = 2
e8b3db8e2d30 commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
9 call assert_equal([2, 3], filter([1, 2, 3], {i, v -> v >= s:x}))
e8b3db8e2d30 commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
10 endfunction
e8b3db8e2d30 commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
11
e8b3db8e2d30 commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
12 function! Test_lambda_with_map()
e8b3db8e2d30 commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
13 let s:x = 1
e8b3db8e2d30 commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
14 call assert_equal([2, 3, 4], map([1, 2, 3], {i, v -> v + s:x}))
e8b3db8e2d30 commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
15 endfunction
e8b3db8e2d30 commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
16
e8b3db8e2d30 commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
17 function! Test_lambda_with_sort()
e8b3db8e2d30 commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
18 call assert_equal([1, 2, 3, 4, 7], sort([3,7,2,1,4], {a, b -> a - b}))
e8b3db8e2d30 commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
19 endfunction
e8b3db8e2d30 commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
20
e8b3db8e2d30 commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
21 function! Test_lambda_with_timer()
e8b3db8e2d30 commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
22 if !has('timers')
e8b3db8e2d30 commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
23 return
e8b3db8e2d30 commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
24 endif
e8b3db8e2d30 commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
25
e8b3db8e2d30 commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
26 let s:n = 0
e8b3db8e2d30 commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
27 let s:timer_id = 0
e8b3db8e2d30 commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
28 function! s:Foo()
e8b3db8e2d30 commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
29 "let n = 0
9686
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
30 let s:timer_id = timer_start(50, {-> execute("let s:n += 1 | echo s:n", "")}, {"repeat": -1})
9527
e8b3db8e2d30 commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
31 endfunction
e8b3db8e2d30 commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
32
e8b3db8e2d30 commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
33 call s:Foo()
e8b3db8e2d30 commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
34 sleep 200ms
e8b3db8e2d30 commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
35 " do not collect lambda
e8b3db8e2d30 commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
36 call test_garbagecollect_now()
e8b3db8e2d30 commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
37 let m = s:n
e8b3db8e2d30 commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
38 sleep 200ms
e8b3db8e2d30 commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
39 call timer_stop(s:timer_id)
e8b3db8e2d30 commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
40 call assert_true(m > 1)
e8b3db8e2d30 commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
41 call assert_true(s:n > m + 1)
e8b3db8e2d30 commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
42 call assert_true(s:n < 9)
e8b3db8e2d30 commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
43 endfunction
e8b3db8e2d30 commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
44
e8b3db8e2d30 commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
45 function! Test_lambda_with_partial()
e8b3db8e2d30 commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
46 let l:Cb = function({... -> ['zero', a:1, a:2, a:3]}, ['one', 'two'])
e8b3db8e2d30 commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
47 call assert_equal(['zero', 'one', 'two', 'three'], l:Cb('three'))
e8b3db8e2d30 commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
48 endfunction
e8b3db8e2d30 commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
49
e8b3db8e2d30 commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
50 function Test_lambda_fails()
e8b3db8e2d30 commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
51 call assert_equal(3, {a, b -> a + b}(1, 2))
e8b3db8e2d30 commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
52 call assert_fails('echo {a, a -> a + a}(1, 2)', 'E15:')
e8b3db8e2d30 commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
53 call assert_fails('echo {a, b -> a + b)}(1, 2)', 'E15:')
e8b3db8e2d30 commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
54 endfunc
9597
3ca0fd9709b1 commit https://github.com/vim/vim/commit/4f0383bc3fe5af0229fb66b53fe94329af783eff
Christian Brabandt <cb@256bit.org>
parents: 9527
diff changeset
55
3ca0fd9709b1 commit https://github.com/vim/vim/commit/4f0383bc3fe5af0229fb66b53fe94329af783eff
Christian Brabandt <cb@256bit.org>
parents: 9527
diff changeset
56 func Test_not_lamda()
3ca0fd9709b1 commit https://github.com/vim/vim/commit/4f0383bc3fe5af0229fb66b53fe94329af783eff
Christian Brabandt <cb@256bit.org>
parents: 9527
diff changeset
57 let x = {'>' : 'foo'}
3ca0fd9709b1 commit https://github.com/vim/vim/commit/4f0383bc3fe5af0229fb66b53fe94329af783eff
Christian Brabandt <cb@256bit.org>
parents: 9527
diff changeset
58 call assert_equal('foo', x['>'])
3ca0fd9709b1 commit https://github.com/vim/vim/commit/4f0383bc3fe5af0229fb66b53fe94329af783eff
Christian Brabandt <cb@256bit.org>
parents: 9527
diff changeset
59 endfunc
9686
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
60
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
61 function! Test_lambda_capture_by_reference()
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
62 let v = 1
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
63 let l:F = {x -> x + v}
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
64 let v = 2
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
65 call assert_equal(12, l:F(10))
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
66 endfunction
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
67
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
68 function! Test_lambda_side_effect()
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
69 function! s:update_and_return(arr)
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
70 let a:arr[1] = 5
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
71 return a:arr
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
72 endfunction
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
73
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
74 function! s:foo(arr)
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
75 return {-> s:update_and_return(a:arr)}
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
76 endfunction
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
77
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
78 let arr = [3,2,1]
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
79 call assert_equal([3, 5, 1], s:foo(arr)())
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
80 endfunction
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
81
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
82 function! Test_lambda_refer_local_variable_from_other_scope()
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
83 function! s:foo(X)
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
84 return a:X() " refer l:x in s:bar()
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
85 endfunction
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
86
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
87 function! s:bar()
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
88 let x = 123
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
89 return s:foo({-> x})
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
90 endfunction
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
91
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
92 call assert_equal(123, s:bar())
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
93 endfunction
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
94
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
95 function! Test_lambda_do_not_share_local_variable()
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
96 function! s:define_funcs()
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
97 let l:One = {-> split(execute("let a = 'abc' | echo a"))[0]}
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
98 let l:Two = {-> exists("a") ? a : "no"}
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
99 return [l:One, l:Two]
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
100 endfunction
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
101
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
102 let l:F = s:define_funcs()
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
103
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
104 call assert_equal('no', l:F[1]())
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
105 call assert_equal('abc', l:F[0]())
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
106 call assert_equal('no', l:F[1]())
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
107 endfunction
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
108
9688
2ea935bdd1a1 commit https://github.com/vim/vim/commit/10ce39a0d52272a3dfff2feb8c631529f29e6740
Christian Brabandt <cb@256bit.org>
parents: 9686
diff changeset
109 function! Test_lambda_closure_counter()
9686
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
110 function! s:foo()
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
111 let x = 0
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
112 return {-> [execute("let x += 1"), x][-1]}
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
113 endfunction
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
114
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
115 let l:F = s:foo()
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
116 call test_garbagecollect_now()
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
117 call assert_equal(1, l:F())
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
118 call assert_equal(2, l:F())
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
119 call assert_equal(3, l:F())
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
120 call assert_equal(4, l:F())
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
121 endfunction
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
122
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
123 function! Test_lambda_with_a_var()
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
124 function! s:foo()
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
125 let x = 2
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
126 return {... -> a:000 + [x]}
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
127 endfunction
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
128 function! s:bar()
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
129 return s:foo()(1)
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
130 endfunction
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
131
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
132 call assert_equal([1, 2], s:bar())
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
133 endfunction
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
134
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
135 function! Test_lambda_call_lambda_from_lambda()
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
136 function! s:foo(x)
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
137 let l:F1 = {-> {-> a:x}}
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
138 return {-> l:F1()}
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
139 endfunction
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
140
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
141 let l:F = s:foo(1)
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
142 call assert_equal(1, l:F()())
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
143 endfunction
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
144
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
145 function! Test_lambda_delfunc()
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
146 function! s:gen()
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
147 let pl = l:
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
148 let l:Foo = {-> get(pl, "Foo", get(pl, "Bar", {-> 0}))}
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
149 let l:Bar = l:Foo
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
150 delfunction l:Foo
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
151 return l:Bar
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
152 endfunction
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
153
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
154 let l:F = s:gen()
9723
80ac9cf77c9b commit https://github.com/vim/vim/commit/437bafe4c8a83ed71ee006eda7f54b65a90f0d4c
Christian Brabandt <cb@256bit.org>
parents: 9721
diff changeset
155 call assert_fails(':call l:F()', 'E933:')
9686
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
156 endfunction
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
157
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
158 function! Test_lambda_scope()
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
159 function! s:NewCounter()
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
160 let c = 0
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
161 return {-> [execute('let c += 1'), c][-1]}
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
162 endfunction
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
163
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
164 function! s:NewCounter2()
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
165 return {-> [execute('let c += 100'), c][-1]}
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
166 endfunction
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
167
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
168 let l:C = s:NewCounter()
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
169 let l:D = s:NewCounter2()
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
170
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
171 call assert_equal(1, l:C())
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
172 call assert_fails(':call l:D()', 'E15:') " E121: then E15:
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
173 call assert_equal(2, l:C())
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
174 endfunction
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
175
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
176 function! Test_lambda_share_scope()
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
177 function! s:New()
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
178 let c = 0
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
179 let l:Inc0 = {-> [execute('let c += 1'), c][-1]}
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
180 let l:Dec0 = {-> [execute('let c -= 1'), c][-1]}
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
181 return [l:Inc0, l:Dec0]
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
182 endfunction
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
183
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
184 let [l:Inc, l:Dec] = s:New()
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
185
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
186 call assert_equal(1, l:Inc())
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
187 call assert_equal(2, l:Inc())
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
188 call assert_equal(1, l:Dec())
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
189 endfunction
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
190
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
191 function! Test_lambda_circular_reference()
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
192 function! s:Foo()
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
193 let d = {}
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
194 let d.f = {-> d}
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
195 return d.f
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
196 endfunction
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
197
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
198 call s:Foo()
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
199 call test_garbagecollect_now()
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
200 let i = 0 | while i < 10000 | call s:Foo() | let i+= 1 | endwhile
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
201 call test_garbagecollect_now()
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
202 endfunction
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
203
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
204 function! Test_lambda_combination()
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
205 call assert_equal(2, {x -> {x -> x}}(1)(2))
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
206 call assert_equal(10, {y -> {x -> x(y)(10)}({y -> y})}({z -> z}))
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
207 call assert_equal(5.0, {x -> {y -> x / y}}(10)(2.0))
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
208 call assert_equal(6, {x -> {y -> {z -> x + y + z}}}(1)(2)(3))
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
209
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
210 call assert_equal(6, {x -> {f -> f(x)}}(3)({x -> x * 2}))
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
211 call assert_equal(6, {f -> {x -> f(x)}}({x -> x * 2})(3))
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
212
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
213 " Z combinator
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
214 let Z = {f -> {x -> f({y -> x(x)(y)})}({x -> f({y -> x(x)(y)})})}
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
215 let Fact = {f -> {x -> x == 0 ? 1 : x * f(x - 1)}}
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
216 call assert_equal(120, Z(Fact)(5))
8c2553beff0f commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents: 9597
diff changeset
217 endfunction
9688
2ea935bdd1a1 commit https://github.com/vim/vim/commit/10ce39a0d52272a3dfff2feb8c631529f29e6740
Christian Brabandt <cb@256bit.org>
parents: 9686
diff changeset
218
2ea935bdd1a1 commit https://github.com/vim/vim/commit/10ce39a0d52272a3dfff2feb8c631529f29e6740
Christian Brabandt <cb@256bit.org>
parents: 9686
diff changeset
219 function! Test_closure_counter()
2ea935bdd1a1 commit https://github.com/vim/vim/commit/10ce39a0d52272a3dfff2feb8c631529f29e6740
Christian Brabandt <cb@256bit.org>
parents: 9686
diff changeset
220 function! s:foo()
2ea935bdd1a1 commit https://github.com/vim/vim/commit/10ce39a0d52272a3dfff2feb8c631529f29e6740
Christian Brabandt <cb@256bit.org>
parents: 9686
diff changeset
221 let x = 0
2ea935bdd1a1 commit https://github.com/vim/vim/commit/10ce39a0d52272a3dfff2feb8c631529f29e6740
Christian Brabandt <cb@256bit.org>
parents: 9686
diff changeset
222 function! s:bar() closure
2ea935bdd1a1 commit https://github.com/vim/vim/commit/10ce39a0d52272a3dfff2feb8c631529f29e6740
Christian Brabandt <cb@256bit.org>
parents: 9686
diff changeset
223 let x += 1
2ea935bdd1a1 commit https://github.com/vim/vim/commit/10ce39a0d52272a3dfff2feb8c631529f29e6740
Christian Brabandt <cb@256bit.org>
parents: 9686
diff changeset
224 return x
2ea935bdd1a1 commit https://github.com/vim/vim/commit/10ce39a0d52272a3dfff2feb8c631529f29e6740
Christian Brabandt <cb@256bit.org>
parents: 9686
diff changeset
225 endfunction
2ea935bdd1a1 commit https://github.com/vim/vim/commit/10ce39a0d52272a3dfff2feb8c631529f29e6740
Christian Brabandt <cb@256bit.org>
parents: 9686
diff changeset
226 return function('s:bar')
2ea935bdd1a1 commit https://github.com/vim/vim/commit/10ce39a0d52272a3dfff2feb8c631529f29e6740
Christian Brabandt <cb@256bit.org>
parents: 9686
diff changeset
227 endfunction
2ea935bdd1a1 commit https://github.com/vim/vim/commit/10ce39a0d52272a3dfff2feb8c631529f29e6740
Christian Brabandt <cb@256bit.org>
parents: 9686
diff changeset
228
2ea935bdd1a1 commit https://github.com/vim/vim/commit/10ce39a0d52272a3dfff2feb8c631529f29e6740
Christian Brabandt <cb@256bit.org>
parents: 9686
diff changeset
229 let l:F = s:foo()
2ea935bdd1a1 commit https://github.com/vim/vim/commit/10ce39a0d52272a3dfff2feb8c631529f29e6740
Christian Brabandt <cb@256bit.org>
parents: 9686
diff changeset
230 call test_garbagecollect_now()
2ea935bdd1a1 commit https://github.com/vim/vim/commit/10ce39a0d52272a3dfff2feb8c631529f29e6740
Christian Brabandt <cb@256bit.org>
parents: 9686
diff changeset
231 call assert_equal(1, l:F())
2ea935bdd1a1 commit https://github.com/vim/vim/commit/10ce39a0d52272a3dfff2feb8c631529f29e6740
Christian Brabandt <cb@256bit.org>
parents: 9686
diff changeset
232 call assert_equal(2, l:F())
2ea935bdd1a1 commit https://github.com/vim/vim/commit/10ce39a0d52272a3dfff2feb8c631529f29e6740
Christian Brabandt <cb@256bit.org>
parents: 9686
diff changeset
233 call assert_equal(3, l:F())
2ea935bdd1a1 commit https://github.com/vim/vim/commit/10ce39a0d52272a3dfff2feb8c631529f29e6740
Christian Brabandt <cb@256bit.org>
parents: 9686
diff changeset
234 call assert_equal(4, l:F())
2ea935bdd1a1 commit https://github.com/vim/vim/commit/10ce39a0d52272a3dfff2feb8c631529f29e6740
Christian Brabandt <cb@256bit.org>
parents: 9686
diff changeset
235 endfunction
2ea935bdd1a1 commit https://github.com/vim/vim/commit/10ce39a0d52272a3dfff2feb8c631529f29e6740
Christian Brabandt <cb@256bit.org>
parents: 9686
diff changeset
236
2ea935bdd1a1 commit https://github.com/vim/vim/commit/10ce39a0d52272a3dfff2feb8c631529f29e6740
Christian Brabandt <cb@256bit.org>
parents: 9686
diff changeset
237 function! Test_closure_unlet()
2ea935bdd1a1 commit https://github.com/vim/vim/commit/10ce39a0d52272a3dfff2feb8c631529f29e6740
Christian Brabandt <cb@256bit.org>
parents: 9686
diff changeset
238 function! s:foo()
2ea935bdd1a1 commit https://github.com/vim/vim/commit/10ce39a0d52272a3dfff2feb8c631529f29e6740
Christian Brabandt <cb@256bit.org>
parents: 9686
diff changeset
239 let x = 1
2ea935bdd1a1 commit https://github.com/vim/vim/commit/10ce39a0d52272a3dfff2feb8c631529f29e6740
Christian Brabandt <cb@256bit.org>
parents: 9686
diff changeset
240 function! s:bar() closure
2ea935bdd1a1 commit https://github.com/vim/vim/commit/10ce39a0d52272a3dfff2feb8c631529f29e6740
Christian Brabandt <cb@256bit.org>
parents: 9686
diff changeset
241 unlet x
2ea935bdd1a1 commit https://github.com/vim/vim/commit/10ce39a0d52272a3dfff2feb8c631529f29e6740
Christian Brabandt <cb@256bit.org>
parents: 9686
diff changeset
242 endfunction
2ea935bdd1a1 commit https://github.com/vim/vim/commit/10ce39a0d52272a3dfff2feb8c631529f29e6740
Christian Brabandt <cb@256bit.org>
parents: 9686
diff changeset
243 call s:bar()
2ea935bdd1a1 commit https://github.com/vim/vim/commit/10ce39a0d52272a3dfff2feb8c631529f29e6740
Christian Brabandt <cb@256bit.org>
parents: 9686
diff changeset
244 return l:
2ea935bdd1a1 commit https://github.com/vim/vim/commit/10ce39a0d52272a3dfff2feb8c631529f29e6740
Christian Brabandt <cb@256bit.org>
parents: 9686
diff changeset
245 endfunction
2ea935bdd1a1 commit https://github.com/vim/vim/commit/10ce39a0d52272a3dfff2feb8c631529f29e6740
Christian Brabandt <cb@256bit.org>
parents: 9686
diff changeset
246
2ea935bdd1a1 commit https://github.com/vim/vim/commit/10ce39a0d52272a3dfff2feb8c631529f29e6740
Christian Brabandt <cb@256bit.org>
parents: 9686
diff changeset
247 call assert_false(has_key(s:foo(), 'x'))
2ea935bdd1a1 commit https://github.com/vim/vim/commit/10ce39a0d52272a3dfff2feb8c631529f29e6740
Christian Brabandt <cb@256bit.org>
parents: 9686
diff changeset
248 call test_garbagecollect_now()
2ea935bdd1a1 commit https://github.com/vim/vim/commit/10ce39a0d52272a3dfff2feb8c631529f29e6740
Christian Brabandt <cb@256bit.org>
parents: 9686
diff changeset
249 endfunction
9721
79862f44c647 commit https://github.com/vim/vim/commit/580164481924ed8611eb79f0247a0eb1ca0b3b9a
Christian Brabandt <cb@256bit.org>
parents: 9690
diff changeset
250
79862f44c647 commit https://github.com/vim/vim/commit/580164481924ed8611eb79f0247a0eb1ca0b3b9a
Christian Brabandt <cb@256bit.org>
parents: 9690
diff changeset
251 function! LambdaFoo()
79862f44c647 commit https://github.com/vim/vim/commit/580164481924ed8611eb79f0247a0eb1ca0b3b9a
Christian Brabandt <cb@256bit.org>
parents: 9690
diff changeset
252 let x = 0
79862f44c647 commit https://github.com/vim/vim/commit/580164481924ed8611eb79f0247a0eb1ca0b3b9a
Christian Brabandt <cb@256bit.org>
parents: 9690
diff changeset
253 function! LambdaBar() closure
79862f44c647 commit https://github.com/vim/vim/commit/580164481924ed8611eb79f0247a0eb1ca0b3b9a
Christian Brabandt <cb@256bit.org>
parents: 9690
diff changeset
254 let x += 1
79862f44c647 commit https://github.com/vim/vim/commit/580164481924ed8611eb79f0247a0eb1ca0b3b9a
Christian Brabandt <cb@256bit.org>
parents: 9690
diff changeset
255 return x
79862f44c647 commit https://github.com/vim/vim/commit/580164481924ed8611eb79f0247a0eb1ca0b3b9a
Christian Brabandt <cb@256bit.org>
parents: 9690
diff changeset
256 endfunction
79862f44c647 commit https://github.com/vim/vim/commit/580164481924ed8611eb79f0247a0eb1ca0b3b9a
Christian Brabandt <cb@256bit.org>
parents: 9690
diff changeset
257 return function('LambdaBar')
79862f44c647 commit https://github.com/vim/vim/commit/580164481924ed8611eb79f0247a0eb1ca0b3b9a
Christian Brabandt <cb@256bit.org>
parents: 9690
diff changeset
258 endfunction
79862f44c647 commit https://github.com/vim/vim/commit/580164481924ed8611eb79f0247a0eb1ca0b3b9a
Christian Brabandt <cb@256bit.org>
parents: 9690
diff changeset
259
79862f44c647 commit https://github.com/vim/vim/commit/580164481924ed8611eb79f0247a0eb1ca0b3b9a
Christian Brabandt <cb@256bit.org>
parents: 9690
diff changeset
260 func Test_closure_refcount()
79862f44c647 commit https://github.com/vim/vim/commit/580164481924ed8611eb79f0247a0eb1ca0b3b9a
Christian Brabandt <cb@256bit.org>
parents: 9690
diff changeset
261 let g:Count = LambdaFoo()
79862f44c647 commit https://github.com/vim/vim/commit/580164481924ed8611eb79f0247a0eb1ca0b3b9a
Christian Brabandt <cb@256bit.org>
parents: 9690
diff changeset
262 call test_garbagecollect_now()
79862f44c647 commit https://github.com/vim/vim/commit/580164481924ed8611eb79f0247a0eb1ca0b3b9a
Christian Brabandt <cb@256bit.org>
parents: 9690
diff changeset
263 call assert_equal(1, g:Count())
79862f44c647 commit https://github.com/vim/vim/commit/580164481924ed8611eb79f0247a0eb1ca0b3b9a
Christian Brabandt <cb@256bit.org>
parents: 9690
diff changeset
264 let g:Count2 = LambdaFoo()
79862f44c647 commit https://github.com/vim/vim/commit/580164481924ed8611eb79f0247a0eb1ca0b3b9a
Christian Brabandt <cb@256bit.org>
parents: 9690
diff changeset
265 call test_garbagecollect_now()
79862f44c647 commit https://github.com/vim/vim/commit/580164481924ed8611eb79f0247a0eb1ca0b3b9a
Christian Brabandt <cb@256bit.org>
parents: 9690
diff changeset
266 call assert_equal(1, g:Count2())
79862f44c647 commit https://github.com/vim/vim/commit/580164481924ed8611eb79f0247a0eb1ca0b3b9a
Christian Brabandt <cb@256bit.org>
parents: 9690
diff changeset
267 call assert_equal(2, g:Count())
79862f44c647 commit https://github.com/vim/vim/commit/580164481924ed8611eb79f0247a0eb1ca0b3b9a
Christian Brabandt <cb@256bit.org>
parents: 9690
diff changeset
268 call assert_equal(3, g:Count2())
79862f44c647 commit https://github.com/vim/vim/commit/580164481924ed8611eb79f0247a0eb1ca0b3b9a
Christian Brabandt <cb@256bit.org>
parents: 9690
diff changeset
269
9727
8436bb5134f5 commit https://github.com/vim/vim/commit/0588d4f9d2741f35a271400a37fddbdd72d84219
Christian Brabandt <cb@256bit.org>
parents: 9723
diff changeset
270 delfunc LambdaFoo
8436bb5134f5 commit https://github.com/vim/vim/commit/0588d4f9d2741f35a271400a37fddbdd72d84219
Christian Brabandt <cb@256bit.org>
parents: 9723
diff changeset
271 delfunc LambdaBar
9721
79862f44c647 commit https://github.com/vim/vim/commit/580164481924ed8611eb79f0247a0eb1ca0b3b9a
Christian Brabandt <cb@256bit.org>
parents: 9690
diff changeset
272 endfunc
9735
8037eb704e93 commit https://github.com/vim/vim/commit/bc7ce675b2d1c9fb58c067eff3edd59abc30aba4
Christian Brabandt <cb@256bit.org>
parents: 9727
diff changeset
273
8037eb704e93 commit https://github.com/vim/vim/commit/bc7ce675b2d1c9fb58c067eff3edd59abc30aba4
Christian Brabandt <cb@256bit.org>
parents: 9727
diff changeset
274 func Test_named_function_closure()
8037eb704e93 commit https://github.com/vim/vim/commit/bc7ce675b2d1c9fb58c067eff3edd59abc30aba4
Christian Brabandt <cb@256bit.org>
parents: 9727
diff changeset
275 func! Afoo()
8037eb704e93 commit https://github.com/vim/vim/commit/bc7ce675b2d1c9fb58c067eff3edd59abc30aba4
Christian Brabandt <cb@256bit.org>
parents: 9727
diff changeset
276 let x = 14
8037eb704e93 commit https://github.com/vim/vim/commit/bc7ce675b2d1c9fb58c067eff3edd59abc30aba4
Christian Brabandt <cb@256bit.org>
parents: 9727
diff changeset
277 func! s:Abar() closure
8037eb704e93 commit https://github.com/vim/vim/commit/bc7ce675b2d1c9fb58c067eff3edd59abc30aba4
Christian Brabandt <cb@256bit.org>
parents: 9727
diff changeset
278 return x
8037eb704e93 commit https://github.com/vim/vim/commit/bc7ce675b2d1c9fb58c067eff3edd59abc30aba4
Christian Brabandt <cb@256bit.org>
parents: 9727
diff changeset
279 endfunc
8037eb704e93 commit https://github.com/vim/vim/commit/bc7ce675b2d1c9fb58c067eff3edd59abc30aba4
Christian Brabandt <cb@256bit.org>
parents: 9727
diff changeset
280 call assert_equal(14, s:Abar())
8037eb704e93 commit https://github.com/vim/vim/commit/bc7ce675b2d1c9fb58c067eff3edd59abc30aba4
Christian Brabandt <cb@256bit.org>
parents: 9727
diff changeset
281 endfunc
8037eb704e93 commit https://github.com/vim/vim/commit/bc7ce675b2d1c9fb58c067eff3edd59abc30aba4
Christian Brabandt <cb@256bit.org>
parents: 9727
diff changeset
282 call Afoo()
8037eb704e93 commit https://github.com/vim/vim/commit/bc7ce675b2d1c9fb58c067eff3edd59abc30aba4
Christian Brabandt <cb@256bit.org>
parents: 9727
diff changeset
283 call assert_equal(14, s:Abar())
8037eb704e93 commit https://github.com/vim/vim/commit/bc7ce675b2d1c9fb58c067eff3edd59abc30aba4
Christian Brabandt <cb@256bit.org>
parents: 9727
diff changeset
284 call test_garbagecollect_now()
8037eb704e93 commit https://github.com/vim/vim/commit/bc7ce675b2d1c9fb58c067eff3edd59abc30aba4
Christian Brabandt <cb@256bit.org>
parents: 9727
diff changeset
285 call assert_equal(14, s:Abar())
8037eb704e93 commit https://github.com/vim/vim/commit/bc7ce675b2d1c9fb58c067eff3edd59abc30aba4
Christian Brabandt <cb@256bit.org>
parents: 9727
diff changeset
286 endfunc