From 9c0a549eea314fb23c7fa06c1e495d065d31358b Mon Sep 17 00:00:00 2001 From: metagn Date: Fri, 14 Apr 2023 13:39:23 +0300 Subject: [PATCH] runnableExamples imports std/assertions by default (#21658) closes https://github.com/nim-lang/RFCs/issues/499 --- compiler/docgen.nim | 4 +++- tests/nimdoc/trunnableexamples.nim | 4 ++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/compiler/docgen.nim b/compiler/docgen.nim index 4a8e38054093f..f592d8845b732 100644 --- a/compiler/docgen.nim +++ b/compiler/docgen.nim @@ -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) @@ -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 @@ -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 = """ @@ -642,6 +643,7 @@ $# #[ $# ]# +import std/assertions import $# $# """ % [comment, d.filename.quoted, code2] diff --git a/tests/nimdoc/trunnableexamples.nim b/tests/nimdoc/trunnableexamples.nim index 1188cdd1b7d92..1886ceeb3b3a2 100644 --- a/tests/nimdoc/trunnableexamples.nim +++ b/tests/nimdoc/trunnableexamples.nim @@ -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.