lunarmodules / luassert

Assertion library for Lua
MIT License
202 stars 77 forks source link

Fix error message for has_error() #72

Closed o-lim closed 9 years ago

o-lim commented 9 years ago

When has_error() fails, the error message is misleading, implying that an error was thrown, but did not match what was expected, yet Caught and Expected both match each other.

The following failure:

assert.has_error(function() end)

produces the following error message:

say/init.lua:46: bad argument #2 to 'format' (no value)

stack traceback:
    say/init.lua:46: in function 's'
    luassert/assert.lua:60: in function 'error'

Fix has_error() to show the proper error message of the form:

Expected a different error.
Caught:
...
Expected:
...

and update the error message to more accurately describe the error when no error message is passed in, showing:

Expected a different error.
Caught:
(no error)
Expected:
(error)

This way, it is a bit clearer that no error was thrown/caught, while an error was expected. If an expected error message is provided, that error message will be displayed instead of (error). If the function being tested calls error(), with no arguments, the caught message will display (nil) instead of (no error), since nil is a valid error object.

o-lim commented 9 years ago

This PR does not pass the travis build without PR #68, so this PR is on top of PR #68.