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 == 1
Example: cmd: --assertions:off
assert 1 == 2 # no code generated, no failure here
Example: cmd: -d:danger
assert 1 == 2 # ditto
Source Edit template doAssertRaises(exception: typedesc; code: untyped)
- Raises
AssertionDefectif specifiedcodedoes not raiseexception.Example:
doAssertRaises(ValueError): raise newException(ValueError, "Hello World") doAssertRaises(CatchableError): raise newException(ValueError, "Hello World") doAssertRaises(AssertionDefect): doAssert false
Source Edit template onFailedAssert(msg, code: untyped): untyped {.dirty.}- Sets an assertion failure handler that will intercept any assert statements following
onFailedAssertin the current scope.Example:
type 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
Source Edit
© 2006–2024 Andreas Rumpf
Licensed under the MIT License.
https://nim-lang.org/docs/assertions.html