trepr.nim 5.1 KB

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