Implement doAssertRaises (#6819)

This commit is contained in:
Dominik Picheta
2017-11-28 00:57:25 +00:00
committed by Andreas Rumpf
parent c47ed6c537
commit 06a4dcb17d

View File

@@ -3752,6 +3752,7 @@ template assert*(cond: bool, msg = "") =
## that ``AssertionError`` is hidden from the effect system, so it doesn't
## produce ``{.raises: [AssertionError].}``. This exception is only supposed
## to be caught by unit testing frameworks.
##
## The compiler may not generate any code at all for ``assert`` if it is
## advised to do so through the ``-d:release`` or ``--assertions:off``
## `command line switches <nimc.html#command-line-switches>`_.
@@ -4009,3 +4010,21 @@ when defined(nimHasRunnableExamples):
else:
template runnableExamples*(body: untyped) =
discard
template doAssertRaises*(exception, code: untyped): typed =
## Raises ``AssertionError`` if specified ``code`` does not raise the
## specified exception.
runnableExamples:
doAssertRaises(ValueError):
raise newException(ValueError, "Hello World")
try:
block:
code
raiseAssert(astToStr(exception) & " wasn't raised by:\n" & astToStr(code))
except exception:
discard
except Exception as exc:
raiseAssert(astToStr(exception) &
" wasn't raised, another error was raised instead by:\n"&
astToStr(code))