tmarshal.nim 3.7 KB

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