tmarshal.nim 4.4 KB

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