view src/testdir/test_lambda.vim @ 9632:3e0ab13ddd12 v7.4.2093

commit https://github.com/vim/vim/commit/a58c58b7e1404bb91d59d14539b41390284e7026 Author: Bram Moolenaar <Bram@vim.org> Date: Sat Jul 23 14:01:15 2016 +0200 patch 7.4.2093 Problem: Netbeans test fails once in a while. Leaving log file behind. Solution: Add it to the list of flaky tests. Disable logfile.
author Christian Brabandt <cb@256bit.org>
date Sat, 23 Jul 2016 14:15:05 +0200
parents 3ca0fd9709b1
children 8c2553beff0f
line wrap: on
line source

function! Test_lambda_with_filter()
  let s:x = 2
  call assert_equal([2, 3], filter([1, 2, 3], {i, v -> v >= s:x}))
endfunction

function! Test_lambda_with_map()
  let s:x = 1
  call assert_equal([2, 3, 4], map([1, 2, 3], {i, v -> v + s:x}))
endfunction

function! Test_lambda_with_sort()
  call assert_equal([1, 2, 3, 4, 7], sort([3,7,2,1,4], {a, b -> a - b}))
endfunction

function! Test_lambda_with_timer()
  if !has('timers')
    return
  endif

  let s:n = 0
  let s:timer_id = 0
  function! s:Foo()
    "let n = 0
    let s:timer_id = timer_start(50, {-> execute("let s:n += 1 | echo s:n")}, {"repeat": -1})
  endfunction

  call s:Foo()
  sleep 200ms
  " do not collect lambda
  call test_garbagecollect_now()
  let m = s:n
  sleep 200ms
  call timer_stop(s:timer_id)
  call assert_true(m > 1)
  call assert_true(s:n > m + 1)
  call assert_true(s:n < 9)
endfunction

function! Test_lambda_with_partial()
  let l:Cb = function({... -> ['zero', a:1, a:2, a:3]}, ['one', 'two'])
  call assert_equal(['zero', 'one', 'two', 'three'], l:Cb('three'))
endfunction

function Test_lambda_fails()
  call assert_equal(3, {a, b -> a + b}(1, 2))
  call assert_fails('echo {a, a -> a + a}(1, 2)', 'E15:')
  call assert_fails('echo {a, b -> a + b)}(1, 2)', 'E15:')
endfunc

func Test_not_lamda()
  let x = {'>' : 'foo'}
  call assert_equal('foo', x['>'])
endfunc