From ab6dc1f22f7ac46964ac63ce46cbb77570037ab9 Mon Sep 17 00:00:00 2001 From: Timothee Cour Date: Wed, 28 Oct 2020 10:50:59 +0200 Subject: [PATCH 1/8] doAssertRaises now correctly handles foreign exceptions; now shows which exception is raised on mismatch --- changelog.md | 2 ++ lib/system/assertions.nim | 27 +++++++++++++++++++-------- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/changelog.md b/changelog.md index 801d50b948d60..6a5b740e84758 100644 --- a/changelog.md +++ b/changelog.md @@ -35,6 +35,8 @@ [nimble](https://github.com/nim-lang/iup). +- 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. ## Language changes diff --git a/lib/system/assertions.nim b/lib/system/assertions.nim index c6283c89ce034..7c54b4160781f 100644 --- a/lib/system/assertions.nim +++ b/lib/system/assertions.nim @@ -82,19 +82,32 @@ template onFailedAssert*(msg, code: untyped): untyped {.dirty.} = template doAssertRaises*(exception: typedesc, code: untyped) = ## Raises ``AssertionDefect`` if specified ``code`` does not raise the - ## specified exception. Example: + ## specified exception. Use `exception = void` to specify a foreign 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 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() + elif foreign: + try: + if true: + code + wrong = true + except Exception as e: + raiseAssert(begin & " raised '" & $e.name & "'" & msgEnd) + except: discard else: try: if true: @@ -102,9 +115,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) From 3e22b20363f2dc81a68d3d2d9a4f922052a7974f Mon Sep 17 00:00:00 2001 From: Timothee Cour Date: Wed, 28 Oct 2020 22:06:22 +0200 Subject: [PATCH 2/8] nimscript now handles `Exception as e` --- lib/system.nim | 3 ++- tests/test_nimscript.nims | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/system.nim b/lib/system.nim index e41dfe1ca9f31..cd8d3a421d429 100644 --- a/lib/system.nim +++ b/lib/system.nim @@ -2360,7 +2360,8 @@ when notJSnotNims and hostOS != "standalone": ## ## **Warning**: Only use this if you know what you are doing. currException = exc - +else: + proc getCurrentException*(): ref Exception {.compilerRtl.} = discard when notJSnotNims: {.push stackTrace: off, profiler: off.} diff --git a/tests/test_nimscript.nims b/tests/test_nimscript.nims index b94146b1e3971..ea640cac69203 100644 --- a/tests/test_nimscript.nims +++ b/tests/test_nimscript.nims @@ -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 From 9f3ab975bae217a3173fc87e1702e1719b62b3e4 Mon Sep 17 00:00:00 2001 From: Timothee Cour Date: Wed, 28 Oct 2020 23:42:05 +0200 Subject: [PATCH 3/8] changelog --- changelog.md | 1 + 1 file changed, 1 insertion(+) diff --git a/changelog.md b/changelog.md index 6a5b740e84758..042d84a2d1369 100644 --- a/changelog.md +++ b/changelog.md @@ -40,6 +40,7 @@ ## Language changes +- `nimscript` now handles `except Exception as e` ## Compiler changes From f8332f1ba266f4531a68d94e997e89977a470d0e Mon Sep 17 00:00:00 2001 From: Timothee Cour Date: Thu, 29 Oct 2020 00:50:08 +0200 Subject: [PATCH 4/8] fixu --- lib/system.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/system.nim b/lib/system.nim index cd8d3a421d429..fd5eb2dac4f2e 100644 --- a/lib/system.nim +++ b/lib/system.nim @@ -2360,7 +2360,7 @@ when notJSnotNims and hostOS != "standalone": ## ## **Warning**: Only use this if you know what you are doing. currException = exc -else: +elif defined(nimscript): proc getCurrentException*(): ref Exception {.compilerRtl.} = discard when notJSnotNims: From 042051d09131a2d08a676b2a961b1f6cd3c4b0ac Mon Sep 17 00:00:00 2001 From: Timothee Cour Date: Fri, 6 Nov 2020 16:36:24 -0800 Subject: [PATCH 5/8] address comment --- changelog.md | 1 + lib/system/assertions.nim | 28 +++++++++++++++++----------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/changelog.md b/changelog.md index 042d84a2d1369..f0227abf39880 100644 --- a/changelog.md +++ b/changelog.md @@ -37,6 +37,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 diff --git a/lib/system/assertions.nim b/lib/system/assertions.nim index 7c54b4160781f..a8af8a1316183 100644 --- a/lib/system/assertions.nim +++ b/lib/system/assertions.nim @@ -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: @@ -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: @@ -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)) From 6d0bdb7561e81bb0a0c21aadbaaf75fc37a4814a Mon Sep 17 00:00:00 2001 From: Timothee Cour Date: Mon, 9 Nov 2020 10:54:00 -0600 Subject: [PATCH 6/8] fixup changelog --- changelog.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog.md b/changelog.md index f0227abf39880..ab11acc6aecbf 100644 --- a/changelog.md +++ b/changelog.md @@ -36,7 +36,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 From d15a7d379ca5c2c902d60d668f6b66705c7ea846 Mon Sep 17 00:00:00 2001 From: Timothee Cour Date: Tue, 10 Nov 2020 10:23:24 -0600 Subject: [PATCH 7/8] remove catch-all doAssertRaises overload from this PR --- changelog.md | 2 +- lib/system/assertions.nim | 18 +----------------- 2 files changed, 2 insertions(+), 18 deletions(-) diff --git a/changelog.md b/changelog.md index ab11acc6aecbf..fb211738675d5 100644 --- a/changelog.md +++ b/changelog.md @@ -37,7 +37,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; it also allows a catch-all form that includes foreign exceptions. +- `doAssertRaises` now correctly handles foreign exceptions. ## Language changes diff --git a/lib/system/assertions.nim b/lib/system/assertions.nim index a8af8a1316183..1a4fda1233968 100644 --- a/lib/system/assertions.nim +++ b/lib/system/assertions.nim @@ -81,8 +81,7 @@ 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. + ## Raises ``AssertionDefect`` if specified ``code`` does not raise `exception`. ## Example: ## ## .. code-block:: nim @@ -110,18 +109,3 @@ 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)) From b08abae2af6f4a185c4c7c8b08885bb8723c3ec2 Mon Sep 17 00:00:00 2001 From: Timothee Cour Date: Tue, 10 Nov 2020 10:24:38 -0600 Subject: [PATCH 8/8] fixup --- changelog.md | 3 --- 1 file changed, 3 deletions(-) diff --git a/changelog.md b/changelog.md index fb211738675d5..fca18643bc810 100644 --- a/changelog.md +++ b/changelog.md @@ -34,9 +34,6 @@ - Removed deprecated `iup` module from stdlib, it has already moved to [nimble](https://github.com/nim-lang/iup). - -- Removed deprecated `iup` module from stdlib, it has already moved to [nimble](https://github.com/nim-lang/iup). - - `doAssertRaises` now correctly handles foreign exceptions. ## Language changes