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>
This commit is contained in:
parent
1f9bf43100
commit
e5db5316c2
4 changed files with 20 additions and 11 deletions
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue