Testing
47.1 busted (BDD-Style)
-- spec/mathx_spec.lua
local mathx = require("mathx")
describe("mathx", function()
describe("clamp", function()
it("clamps above the maximum", function()
assert.are.equal(10, mathx.clamp(15, 0, 10))
end)
it("keeps values in range", function()
assert.equal(5, mathx.clamp(5, 0, 10))
end)
end)
it("doubles numbers", function()
assert.same({2, 4}, {mathx.double(1), mathx.double(2)}) -- deep equality
end)
it("raises on bad input", function()
assert.has_error(function() mathx.double(nil) end)
end)
it("uses spies and stubs", function()
local s = spy.on(mathx, "double")
mathx.double(3)
assert.spy(s).was.called_with(3)
stub(os, "time").returns(0)
assert.equal(0, os.time())
os.time:revert()
end)
before_each(function() --[[ setup ]] end)
after_each(function() --[[ teardown ]] end)
end)luarocks install busted
busted # runs spec/*_spec.lua
busted --coverage && luacov # coverage via luacov
busted -o TAP # output formats: utfTerminal, TAP, json, junit47.2 luaunit (xUnit-Style, Single File)
Other tools: telescope, lust (tiny), gambiarra, plenary.test_harness (Neovim plugins), TestEZ (Roblox).
local lu = require("luaunit")
TestMath = {}
function TestMath:testAdd() lu.assertEquals(1 + 1, 2) end
function TestMath:testError() lu.assertErrorMsgContains("division", function() error("division by zero") end) end
os.exit(lu.LuaUnit.run())