trepr.nim 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. # xxx consider merging with `tests/stdlib/trepr.nim` to increase overall test coverage
  2. block ints:
  3. let
  4. na: int8 = -120'i8
  5. nb: int16 = -32700'i16
  6. nc: int32 = -2147483000'i32
  7. nd: int64 = -9223372036854775000'i64
  8. ne: int = -1234567
  9. pa: int8 = 120'i8
  10. pb: int16 = 32700'i16
  11. pc: int32 = 2147483000'i32
  12. pd: int64 = 9223372036854775000'i64
  13. pe: int = 1234567
  14. doAssert(repr(na) == "-120")
  15. doAssert(repr(nb) == "-32700")
  16. doAssert(repr(nc) == "-2147483000")
  17. doAssert(repr(nd) == "-9223372036854775000")
  18. doAssert(repr(ne) == "-1234567")
  19. doAssert(repr(pa) == "120")
  20. doAssert(repr(pb) == "32700")
  21. doAssert(repr(pc) == "2147483000")
  22. doAssert(repr(pd) == "9223372036854775000")
  23. doAssert(repr(pe) == "1234567")
  24. block uints:
  25. let
  26. a: uint8 = 254'u8
  27. b: uint16 = 65300'u16
  28. c: uint32 = 4294967290'u32
  29. # d: uint64 = 18446744073709551610'u64 -> unknown node type
  30. e: uint = 1234567
  31. doAssert(repr(a) == "254")
  32. doAssert(repr(b) == "65300")
  33. doAssert(repr(c) == "4294967290")
  34. # doAssert(repr(d) == "18446744073709551610")
  35. doAssert(repr(e) == "1234567")
  36. block floats:
  37. let
  38. a: float32 = 3.4e38'f32
  39. b: float64 = 1.7976931348623157e308'f64
  40. c: float = 1234.567e89
  41. when defined js:
  42. doAssert(repr(a) == "3.4e+38") # in C: 3.399999952144364e+038
  43. doAssert(repr(b) == "1.7976931348623157e+308") # in C: 1.797693134862316e+308
  44. doAssert(repr(c) == "1.234567e+92") # in C: 1.234567e+092
  45. block bools:
  46. let
  47. a: bool = true
  48. b: bool = false
  49. doAssert(repr(a) == "true")
  50. doAssert(repr(b) == "false")
  51. block enums:
  52. type
  53. AnEnum = enum
  54. aeA
  55. aeB
  56. aeC
  57. HoledEnum = enum
  58. heA = -12
  59. heB = 15
  60. heC = 123
  61. doAssert(repr(aeA) == "aeA")
  62. doAssert(repr(aeB) == "aeB")
  63. doAssert(repr(aeC) == "aeC")
  64. doAssert(repr(heA) == "heA")
  65. doAssert(repr(heB) == "heB")
  66. doAssert(repr(heC) == "heC")
  67. block emums_and_unicode: #6741
  68. type K = enum Kanji = "漢字"
  69. let kanji = Kanji
  70. doAssert(kanji == Kanji, "Enum values are not equal")
  71. doAssert($kanji == $Kanji, "Enum string values are not equal")
  72. block chars:
  73. let
  74. a = 'a'
  75. b = 'z'
  76. one = '1'
  77. nl = '\x0A'
  78. doAssert(repr(a) == "'a'")
  79. doAssert(repr(b) == "'z'")
  80. doAssert(repr(one) == "'1'")
  81. doAssert(repr(nl) == "'\\10'")
  82. block strings:
  83. let
  84. a: string = "12345"
  85. b: string = "hello,repr"
  86. c: string = "hi\nthere"
  87. when defined js: # C prepends the pointer, JS does not.
  88. doAssert(repr(a) == "\"12345\"")
  89. doAssert(repr(b) == "\"hello,repr\"")
  90. doAssert(repr(c) == "\"hi\\10there\"")
  91. block sets:
  92. let
  93. a: set[int16] = {1'i16, 2'i16, 3'i16}
  94. b: set[char] = {'A', 'k'}
  95. doAssert(repr(a) == "{1, 2, 3}")
  96. doAssert(repr(b) == "{'A', 'k'}")
  97. block ranges:
  98. let
  99. a: range[0..12] = 6
  100. b: range[-12..0] = -6
  101. doAssert(repr(a) == "6")
  102. doAssert(repr(b) == "-6")
  103. block tuples:
  104. type
  105. ATuple = tuple
  106. a: int
  107. b: float
  108. c: string
  109. d: OtherTuple
  110. OtherTuple = tuple
  111. a: bool
  112. b: int8
  113. let
  114. ot: OtherTuple = (a: true, b: 120'i8)
  115. t: ATuple = (a: 42, b: 12.34, c: "tuple", d: ot)
  116. when defined js:
  117. doAssert(repr(ot) == """
  118. [Field0 = true,
  119. Field1 = 120]""")
  120. doAssert(repr(t) == """
  121. [Field0 = 42,
  122. Field1 = 12.34,
  123. Field2 = "tuple",
  124. Field3 = [Field0 = true,
  125. Field1 = 120]]""")
  126. block objects:
  127. type
  128. AnObj = object
  129. a: int
  130. b: float
  131. c: OtherObj
  132. OtherObj = object
  133. a: bool
  134. b: int8
  135. let
  136. oo: OtherObj = OtherObj(a: true, b: 120'i8)
  137. o: AnObj = AnObj(a: 42, b: 12.34, c: oo)
  138. doAssert(repr(oo) == """
  139. [a = true,
  140. b = 120]""")
  141. doAssert(repr(o) == """
  142. [a = 42,
  143. b = 12.34,
  144. c = [a = true,
  145. b = 120]]""")
  146. block arrays:
  147. type
  148. AObj = object
  149. x: int
  150. y: array[3,float]
  151. let
  152. a = [0.0, 1, 2]
  153. b = [a, a, a]
  154. o = AObj(x: 42, y: a)
  155. c = [o, o, o]
  156. d = ["hi", "array", "!"]
  157. doAssert(repr(a) == "[0.0, 1.0, 2.0]")
  158. doAssert(repr(b) == "[[0.0, 1.0, 2.0], [0.0, 1.0, 2.0], [0.0, 1.0, 2.0]]")
  159. doAssert(repr(c) == """
  160. [[x = 42,
  161. y = [0.0, 1.0, 2.0]], [x = 42,
  162. y = [0.0, 1.0, 2.0]], [x = 42,
  163. y = [0.0, 1.0, 2.0]]]""")
  164. doAssert(repr(d) == "[\"hi\", \"array\", \"!\"]")
  165. block seqs:
  166. type
  167. AObj = object
  168. x: int
  169. y: seq[float]
  170. let
  171. a = @[0.0, 1, 2]
  172. b = @[a, a, a]
  173. o = AObj(x: 42, y: a)
  174. c = @[o, o, o]
  175. d = @["hi", "array", "!"]
  176. doAssert(repr(a) == "@[0.0, 1.0, 2.0]")
  177. doAssert(repr(b) == "@[@[0.0, 1.0, 2.0], @[0.0, 1.0, 2.0], @[0.0, 1.0, 2.0]]")
  178. doAssert(repr(c) == """
  179. @[[x = 42,
  180. y = @[0.0, 1.0, 2.0]], [x = 42,
  181. y = @[0.0, 1.0, 2.0]], [x = 42,
  182. y = @[0.0, 1.0, 2.0]]]""")
  183. doAssert(repr(d) == "@[\"hi\", \"array\", \"!\"]")
  184. block ptrs:
  185. type
  186. AObj = object
  187. x: ptr array[2, AObj]
  188. y: int
  189. var
  190. a = [12.0, 13.0, 14.0]
  191. b = addr a[0]
  192. c = addr a[2]
  193. d = AObj()
  194. doAssert(repr(a) == "[12.0, 13.0, 14.0]")
  195. doAssert(repr(b) == "ref 0 --> 12.0")
  196. doAssert(repr(c) == "ref 2 --> 14.0")
  197. doAssert(repr(d) == """
  198. [x = nil,
  199. y = 0]""")
  200. block ptrs:
  201. type
  202. AObj = object
  203. x: ref array[2, AObj]
  204. y: int
  205. var
  206. a = AObj()
  207. new(a.x)
  208. doAssert(repr(a) == """
  209. [x = ref 0 --> [[x = nil,
  210. y = 0], [x = nil,
  211. y = 0]],
  212. y = 0]""")
  213. block procs:
  214. proc test(): int =
  215. echo "hello"
  216. var
  217. ptest = test
  218. nilproc: proc(): int
  219. doAssert(repr(test) == "0")
  220. doAssert(repr(ptest) == "0")
  221. doAssert(repr(nilproc) == "nil")
  222. block bunch:
  223. type
  224. AnEnum = enum
  225. eA, eB, eC
  226. B = object
  227. a: string
  228. b: seq[char]
  229. A = object
  230. a: uint32
  231. b: int
  232. c: float
  233. d: char
  234. e: AnEnum
  235. f: string
  236. g: set[char]
  237. h: set[int16]
  238. i: array[3,string]
  239. j: seq[string]
  240. k: range[-12..12]
  241. l: B
  242. m: ref B
  243. n: ptr B
  244. o: tuple[x: B, y: string]
  245. p: proc(b: B): ref B
  246. q: cstring
  247. proc refB(b:B):ref B =
  248. new result
  249. result[] = b
  250. var
  251. aa: A
  252. bb: B = B(a: "inner", b: @['o', 'b', 'j'])
  253. cc: A = A(a: 12, b: 1, c: 1.2, d: '\0', e: eC,
  254. f: "hello", g: {'A'}, h: {2'i16},
  255. i: ["hello", "world", "array"],
  256. j: @["hello", "world", "seq"], k: -1,
  257. l: bb, m: refB(bb), n: addr bb,
  258. o: (bb, "tuple!"), p: refB, q: "cstringtest" )
  259. doAssert(repr(aa) == """
  260. [a = 0,
  261. b = 0,
  262. c = 0.0,
  263. d = '\0',
  264. e = eA,
  265. f = "",
  266. g = {},
  267. h = {},
  268. i = ["", "", ""],
  269. j = @[],
  270. k = 0,
  271. l = [a = "",
  272. b = @[]],
  273. m = nil,
  274. n = nil,
  275. o = [Field0 = [a = "",
  276. b = @[]],
  277. Field1 = ""],
  278. p = nil,
  279. q = nil]""")
  280. doAssert(repr(cc) == """
  281. [a = 12,
  282. b = 1,
  283. c = 1.2,
  284. d = '\0',
  285. e = eC,
  286. f = "hello",
  287. g = {'A'},
  288. h = {2},
  289. i = ["hello", "world", "array"],
  290. j = @["hello", "world", "seq"],
  291. k = -1,
  292. l = [a = "inner",
  293. b = @['o', 'b', 'j']],
  294. m = ref 0 --> [a = "inner",
  295. b = @['o', 'b', 'j']],
  296. n = ref 0 --> [a = "inner",
  297. b = @['o', 'b', 'j']],
  298. o = [Field0 = [a = "inner",
  299. b = @['o', 'b', 'j']],
  300. Field1 = "tuple!"],
  301. p = 0,
  302. q = "cstringtest"]""")
  303. block another:
  304. type
  305. Size1 = enum
  306. s1a, s1b
  307. Size2 = enum
  308. s2c=0, s2d=20000
  309. Size3 = enum
  310. s3e=0, s3f=2000000000
  311. doAssert(repr([s1a, s1b]) == "[s1a, s1b]")
  312. doAssert(repr([s2c, s2d]) == "[s2c, s2d]")
  313. doAssert(repr([s3e, s3f]) == "[s3e, s3f]")
  314. block another2:
  315. type
  316. AnEnum = enum
  317. en1, en2, en3, en4, en5, en6
  318. Point {.final.} = object
  319. x, y, z: int
  320. s: array[0..1, string]
  321. e: AnEnum
  322. var
  323. p: Point
  324. q: ref Point
  325. s: seq[ref Point]
  326. p.x = 0
  327. p.y = 13
  328. p.z = 45
  329. p.s[0] = "abc"
  330. p.s[1] = "xyz"
  331. p.e = en6
  332. new(q)
  333. q[] = p
  334. s = @[q, q, q, q]
  335. doAssert(repr(p) == """
  336. [x = 0,
  337. y = 13,
  338. z = 45,
  339. s = ["abc", "xyz"],
  340. e = en6]""")
  341. doAssert(repr(q) == """
  342. ref 0 --> [x = 0,
  343. y = 13,
  344. z = 45,
  345. s = ["abc", "xyz"],
  346. e = en6]""")
  347. doAssert(repr(s) == """
  348. @[ref 0 --> [x = 0,
  349. y = 13,
  350. z = 45,
  351. s = ["abc", "xyz"],
  352. e = en6], ref 1 --> [x = 0,
  353. y = 13,
  354. z = 45,
  355. s = ["abc", "xyz"],
  356. e = en6], ref 2 --> [x = 0,
  357. y = 13,
  358. z = 45,
  359. s = ["abc", "xyz"],
  360. e = en6], ref 3 --> [x = 0,
  361. y = 13,
  362. z = 45,
  363. s = ["abc", "xyz"],
  364. e = en6]]""")
  365. doAssert(repr(en4) == "en4")
  366. doAssert(repr({'a'..'p'}) == "{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p'}")