Skip to content

Commit

Permalink
runnableExamples: compile and test them as a single file; fixes #9300
Browse files Browse the repository at this point in the history
  • Loading branch information
Araq authored and narimiran committed Jan 9, 2019
1 parent ecdcded commit 5fc78d8
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions compiler/docgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type
# already. See bug #3655
destFile*: AbsoluteFile
thisDir*: AbsoluteDir
examples: string

PDoc* = ref TDocumentor ## Alias to type less.

Expand Down Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 5fc78d8

Please sign in to comment.