testing
is a module I wrote for learning some Lua, and I find it quite useful for testing my modules written for neovim.
Since I come from the perspective of neovim, I simply has the module among my regular configuration, i.e. ~/.config/nvim/lua/testing.lua
.
Below shows a lua script that use the testing
module.
local testing = require("testing")
local assert = testing.assert
local function square(n)
return n*n
end
-- Register test cases
testing.register("square 3", function()
assert.equal(9, square(3))
end)
testing.register("square 5", function()
assert.equal(24, square(5)) -- Oops!
end)
-- Run tests, and exit with status 1 if any test fails
testing.run(1)
Now run the script:
$ lua mymod_test.lua
OK: square 3
FAIL: square 5
Error: expected values to be equal: 24 != 25
Because how Lua works, you can utilize monkey patching for mocking nvim functions, e.g.
the global vim
variable:
local testing = require("testing")
local assert = testing.assert
_G.vim = {
api = {
nvim_set_option(...)
print("Do something useful instead of just printing")
end
}
}
local momod = require("momod")
...