testproject.nim 8.3 KB

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