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
AssertionDefectwithmsgifcondis false. Note thatAssertionDefectis 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
assertif it is advised to do so through the-d:dangeror--assertions:offcommand line switches.static: assert 1 == 9, "This assertion generates code when not built with -d:danger or --assertions:off"
Source Edit template doAssert(cond: untyped; msg = "")
- Similar to
assertbut is always turned on regardless of--assertions.static: doAssert 1 == 9, "This assertion generates code when built with/without -d:danger or --assertions:off"
Source Edit template onFailedAssert(msg, code: untyped): untyped {...}{.dirty.}- Sets an assertion failure handler that will intercept any assert statements following
onFailedAssertin the current module scope.# 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
Source Edit template doAssertRaises(exception: typedesc; code: untyped)
- Raises
AssertionDefectif specifiedcodedoes not raise the specified exception. Example:doAssertRaises(ValueError): raise newException(ValueError, "Hello World")
Source Edit
© 2006–2021 Andreas Rumpf
Licensed under the MIT License.
https://nim-lang.org/docs/assertions.html