From 0de3d4292f328f94c7a94af7e3e61e58ff43a252 Mon Sep 17 00:00:00 2001 From: Timothee Cour Date: Wed, 2 Jun 2021 09:02:14 -0700 Subject: fix #16993, #18054, #17835 runnableExamples now works with templates and nested templates (#18082) --- lib/system/assertions.nim | 47 ++++++++++++++++++----------------------------- 1 file changed, 18 insertions(+), 29 deletions(-) (limited to 'lib') diff --git a/lib/system/assertions.nim b/lib/system/assertions.nim index cf705dd6e..cd789845b 100644 --- a/lib/system/assertions.nim +++ b/lib/system/assertions.nim @@ -3,35 +3,6 @@ ## **Note:** This module is reexported by `system` and thus does not need to be ## imported directly (with `system/assertions`). -runnableExamples: - # assert - assert 1 == 1 - - # onFailedAssert - block: - type MyError = object of CatchableError - lineinfo: tuple[filename: string, line: int, column: int] - - # block-wide policy to change the failed assert - # exception type in order to include a lineinfo - onFailedAssert(msg): - var e = new(MyError) - e.msg = msg - e.lineinfo = instantiationInfo(-2) - raise e - - # doAssert - doAssert 1 == 1 # generates code even when built with `-d:danger` or `--assertions:off` - - # doAssertRaises - doAssertRaises(ValueError): - raise newException(ValueError, "Hello World") - -runnableExamples("--assertions:off"): - assert 1 == 2 # no code generated - -# xxx pending bug #16993: move runnableExamples to respective templates - when not declared(sysFatal): include "system/fatal" @@ -86,21 +57,39 @@ template assert*(cond: untyped, msg = "") = ## ## No code will be generated for `assert` when passing `-d:danger` (implied by `--assertions:off`). ## See `command line switches `_. + runnableExamples: assert 1 == 1 + runnableExamples("--assertions:off"): + assert 1 == 2 # no code generated, no failure here + runnableExamples("-d:danger"): assert 1 == 2 # ditto assertImpl(cond, msg, astToStr(cond), compileOption("assertions")) template doAssert*(cond: untyped, msg = "") = ## Similar to `assert <#assert.t,untyped,string>`_ but is always turned on regardless of `--assertions`. + runnableExamples: + doAssert 1 == 1 # generates code even when built with `-d:danger` or `--assertions:off` assertImpl(cond, msg, astToStr(cond), true) template onFailedAssert*(msg, code: untyped): untyped {.dirty.} = ## Sets an assertion failure handler that will intercept any assert ## statements following `onFailedAssert` in the current scope. + runnableExamples: + type MyError = object of CatchableError + lineinfo: tuple[filename: string, line: int, column: int] + # block-wide policy to change the failed assert exception type in order to + # include a lineinfo + onFailedAssert(msg): + raise (ref MyError)(msg: msg, lineinfo: instantiationInfo(-2)) + doAssertRaises(MyError): doAssert false template failedAssertImpl(msgIMPL: string): untyped {.dirty.} = let msg = msgIMPL code template doAssertRaises*(exception: typedesc, code: untyped) = ## Raises `AssertionDefect` if specified `code` does not raise `exception`. + runnableExamples: + doAssertRaises(ValueError): raise newException(ValueError, "Hello World") + doAssertRaises(CatchableError): raise newException(ValueError, "Hello World") + doAssertRaises(AssertionDefect): doAssert false var wrong = false const begin = "expected raising '" & astToStr(exception) & "', instead" const msgEnd = " by: " & astToStr(code) -- cgit 1.4.1-2-gfad0 vision' href='/akkartik/mu/blame/html/078table.mu.html?h=main&id=0f5a2f4e21046e319ce0fadec32cc5e89d2f4620'>^
1
2
3
4
5
6
7
8
9