summary refs log tree commit diff stats
path: root/compiler/docgen.nim
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2019-01-04 09:52:01 +0100
committerAraq <rumpf_a@web.de>2019-01-04 09:52:22 +0100
commit086229459aa72f06d31a4f920653a82a74cfc6b0 (patch)
tree64d3e471acf69a7ae3df524de4b5a15e39da946c /compiler/docgen.nim
parent77166ba795b03d4536f8efb0f225ba3ffc8531be (diff)
downloadNim-086229459aa72f06d31a4f920653a82a74cfc6b0.tar.gz
runnableExamples: compile and test them as a single file; fixes #9300
Diffstat (limited to 'compiler/docgen.nim')
-rw-r--r--compiler/docgen.nim12
1 files changed, 10 insertions, 2 deletions
diff --git a/compiler/docgen.nim b/compiler/docgen.nim
index 67f4108e1..0fe19c5bc 100644
--- a/compiler/docgen.nim
+++ b/compiler/docgen.nim
@@ -40,6 +40,7 @@ type
                     # already. See bug #3655
     destFile*: AbsoluteFile
     thisDir*: AbsoluteDir
+    examples: string
 
   PDoc* = ref TDocumentor ## Alias to type less.
 
@@ -378,6 +379,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"
@@ -400,11 +408,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,
       docComment,
       newTree(nkImportStmt, newStrNode(nkStrLit, d.filename)))
@@ -935,6 +941,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)
@@ -947,6 +954,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