tmarshal.nim 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. discard """
  2. output: '''{"age": 12, "bio": "Я Cletus", "blob": [65, 66, 67, 128], "name": "Cletus"}
  3. true
  4. true
  5. alpha 100
  6. omega 200
  7. '''
  8. joinable: false
  9. """
  10. #[
  11. joinable: false pending https://github.com/nim-lang/Nim/issues/9754
  12. ]#
  13. import marshal
  14. template testit(x) = discard $$to[type(x)]($$x)
  15. var x: array[0..4, array[0..4, string]] = [
  16. ["test", "1", "2", "3", "4"], ["test", "1", "2", "3", "4"],
  17. ["test", "1", "2", "3", "4"], ["test", "1", "2", "3", "4"],
  18. ["test", "1", "2", "3", "4"]]
  19. testit(x)
  20. var test2: tuple[name: string, s: int] = ("tuple test", 56)
  21. testit(test2)
  22. type
  23. TE = enum
  24. blah, blah2
  25. TestObj = object
  26. test, asd: int
  27. case test2: TE
  28. of blah:
  29. help: string
  30. else:
  31. discard
  32. PNode = ref TNode
  33. TNode = object
  34. next, prev: PNode
  35. data: string
  36. proc buildList(): PNode =
  37. new(result)
  38. new(result.next)
  39. new(result.prev)
  40. result.data = "middle"
  41. result.next.data = "next"
  42. result.prev.data = "prev"
  43. result.next.next = result.prev
  44. result.next.prev = result
  45. result.prev.next = result
  46. result.prev.prev = result.next
  47. var test3: TestObj
  48. test3.test = 42
  49. test3.test2 = blah
  50. testit(test3)
  51. var test4: ref tuple[a, b: string]
  52. new(test4)
  53. test4.a = "ref string test: A"
  54. test4.b = "ref string test: B"
  55. testit(test4)
  56. var test5 = @[(0,1),(2,3),(4,5)]
  57. testit(test5)
  58. var test7 = buildList()
  59. testit(test7)
  60. var test6: set[char] = {'A'..'Z', '_'}
  61. testit(test6)
  62. # bug #1352
  63. type
  64. Entity = object of RootObj
  65. name: string
  66. Person = object of Entity
  67. age: int
  68. bio: string
  69. blob: string
  70. var instance1 = Person(name: "Cletus", age: 12,
  71. bio: "Я Cletus",
  72. blob: "ABC\x80")
  73. echo($$instance1)
  74. echo(to[Person]($$instance1).bio == instance1.bio)
  75. echo(to[Person]($$instance1).blob == instance1.blob)
  76. # bug 5757
  77. type
  78. Something = object
  79. x: string
  80. y: int
  81. var data1 = """{"x": "alpha", "y": 100}"""
  82. var data2 = """{"x": "omega", "y": 200}"""
  83. var r = to[Something](data1)
  84. echo r.x, " ", r.y
  85. r = to[Something](data2)
  86. echo r.x, " ", r.y
  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. testit(foo)