trunnableexamples.nim 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. discard """
  2. cmd: '''nim doc --doccmd:"-d:testFooExternal --hints:off" --hints:off $file'''
  3. action: "compile"
  4. nimoutFull: true
  5. nimout: '''
  6. foo1
  7. foo2
  8. foo3
  9. foo5
  10. foo7
  11. in examplesInTemplate1
  12. doc in outer
  13. doc in inner1
  14. doc in inner2
  15. foo8
  16. foo9
  17. foo6
  18. '''
  19. joinable: false
  20. """
  21. proc fun*() =
  22. runnableExamples:
  23. block: # `defer` only allowed inside a block
  24. defer: echo "foo1"
  25. runnableExamples:
  26. # `fun*` only allowed at top level
  27. proc fun*()=echo "foo2"
  28. fun()
  29. block:
  30. defer: echo "foo3"
  31. runnableExamples:
  32. # ditto
  33. proc fun*()=echo "foo5"
  34. fun()
  35. runnableExamples("--experimental:codeReordering --warnings:off"):
  36. # `codeReordering` only allowed at top level
  37. {.experimental: "codeReordering".}
  38. proc fun1() = fun2()
  39. proc fun2() = echo "foo6"
  40. fun1()
  41. runnableExamples:
  42. # only works at top level
  43. import std/macros
  44. macro myImport(a: static string): untyped =
  45. newTree(nnkImportStmt, [newLit a])
  46. myImport "str" & "utils"
  47. doAssert declared(isAlphaAscii)
  48. echo "foo7"
  49. when true: # issue #12746
  50. # this proc on its own works fine with `nim doc`
  51. proc goodProc*() =
  52. runnableExamples:
  53. try:
  54. discard
  55. except CatchableError:
  56. # just the general except will work
  57. discard
  58. # FIXED: this proc fails with `nim doc`
  59. proc badProc*() =
  60. runnableExamples:
  61. try:
  62. discard
  63. except IOError:
  64. # specifying Error is culprit
  65. discard
  66. when true: # runnableExamples with rdoccmd
  67. runnableExamples "-d:testFoo -d:testBar":
  68. doAssert defined(testFoo) and defined(testBar)
  69. doAssert defined(testFooExternal)
  70. runnableExamples "-d:testFoo2":
  71. doAssert defined(testFoo2)
  72. doAssert not defined(testFoo) # doesn't get confused by other examples
  73. ## all these syntaxes work too
  74. runnableExamples("-d:testFoo2"): discard
  75. runnableExamples(): discard
  76. runnableExamples: discard
  77. runnableExamples "-r:off": # issue #10731
  78. doAssert false ## we compile only (-r:off), so this won't be run
  79. runnableExamples "-b:js":
  80. import std/compilesettings
  81. proc startsWith*(s, prefix: cstring): bool {.noSideEffect, importjs: "#.startsWith(#)".}
  82. doAssert querySetting(backend) == "js"
  83. runnableExamples "-b:cpp":
  84. static: doAssert defined(cpp)
  85. type std_exception {.importcpp: "std::exception", header: "<exception>".} = object
  86. proc fun2*() =
  87. runnableExamples "-d:foo": discard # checks that it also works inside procs
  88. template fun3Impl(): untyped =
  89. runnableExamples(rdoccmd="-d:foo"):
  90. nonexistent
  91. # bugfix: this shouldn't be semchecked when `runnableExamples`
  92. # has more than 1 argument
  93. discard
  94. proc fun3*[T]() =
  95. fun3Impl()
  96. when false: # future work
  97. # passing non-string-litterals (for reuse)
  98. const a = "-b:cpp"
  99. runnableExamples(a): discard
  100. # passing seq (to run with multiple compilation options)
  101. runnableExamples(@["-b:cpp", "-b:js"]): discard
  102. when true: # bug #16993
  103. template examplesInTemplate1*(cond: untyped) =
  104. ## in examplesInTemplate1
  105. runnableExamples:
  106. echo "in examplesInTemplate1"
  107. discard
  108. examplesInTemplate1 true
  109. examplesInTemplate1 true
  110. examplesInTemplate1 true
  111. when true: # bug #18054
  112. template outer*(body: untyped) =
  113. ## outer template doc string.
  114. runnableExamples:
  115. echo "doc in outer"
  116. ##
  117. template inner1*() =
  118. ## inner1 template doc string.
  119. runnableExamples:
  120. echo "doc in inner1"
  121. ##
  122. template inner2*() =
  123. ## inner2 template doc string.
  124. runnableExamples:
  125. echo "doc in inner2"
  126. body
  127. outer:
  128. inner1()
  129. inner2()
  130. when true: # bug #17835
  131. template anyItFake*(s, pred: untyped): bool =
  132. ## Foo
  133. runnableExamples: discard
  134. true
  135. proc anyItFakeMain*(n: seq[int]): bool =
  136. result = anyItFake(n, it == 0)
  137. # this was giving: Error: runnableExamples must appear before the first non-comment statement
  138. runnableExamples:
  139. block: # bug #17279
  140. when int.sizeof == 8:
  141. let x = 0xffffffffffffffff
  142. doAssert x == -1
  143. # bug #13491
  144. block:
  145. proc fun(): int = doAssert false
  146. doAssertRaises(AssertionDefect, (discard fun()))
  147. block:
  148. template foo(body) = discard
  149. foo (discard)
  150. block:
  151. template fn(body: untyped): untyped = true
  152. doAssert(fn do: nonexistent)
  153. import std/macros
  154. macro foo*(x, y) =
  155. result = newLetStmt(x[0][0], x[0][1])
  156. foo:
  157. a = 1
  158. do: discard
  159. # also check for runnableExamples at module scope
  160. runnableExamples:
  161. block:
  162. defer: echo "foo8"
  163. runnableExamples:
  164. proc fun*()=echo "foo9"
  165. fun()
  166. # import std/assertions by default
  167. runnableExamples("-d:nimPreviewSlimSystem"):
  168. doAssert true
  169. # note: there are yet other examples where putting runnableExamples at module
  170. # scope is needed, for example when using an `include` before an `import`, etc.
  171. ##[
  172. snippet:
  173. .. code-block:: Nim
  174. :test:
  175. doAssert defined(testFooExternal)
  176. ]##
  177. when true: # runnableExamples with rdoccmd
  178. runnableExamples "-d:testFoo -d:testBar":
  179. doAssert defined(testFoo) and defined(testBar)
  180. doAssert defined(testFooExternal)