Files
neovim/test/functional/autocmd/tabmove_spec.lua
Justin M. Keyes 6d8c8b18d5 test(harness): migrate away from magic globals
Problem:
The magic globals `it`, `describe`, etc., are more trouble than they are
worth.

- Hooking into `after_each` requires `getfenv()` hacks.
- They confuse luals/emmylua, because the top-level `.luarc.json` isn't
  merged with `test/.luarc.json` (apparently a luals limitation?)
- They totally defeat discoverability because the user just has to
  "know" about the various magic symbols.

So they harm DX, which means they serve no purpose at all.

Solution:
- Expose the test API from `testutil`, so tests can call `t.it()`,
  `t.describe()`, etc., in the conventional way.
- Drop `getfenv()` hacks.
- Drop the `setfenv()` injection in `load_chunk`.
- Drop `test/_meta.lua`.
2026-07-21 13:22:39 +02:00

111 lines
3.0 KiB
Lua

local t = require('test.testutil')
local n = require('test.functional.testnvim')()
local Screen = require('test.functional.ui.screen')
local describe, it, before_each = t.describe, t.it, t.before_each
local clear = n.clear
local command = n.command
local exec_lua = n.exec_lua
local poke_eventloop = n.poke_eventloop
local api = n.api
local eval = n.eval
local eq = t.eq
describe('TabMoved', function()
before_each(function()
clear()
end)
it('sets <amatch>, event-data', function()
-- start on tabpage 1, create 2 more so tabpage 3 is "current tab"
command('tabnew')
command('tabnew')
exec_lua([[
_G.tm_events = {}
vim.api.nvim_create_autocmd('TabMoved', {
callback = function(ev)
table.insert(_G.tm_events, ev)
end,
})
]])
command('tabmove 0')
poke_eventloop()
local events = exec_lua('return _G.tm_events')
eq('3', events[1].match)
eq({ tabnr_new = 1, tabnr_old = 3 }, events[1].data)
command('tabnext 2')
command('tabmove $')
poke_eventloop()
events = exec_lua('return _G.tm_events')
eq('2', events[2].match)
eq({ tabnr_new = 3, tabnr_old = 2 }, events[2].data)
command('tabmove -1')
poke_eventloop()
events = exec_lua('return _G.tm_events')
eq('3', events[3].match)
eq({ tabnr_new = 2, tabnr_old = 3 }, events[3].data)
end)
it('does not trigger when a tab is moved to same position', function()
command('tabnew')
command('tabnew')
command('let g:test = 0')
command('au! TabMoved * let g:test += 1')
command('tabmove $') -- already at last position, no-op
poke_eventloop()
eq(0, eval('g:test'))
end)
it('is not triggered when creating or closing tabs', function()
command('let g:test = 0')
command('au! TabMoved * let g:test += 1')
command('file Xtestfile1')
command('0tabedit Xtestfile2')
command('tabclose')
poke_eventloop()
eq(0, eval('g:test'))
end)
it('handles mouse interactions on the tabline', function()
local function setup()
Screen.new(25, 5)
command('set mouse=a')
command('tabnew')
command('tabnew')
exec_lua([[
_G.tm_events = {}
vim.api.nvim_create_autocmd('TabMoved', {
callback = function(ev)
table.insert(_G.tm_events, ev)
end,
})
]])
end
-- clicking tab 2 shouldn't trigger the event.
setup()
api.nvim_input_mouse('left', 'press', '', 0, 0, 5)
api.nvim_input_mouse('left', 'release', '', 0, 0, 5)
poke_eventloop()
eq({}, exec_lua('return _G.tm_events'))
clear()
-- dragging tab 1 to the right should trigger the event.
setup()
api.nvim_input_mouse('left', 'press', '', 0, 0, 4)
poke_eventloop()
eq(0, #exec_lua('return _G.tm_events'))
api.nvim_input_mouse('left', 'drag', '', 0, 0, 14)
poke_eventloop()
local events = exec_lua('return _G.tm_events')
eq(1, #events)
eq({ tabnr_new = 2, tabnr_old = 1 }, events[1].data)
end)
end)