assertions.nim 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 loc = $instantiationInfo(-1, true)
  22. bind instantiationInfo
  23. mixin failedAssertImpl
  24. when enabled:
  25. {.line: instantiationInfo(fullPaths = compileOption("excessiveStackTrace")).}:
  26. if not cond:
  27. failedAssertImpl(loc & " `" & expr & "` " & msg)
  28. template assert*(cond: untyped, msg = "") =
  29. ## Raises ``AssertionError`` with `msg` if `cond` is false. Note
  30. ## that ``AssertionError`` is hidden from the effect system, so it doesn't
  31. ## produce ``{.raises: [AssertionError].}``. This exception is only supposed
  32. ## to be caught by unit testing frameworks.
  33. ##
  34. ## The compiler may not generate any code at all for ``assert`` if it is
  35. ## advised to do so through the ``-d:release`` or ``--assertions:off``
  36. ## `command line switches <nimc.html#command-line-switches>`_.
  37. const expr = astToStr(cond)
  38. assertImpl(cond, msg, expr, compileOption("assertions"))
  39. template doAssert*(cond: untyped, msg = "") =
  40. ## Similar to ``assert`` but is always turned on regardless of ``--assertions``.
  41. const expr = astToStr(cond)
  42. assertImpl(cond, msg, expr, true)
  43. template onFailedAssert*(msg, code: untyped): untyped {.dirty.} =
  44. ## Sets an assertion failure handler that will intercept any assert
  45. ## statements following `onFailedAssert` in the current module scope.
  46. ##
  47. ## .. code-block:: nim
  48. ## # module-wide policy to change the failed assert
  49. ## # exception type in order to include a lineinfo
  50. ## onFailedAssert(msg):
  51. ## var e = new(TMyError)
  52. ## e.msg = msg
  53. ## e.lineinfo = instantiationInfo(-2)
  54. ## raise e
  55. ##
  56. template failedAssertImpl(msgIMPL: string): untyped {.dirty.} =
  57. let msg = msgIMPL
  58. code
  59. template doAssertRaises*(exception: typedesc, code: untyped) =
  60. ## Raises ``AssertionError`` if specified ``code`` does not raise the
  61. ## specified exception. Example:
  62. ##
  63. ## .. code-block:: nim
  64. ## doAssertRaises(ValueError):
  65. ## raise newException(ValueError, "Hello World")
  66. var wrong = false
  67. when Exception is exception:
  68. try:
  69. if true:
  70. code
  71. wrong = true
  72. except Exception:
  73. discard
  74. else:
  75. try:
  76. if true:
  77. code
  78. wrong = true
  79. except exception:
  80. discard
  81. except Exception:
  82. raiseAssert(astToStr(exception) &
  83. " wasn't raised, another error was raised instead by:\n"&
  84. astToStr(code))
  85. if wrong:
  86. raiseAssert(astToStr(exception) & " wasn't raised by:\n" & astToStr(code))