trepr.nim 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. discard """
  2. targets: "c cpp js"
  3. matrix: ";--gc:arc"
  4. """
  5. # if excessive, could remove 'cpp' from targets
  6. from strutils import endsWith, contains, strip
  7. from std/macros import newLit
  8. macro deb(a): string = newLit a.repr.strip
  9. macro debTyped(a: typed): string = newLit a.repr.strip
  10. template main() =
  11. doAssert repr({3,5}) == "{3, 5}"
  12. block:
  13. type TEnum = enum a, b
  14. var val = {a, b}
  15. when nimvm:
  16. discard
  17. #[
  18. # BUG:
  19. {0, 1}
  20. {97..99, 65..67}
  21. ]#
  22. else:
  23. doAssert repr(val) == "{a, b}"
  24. doAssert repr({'a'..'c', 'A'..'C'}) == "{'A', 'B', 'C', 'a', 'b', 'c'}"
  25. type
  26. TObj {.pure, inheritable.} = object
  27. data: int
  28. TFoo = ref object of TObj
  29. d2: float
  30. var foo: TFoo
  31. new(foo)
  32. #[
  33. BUG:
  34. --gc:arc returns `"abc"`
  35. regular gc returns with address, e.g. 0x1068aae60"abc", but only
  36. for c,cpp backends (not js, vm)
  37. ]#
  38. block:
  39. doAssert repr("abc").endsWith "\"abc\""
  40. var b: cstring = "def"
  41. doAssert repr(b).endsWith "\"def\""
  42. block:
  43. var c = @[1,2]
  44. when nimvm:
  45. discard # BUG: this shows [1, 2] instead of @[1, 2]
  46. else:
  47. # BUG (already mentioned above): some backends / gc show address, others don't
  48. doAssert repr(c).endsWith "@[1, 2]"
  49. let d = @["foo", "bar"]
  50. let s = repr(d)
  51. # depending on backend/gc, we get 0x106a1c350@[0x106a1c390"foo", 0x106a1c3c0"bar"]
  52. doAssert "\"foo\"," in s
  53. var arr = [1, 2, 3]
  54. doAssert repr(arr) == "[1, 2, 3]"
  55. block: # bug #7878
  56. proc reprOpenarray(variable: var openarray[int]): string = repr(variable)
  57. when defined(js): discard # BUG: doesn't work
  58. else:
  59. doAssert reprOpenarray(arr) == "[1, 2, 3]"
  60. block: # bug #17292 repr with `do`
  61. template foo(a, b, c, d) = discard
  62. block:
  63. let a = deb:
  64. foo(1, 2, 3, 4)
  65. doAssert a == "foo(1, 2, 3, 4)"
  66. block:
  67. let a = deb:
  68. foo(1, 2, 3): 4
  69. doAssert a == """
  70. foo(1, 2, 3):
  71. 4"""
  72. block:
  73. let a = deb:
  74. foo(1, 2): 3
  75. do: 4
  76. doAssert a == """
  77. foo(1, 2):
  78. 3
  79. do:
  80. 4"""
  81. block:
  82. let a = deb:
  83. foo(1): 3
  84. do: 3
  85. do: 4
  86. doAssert a == """
  87. foo(1):
  88. 3
  89. do:
  90. 3
  91. do:
  92. 4"""
  93. block:
  94. let a = deb:
  95. foo(1):
  96. 3
  97. do:
  98. discard
  99. 3
  100. do:
  101. discard
  102. 4
  103. doAssert a == """
  104. foo(1):
  105. 3
  106. do:
  107. discard
  108. 3
  109. do:
  110. discard
  111. 4"""
  112. block:
  113. let a = deb:
  114. foo: 1
  115. do: 2
  116. do: 3
  117. do: 4
  118. doAssert a == """
  119. foo:
  120. 1
  121. do:
  122. 2
  123. do:
  124. 3
  125. do:
  126. 4"""
  127. block: # bug #17292 repr with `(discard)` (`discard` would result in illegal code)
  128. let a = deb:
  129. let f {.inject.} = () => (discard)
  130. doAssert a == """
  131. let f {.inject.} = () =>
  132. (discard )"""
  133. let a2 = deb:
  134. block:
  135. discard
  136. discard
  137. block:
  138. when true: discard
  139. # let a = b => discard # illegal
  140. discard b => (discard) # legal
  141. block:
  142. return
  143. doAssert a2 == """
  144. block:
  145. discard
  146. discard
  147. block:
  148. when true:
  149. discard
  150. discard b =>
  151. (discard )
  152. block:
  153. return"""
  154. block: # bug #17292 (bug 4)
  155. let a = deb:
  156. proc `=destroy`() = discard
  157. proc `'foo`(): int = discard
  158. proc `foo bar baz`(): int = discard
  159. let a2 = """
  160. proc `=destroy`() =
  161. discard
  162. proc `'foo`(): int =
  163. discard
  164. proc `foo bar baz`(): int =
  165. discard"""
  166. doAssert a2 == a
  167. block: # setters: `foo=`
  168. let a = deb:
  169. proc `foo=`() = discard
  170. doAssert a == """
  171. proc `foo=`() =
  172. discard"""
  173. block: # bug #14850
  174. block:
  175. let a = deb:
  176. template bar(): untyped =
  177. foo1:
  178. discard
  179. 4
  180. foo2(1):
  181. discard
  182. 4
  183. foo3(1):
  184. discard
  185. 4
  186. do: 1
  187. do: 2
  188. x.add foo4
  189. x.add: foo5: 3
  190. x.add foo6 do: 4
  191. a.add(foo7 do:
  192. echo "baz"
  193. 4)
  194. doAssert a == """
  195. template bar(): untyped =
  196. foo1:
  197. discard
  198. 4
  199. foo2(1):
  200. discard
  201. 4
  202. foo3(1):
  203. discard
  204. 4
  205. do:
  206. 1
  207. do:
  208. 2
  209. x.add foo4
  210. x.add:
  211. foo5:
  212. 3
  213. x.add foo6 do:
  214. 4
  215. a.add(foo7 do:
  216. echo "baz"
  217. 4)"""
  218. block: # one liner doc comments
  219. let a1 = deb:
  220. func fn1(): int = 1 ## comment
  221. func fn2(): int = 1
  222. ## comment
  223. let a2 = debTyped:
  224. func fn1(): int = 1 ## comment
  225. func fn2(): int = 1
  226. ## comment
  227. doAssert a1 == """
  228. func fn1(): int =
  229. ## comment
  230. 1
  231. func fn2(): int =
  232. ## comment
  233. 1"""
  234. doAssert a2 == """
  235. func fn1(): int =
  236. ## comment
  237. result = 1
  238. func fn2(): int =
  239. ## comment
  240. result = 1"""
  241. static: main()
  242. main()