tmarshal.nim 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. discard """
  2. matrix: "--mm:orc"
  3. """
  4. # TODO: --mm:refc
  5. import std/marshal
  6. import std/[assertions, objectdollar, streams]
  7. # TODO: add static tests
  8. proc testit[T](x: T): string = $$to[T]($$x)
  9. template check1 =
  10. let test1: array[0..1, array[0..4, string]] = [
  11. ["test", "1", "2", "3", "4"], ["test", "1", "2", "3", "4"]]
  12. doAssert testit(test1) ==
  13. """[["test", "1", "2", "3", "4"], ["test", "1", "2", "3", "4"]]"""
  14. let test2: tuple[name: string, s: int] = ("tuple test", 56)
  15. doAssert testit(test2) == """{"Field0": "tuple test", "Field1": 56}"""
  16. static: check1()
  17. check1()
  18. type
  19. TE = enum
  20. blah, blah2
  21. TestObj = object
  22. test, asd: int
  23. case test2: TE
  24. of blah:
  25. help: string
  26. else:
  27. discard
  28. PNode = ref TNode
  29. TNode = object
  30. next, prev: PNode
  31. data: string
  32. proc buildList(): PNode =
  33. new(result)
  34. new(result.next)
  35. new(result.prev)
  36. result.data = "middle"
  37. result.next.data = "next"
  38. result.prev.data = "prev"
  39. result.next.next = result.prev
  40. result.next.prev = result
  41. result.prev.next = result
  42. result.prev.prev = result.next
  43. let test3 = TestObj(test: 42, test2: blah)
  44. doAssert testit(test3) ==
  45. """{"test": 42, "asd": 0, "test2": "blah", "help": ""}"""
  46. var test4: ref tuple[a, b: string]
  47. new(test4)
  48. test4.a = "ref string test: A"
  49. test4.b = "ref string test: B"
  50. discard testit(test4) # serialization uses the pointer address, which is not consistent
  51. let test5 = @[(0,1),(2,3),(4,5)]
  52. doAssert testit(test5) ==
  53. """[{"Field0": 0, "Field1": 1}, {"Field0": 2, "Field1": 3}, {"Field0": 4, "Field1": 5}]"""
  54. let test6: set[char] = {'A'..'Z', '_'}
  55. doAssert testit(test6) ==
  56. """[65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 95]"""
  57. let test7 = buildList()
  58. discard testit(test7) # serialization uses the pointer address, which is not consistent
  59. # bug #1352
  60. block:
  61. type
  62. Entity = object of RootObj
  63. name: string
  64. Person = object of Entity
  65. age: int
  66. bio: string
  67. blob: string
  68. let instance1 = Person(name: "Cletus", age: 12,
  69. bio: "Я Cletus",
  70. blob: "ABC\x80")
  71. doAssert $$instance1 == """{"age": 12, "bio": "Я Cletus", "blob": [65, 66, 67, 128], "name": "Cletus"}"""
  72. doAssert to[Person]($$instance1).bio == instance1.bio
  73. doAssert to[Person]($$instance1).blob == instance1.blob
  74. # bug #5757
  75. block:
  76. type
  77. Something = object
  78. x: string
  79. y: int
  80. let data1 = """{"x": "alpha", "y": 100}"""
  81. let data2 = """{"x": "omega", "y": 200}"""
  82. var r = to[Something](data1)
  83. doAssert $r.x & " " & $r.y == "alpha 100"
  84. r = to[Something](data2)
  85. doAssert $r.x & " " & $r.y == "omega 200"
  86. block:
  87. type
  88. Foo = object
  89. a1: string
  90. a2: string
  91. a3: seq[string]
  92. a4: seq[int]
  93. a5: seq[int]
  94. a6: seq[int]
  95. var foo = Foo(a2: "", a4: @[], a6: @[1])
  96. foo.a6.setLen 0
  97. doAssert $$foo == """{"a1": "", "a2": "", "a3": [], "a4": [], "a5": [], "a6": []}"""
  98. doAssert testit(foo) == """{"a1": "", "a2": "", "a3": [], "a4": [], "a5": [], "a6": []}"""
  99. import std/[options, json]
  100. # bug #15934
  101. block:
  102. let
  103. a1 = some(newJNull())
  104. a2 = none(JsonNode)
  105. doAssert $($$a1).to[:Option[JsonNode]] == "some(null)"
  106. doAssert $($$a2).to[:Option[JsonNode]] == "none(JsonNode)"
  107. doAssert ($$a1).to[:Option[JsonNode]] == some(newJNull())
  108. doAssert ($$a2).to[:Option[JsonNode]] == none(JsonNode)
  109. # bug #15620
  110. block:
  111. let str = """{"numeric": null}"""
  112. type
  113. LegacyEntry = object
  114. numeric: string
  115. let test = to[LegacyEntry](str)
  116. doAssert $test == """(numeric: "")"""
  117. block:
  118. let str = """{"numeric": null}"""
  119. type
  120. LegacyEntry = object
  121. numeric: seq[int]
  122. var test = to[LegacyEntry](str)
  123. doAssert $test == """(numeric: @[])"""
  124. # bug #16022
  125. block:
  126. let p: proc (): string = proc (): string = "hello world"
  127. let poc = to[typeof(p)]($$p)
  128. doAssert poc() == "hello world"
  129. block:
  130. type
  131. A {.inheritable.} = object
  132. B = object of A
  133. f: int
  134. let a: ref A = new(B)
  135. doAssert $$a[] == "{}" # not "{f: 0}"
  136. # bug #16496
  137. block:
  138. type
  139. A = ref object
  140. data: seq[int]
  141. B = ref object
  142. x: A
  143. let o = A(data: @[1, 2, 3, 4])
  144. let s1 = @[B(x: o), B(x: o)]
  145. let m = $$ s1
  146. let s2 = to[seq[B]](m)
  147. doAssert s2[0].x.data == s2[1].x.data
  148. doAssert s1[0].x.data == s2[1].x.data
  149. block:
  150. type
  151. Obj = ref object
  152. i: int
  153. b: bool
  154. let
  155. strm = newStringStream()
  156. var
  157. o = Obj(i: 1, b: false)
  158. t1 = @[o, o]
  159. t2: seq[Obj]
  160. doAssert t1[0] == t1[1]
  161. strm.store(t1)
  162. strm.setPosition(0)
  163. strm.load(t2)
  164. strm.close()
  165. doAssert t2[0] == t2[1]
  166. template checkMarshal(data: typed) =
  167. let orig = data
  168. let m = $$orig
  169. let old = to[typeof(orig)](m)
  170. doAssert data == old
  171. template main() =
  172. type
  173. Book = object
  174. page: int
  175. name: string
  176. let book = Book(page: 12, name: "persona")
  177. checkMarshal(486)
  178. checkMarshal(3.14)
  179. checkMarshal("azure sky")
  180. checkMarshal(book)
  181. checkMarshal([1, 2, 3])
  182. checkMarshal(@[1.5, 2.7, 3.9, 4.2])
  183. checkMarshal(@["dream", "is", "possible"])
  184. static: main()
  185. main()