cursormoved_spec.lua (2436B)
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 eval = n.eval 7 local api = n.api 8 local source = n.source 9 local command = n.command 10 local feed = n.feed 11 12 describe('CursorMoved', function() 13 before_each(clear) 14 15 it('is triggered after BufEnter when changing or splitting windows #11878 #12031', function() 16 source([[ 17 call setline(1, 'foo') 18 let g:log = [] 19 autocmd BufEnter * let g:log += ['BufEnter' .. expand("<abuf>")] 20 autocmd CursorMoved * let g:log += ['CursorMoved' .. expand("<abuf>")] 21 ]]) 22 eq({}, eval('g:log')) 23 command('new') 24 eq({ 'BufEnter2', 'CursorMoved2' }, eval('g:log')) 25 command('wincmd w') 26 eq({ 'BufEnter2', 'CursorMoved2', 'BufEnter1', 'CursorMoved1' }, eval('g:log')) 27 end) 28 29 it('is not triggered by temporarily switching window', function() 30 source([[ 31 let g:cursormoved = 0 32 vnew 33 autocmd CursorMoved * let g:cursormoved += 1 34 ]]) 35 command('wincmd w | wincmd p') 36 eq(0, eval('g:cursormoved')) 37 end) 38 39 it("is not triggered by functions that don't change the window", function() 40 source([[ 41 let g:cursormoved = 0 42 let g:buf = bufnr('%') 43 vsplit foo 44 autocmd CursorMoved * let g:cursormoved += 1 45 ]]) 46 api.nvim_buf_set_lines(eval('g:buf'), 0, -1, true, { 'aaa' }) 47 eq(0, eval('g:cursormoved')) 48 eq({ 'aaa' }, api.nvim_buf_get_lines(eval('g:buf'), 0, -1, true)) 49 eq(0, eval('g:cursormoved')) 50 end) 51 52 it('is not triggered by cursor movement prior to first CursorMoved instantiation', function() 53 eq({}, api.nvim_get_autocmds({ event = 'CursorMoved' })) 54 feed('ifoobar<Esc>') 55 source([[ 56 let g:cursormoved = 0 57 autocmd CursorMoved * let g:cursormoved += 1 58 ]]) 59 eq(0, eval('g:cursormoved')) 60 feed('<Ignore>') 61 eq(0, eval('g:cursormoved')) 62 feed('0') 63 eq(1, eval('g:cursormoved')) 64 end) 65 end) 66 67 describe('CursorMovedI', function() 68 before_each(clear) 69 70 it('is not triggered by cursor movement prior to first CursorMovedI instantiation', function() 71 eq({}, api.nvim_get_autocmds({ event = 'CursorMovedI' })) 72 feed('ifoobar') 73 source([[ 74 let g:cursormovedi = 0 75 autocmd CursorMovedI * let g:cursormovedi += 1 76 ]]) 77 eq(0, eval('g:cursormovedi')) 78 feed('<Ignore>') 79 eq(0, eval('g:cursormovedi')) 80 feed('<Home>') 81 eq(1, eval('g:cursormovedi')) 82 end) 83 end)