On this page
std/assertions
Source EditThis module implements assertion handling.
Imports
Procs
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.No code will be generated for
assertwhen passing-d:danger(implied by--assertions:off). See command line switches.Example:
assert 1 == 1Example: cmd: --assertions:off
assert 1 == 2 # no code generated, no failure hereExample: cmd: -d:danger
Source Editassert 1 == 2 # ditto -
template doAssertRaises(exception: typedesc; code: untyped) -
Raises
AssertionDefectif specifiedcodedoes not raiseexception.Example:
Source EditdoAssertRaises(ValueError): raise newException(ValueError, "Hello World") doAssertRaises(CatchableError): raise newException(ValueError, "Hello World") doAssertRaises(AssertionDefect): doAssert false -
template onFailedAssert(msg, code: untyped): untyped {.dirty.} -
Sets an assertion failure handler that will intercept any assert statements following
onFailedAssertin the current scope.Example:
Source Edittype MyError = object of CatchableError lineinfo: tuple[filename: string, line: int, column: int] # block-wide policy to change the failed assert exception type in order to # include a lineinfo onFailedAssert(msg): raise (ref MyError)(msg: msg, lineinfo: instantiationInfo(-2)) doAssertRaises(MyError): doAssert false
© 2006–2024 Andreas Rumpf
Licensed under the MIT License.
https://nim-lang.org/docs/assertions.html