assertions.nim 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. when not declared(sysFatal):
  2. include "system/fatal"
  3. # ---------------------------------------------------------------------------
  4. # helpers
  5. type InstantiationInfo = tuple[filename: string, line: int, column: int]
  6. proc `$`(x: int): string {.magic: "IntToStr", noSideEffect.}
  7. proc `$`(info: InstantiationInfo): string =
  8. # The +1 is needed here
  9. # instead of overriding `$` (and changing its meaning), consider explicit name.
  10. info.filename & "(" & $info.line & ", " & $(info.column+1) & ")"
  11. # ---------------------------------------------------------------------------
  12. proc raiseAssert*(msg: string) {.noinline, noreturn.} =
  13. sysFatal(AssertionError, msg)
  14. proc failedAssertImpl*(msg: string) {.raises: [], tags: [].} =
  15. # trick the compiler to not list ``AssertionError`` when called
  16. # by ``assert``.
  17. type Hide = proc (msg: string) {.noinline, raises: [], noSideEffect,
  18. tags: [].}
  19. Hide(raiseAssert)(msg)
  20. template assertImpl(cond: bool, msg: string, expr: string, enabled: static[bool]) =
  21. const
  22. loc = instantiationInfo(fullPaths = compileOption("excessiveStackTrace"))
  23. ploc = $loc
  24. bind instantiationInfo
  25. mixin failedAssertImpl
  26. when enabled:
  27. {.line: loc.}:
  28. if not cond:
  29. failedAssertImpl(ploc & " `" & expr & "` " & msg)
  30. template assert*(cond: untyped, msg = "") =
  31. ## Raises ``AssertionError`` with `msg` if `cond` is false. Note
  32. ## that ``AssertionError`` is hidden from the effect system, so it doesn't
  33. ## produce ``{.raises: [AssertionError].}``. This exception is only supposed
  34. ## to be caught by unit testing frameworks.
  35. ##
  36. ## The compiler may not generate any code at all for ``assert`` if it is
  37. ## advised to do so through the ``-d:release`` or ``--assertions:off``
  38. ## `command line switches <nimc.html#command-line-switches>`_.
  39. const expr = astToStr(cond)
  40. assertImpl(cond, msg, expr, compileOption("assertions"))
  41. template doAssert*(cond: untyped, msg = "") =
  42. ## Similar to ``assert`` but is always turned on regardless of ``--assertions``.
  43. const expr = astToStr(cond)
  44. assertImpl(cond, msg, expr, true)
  45. template onFailedAssert*(msg, code: untyped): untyped {.dirty.} =
  46. ## Sets an assertion failure handler that will intercept any assert
  47. ## statements following `onFailedAssert` in the current module scope.
  48. ##
  49. ## .. code-block:: nim
  50. ## # module-wide policy to change the failed assert
  51. ## # exception type in order to include a lineinfo
  52. ## onFailedAssert(msg):
  53. ## var e = new(TMyError)
  54. ## e.msg = msg
  55. ## e.lineinfo = instantiationInfo(-2)
  56. ## raise e
  57. ##
  58. template failedAssertImpl(msgIMPL: string): untyped {.dirty.} =
  59. let msg = msgIMPL
  60. code
  61. template doAssertRaises*(exception: typedesc, code: untyped) =
  62. ## Raises ``AssertionError`` if specified ``code`` does not raise the
  63. ## specified exception. Example:
  64. ##
  65. ## .. code-block:: nim
  66. ## doAssertRaises(ValueError):
  67. ## raise newException(ValueError, "Hello World")
  68. var wrong = false
  69. when Exception is exception:
  70. try:
  71. if true:
  72. code
  73. wrong = true
  74. except Exception:
  75. discard
  76. else:
  77. try:
  78. if true:
  79. code
  80. wrong = true
  81. except exception:
  82. discard
  83. except Exception:
  84. raiseAssert(astToStr(exception) &
  85. " wasn't raised, another error was raised instead by:\n"&
  86. astToStr(code))
  87. if wrong:
  88. raiseAssert(astToStr(exception) & " wasn't raised by:\n" & astToStr(code))