# assertions

- マニュアル
- 標準ライブラリ
- 索引

検索
分類:

# プロシージャ

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

  ソース 編集

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

  ソース 編集

# テンプレート

    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.

  ソース 編集

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

  Similar to `assert` but is always turned on regardless of `--assertions`. ソース 編集

    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

  ソース 編集

    template doAssertRaises(exception: typedesc; code: untyped)

  Raises `AssertionError` if specified `code` does not raise the specified exception. 用例:

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

  ソース 編集