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
|
|
@ -31,10 +31,11 @@
|
||||||
- Removed deprecated `iup` module from stdlib, it has already moved to
|
- Removed deprecated `iup` module from stdlib, it has already moved to
|
||||||
[nimble](https://github.com/nim-lang/iup).
|
[nimble](https://github.com/nim-lang/iup).
|
||||||
|
|
||||||
|
- `doAssertRaises` now correctly handles foreign exceptions.
|
||||||
|
|
||||||
## Language changes
|
## Language changes
|
||||||
|
|
||||||
|
- `nimscript` now handles `except Exception as e`
|
||||||
- The `cstring` doesn't support `[]=` operator in JS backend.
|
- The `cstring` doesn't support `[]=` operator in JS backend.
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2360,7 +2360,8 @@ when notJSnotNims and hostOS != "standalone":
|
||||||
##
|
##
|
||||||
## **Warning**: Only use this if you know what you are doing.
|
## **Warning**: Only use this if you know what you are doing.
|
||||||
currException = exc
|
currException = exc
|
||||||
|
elif defined(nimscript):
|
||||||
|
proc getCurrentException*(): ref Exception {.compilerRtl.} = discard
|
||||||
|
|
||||||
when notJSnotNims:
|
when notJSnotNims:
|
||||||
{.push stackTrace: off, profiler: off.}
|
{.push stackTrace: off, profiler: off.}
|
||||||
|
|
|
||||||
|
|
@ -81,20 +81,23 @@ template onFailedAssert*(msg, code: untyped): untyped {.dirty.} =
|
||||||
code
|
code
|
||||||
|
|
||||||
template doAssertRaises*(exception: typedesc, code: untyped) =
|
template doAssertRaises*(exception: typedesc, code: untyped) =
|
||||||
## Raises ``AssertionDefect`` if specified ``code`` does not raise the
|
## Raises ``AssertionDefect`` if specified ``code`` does not raise `exception`.
|
||||||
## specified exception. Example:
|
## Example:
|
||||||
##
|
##
|
||||||
## .. code-block:: nim
|
## .. code-block:: nim
|
||||||
## doAssertRaises(ValueError):
|
## doAssertRaises(ValueError):
|
||||||
## raise newException(ValueError, "Hello World")
|
## raise newException(ValueError, "Hello World")
|
||||||
var wrong = false
|
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:
|
when Exception is exception:
|
||||||
try:
|
try:
|
||||||
if true:
|
if true:
|
||||||
code
|
code
|
||||||
wrong = true
|
wrong = true
|
||||||
except Exception:
|
except Exception as e: discard
|
||||||
discard
|
except: raisedForeign()
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
if true:
|
if true:
|
||||||
|
|
@ -102,9 +105,7 @@ template doAssertRaises*(exception: typedesc, code: untyped) =
|
||||||
wrong = true
|
wrong = true
|
||||||
except exception:
|
except exception:
|
||||||
discard
|
discard
|
||||||
except Exception:
|
except Exception as e: raiseAssert(begin & " raised '" & $e.name & "'" & msgEnd)
|
||||||
raiseAssert(astToStr(exception) &
|
except: raisedForeign()
|
||||||
" wasn't raised, another error was raised instead by:\n"&
|
|
||||||
astToStr(code))
|
|
||||||
if wrong:
|
if wrong:
|
||||||
raiseAssert(astToStr(exception) & " wasn't raised by:\n" & astToStr(code))
|
raiseAssert(begin & " nothing was raised" & msgEnd)
|
||||||
|
|
|
||||||
|
|
@ -81,3 +81,9 @@ block: # #14142
|
||||||
discard dirExists("/usr")
|
discard dirExists("/usr")
|
||||||
discard fileExists("/usr/foo")
|
discard fileExists("/usr/foo")
|
||||||
discard findExe("nim")
|
discard findExe("nim")
|
||||||
|
|
||||||
|
block:
|
||||||
|
doAssertRaises(AssertionDefect): doAssert false
|
||||||
|
try: doAssert false
|
||||||
|
except Exception as e:
|
||||||
|
discard
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue