tmarshal.nim 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. """
  9. import marshal
  10. template testit(x) = discard $$to[type(x)]($$x)
  11. var x: array[0..4, array[0..4, string]] = [
  12. ["test", "1", "2", "3", "4"], ["test", "1", "2", "3", "4"],
  13. ["test", "1", "2", "3", "4"], ["test", "1", "2", "3", "4"],
  14. ["test", "1", "2", "3", "4"]]
  15. testit(x)
  16. var test2: tuple[name: string, s: int] = ("tuple test", 56)
  17. testit(test2)
  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. var test3: TestObj
  44. test3.test = 42
  45. test3.test2 = blah
  46. testit(test3)
  47. var test4: ref tuple[a, b: string]
  48. new(test4)
  49. test4.a = "ref string test: A"
  50. test4.b = "ref string test: B"
  51. testit(test4)
  52. var test5 = @[(0,1),(2,3),(4,5)]
  53. testit(test5)
  54. var test7 = buildList()
  55. testit(test7)
  56. var test6: set[char] = {'A'..'Z', '_'}
  57. testit(test6)
  58. # bug #1352
  59. type
  60. Entity = object of RootObj
  61. name: string
  62. Person = object of Entity
  63. age: int
  64. bio: string
  65. blob: string
  66. var instance1 = Person(name: "Cletus", age: 12,
  67. bio: "Я Cletus",
  68. blob: "ABC\x80")
  69. echo($$instance1)
  70. echo(to[Person]($$instance1).bio == instance1.bio)
  71. echo(to[Person]($$instance1).blob == instance1.blob)
  72. # bug 5757
  73. type
  74. Something = object
  75. x: string
  76. y: int
  77. var data1 = """{"x": "alpha", "y": 100}"""
  78. var data2 = """{"x": "omega", "y": 200}"""
  79. var r = to[Something](data1)
  80. echo r.x, " ", r.y
  81. r = to[Something](data2)
  82. echo r.x, " ", r.y
  83. type
  84. Foo = object
  85. a1: string
  86. a2: string
  87. a3: seq[string]
  88. a4: seq[int]
  89. a5: seq[int]
  90. a6: seq[int]
  91. var foo = Foo(a2: "", a4: @[], a6: @[1])
  92. foo.a6.setLen 0
  93. doAssert $$foo == """{"a1": "", "a2": "", "a3": [], "a4": [], "a5": [], "a6": []}"""
  94. testit(foo)