Bram Moolenaar | 069c1e7 | 2016-07-15 21:25:08 +0200 | [diff] [blame] | 1 | function! Test_lambda_with_filter() |
| 2 | let s:x = 2 |
| 3 | call assert_equal([2, 3], filter([1, 2, 3], {i, v -> v >= s:x})) |
| 4 | endfunction |
| 5 | |
| 6 | function! Test_lambda_with_map() |
| 7 | let s:x = 1 |
| 8 | call assert_equal([2, 3, 4], map([1, 2, 3], {i, v -> v + s:x})) |
| 9 | endfunction |
| 10 | |
| 11 | function! Test_lambda_with_sort() |
| 12 | call assert_equal([1, 2, 3, 4, 7], sort([3,7,2,1,4], {a, b -> a - b})) |
| 13 | endfunction |
| 14 | |
| 15 | function! Test_lambda_with_timer() |
| 16 | if !has('timers') |
| 17 | return |
| 18 | endif |
| 19 | |
| 20 | let s:n = 0 |
| 21 | let s:timer_id = 0 |
| 22 | function! s:Foo() |
| 23 | "let n = 0 |
| 24 | let s:timer_id = timer_start(50, {-> execute("let s:n += 1 | echo s:n")}, {"repeat": -1}) |
| 25 | endfunction |
| 26 | |
| 27 | call s:Foo() |
| 28 | sleep 200ms |
| 29 | " do not collect lambda |
| 30 | call test_garbagecollect_now() |
| 31 | let m = s:n |
| 32 | sleep 200ms |
| 33 | call timer_stop(s:timer_id) |
| 34 | call assert_true(m > 1) |
| 35 | call assert_true(s:n > m + 1) |
| 36 | call assert_true(s:n < 9) |
| 37 | endfunction |
| 38 | |
| 39 | function! Test_lambda_with_partial() |
| 40 | let l:Cb = function({... -> ['zero', a:1, a:2, a:3]}, ['one', 'two']) |
| 41 | call assert_equal(['zero', 'one', 'two', 'three'], l:Cb('three')) |
| 42 | endfunction |
| 43 | |
| 44 | function Test_lambda_fails() |
| 45 | call assert_equal(3, {a, b -> a + b}(1, 2)) |
| 46 | call assert_fails('echo {a, a -> a + a}(1, 2)', 'E15:') |
| 47 | call assert_fails('echo {a, b -> a + b)}(1, 2)', 'E15:') |
| 48 | endfunc |
Bram Moolenaar | 4f0383b | 2016-07-19 22:43:11 +0200 | [diff] [blame] | 49 | |
| 50 | func Test_not_lamda() |
| 51 | let x = {'>' : 'foo'} |
| 52 | call assert_equal('foo', x['>']) |
| 53 | endfunc |