tproper_stacktrace.nim 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. discard """
  2. matrix: "--stackTrace:on --hint:all:off --warnings:off"
  3. output: '''ok'''
  4. """
  5. import strscans, strutils
  6. proc raiseTestException*() =
  7. raise newException(Exception, "test")
  8. proc matchStackTrace(actualEntries: openArray[StackTraceEntry], expected: string) =
  9. var expectedEntries = newSeq[StackTraceEntry]()
  10. var i = 0
  11. template checkEqual(actual, expected: typed, subject: string) =
  12. if actual != expected:
  13. echo "Unexpected ", subject, " on line ", i
  14. echo "Actual: ", actual
  15. echo "Expected: ", expected
  16. doAssert(false)
  17. for l in splitLines(expected.strip):
  18. var procname, filename: string
  19. var line: int
  20. if not scanf(l, "$s$w.nim($i) $w", filename, line, procname):
  21. doAssert(false, "Wrong expected stack trace")
  22. checkEqual(actualEntries[i].filename.`$`.split('/')[^1], filename & ".nim", "file name")
  23. if line != 0:
  24. checkEqual(actualEntries[i].line, line, "line number")
  25. checkEqual($actualEntries[i].procname, procname, "proc name")
  26. inc i
  27. doAssert(i == actualEntries.len, "Unexpected number of lines in stack trace")
  28. template verifyStackTrace*(expectedStackTrace: string, body: untyped) =
  29. var verified = false
  30. try:
  31. body
  32. except Exception as e:
  33. verified = true
  34. # echo "Stack trace:"
  35. # echo e.getStackTrace
  36. matchStackTrace(e.getStackTraceEntries(), expectedStackTrace)
  37. doAssert(verified, "No exception was raised")
  38. when true:
  39. # <-- Align with line 70 in the text editor
  40. block:
  41. proc bar() =
  42. raiseTestException()
  43. proc foo() =
  44. bar()
  45. const expectedStackTrace = """
  46. tproper_stacktrace.nim(87) tproper_stacktrace
  47. tproper_stacktrace.nim(77) foo
  48. tproper_stacktrace.nim(74) bar
  49. tproper_stacktrace.nim(8) raiseTestException
  50. """
  51. verifyStackTrace expectedStackTrace:
  52. foo()
  53. block:
  54. proc bar(x: int) =
  55. raiseTestException()
  56. template foo(x: int) =
  57. bar(x)
  58. const expectedStackTrace = """
  59. tproper_stacktrace.nim(104) tproper_stacktrace
  60. tproper_stacktrace.nim(91) bar
  61. tproper_stacktrace.nim(8) raiseTestException
  62. """
  63. verifyStackTrace expectedStackTrace:
  64. var x: int
  65. foo(x)
  66. block: #6803
  67. proc bar(x = 500) =
  68. raiseTestException()
  69. proc foo() =
  70. bar()
  71. const expectedStackTrace = """
  72. tproper_stacktrace.nim(121) tproper_stacktrace
  73. tproper_stacktrace.nim(111) foo
  74. tproper_stacktrace.nim(108) bar
  75. tproper_stacktrace.nim(8) raiseTestException
  76. """
  77. verifyStackTrace expectedStackTrace:
  78. foo()
  79. block:
  80. proc bar() {.stackTrace: off.} =
  81. proc baz() = # Stack trace should be enabled
  82. raiseTestException()
  83. baz()
  84. proc foo() =
  85. bar()
  86. const expectedStackTrace = """
  87. tproper_stacktrace.nim(140) tproper_stacktrace
  88. tproper_stacktrace.nim(130) foo
  89. tproper_stacktrace.nim(126) baz
  90. tproper_stacktrace.nim(8) raiseTestException
  91. """
  92. verifyStackTrace expectedStackTrace:
  93. foo()
  94. echo "ok"