tassert2.nim 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. discard """
  2. output: '''
  3. `false` first assertion from bar
  4. `false` second assertion from bar
  5. -1
  6. '''
  7. """
  8. from strutils import endsWith
  9. type
  10. TLineInfo = tuple[filename: string, line: int, column: int]
  11. TMyError = object of Exception
  12. lineinfo: TLineInfo
  13. EMyError = ref TMyError
  14. # NOTE: when entering newlines, adjust `expectedEnd` outputs
  15. try:
  16. doAssert(false, "msg1") # doAssert test
  17. except AssertionDefect as e:
  18. assert e.msg.endsWith "tassert2.nim(20, 11) `false` msg1"
  19. try:
  20. assert false # assert test with no msg
  21. except AssertionDefect as e:
  22. assert e.msg.endsWith "tassert2.nim(25, 10) `false` "
  23. try:
  24. let a = 1
  25. doAssert(a+a==1) # assert test with Ast expression
  26. # BUG: const folding would make "1+1==1" appear as `false` in
  27. # assert message
  28. except AssertionDefect as e:
  29. assert e.msg.endsWith "`a + a == 1` "
  30. try:
  31. let a = 1
  32. doAssert a+a==1 # ditto with `doAssert` and no parens
  33. except AssertionDefect as e:
  34. assert e.msg.endsWith "`a + a == 1` "
  35. proc fooStatic() =
  36. # protect against https://github.com/nim-lang/Nim/issues/8758
  37. static: doAssert(true)
  38. fooStatic()
  39. block:
  40. # scope-wide policy to change the failed assert
  41. # exception type in order to include a lineinfo
  42. onFailedAssert(msg):
  43. var e = new(TMyError)
  44. e.msg = msg
  45. e.lineinfo = instantiationInfo(-2)
  46. raise e
  47. proc foo =
  48. assert(false, "assertion from foo")
  49. proc bar: int =
  50. # local overrides that are active only in this proc
  51. onFailedAssert(msg):
  52. echo msg[^32..^1]
  53. assert(false, "first assertion from bar")
  54. onFailedAssert(msg):
  55. echo msg[^33..^1]
  56. return -1
  57. assert(false, "second assertion from bar")
  58. return 10
  59. echo(bar())
  60. try:
  61. foo()
  62. except:
  63. let e = EMyError(getCurrentException())
  64. assert e.msg.endsWith "tassert2.nim(62, 11) `false` assertion from foo"
  65. block: ## checks for issue https://github.com/nim-lang/Nim/issues/8518
  66. template fun(a: string): string =
  67. const pattern = a & a
  68. pattern
  69. try:
  70. doAssert fun("foo1") == fun("foo2"), "mymsg"
  71. except AssertionDefect as e:
  72. # used to expand out the template instantiaiton, sometimes filling hundreds of lines
  73. assert e.msg.endsWith ""
  74. block: ## checks for issue https://github.com/nim-lang/Nim/issues/9301
  75. try:
  76. doAssert 1 + 1 == 3
  77. except AssertionDefect as e:
  78. # used to const fold as false
  79. assert e.msg.endsWith "tassert2.nim(100, 14) `1 + 1 == 3` "
  80. block: ## checks AST isn't transformed as it used to
  81. let a = 1
  82. try:
  83. doAssert a > 1
  84. except AssertionDefect as e:
  85. # used to rewrite as `1 < a`
  86. assert e.msg.endsWith "tassert2.nim(108, 14) `a > 1` "