Closed jjvbsag closed 3 years ago
Hi,
It's easy to write your own assert functions. Just look on how luaunit write them. For example, here the code for assertIsNil() 👍
function M.assertIsNil(value, extra_msg_or_nil)
if value ~= nil then
failure("expected: nil, actual: " ..prettystr(value), extra_msg_or_nil, 2)
end
end
In your case, this should be something along the lines of :
local function assertNilMsg(want,ok,msg)
if ok ~= nil or want ~= msg then
failure("Expected: nil with msg '"..want.."', got "..prettystr(ok).." with msg '"..msg.."'", nil, 2)
end
end
A few advices : it is better to always show what you expected and what you actually got in the same message. The failure() function will take care of generating the proper error.
Thank you. You code will throw an error, is msg is nil, so I changed it to
function M.assertNilMsg(want,ok,msg)
if ok ~= nil or want ~= msg then
failure("Expected: nil with msg "..prettystr(want)..", got "..prettystr(ok).." with msg "..prettystr(msg), nil, 2)
end
end
But thanks for the inspiration. Regards
Lua has a convention for functions to return nil and an error string in case of problems. Is there an assert for this? If not, how would you implement it?
Example:
I'd prefer something like
I tried by myself with
but in error case I get the wrong line in the test results
Line 17 is my assertNilMsg and not the test case