Skip to content

Commit e5db531

Browse files
timotheecourAraq
andauthored
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 <[email protected]>
1 parent 1f9bf43 commit e5db531

File tree

4 files changed

+20
-11
lines changed

4 files changed

+20
-11
lines changed

changelog.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@
3131
- Removed deprecated `iup` module from stdlib, it has already moved to
3232
[nimble](https://github.com/nim-lang/iup).
3333

34-
34+
- `doAssertRaises` now correctly handles foreign exceptions.
3535

3636
## Language changes
3737

38+
- `nimscript` now handles `except Exception as e`
3839
- The `cstring` doesn't support `[]=` operator in JS backend.
3940

4041

lib/system.nim

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2360,7 +2360,8 @@ when notJSnotNims and hostOS != "standalone":
23602360
##
23612361
## **Warning**: Only use this if you know what you are doing.
23622362
currException = exc
2363-
2363+
elif defined(nimscript):
2364+
proc getCurrentException*(): ref Exception {.compilerRtl.} = discard
23642365

23652366
when notJSnotNims:
23662367
{.push stackTrace: off, profiler: off.}

lib/system/assertions.nim

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,30 +81,31 @@ template onFailedAssert*(msg, code: untyped): untyped {.dirty.} =
8181
code
8282

8383
template doAssertRaises*(exception: typedesc, code: untyped) =
84-
## Raises ``AssertionDefect`` if specified ``code`` does not raise the
85-
## specified exception. Example:
84+
## Raises ``AssertionDefect`` if specified ``code`` does not raise `exception`.
85+
## Example:
8686
##
8787
## .. code-block:: nim
8888
## doAssertRaises(ValueError):
8989
## raise newException(ValueError, "Hello World")
9090
var wrong = false
91+
const begin = "expected raising '" & astToStr(exception) & "', instead"
92+
const msgEnd = " by: " & astToStr(code)
93+
template raisedForeign = raiseAssert(begin & " raised foreign exception" & msgEnd)
9194
when Exception is exception:
9295
try:
9396
if true:
9497
code
9598
wrong = true
96-
except Exception:
97-
discard
99+
except Exception as e: discard
100+
except: raisedForeign()
98101
else:
99102
try:
100103
if true:
101104
code
102105
wrong = true
103106
except exception:
104107
discard
105-
except Exception:
106-
raiseAssert(astToStr(exception) &
107-
" wasn't raised, another error was raised instead by:\n"&
108-
astToStr(code))
108+
except Exception as e: raiseAssert(begin & " raised '" & $e.name & "'" & msgEnd)
109+
except: raisedForeign()
109110
if wrong:
110-
raiseAssert(astToStr(exception) & " wasn't raised by:\n" & astToStr(code))
111+
raiseAssert(begin & " nothing was raised" & msgEnd)

tests/test_nimscript.nims

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,9 @@ block: # #14142
8181
discard dirExists("/usr")
8282
discard fileExists("/usr/foo")
8383
discard findExe("nim")
84+
85+
block:
86+
doAssertRaises(AssertionDefect): doAssert false
87+
try: doAssert false
88+
except Exception as e:
89+
discard

0 commit comments

Comments
 (0)