Skip to content

Commit

Permalink
runnableExamples imports std/assertions by default (nim-lang#21658)
Browse files Browse the repository at this point in the history
  • Loading branch information
metagn authored and capocasa committed May 16, 2023
1 parent d4afe95 commit 9c0a549
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
4 changes: 3 additions & 1 deletion compiler/docgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,6 @@ proc prepareExample(d: PDoc; n: PNode, topLevel: bool): tuple[rdoccmd: string, c
let useRenderModule = false
let loc = d.conf.toFileLineCol(n.info)
let code = extractRunnableExamplesSource(d.conf, n)
let codeIndent = extractRunnableExamplesSource(d.conf, n, indent = 2)

if d.conf.errorCounter > 0:
return (rdoccmd, code)
Expand All @@ -619,6 +618,7 @@ proc prepareExample(d: PDoc; n: PNode, topLevel: bool): tuple[rdoccmd: string, c
docComment.comment = comment
var runnableExamples = newTree(nkStmtList,
docComment,
newTree(nkImportStmt, newStrNode(nkStrLit, "std/assertions")),
newTree(nkImportStmt, newStrNode(nkStrLit, d.filename)))
runnableExamples.info = n.info
for a in n.lastSon: runnableExamples.add a
Expand All @@ -632,6 +632,7 @@ proc prepareExample(d: PDoc; n: PNode, topLevel: bool): tuple[rdoccmd: string, c
else:
var code2 = code
if code.len > 0 and "codeReordering" notin code:
let codeIndent = extractRunnableExamplesSource(d.conf, n, indent = 2)
# hacky but simplest solution, until we devise a way to make `{.line.}`
# work without introducing a scope
code2 = """
Expand All @@ -642,6 +643,7 @@ $#
#[
$#
]#
import std/assertions
import $#
$#
""" % [comment, d.filename.quoted, code2]
Expand Down
4 changes: 4 additions & 0 deletions tests/nimdoc/trunnableexamples.nim
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ runnableExamples:
proc fun*()=echo "foo9"
fun()

# import std/assertions by default
runnableExamples("-d:nimPreviewSlimSystem"):
doAssert true

# note: there are yet other examples where putting runnableExamples at module
# scope is needed, for example when using an `include` before an `import`, etc.

Expand Down

0 comments on commit 9c0a549

Please sign in to comment.