From e98a1ed79ffe8071efbdc1898b3bfa4f6e9ce2d3 Mon Sep 17 00:00:00 2001 From: andri lim Date: Mon, 22 Jan 2024 18:59:23 +0700 Subject: [PATCH] Fix allow break in test body regression (#38) * Fix allow break in test body regression * Silence UnnamedBreak warning * Use push/pop pragma to silence UnnamedBreak warning --- tests/tunittest.nim | 14 ++++++++++++-- unittest2.nim | 8 ++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/tests/tunittest.nim b/tests/tunittest.nim index 1ed528f..2b41665 100644 --- a/tests/tunittest.nim +++ b/tests/tunittest.nim @@ -59,7 +59,7 @@ suite "PR #36": test "test body": server = new(string) server[] = "hello" - + #------------------------------------------------------------------------------ # Regular tests #------------------------------------------------------------------------------ @@ -227,4 +227,14 @@ when defined(testing): # Also supposed to work outside tests: check 1 == 1 - \ No newline at end of file + +suite "break should works inside test body": + var number: int = 0 + test "step one": + number = 2 + test "step two": + if number == 2: + break + number = 3 + test "step three": + check number == 2 diff --git a/unittest2.nim b/unittest2.nim index d6b2005..3e816a9 100644 --- a/unittest2.nim +++ b/unittest2.nim @@ -1110,14 +1110,19 @@ template runtimeTest*(nameParam: string, body: untyped) = fail() template failingOnExceptions(prefix: string, code: untyped): untyped = + when NimMajor>=2: + {.push warning[UnnamedBreak]:off.} try: - code + block: + code except CatchableError as e: prefix.fail("error", e) except Defect as e: # This may or may not work dependings on --panics prefix.fail("defect", e) except Exception as e: prefix.fail("exception that may cause undefined behavior", e) + when NimMajor>=2: + {.pop.} failingOnExceptions("[setup] "): when declared(testSetupIMPLFlag): testSetupIMPL() @@ -1145,7 +1150,6 @@ template runtimeTest*(nameParam: string, body: untyped) = else: runDirect(instance) - template staticTest*(nameParam: string, body: untyped) = ## Similar to `test` but runs only at compiletime, no matter the ## `unittest2Static` flag