Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doAssertRaises improvements; nimscript supports except Exception as e #15765

Merged
merged 9 commits into from
Nov 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@
- Removed deprecated `iup` module from stdlib, it has already moved to
[nimble](https://github.com/nim-lang/iup).


- `doAssertRaises` now correctly handles foreign exceptions.

timotheecour marked this conversation as resolved.
Show resolved Hide resolved
## Language changes

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


Expand Down
3 changes: 2 additions & 1 deletion lib/system.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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.}
Expand Down
19 changes: 10 additions & 9 deletions lib/system/assertions.nim
Original file line number Diff line number Diff line change
Expand Up @@ -81,30 +81,31 @@ 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:
code
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)
6 changes: 6 additions & 0 deletions tests/test_nimscript.nims
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,9 @@ block: # #14142
discard dirExists("/usr")
discard fileExists("/usr/foo")
discard findExe("nim")

block:
doAssertRaises(AssertionDefect): doAssert false
try: doAssert false
except Exception as e:
discard