Skip to content

Commit

Permalink
fix #16993, #18054, #17835 runnableExamples now works with templates …
Browse files Browse the repository at this point in the history
…and nested templates (#18082)
  • Loading branch information
timotheecour authored Jun 2, 2021
1 parent 4ee6edd commit 0de3d42
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 46 deletions.
12 changes: 8 additions & 4 deletions compiler/evaltempl.nim
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,14 @@ proc evalTemplateAux(templ, actual: PNode, c: var TemplCtx, result: PNode) =
not c.isDeclarative:
c.isDeclarative = true
isDeclarative = true
var res = copyNode(c, templ, actual)
for i in 0..<templ.len:
evalTemplateAux(templ[i], actual, c, res)
result.add res
if (not c.isDeclarative) and templ.kind in nkCallKinds and isRunnableExamples(templ[0]):
# fixes bug #16993, bug #18054
discard
else:
var res = copyNode(c, templ, actual)
for i in 0..<templ.len:
evalTemplateAux(templ[i], actual, c, res)
result.add res
if isDeclarative: c.isDeclarative = false

const
Expand Down
47 changes: 18 additions & 29 deletions lib/system/assertions.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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 <nimc.html#compiler-usage-commandminusline-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)
Expand Down
4 changes: 2 additions & 2 deletions nimdoc/testproject/expected/subdir/subdir_b/utils.html
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,9 @@ <h1><a class="toc-backref" href="#18">Templates</a></h1>
<dt><pre><span class="Keyword">template</span> <a href="#fromUtilsGen.t"><span class="Identifier">fromUtilsGen</span></a><span class="Other">(</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">untyped</span></pre></dt>
<dd>

this should be shown in utils.html
should be shown in utils.html only
<p><strong class="examples_text">Example:</strong></p>
<pre class="listing"><span class="Identifier">assert</span> <span class="DecNumber">3</span><span class="Operator">*</span><span class="DecNumber">2</span> <span class="Operator">==</span> <span class="DecNumber">6</span></pre>ditto
<pre class="listing"><span class="Keyword">discard</span> <span class="StringLit">&quot;should be in utils.html only, not in module that calls fromUtilsGen&quot;</span></pre>ditto

</dd>

Expand Down
11 changes: 6 additions & 5 deletions nimdoc/testproject/expected/testproject.html
Original file line number Diff line number Diff line change
Expand Up @@ -431,10 +431,7 @@ <h1 class="title">testproject</h1>
<span class="Keyword">discard</span> <span class="StringLit">&quot;in top2&quot;</span></pre>top2 after
<p><strong class="examples_text">Example:</strong></p>
<pre class="listing"><span class="Keyword">import</span> <span class="Identifier">testproject</span>
<span class="Keyword">discard</span> <span class="StringLit">&quot;in top3&quot;</span></pre>top3 after
<p><strong class="examples_text">Example:</strong></p>
<pre class="listing"><span class="Keyword">import</span> <span class="Identifier">testproject</span>
<span class="Identifier">assert</span> <span class="DecNumber">3</span><span class="Operator">*</span><span class="DecNumber">2</span> <span class="Operator">==</span> <span class="DecNumber">6</span></pre></p>
<span class="Keyword">discard</span> <span class="StringLit">&quot;in top3&quot;</span></pre>top3 after</p>
<div class="section" id="6">
<h1><a class="toc-backref" href="#6">Imports</a></h1>
<dl class="item">
Expand Down Expand Up @@ -574,7 +571,8 @@ <h1><a class="toc-backref" href="#12">Procs</a></h1>

came form utils but should be shown where <tt class="docutils literal"><span class="pre"><span class="Identifier">fromUtilsGen</span></span></tt> is called
<p><strong class="examples_text">Example:</strong></p>
<pre class="listing"><span class="Keyword">discard</span> <span class="DecNumber">1</span></pre>
<pre class="listing"><span class="Keyword">discard</span> <span class="LongStringLit">&quot;&quot;&quot;should be shown as examples for fromUtils3
in module calling fromUtilsGen&quot;&quot;&quot;</span></pre>

</dd>
<a id="isValid,T"></a>
Expand Down Expand Up @@ -957,6 +955,9 @@ <h1><a class="toc-backref" href="#18">Templates</a></h1>
<dd>

ok3
<p><strong class="examples_text">Example:</strong></p>
<pre class="listing"><span class="Keyword">discard</span> <span class="LongStringLit">&quot;&quot;&quot;should be shown as examples for fromUtils2
in module calling fromUtilsGen&quot;&quot;&quot;</span></pre>

</dd>
<a id="z6t.t"></a>
Expand Down
10 changes: 7 additions & 3 deletions nimdoc/testproject/subdir/subdir_b/utils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ template bEnum*(): untyped =
discard

template fromUtilsGen*(): untyped =
## this should be shown in utils.html
## should be shown in utils.html only
runnableExamples:
assert 3*2 == 6
discard "should be in utils.html only, not in module that calls fromUtilsGen"
## ditto

iterator fromUtils1*(): int =
Expand All @@ -64,7 +64,11 @@ template fromUtilsGen*(): untyped =

template fromUtils2*() =
## ok3
runnableExamples:
discard """should be shown as examples for fromUtils2
in module calling fromUtilsGen"""

proc fromUtils3*() =
## came form utils but should be shown where `fromUtilsGen` is called
runnableExamples: discard 1
runnableExamples: discard """should be shown as examples for fromUtils3
in module calling fromUtilsGen"""
2 changes: 1 addition & 1 deletion tests/js/tstdlib_various.nim
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ block tsplit2:
var errored = false
try:
discard "hello".split("")
except AssertionError:
except AssertionDefect:
errored = true
doAssert errored

Expand Down
57 changes: 55 additions & 2 deletions tests/nimdoc/trunnableexamples.nim
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
discard """
cmd: "nim doc --doccmd:-d:testFooExternal --hints:off $file"
cmd: "nim doc --doccmd:--hints:off --hints:off $file"
action: "compile"
nimoutFull: true
nimout: '''
foo1
foo2
foo3
foo5
foo6
foo7
in examplesInTemplate1
doc in outer
doc in inner1
doc in inner2
foo8
foo9
'''
joinable: false
"""

#[
pending bug #18077, use instead:
cmd: "nim doc --doccmd:'-d:testFooExternal --hints:off' --hints:off $file"
and merge trunnableexamples2 back here
]#
{.define(testFooExternal).}

proc fun*() =
runnableExamples:
block: # `defer` only allowed inside a block
Expand Down Expand Up @@ -109,6 +121,47 @@ when true: # runnableExamples with rdoccmd
# passing seq (to run with multiple compilation options)
runnableExamples(@["-b:cpp", "-b:js"]): discard

when true: # bug #16993
template examplesInTemplate1*(cond: untyped) =
## in examplesInTemplate1
runnableExamples:
echo "in examplesInTemplate1"
discard
examplesInTemplate1 true
examplesInTemplate1 true
examplesInTemplate1 true

when true: # bug #18054
template outer*(body: untyped) =
## outer template doc string.
runnableExamples:
echo "doc in outer"
##
template inner1*() =
## inner1 template doc string.
runnableExamples:
echo "doc in inner1"
##

template inner2*() =
## inner2 template doc string.
runnableExamples:
echo "doc in inner2"
body
outer:
inner1()
inner2()

when true: # bug #17835
template anyItFake*(s, pred: untyped): bool =
## Foo
runnableExamples: discard
true

proc anyItFakeMain*(n: seq[int]): bool =
result = anyItFake(n, it == 0)
# this was giving: Error: runnableExamples must appear before the first non-comment statement

runnableExamples:
block: # bug #17279
when int.sizeof == 8:
Expand All @@ -118,7 +171,7 @@ runnableExamples:
# bug #13491
block:
proc fun(): int = doAssert false
doAssertRaises(AssertionError, (discard fun()))
doAssertRaises(AssertionDefect, (discard fun()))

block:
template foo(body) = discard
Expand Down
11 changes: 11 additions & 0 deletions tests/nimdoc/trunnableexamples2.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
discard """
cmd: "nim doc --doccmd:-d:testFooExternal --hints:off $file"
action: "compile"
joinable: false
"""

# pending bug #18077, merge back inside trunnableexamples.nim
when true: # runnableExamples with rdoccmd
runnableExamples "-d:testFoo -d:testBar":
doAssert defined(testFoo) and defined(testBar)
doAssert defined(testFooExternal)

0 comments on commit 0de3d42

Please sign in to comment.