Skip to content

Commit

Permalink
address comment
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheecour committed Nov 7, 2020
1 parent 16be39b commit f8b7583
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

- Removed deprecated `iup` module from stdlib, it has already moved to [nimble](https://github.com/nim-lang/iup).
- `doAssertRaises` now correctly handles foreign exceptions and allows expecting those.
- `doAssertRaises` now correctly handles foreign exceptions; it also allows a catch-all form that includes foreign exceptions.

## Language changes

Expand Down
28 changes: 17 additions & 11 deletions lib/system/assertions.nim
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,14 @@ template onFailedAssert*(msg, code: untyped): untyped {.dirty.} =

template doAssertRaises*(exception: typedesc, code: untyped) =
## Raises ``AssertionDefect`` if specified ``code`` does not raise the
## specified exception. Use `exception = void` to specify a foreign exception.
## specified exception.
## Example:
##
## .. code-block:: nim
## doAssertRaises(ValueError):
## raise newException(ValueError, "Hello World")
var wrong = false
const foreign = exception is void
const begin = "expected raising '" & (when foreign: "foreign exception" else: astToStr(exception)) & "', instead"
const begin = "expected raising '" & astToStr(exception) & "', instead"
const msgEnd = " by: " & astToStr(code)
template raisedForeign = raiseAssert(begin & " raised foreign exception" & msgEnd)
when Exception is exception:
Expand All @@ -100,14 +99,6 @@ template doAssertRaises*(exception: typedesc, code: untyped) =
wrong = true
except Exception as e: discard
except: raisedForeign()
elif foreign:
try:
if true:
code
wrong = true
except Exception as e:
raiseAssert(begin & " raised '" & $e.name & "'" & msgEnd)
except: discard
else:
try:
if true:
Expand All @@ -119,3 +110,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))

0 comments on commit f8b7583

Please sign in to comment.