robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
2.8 KiB104 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3
4local describe, it, before_each = t.describe, t.it, t.before_each
5local clear = n.clear
6local eq = t.eq
7local feed = n.feed
8local retry = t.retry
9local exec = n.source
10local sleep = vim.uv.sleep
11local api = n.api
12
13before_each(clear)
14
15describe('CursorHold', function()
16  before_each(function()
17    exec([[
18      let g:cursorhold = 0
19      augroup test
20        au CursorHold * let g:cursorhold += 1
21      augroup END
22    ]])
23  end)
24
25  it('is triggered correctly #12587', function()
26    local function test_cursorhold(fn, early)
27      local ut = 2
28      -- if testing with small 'updatetime' fails, double its value and test again
29      retry(10, nil, function()
30        ut = ut * 2
31        api.nvim_set_option_value('updatetime', ut, {})
32        feed('0') -- reset did_cursorhold
33        api.nvim_set_var('cursorhold', 0)
34        sleep(ut / 4)
35        fn()
36        eq(0, api.nvim_get_var('cursorhold'))
37        sleep(ut / 2)
38        fn()
39        eq(0, api.nvim_get_var('cursorhold'))
40        sleep(ut / 2)
41        eq(early, api.nvim_get_var('cursorhold'))
42        sleep(ut / 4 * 3)
43        eq(1, api.nvim_get_var('cursorhold'))
44      end)
45    end
46
47    local ignore_key = api.nvim_replace_termcodes('<Ignore>', true, true, true)
48    test_cursorhold(function() end, 1)
49    test_cursorhold(function()
50      feed('')
51    end, 1)
52    test_cursorhold(function()
53      api.nvim_feedkeys('', 'n', true)
54    end, 1)
55    test_cursorhold(function()
56      feed('<Ignore>')
57    end, 0)
58    test_cursorhold(function()
59      api.nvim_feedkeys(ignore_key, 'n', true)
60    end, 0)
61  end)
62
63  it("reducing 'updatetime' while waiting for CursorHold #20241", function()
64    api.nvim_set_option_value('updatetime', 10000, {})
65    feed('0') -- reset did_cursorhold
66    api.nvim_set_var('cursorhold', 0)
67    sleep(50)
68    eq(0, api.nvim_get_var('cursorhold'))
69    api.nvim_set_option_value('updatetime', 20, {})
70    sleep(10)
71    eq(1, api.nvim_get_var('cursorhold'))
72  end)
73
74  it('is not triggered after only K_EVENT on startup', function()
75    api.nvim_set_option_value('updatetime', 20, {})
76    sleep(50)
77    eq(0, api.nvim_get_var('cursorhold'))
78    feed('0')
79    -- Poll: CursorHold may take longer than one 'updatetime' to fire.
80    retry(nil, 1000, function()
81      eq(1, api.nvim_get_var('cursorhold'))
82    end)
83  end)
84end)
85
86describe('CursorHoldI', function()
87  -- NOTE: since this test uses RPC it is not necessary to trigger the initial
88  --       issue (#3757) via timer's or RPC callbacks in the first place.
89  it('is triggered after input', function()
90    exec([[
91      set updatetime=1
92
93      let g:cursorhold = 0
94      augroup test
95        au CursorHoldI * let g:cursorhold += 1
96      augroup END
97    ]])
98    feed('ifoo')
99    retry(5, nil, function()
100      sleep(1)
101      eq(1, api.nvim_get_var('cursorhold'))
102    end)
103  end)
104end)