summary refs log tree commit diff stats
path: root/tests/exception
diff options
context:
space:
mode:
Diffstat (limited to 'tests/exception')
-rw-r--r--tests/exception/texceptionbreak.nim45
-rw-r--r--tests/exception/tfinally4.nim40
-rw-r--r--tests/exception/tnestedreturn.nim40
-rw-r--r--tests/exception/tnestedreturn2.nim20
4 files changed, 145 insertions, 0 deletions
diff --git a/tests/exception/texceptionbreak.nim b/tests/exception/texceptionbreak.nim
new file mode 100644
index 000000000..76e986787
--- /dev/null
+++ b/tests/exception/texceptionbreak.nim
@@ -0,0 +1,45 @@
+discard """
+  file: "tnestedbreak.nim"
+  output: "1\n2\n3\n4"
+"""
+
+# First variety
+try:
+  raise newException(EOS, "Problem")
+except EOS:
+  for y in [1, 2, 3]:
+    discard
+  try:
+    discard
+  except EOS:
+    discard
+echo "1"
+
+# Second Variety
+try:
+  raise newException(EOS, "Problem")
+except EOS:
+  for y in [1, 2, 3]:
+    discard
+  for y in [1, 2, 3]:
+    discard
+
+echo "2"
+
+# Third Variety
+try:
+  raise newException(EOS, "Problem")
+except EOS:
+  block:
+    break
+
+echo "3"
+
+# Fourth Variety
+block:
+  try:
+    raise newException(EOS, "Problem")
+  except EOS:
+    break
+
+echo "4"
\ No newline at end of file
diff --git a/tests/exception/tfinally4.nim b/tests/exception/tfinally4.nim
new file mode 100644
index 000000000..05c57c4f5
--- /dev/null
+++ b/tests/exception/tfinally4.nim
@@ -0,0 +1,40 @@
+discard """
+  file: "tfinally4.nim"
+  output: "B1\nA1\n1\nB1\nB2\ncatch\nA1\n1\nB1\nA1\nA2\n2\nB1\nB2\ncatch\nA1\nA2\n0\nB1\nA1\n1\nB1\nB2\nA1\n1\nB1\nA1\nA2\n2\nB1\nB2\nA1\nA2\n3"
+"""
+
+# More thorough test of return-in-finaly
+
+var raiseEx = true
+var returnA = true
+var returnB = false
+ 
+proc main: int = 
+  try: #A
+    try: #B
+      if raiseEx:
+        raise newException(EOS, "")
+      return 3
+    finally: #B
+      echo "B1"
+      if returnB:
+        return 2
+      echo "B2"
+  except EOS: #A
+    echo "catch"
+  finally: #A
+    echo "A1"
+    if returnA:
+      return 1
+    echo "A2"
+
+for x in [true, false]:
+  for y in [true, false]:
+    for z in [true, false]:
+      # echo "raiseEx: " & $x
+      # echo "returnA: " & $y
+      # echo "returnB: " & $z
+      raiseEx = x
+      returnA = y
+      returnB = z
+      echo main()
diff --git a/tests/exception/tnestedreturn.nim b/tests/exception/tnestedreturn.nim
new file mode 100644
index 000000000..b9f7843f6
--- /dev/null
+++ b/tests/exception/tnestedreturn.nim
@@ -0,0 +1,40 @@
+discard """
+  file: "tnestedreturn.nim"
+  output: "A\nB\nC\n"
+"""
+
+# Various tests of return nested in double try/except statements
+
+proc test1() =
+
+  finally: echo "A"
+
+  try:
+    raise newException(EOS, "Problem")
+  except EOS:
+    return
+
+test1()
+
+
+proc test2() =
+
+  finally: echo "B"
+
+  try:
+    return
+  except EOS:
+    discard
+
+test2()
+
+proc test3() =
+  try:
+    try:
+      raise newException(EOS, "Problem")
+    except EOS:
+      return
+  finally:
+    echo "C"
+
+test3()
diff --git a/tests/exception/tnestedreturn2.nim b/tests/exception/tnestedreturn2.nim
new file mode 100644
index 000000000..14a2dab92
--- /dev/null
+++ b/tests/exception/tnestedreturn2.nim
@@ -0,0 +1,20 @@
+discard """
+  file: "tnestedreturn.nim"
+  outputsub: "Error: unhandled exception: Problem [EOS]"
+  exitcode: "1"
+"""
+
+proc test4() =
+  try:
+    try:
+      raise newException(EOS, "Problem")
+    except EOS:
+      return
+  finally:
+    discard
+
+# Should cause unhandled exception error,
+# but could cause segmentation fault if 
+# exceptions are not handled properly.
+test4()
+raise newException(EOS, "Problem")