This commit is contained in:
parent
4ee6eddad4
commit
0de3d4292f
8 changed files with 108 additions and 46 deletions
|
|
@ -83,6 +83,10 @@ proc evalTemplateAux(templ, actual: PNode, c: var TemplCtx, result: PNode) =
|
||||||
not c.isDeclarative:
|
not c.isDeclarative:
|
||||||
c.isDeclarative = true
|
c.isDeclarative = true
|
||||||
isDeclarative = true
|
isDeclarative = true
|
||||||
|
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)
|
var res = copyNode(c, templ, actual)
|
||||||
for i in 0..<templ.len:
|
for i in 0..<templ.len:
|
||||||
evalTemplateAux(templ[i], actual, c, res)
|
evalTemplateAux(templ[i], actual, c, res)
|
||||||
|
|
|
||||||
|
|
@ -3,35 +3,6 @@
|
||||||
## **Note:** This module is reexported by `system` and thus does not need to be
|
## **Note:** This module is reexported by `system` and thus does not need to be
|
||||||
## imported directly (with `system/assertions`).
|
## 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):
|
when not declared(sysFatal):
|
||||||
include "system/fatal"
|
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`).
|
## 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>`_.
|
## 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"))
|
assertImpl(cond, msg, astToStr(cond), compileOption("assertions"))
|
||||||
|
|
||||||
template doAssert*(cond: untyped, msg = "") =
|
template doAssert*(cond: untyped, msg = "") =
|
||||||
## Similar to `assert <#assert.t,untyped,string>`_ but is always turned on regardless of `--assertions`.
|
## 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)
|
assertImpl(cond, msg, astToStr(cond), true)
|
||||||
|
|
||||||
template onFailedAssert*(msg, code: untyped): untyped {.dirty.} =
|
template onFailedAssert*(msg, code: untyped): untyped {.dirty.} =
|
||||||
## Sets an assertion failure handler that will intercept any assert
|
## Sets an assertion failure handler that will intercept any assert
|
||||||
## statements following `onFailedAssert` in the current scope.
|
## 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.} =
|
template failedAssertImpl(msgIMPL: string): untyped {.dirty.} =
|
||||||
let msg = msgIMPL
|
let msg = msgIMPL
|
||||||
code
|
code
|
||||||
|
|
||||||
template doAssertRaises*(exception: typedesc, code: untyped) =
|
template doAssertRaises*(exception: typedesc, code: untyped) =
|
||||||
## Raises `AssertionDefect` if specified `code` does not raise `exception`.
|
## 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
|
var wrong = false
|
||||||
const begin = "expected raising '" & astToStr(exception) & "', instead"
|
const begin = "expected raising '" & astToStr(exception) & "', instead"
|
||||||
const msgEnd = " by: " & astToStr(code)
|
const msgEnd = " by: " & astToStr(code)
|
||||||
|
|
|
||||||
|
|
@ -206,9 +206,9 @@ constructor.
|
||||||
<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>
|
<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>
|
<dd>
|
||||||
|
|
||||||
this should be shown in utils.html
|
should be shown in utils.html only
|
||||||
<p><strong class="examples_text">Example:</strong></p>
|
<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">"should be in utils.html only, not in module that calls fromUtilsGen"</span></pre>ditto
|
||||||
|
|
||||||
</dd>
|
</dd>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -431,10 +431,7 @@ window.addEventListener('DOMContentLoaded', main);
|
||||||
<span class="Keyword">discard</span> <span class="StringLit">"in top2"</span></pre>top2 after
|
<span class="Keyword">discard</span> <span class="StringLit">"in top2"</span></pre>top2 after
|
||||||
<p><strong class="examples_text">Example:</strong></p>
|
<p><strong class="examples_text">Example:</strong></p>
|
||||||
<pre class="listing"><span class="Keyword">import</span> <span class="Identifier">testproject</span>
|
<pre class="listing"><span class="Keyword">import</span> <span class="Identifier">testproject</span>
|
||||||
<span class="Keyword">discard</span> <span class="StringLit">"in top3"</span></pre>top3 after
|
<span class="Keyword">discard</span> <span class="StringLit">"in top3"</span></pre>top3 after</p>
|
||||||
<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>
|
|
||||||
<div class="section" id="6">
|
<div class="section" id="6">
|
||||||
<h1><a class="toc-backref" href="#6">Imports</a></h1>
|
<h1><a class="toc-backref" href="#6">Imports</a></h1>
|
||||||
<dl class="item">
|
<dl class="item">
|
||||||
|
|
@ -574,7 +571,8 @@ My someFunc. Stuff in <tt class="docutils literal"><span class="pre"><span class
|
||||||
|
|
||||||
came form utils but should be shown where <tt class="docutils literal"><span class="pre"><span class="Identifier">fromUtilsGen</span></span></tt> is called
|
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>
|
<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">"""should be shown as examples for fromUtils3
|
||||||
|
in module calling fromUtilsGen"""</span></pre>
|
||||||
|
|
||||||
</dd>
|
</dd>
|
||||||
<a id="isValid,T"></a>
|
<a id="isValid,T"></a>
|
||||||
|
|
@ -957,6 +955,9 @@ cz18
|
||||||
<dd>
|
<dd>
|
||||||
|
|
||||||
ok3
|
ok3
|
||||||
|
<p><strong class="examples_text">Example:</strong></p>
|
||||||
|
<pre class="listing"><span class="Keyword">discard</span> <span class="LongStringLit">"""should be shown as examples for fromUtils2
|
||||||
|
in module calling fromUtilsGen"""</span></pre>
|
||||||
|
|
||||||
</dd>
|
</dd>
|
||||||
<a id="z6t.t"></a>
|
<a id="z6t.t"></a>
|
||||||
|
|
|
||||||
|
|
@ -50,9 +50,9 @@ template bEnum*(): untyped =
|
||||||
discard
|
discard
|
||||||
|
|
||||||
template fromUtilsGen*(): untyped =
|
template fromUtilsGen*(): untyped =
|
||||||
## this should be shown in utils.html
|
## should be shown in utils.html only
|
||||||
runnableExamples:
|
runnableExamples:
|
||||||
assert 3*2 == 6
|
discard "should be in utils.html only, not in module that calls fromUtilsGen"
|
||||||
## ditto
|
## ditto
|
||||||
|
|
||||||
iterator fromUtils1*(): int =
|
iterator fromUtils1*(): int =
|
||||||
|
|
@ -64,7 +64,11 @@ template fromUtilsGen*(): untyped =
|
||||||
|
|
||||||
template fromUtils2*() =
|
template fromUtils2*() =
|
||||||
## ok3
|
## ok3
|
||||||
|
runnableExamples:
|
||||||
|
discard """should be shown as examples for fromUtils2
|
||||||
|
in module calling fromUtilsGen"""
|
||||||
|
|
||||||
proc fromUtils3*() =
|
proc fromUtils3*() =
|
||||||
## came form utils but should be shown where `fromUtilsGen` is called
|
## 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"""
|
||||||
|
|
|
||||||
|
|
@ -153,7 +153,7 @@ block tsplit2:
|
||||||
var errored = false
|
var errored = false
|
||||||
try:
|
try:
|
||||||
discard "hello".split("")
|
discard "hello".split("")
|
||||||
except AssertionError:
|
except AssertionDefect:
|
||||||
errored = true
|
errored = true
|
||||||
doAssert errored
|
doAssert errored
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
discard """
|
discard """
|
||||||
cmd: "nim doc --doccmd:-d:testFooExternal --hints:off $file"
|
cmd: "nim doc --doccmd:--hints:off --hints:off $file"
|
||||||
action: "compile"
|
action: "compile"
|
||||||
|
nimoutFull: true
|
||||||
nimout: '''
|
nimout: '''
|
||||||
foo1
|
foo1
|
||||||
foo2
|
foo2
|
||||||
|
|
@ -8,12 +9,23 @@ foo3
|
||||||
foo5
|
foo5
|
||||||
foo6
|
foo6
|
||||||
foo7
|
foo7
|
||||||
|
in examplesInTemplate1
|
||||||
|
doc in outer
|
||||||
|
doc in inner1
|
||||||
|
doc in inner2
|
||||||
foo8
|
foo8
|
||||||
foo9
|
foo9
|
||||||
'''
|
'''
|
||||||
joinable: false
|
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*() =
|
proc fun*() =
|
||||||
runnableExamples:
|
runnableExamples:
|
||||||
block: # `defer` only allowed inside a block
|
block: # `defer` only allowed inside a block
|
||||||
|
|
@ -109,6 +121,47 @@ when true: # runnableExamples with rdoccmd
|
||||||
# passing seq (to run with multiple compilation options)
|
# passing seq (to run with multiple compilation options)
|
||||||
runnableExamples(@["-b:cpp", "-b:js"]): discard
|
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:
|
runnableExamples:
|
||||||
block: # bug #17279
|
block: # bug #17279
|
||||||
when int.sizeof == 8:
|
when int.sizeof == 8:
|
||||||
|
|
@ -118,7 +171,7 @@ runnableExamples:
|
||||||
# bug #13491
|
# bug #13491
|
||||||
block:
|
block:
|
||||||
proc fun(): int = doAssert false
|
proc fun(): int = doAssert false
|
||||||
doAssertRaises(AssertionError, (discard fun()))
|
doAssertRaises(AssertionDefect, (discard fun()))
|
||||||
|
|
||||||
block:
|
block:
|
||||||
template foo(body) = discard
|
template foo(body) = discard
|
||||||
|
|
|
||||||
11
tests/nimdoc/trunnableexamples2.nim
Normal file
11
tests/nimdoc/trunnableexamples2.nim
Normal file
|
|
@ -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)
|
||||||
Loading…
Add table
Add a link
Reference in a new issue