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