Files
neovim/test/functional/example_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

69 lines
1.9 KiB
Lua

-- To run this test:
-- make functionaltest TEST_FILE=test/functional/example_spec.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 eq = t.eq
local feed = n.feed
describe('example', function()
local screen
before_each(function()
clear()
screen = Screen.new(20, 5)
end)
it('screen test', function()
-- Do some stuff.
feed('iline1<cr>line2<esc>')
-- For debugging only: prints the current screen.
-- screen:snapshot_util()
-- Assert the expected state.
screen:expect([[
line1 |
line^2 |
{1:~ }|*2
|
]])
end)
it('override UI event-handler', function()
-- Example: override the "tabline_update" UI event handler.
--
-- screen.lua defines default handlers for UI events, but tests
-- may sometimes want to override a handler.
-- The UI must declare that it wants to handle the UI events.
-- For this example, we enable `ext_tabline`:
screen:detach()
screen = Screen.new(25, 5, { rgb = true, ext_tabline = true })
-- From ":help ui" we find that `tabline_update` receives `curtab` and
-- `tabs` objects. So we declare the UI handler like this:
local event_tabs, event_curtab
function screen:_handle_tabline_update(curtab, tabs)
event_curtab, event_tabs = curtab, tabs
end
-- Create a tabpage...
command('tabedit foo')
-- Use screen:expect{condition=…} to check the result.
screen:expect {
condition = function()
eq(2, event_curtab)
eq({
{ tab = 1, name = '[No Name]' },
{ tab = 2, name = 'foo' },
}, event_tabs)
end,
}
end)
end)