testproject.nim 8.8 KB

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