runnableExamples: compile and test them as a single file; fixes #9300

This commit is contained in:
Araq
2019-01-04 09:52:01 +01:00
committed by narimiran
parent ecdcded294
commit 5fc78d86b7

View File

@@ -40,6 +40,7 @@ type
# already. See bug #3655
destFile*: AbsoluteFile
thisDir*: AbsoluteDir
examples: string
PDoc* = ref TDocumentor ## Alias to type less.
@@ -357,6 +358,13 @@ proc testExample(d: PDoc; ex: PNode) =
"_examples" & $d.exampleCounter & ".nim"))
#let nimcache = outp.changeFileExt"" & "_nimcache"
renderModule(ex, d.filename, outp.string, conf = d.conf)
d.examples.add "import r\"" & outp.string & "\"\n"
proc runAllExamples(d: PDoc) =
let outputDir = d.conf.getNimcacheDir / RelativeDir"runnableExamples"
let outp = outputDir / RelativeFile(extractFilename(d.filename.changeFileExt"" &
"_examples.nim"))
writeFile(outp, d.examples)
let backend = if isDefined(d.conf, "js"): "js"
elif isDefined(d.conf, "cpp"): "cpp"
elif isDefined(d.conf, "objc"): "objc"
@@ -379,6 +387,9 @@ proc extractImports(n: PNode; result: PNode) =
for i in 0..<n.safeLen: extractImports(n[i], result)
proc prepareExamples(d: PDoc; n: PNode) =
var docComment = newTree(nkCommentStmt)
let loc = d.conf.toFileLineCol(n.info)
docComment.comment = "autogenerated by docgen from " & loc
var runnableExamples = newTree(nkStmtList,
newTree(nkImportStmt, newStrNode(nkStrLit, d.filename)))
runnableExamples.info = n.info
@@ -913,6 +924,7 @@ proc generateIndex*(d: PDoc) =
writeIndexFile(d[], dest.string)
proc writeOutput*(d: PDoc, useWarning = false) =
runAllExamples(d)
var content = genOutFile(d)
if optStdout in d.conf.globalOptions:
writeRope(stdout, content)
@@ -925,6 +937,7 @@ proc writeOutput*(d: PDoc, useWarning = false) =
outfile.string)
proc writeOutputJson*(d: PDoc, useWarning = false) =
runAllExamples(d)
var modDesc: string
for desc in d.modDesc:
modDesc &= desc