Open HiPhish opened 7 months ago
it
Here is a hack that works: we explicitly inject it
into nio.tests.it
:
nio.tests.it = function(it, name, async_func)
it(name, with_timeout(async_func, tonumber(vim.env.PLENARY_TEST_TIMEOUT)))
end
Now we can write the test:
nio.tests.it(it, 'Discovers nothing in an empty file', function()
vim.fn.writefile({''}, tempfile, 's')
local result = adapter.discover_positions(tempfile)
end)
It is kind of pointless to have separate it
, before_each
and after_each
functions inside nio.tests
if we have to pass the globals explicitly anyway. So we can add a metatable to nio.tests
that lets us call the module and pass the global busted function:
local mt = {
__call = function(_table, hook, name, async_func)
hook(name, with_timeout(async_func, tonumber(vim.env.PLENARY_TEST_TIMEOUT)))
end
}
setmetatable(nio.tests, mt)
With this the module remains backwards compatible and the test is less noisy:
nio.tests(it, 'Discovers nothing in an empty file', function()
vim.fn.writefile({''}, tempfile, 's')
local result = adapter.discover_positions(tempfile)
end)
What do you think? Is this an acceptable solution?
it
and friendsA variant of the previous solution which preserves the existing API without making the module callable. Here we use the __index
metamethod to automatically resolve it
, before_each
and after_each
when they are requested.
local mt = {
__index = function(_table, key)
local env = getfenv(2)
local hook = env[key]
return function(name, async_func)
hook(name, with_timeout(async_func, tonumber(vim.env.PLENARY_TEST_TIMEOUT)))
end
end
}
setmetatable(nio.tests, mt)
Now the test can be written as usual:
nio.tests.it('Discovers nothing in an empty file', function()
vim.fn.writefile({''}, tempfile, 's')
local result = adapter.discover_positions(tempfile)
end)
Continuing the discussion from #349. I am currently writing an adapter for busted and I want to write tests for it in busted as well. I can run tests in general without issue following this blog post. The issue is when executing asynchronous functions like
neotest.lib.treesitter.parse_positions
.Here is a simple test file:
As you can see this busted test is able to call into Neovim's
vim
module. Other than that it is a regular busted test. Now let's adding an actial test.A naive attempt
This fails because
discover_positions
callslib.treesitter.parse_positions
without asynchronous context. The return values oflib.treesitter.parse_positions
are wrong and the file descriptor will be returned as the error. Issue #349 describes the same behaviour.Using
nio.tests.it
This throws another error about
it
beingnil
inside to body ofnio.tests.it
.Replicate
nio.tests.it
If the global
it
only exists within the test, how about we replicate the entire function inside the test?This raises a validation error inside
vim.startswith
.This error is raised by the failing assertion. If the test function does not raise any errors then the test works fine.