From acc6848007a6e1a3d0092aa0f993388e950a24a2 Mon Sep 17 00:00:00 2001 From: Timothee Cour Date: Wed, 28 Oct 2020 10:50:59 +0200 Subject: [PATCH] doAssertRaises catch all --- changelog.md | 6 +++--- lib/system/assertions.nim | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/changelog.md b/changelog.md index 11930cf926d67..d2ff82d545ba5 100644 --- a/changelog.md +++ b/changelog.md @@ -33,14 +33,14 @@ - nodejs now supports osenv: `getEnv`, `putEnv`, `envPairs`, `delEnv`, `existsEnv` -- `doAssertRaises` now correctly handles foreign exceptions. +- `doAssertRaises` now correctly handles foreign exceptions; + it also allows a catch-all form that includes foreign exceptions. ## 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. ## Compiler changes diff --git a/lib/system/assertions.nim b/lib/system/assertions.nim index 1a4fda1233968..d5b7d0c9ed6ee 100644 --- a/lib/system/assertions.nim +++ b/lib/system/assertions.nim @@ -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))