summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorTimothee Cour <timothee.cour2@gmail.com>2020-11-12 02:25:41 -0600
committerGitHub <noreply@github.com>2020-11-12 09:25:41 +0100
commite5db5316c2101a27efd744ba10126fdb57a7eb7c (patch)
tree54b7662b4d509616803306db14b9b44f52d29968 /lib
parent1f9bf43100f7236d8ccbcaa14c43bc18f7e6e5d8 (diff)
downloadNim-e5db5316c2101a27efd744ba10126fdb57a7eb7c.tar.gz
doAssertRaises improvements; nimscript supports `except Exception as e` (#15765)
* doAssertRaises now correctly handles foreign exceptions; now shows which exception is raised on mismatch
* nimscript now handles `Exception as e`
* remove catch-all doAssertRaises overload from this PR

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
Diffstat (limited to 'lib')
-rw-r--r--lib/system.nim3
-rw-r--r--lib/system/assertions.nim19
2 files changed, 12 insertions, 10 deletions
diff --git a/lib/system.nim b/lib/system.nim
index e41dfe1ca..fd5eb2dac 100644
--- a/lib/system.nim
+++ b/lib/system.nim
@@ -2360,7 +2360,8 @@ when notJSnotNims and hostOS != "standalone":
     ##
     ## **Warning**: Only use this if you know what you are doing.
     currException = exc
-
+elif defined(nimscript):
+  proc getCurrentException*(): ref Exception {.compilerRtl.} = discard
 
 when notJSnotNims:
   {.push stackTrace: off, profiler: off.}
diff --git a/lib/system/assertions.nim b/lib/system/assertions.nim
index c6283c89c..1a4fda123 100644
--- a/lib/system/assertions.nim
+++ b/lib/system/assertions.nim
@@ -81,20 +81,23 @@ template onFailedAssert*(msg, code: untyped): untyped {.dirty.} =
     code
 
 template doAssertRaises*(exception: typedesc, code: untyped) =
-  ## Raises ``AssertionDefect`` if specified ``code`` does not raise the
-  ## specified exception. Example:
+  ## Raises ``AssertionDefect`` if specified ``code`` does not raise `exception`.
+  ## Example:
   ##
   ## .. code-block:: nim
   ##  doAssertRaises(ValueError):
   ##    raise newException(ValueError, "Hello World")
   var wrong = false
+  const begin = "expected raising '" & astToStr(exception) & "', instead"
+  const msgEnd = " by: " & astToStr(code)
+  template raisedForeign = raiseAssert(begin & " raised foreign exception" & msgEnd)
   when Exception is exception:
     try:
       if true:
         code
       wrong = true
-    except Exception:
-      discard
+    except Exception as e: discard
+    except: raisedForeign()
   else:
     try:
       if true:
@@ -102,9 +105,7 @@ template doAssertRaises*(exception: typedesc, code: untyped) =
       wrong = true
     except exception:
       discard
-    except Exception:
-      raiseAssert(astToStr(exception) &
-                  " wasn't raised, another error was raised instead by:\n"&
-                  astToStr(code))
+    except Exception as e: raiseAssert(begin & " raised '" & $e.name & "'" & msgEnd)
+    except: raisedForeign()
   if wrong:
-    raiseAssert(astToStr(exception) & " wasn't raised by:\n" & astToStr(code))
+    raiseAssert(begin & " nothing was raised" & msgEnd)