testproject.nim 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. import subdir / subdir_b / utils
  2. ## This is the top level module.
  3. runnableExamples:
  4. import subdir / subdir_b / utils
  5. doAssert bar(3, 4) == 7
  6. foo(enumValueA, enumValueB)
  7. # bug #11078
  8. for x in "xx": discard
  9. var someVariable*: bool ## This should be visible.
  10. when true:
  11. ## top2
  12. runnableExamples:
  13. discard "in top2"
  14. ## top2 after
  15. runnableExamples:
  16. discard "in top3"
  17. ## top3 after
  18. const
  19. C_A* = 0x7FF0000000000000'f64
  20. C_B* = 0o377'i8
  21. C_C* = 0o277'i8
  22. C_D* = 0o177777'i16
  23. proc bar*[T](a, b: T): T =
  24. result = a + b
  25. proc baz*[T](a, b: T): T {.deprecated.} =
  26. ## This is deprecated without message.
  27. result = a + b
  28. proc buzz*[T](a, b: T): T {.deprecated: "since v0.20".} =
  29. ## This is deprecated with a message.
  30. result = a + b
  31. type
  32. FooBuzz* {.deprecated: "FooBuzz msg".} = int
  33. import std/macros
  34. var aVariable*: array[1, int]
  35. # bug #9432
  36. aEnum()
  37. bEnum()
  38. fromUtilsGen()
  39. proc isValid*[T](x: T): bool = x.len > 0
  40. when true:
  41. # these cases appear redundant but they're actually (almost) all different at
  42. # AST level and needed to ensure docgen keeps working, e.g. because of issues
  43. # like D20200526T163511
  44. type
  45. Foo* = enum
  46. enumValueA2
  47. proc z1*(): Foo =
  48. ## cz1
  49. Foo.default
  50. proc z2*() =
  51. ## cz2
  52. runnableExamples:
  53. discard "in cz2"
  54. proc z3*() =
  55. ## cz3
  56. proc z4*() =
  57. ## cz4
  58. discard
  59. when true:
  60. # tests for D20200526T163511
  61. proc z5*(): int =
  62. ## cz5
  63. return 1
  64. proc z6*(): int =
  65. ## cz6
  66. 1
  67. template z6t*(): int =
  68. ## cz6t
  69. 1
  70. proc z7*(): int =
  71. ## cz7
  72. result = 1
  73. proc z8*(): int =
  74. ## cz8
  75. block:
  76. discard
  77. 1+1
  78. when true:
  79. # interleaving 0 or more runnableExamples and doc comments, issue #9227
  80. proc z9*() =
  81. runnableExamples: doAssert 1 + 1 == 2
  82. proc z10*() =
  83. runnableExamples "-d:foobar":
  84. discard 1
  85. ## cz10
  86. proc z11*() =
  87. runnableExamples:
  88. discard 1
  89. discard
  90. proc z12*(): int =
  91. runnableExamples:
  92. discard 1
  93. 12
  94. proc z13*() =
  95. ## cz13
  96. runnableExamples:
  97. discard
  98. proc baz*() = discard
  99. proc bazNonExported() =
  100. ## out (not exported)
  101. runnableExamples:
  102. # BUG: this currently this won't be run since not exported
  103. # but probably should
  104. doAssert false
  105. if false: bazNonExported() # silence XDeclaredButNotUsed
  106. proc z17*() =
  107. # BUG: a comment before 1st doc comment currently doesn't prevent
  108. # doc comment from being docgen'd; probably should be fixed
  109. ## cz17
  110. ## rest
  111. runnableExamples:
  112. discard 1
  113. ## rest
  114. # this comment separates docgen'd doc comments
  115. ## out
  116. when true: # capture non-doc comments correctly even before 1st token
  117. proc p1*() =
  118. ## cp1
  119. runnableExamples: doAssert 1 == 1 # regular comments work here
  120. ## c4
  121. runnableExamples:
  122. # c5 regular comments before 1st token work
  123. # regular comment
  124. #[
  125. nested regular comment
  126. ]#
  127. doAssert 2 == 2 # c8
  128. ## this is a non-nested doc comment
  129. ##[
  130. this is a nested doc comment
  131. ]##
  132. discard "c9"
  133. # also work after
  134. # this should be out
  135. when true: # issue #14485
  136. proc addfBug14485*() =
  137. ## Some proc
  138. runnableExamples:
  139. discard "foo() = " & $[1]
  140. #[
  141. 0: let's also add some broken html to make sure this won't break in future
  142. 1: </span>
  143. 2: </span>
  144. 3: </span
  145. 4: </script>
  146. 5: </script
  147. 6: </script
  148. 7: end of broken html
  149. ]#
  150. when true: # procs without `=` (using comment field)
  151. proc c_printf*(frmt: cstring): cint {.importc: "printf", header: "<stdio.h>", varargs, discardable.}
  152. ## the c printf.
  153. ## etc.
  154. proc c_nonexistent*(frmt: cstring): cint {.importc: "nonexistent", header: "<stdio.h>", varargs, discardable.}
  155. when true: # tests RST inside comments
  156. proc low*[T: Ordinal|enum|range](x: T): T {.magic: "Low", noSideEffect.}
  157. ## Returns the lowest possible value of an ordinal value `x`. As a special
  158. ## semantic rule, `x` may also be a type identifier.
  159. ##
  160. ## See also:
  161. ## * `low2(T) <#low2,T>`_
  162. ##
  163. ## .. code-block:: Nim
  164. ## low(2) # => -9223372036854775808
  165. proc low2*[T: Ordinal|enum|range](x: T): T {.magic: "Low", noSideEffect.} =
  166. ## Returns the lowest possible value of an ordinal value `x`. As a special
  167. ## semantic rule, `x` may also be a type identifier.
  168. ##
  169. ## See also:
  170. ## * `low(T) <#low,T>`_
  171. ##
  172. ## .. code-block:: Nim
  173. ## low2(2) # => -9223372036854775808
  174. runnableExamples:
  175. discard "in low2"
  176. when true: # multiline string litterals
  177. proc tripleStrLitTest*() =
  178. runnableExamples("--hint:XDeclaredButNotUsed:off"):
  179. ## mullitline string litterals are tricky as their indentation can span
  180. ## below that of the runnableExamples
  181. let s1a = """
  182. should appear at indent 0
  183. at indent 2
  184. at indent 0
  185. """
  186. # make sure this works too
  187. let s1b = """start at same line
  188. at indent 2
  189. at indent 0
  190. """ # comment after
  191. let s2 = """sandwich """
  192. let s3 = """"""
  193. when false:
  194. let s5 = """
  195. in s5 """
  196. let s3b = ["""
  197. %!? #[...] # inside a multiline ...
  198. """, "foo"]
  199. ## make sure handles trailing spaces
  200. let s4 = """
  201. """
  202. let s5 = """ x
  203. """
  204. let s6 = """ ""
  205. """
  206. let s7 = """"""""""
  207. let s8 = ["""""""""", """
  208. """ ]
  209. discard
  210. # should be in
  211. # should be out
  212. when true: # methods; issue #14691
  213. type Moo = object
  214. method method1*(self: Moo) {.base.} =
  215. ## foo1
  216. method method2*(self: Moo): int {.base.} =
  217. ## foo2
  218. result = 1
  219. method method3*(self: Moo): int {.base.} =
  220. ## foo3
  221. 1
  222. when true: # iterators
  223. iterator iter1*(n: int): int =
  224. ## foo1
  225. for i in 0..<n:
  226. yield i
  227. iterator iter2*(n: int): int =
  228. ## foo2
  229. runnableExamples:
  230. discard # bar
  231. yield 0
  232. when true: # (most) macros
  233. macro bar*(): untyped =
  234. result = newStmtList()
  235. macro z16*() =
  236. runnableExamples: discard 1
  237. ## cz16
  238. ## after
  239. runnableExamples:
  240. doAssert 2 == 1 + 1
  241. # BUG: we should probably render `cz16\nafter` by keeping newline instead or
  242. # what it currently renders as: `cz16 after`
  243. macro z18*(): int =
  244. ## cz18
  245. newLit 0
  246. when true: # (most) templates
  247. template foo*(a, b: SomeType) =
  248. ## This does nothing
  249. ##
  250. discard
  251. template myfn*() =
  252. runnableExamples:
  253. import std/strutils
  254. ## issue #8871 preserve formatting
  255. ## line doc comment
  256. # bar
  257. doAssert "'foo" == "'foo"
  258. ##[
  259. foo
  260. bar
  261. ]##
  262. doAssert: not "foo".startsWith "ba"
  263. block:
  264. discard 0xff # elu par cette crapule
  265. # should be in
  266. ## should be still in
  267. # out
  268. ## out
  269. template z14*() =
  270. ## cz14
  271. runnableExamples:
  272. discard
  273. template z15*() =
  274. ## cz15
  275. runnableExamples:
  276. discard
  277. runnableExamples: discard 3
  278. runnableExamples: discard 4
  279. ## ok5
  280. ## ok5b
  281. runnableExamples: assert true
  282. runnableExamples: discard 1
  283. ## in or out?
  284. discard 8
  285. ## out
  286. when true: # issue #14473
  287. import std/[sequtils]
  288. template doit(): untyped =
  289. ## doit
  290. ## return output only
  291. toSeq(["D20210427T172228"]) # make it searcheable at least until we figure out a way to avoid echo
  292. echo doit() # using doAssert or similar to avoid echo would "hide" the original bug
  293. when true: # issue #14846
  294. import asyncdispatch
  295. proc asyncFun1*(): Future[int] {.async.} =
  296. ## ok1
  297. result = 1
  298. proc asyncFun2*() {.async.} = discard
  299. proc asyncFun3*() {.async.} =
  300. runnableExamples:
  301. discard
  302. ## ok1
  303. discard
  304. ## should be out
  305. discard
  306. when true:
  307. template testNimDocTrailingExample*() =
  308. # this must be last entry in this file, it checks against a bug (that got fixed)
  309. # where runnableExamples would not show if there was not at least 2 "\n" after
  310. # the last character of runnableExamples
  311. runnableExamples:
  312. discard 2
  313. when true: # issue #15702
  314. type
  315. Shapes* = enum
  316. ## Some shapes.
  317. Circle, ## A circle
  318. Triangle, ## A three-sided shape
  319. Rectangle ## A four-sided shape
  320. when true: # issue #15184
  321. proc anything* =
  322. ##
  323. ## There is no block quote after blank lines at the beginning.
  324. discard