tfailedassert.nim 3.0 KB

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