aboutsummaryrefslogtreecommitdiff
path: root/test/functional/lua/loop_spec.lua
blob: 8cc54e8c13224ca3d4cd215d04f80a74e8960b52 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
-- Test suite for testing interactions with API bindings
local helpers = require('test.functional.helpers')(after_each)
local funcs = helpers.funcs
local meths = helpers.meths
local clear = helpers.clear
local sleep = helpers.sleep
local eq = helpers.eq
local matches = helpers.matches

before_each(clear)

describe('vim.loop', function()

  it('version', function()
    assert(funcs.luaeval('vim.loop.version()')>=72961, "libuv version too old")
    matches("(%d+)%.(%d+)%.(%d+)", funcs.luaeval('vim.loop.version_string()'))
  end)

  it('timer', function()
    meths.execute_lua('vim.api.nvim_set_var("coroutine_cnt", 0)', {})

    local code=[[
      local loop = vim.loop

      local touch = 0
      local function wait(ms)
        local this = coroutine.running()
        assert(this)
        local timer = loop.new_timer()
        timer:start(ms, 0, function ()
          timer:close()
          touch = touch + 1
          coroutine.resume(this)
          touch = touch + 1
          assert(touch==3)
          vim.api.nvim_set_var("coroutine_cnt_1", touch)
        end)
        coroutine.yield()
        touch = touch + 1
        return touch
      end
      coroutine.wrap(function()
        local touched = wait(10)
        assert(touched==touch)
        vim.api.nvim_set_var("coroutine_cnt", touched)
      end)()
    ]]

    eq(0, meths.get_var('coroutine_cnt'))
    meths.execute_lua(code, {})
    sleep(20)
    eq(2, meths.get_var('coroutine_cnt'))
    eq(3, meths.get_var('coroutine_cnt_1'))
  end)
end)