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: stmt catch all, including foreign exceptions #15940

Closed
Closed
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
5 changes: 4 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@

- Added `cmpMem` to `system`.

- `doAssertRaises` now correctly handles foreign exceptions.
- `doAssertRaises` now correctly handles foreign exceptions;
it also allows a catch-all form that includes foreign exceptions.

- Added `asyncdispatch.activeDescriptors` that returns the number of currently
active async event handles/file descriptors.
Expand Down Expand Up @@ -116,7 +117,9 @@ with other backends. see #9125. Use `-d:nimLegacyJsRound` for previous behavior.
- The `cstring` doesn't support `[]=` operator in JS backend.

- nil dereference is not allowed at compile time. `cast[ptr int](nil)[]` is rejected at compile time.
- `nimscript` now handles `except Exception as e`

- The `cstring` doesn't support `[]=` operator in JS backend.

## Compiler changes

Expand Down
15 changes: 15 additions & 0 deletions lib/system/assertions.nim
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,18 @@ template doAssertRaises*(exception: typedesc, code: untyped) =
except: raisedForeign()
if wrong:
raiseAssert(begin & " nothing was raised" & msgEnd)

template doAssertRaises*(code: untyped) =
## Raises `AssertionDefect` if specified `code` does not raise anything,
## including a foreign exception.
## Example:
##
## .. code-block:: nim
## doAssertRaises: raise newException(ValueError, "Hello World")
var wrong = false
try:
if true: code
wrong = true
except: discard
if wrong:
raiseAssert("nothing was raised by: " & astToStr(code))
29 changes: 29 additions & 0 deletions tests/stdlib/tassertions.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
discard """
targets: "c cpp js"
"""

template main =
doAssertRaises(ValueError): raise newException(ValueError, "foo")
doAssertRaises(ValueError, block: raise newException(ValueError, "foo"))
static: main()
main()

when defined(cpp) or defined(js):
when defined(cpp):
{.emit:"""
#include <stdexcept>
void fn(){throw std::runtime_error("asdf");}""".}
proc fn(){.importcpp.}
else:
{.emit:"""
function fn(){ throw 42;} """.}
proc fn(){.importc.}

var witness = false
try:
doAssertRaises(ValueError): fn()
except AssertionDefect:
witness = true
doAssert witness
doAssertRaises: fn()
doAssertRaises(block: fn())