On this page
assertions
Imports
Procs
-
proc raiseAssert(msg: string) {...}{.noinline, noreturn, nosinks, raises: [], tags: [].}
- Source Edit
-
proc failedAssertImpl(msg: string) {...}{.raises: [], tags: [].}
- Source Edit
Templates
-
template assert(cond: untyped; msg = "")
-
Raises
AssertionDefect
withmsg
ifcond
is false. Note thatAssertionDefect
is hidden from the effect system, so it doesn't produce{.raises: [AssertionDefect].}
. 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:danger
or--assertions:off
command line switches.
Source Editstatic: assert 1 == 9, "This assertion generates code when not built with -d:danger or --assertions:off"
-
template doAssert(cond: untyped; msg = "")
-
Similar to
assert
but is always turned on regardless of--assertions
.
Source Editstatic: doAssert 1 == 9, "This assertion generates code when built with/without -d:danger or --assertions:off"
-
template onFailedAssert(msg, code: untyped): untyped {...}{.dirty.}
-
Sets an assertion failure handler that will intercept any assert statements following
onFailedAssert
in the current module scope.
Source Edit# module-wide policy to change the failed assert # exception type in order to include a lineinfo onFailedAssert(msg): var e = new(TMyError) e.msg = msg e.lineinfo = instantiationInfo(-2) raise e
-
template doAssertRaises(exception: typedesc; code: untyped)
-
Raises
AssertionDefect
if specifiedcode
does not raise the specified exception. Example:
Source EditdoAssertRaises(ValueError): raise newException(ValueError, "Hello World")
© 2006–2021 Andreas Rumpf
Licensed under the MIT License.
https://nim-lang.org/docs/assertions.html