summary refs log tree commit diff stats
path: root/tests
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2018-06-11 23:39:27 +0200
committerGitHub <noreply@github.com>2018-06-11 23:39:27 +0200
commit1c58f31a71d2fb8c8569a6ceb6fffc4778d1c7ab (patch)
treea5f698b1bad62fddcfe832c9651b4916717d038f /tests
parentac0f9860081ce20cd1ce1f823db7361ad284b1be (diff)
parent8f067634919eeb232ee04f759c4c1786ddd26ea6 (diff)
downloadNim-1c58f31a71d2fb8c8569a6ceb6fffc4778d1c7ab.tar.gz
Merge pull request #8014 from yglukhov/fix-6803
Fixes #6803
Diffstat (limited to 'tests')
-rw-r--r--tests/errmsgs/tproper_stacktrace.nim124
1 files changed, 118 insertions, 6 deletions
diff --git a/tests/errmsgs/tproper_stacktrace.nim b/tests/errmsgs/tproper_stacktrace.nim
index 57e65fa6f..4e5c5fbf8 100644
--- a/tests/errmsgs/tproper_stacktrace.nim
+++ b/tests/errmsgs/tproper_stacktrace.nim
@@ -1,11 +1,123 @@
 discard """
-  outputsub: '''tproper_stacktrace.nim(7) tproper_stacktrace'''
-  exitcode: 1
+  output: '''ok'''
 """
+import strscans, strutils
 
-template fuzzy(x) =
-  echo x[] != 9
+proc raiseTestException*() =
+  raise newException(Exception, "test")
 
-var p: ptr int
-fuzzy p
+proc matchStackTrace(actualEntries: openarray[StackTraceEntry], expected: string) =
+  var expectedEntries = newSeq[StackTraceEntry]()
+  var i = 0
 
+  template checkEqual(actual, expected: typed, subject: string) =
+    if actual != expected:
+      echo "Unexpected ", subject, " on line ", i
+      echo "Actual: ", actual
+      echo "Expected: ", expected
+      doAssert(false)
+
+  for l in splitLines(expected.strip):
+    var procname, filename: string
+    var line: int
+    if not scanf(l, "$s$w.nim($i) $w", filename, line, procname):
+      doAssert(false, "Wrong expected stack trace")
+    checkEqual($actualEntries[i].filename, filename & ".nim", "file name")
+    if line != 0:
+      checkEqual(actualEntries[i].line, line, "line number")
+    checkEqual($actualEntries[i].procname, procname, "proc name")
+    inc i
+
+  doAssert(i == actualEntries.len, "Unexpected number of lines in stack trace")
+
+template verifyStackTrace*(expectedStackTrace: string, body: untyped) =
+  var verified = false
+  try:
+    body
+  except Exception as e:
+    verified = true
+    # echo "Stack trace:"
+    # echo e.getStackTrace
+    matchStackTrace(e.getStackTraceEntries(), expectedStackTrace)
+
+  doAssert(verified, "No exception was raised")
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+when isMainModule:
+# <-- Align with line 70 in the text editor
+  block:
+    proc bar() =
+      raiseTestException()
+
+    proc foo() =
+      bar()
+
+    const expectedStackTrace = """
+      tproper_stacktrace.nim(86) tproper_stacktrace
+      tproper_stacktrace.nim(76) foo
+      tproper_stacktrace.nim(73) bar
+      tproper_stacktrace.nim(7) raiseTestException
+    """
+
+    verifyStackTrace expectedStackTrace:
+      foo()
+
+  block:
+    proc bar(x: int) =
+      raiseTestException()
+
+    template foo(x: int) =
+      bar(x)
+
+    const expectedStackTrace = """
+      tproper_stacktrace.nim(103) tproper_stacktrace
+      tproper_stacktrace.nim(90) bar
+      tproper_stacktrace.nim(7) raiseTestException
+    """
+
+    verifyStackTrace expectedStackTrace:
+      var x: int
+      foo(x)
+
+  block: #6803
+    proc bar(x = 500) =
+      raiseTestException()
+
+    proc foo() =
+      bar()
+
+    const expectedStackTrace = """
+      tproper_stacktrace.nim(120) tproper_stacktrace
+      tproper_stacktrace.nim(110) foo
+      tproper_stacktrace.nim(107) bar
+      tproper_stacktrace.nim(7) raiseTestException
+    """
+
+    verifyStackTrace expectedStackTrace:
+      foo()
+
+
+  echo "ok"