Implement doAssertRaises (#6819)
This commit is contained in:
parent
c47ed6c537
commit
06a4dcb17d
1 changed files with 19 additions and 0 deletions
|
|
@ -3752,6 +3752,7 @@ template assert*(cond: bool, msg = "") =
|
||||||
## that ``AssertionError`` is hidden from the effect system, so it doesn't
|
## that ``AssertionError`` is hidden from the effect system, so it doesn't
|
||||||
## produce ``{.raises: [AssertionError].}``. This exception is only supposed
|
## produce ``{.raises: [AssertionError].}``. This exception is only supposed
|
||||||
## to be caught by unit testing frameworks.
|
## to be caught by unit testing frameworks.
|
||||||
|
##
|
||||||
## The compiler may not generate any code at all for ``assert`` if it is
|
## 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``
|
## advised to do so through the ``-d:release`` or ``--assertions:off``
|
||||||
## `command line switches <nimc.html#command-line-switches>`_.
|
## `command line switches <nimc.html#command-line-switches>`_.
|
||||||
|
|
@ -4009,3 +4010,21 @@ when defined(nimHasRunnableExamples):
|
||||||
else:
|
else:
|
||||||
template runnableExamples*(body: untyped) =
|
template runnableExamples*(body: untyped) =
|
||||||
discard
|
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))
|
||||||
Loading…
Add table
Add a link
Reference in a new issue