# assertions

Dark Mode

- Manual
- Standard library
- Index
- devel, stable

Search:
Group by:

# Procs

    proc raiseAssert(msg: string) {...}{.noinline, noreturn, nosinks,
                                    raises: [AssertionError], tags: [].}

  Source Edit

    proc failedAssertImpl(msg: string) {...}{.raises: [], tags: [].}

  Source Edit

# Templates

    template assert(cond: untyped; msg = "")

  Raises `AssertionError` with `msg` if `cond` is false. Note that `AssertionError` is hidden from the effect system, so it doesn't produce `{.raises: [AssertionError].}`. 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.

      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 `assert` but 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 `onFailedAssert` in 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 `AssertionError` if specified `code` does not raise the specified exception. Example:

      doAssertRaises(ValueError):
        raise newException(ValueError, "Hello World")

  Source Edit