tnewlit.nim 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import macros
  2. type
  3. MyType = object
  4. a : int
  5. b : string
  6. RefObject = ref object
  7. x: int
  8. RegularObject = object
  9. x: int
  10. ObjectRefAlias = ref RegularObject
  11. macro test_newLit_MyType: untyped =
  12. let mt = MyType(a: 123, b:"foobar")
  13. result = newLit(mt)
  14. doAssert test_newLit_MyType == MyType(a: 123, b:"foobar")
  15. macro test_newLit_array: untyped =
  16. let arr = [1,2,3,4,5]
  17. result = newLit(arr)
  18. doAssert test_newLit_array == [1,2,3,4,5]
  19. macro test_newLit_seq_int: untyped =
  20. let s: seq[int] = @[1,2,3,4,5]
  21. result = newLit(s)
  22. block:
  23. let tmp: seq[int] = test_newLit_seq_int
  24. doAssert tmp == @[1,2,3,4,5]
  25. macro test_newLit_seq_int8: untyped =
  26. let s: seq[int8] = @[1'i8,2,3,4,5]
  27. result = newLit(s)
  28. block:
  29. let tmp: seq[int8] = test_newLit_seq_int8
  30. doAssert tmp == @[1'i8,2,3,4,5]
  31. macro test_newLit_seq_int16: untyped =
  32. let s: seq[int16] = @[1'i16,2,3,4,5]
  33. result = newLit(s)
  34. block:
  35. let tmp: seq[int16] = test_newLit_seq_int16
  36. doAssert tmp == @[1'i16,2,3,4,5]
  37. macro test_newLit_seq_int32: untyped =
  38. let s: seq[int32] = @[1'i32,2,3,4,5]
  39. result = newLit(s)
  40. block:
  41. let tmp: seq[int32] = test_newLit_seq_int32
  42. doAssert tmp == @[1'i32,2,3,4,5]
  43. macro test_newLit_seq_int64: untyped =
  44. let s: seq[int64] = @[1'i64,2,3,4,5]
  45. result = newLit(s)
  46. block:
  47. let tmp: seq[int64] = test_newLit_seq_int64
  48. doAssert tmp == @[1'i64,2,3,4,5]
  49. macro test_newLit_seq_uint: untyped =
  50. let s: seq[uint] = @[1u,2,3,4,5]
  51. result = newLit(s)
  52. block:
  53. let tmp: seq[uint] = test_newLit_seq_uint
  54. doAssert tmp == @[1u,2,3,4,5]
  55. macro test_newLit_seq_uint8: untyped =
  56. let s: seq[uint8] = @[1'u8,2,3,4,5]
  57. result = newLit(s)
  58. block:
  59. let tmp: seq[uint8] = test_newLit_seq_uint8
  60. doAssert tmp == @[1'u8,2,3,4,5]
  61. macro test_newLit_seq_uint16: untyped =
  62. let s: seq[uint16] = @[1'u16,2,3,4,5]
  63. result = newLit(s)
  64. block:
  65. let tmp: seq[uint16] = test_newLit_seq_uint16
  66. doAssert tmp == @[1'u16,2,3,4,5]
  67. macro test_newLit_seq_uint32: untyped =
  68. let s: seq[uint32] = @[1'u32,2,3,4,5]
  69. result = newLit(s)
  70. block:
  71. let tmp: seq[uint32] = test_newLit_seq_uint32
  72. doAssert tmp == @[1'u32,2,3,4,5]
  73. macro test_newLit_seq_uint64: untyped =
  74. let s: seq[uint64] = @[1'u64,2,3,4,5]
  75. result = newLit(s)
  76. block:
  77. let tmp: seq[uint64] = test_newLit_seq_uint64
  78. doAssert tmp == @[1'u64,2,3,4,5]
  79. macro test_newLit_seq_float: untyped =
  80. let s: seq[float] = @[1.0, 2,3,4,5]
  81. result = newLit(s)
  82. block:
  83. let tmp: seq[float] = test_newLit_seq_float
  84. doAssert tmp == @[1.0, 2,3,4,5]
  85. macro test_newLit_seq_float32: untyped =
  86. let s: seq[float32] = @[1.0'f32, 2,3,4,5]
  87. result = newLit(s)
  88. block:
  89. let tmp: seq[float32] = test_newLit_seq_float32
  90. doAssert tmp == @[1.0'f32, 2,3,4,5]
  91. macro test_newLit_seq_float64: untyped =
  92. let s: seq[float64] = @[1.0'f64, 2,3,4,5]
  93. result = newLit(s)
  94. block:
  95. let tmp: seq[float64] = test_newLit_seq_float64
  96. doAssert tmp == @[1.0'f64, 2,3,4,5]
  97. macro test_newLit_tuple: untyped =
  98. let tup: tuple[a:int,b:string] = (a: 123, b: "223")
  99. result = newLit(tup)
  100. doAssert test_newLit_tuple == (a: 123, b: "223")
  101. type
  102. ComposedType = object
  103. mt: MyType
  104. arr: array[4,int]
  105. data: seq[byte]
  106. macro test_newLit_ComposedType: untyped =
  107. let ct = ComposedType(mt: MyType(a: 123, b:"abc"), arr: [1,2,3,4], data: @[1.byte, 3, 7, 127])
  108. result = newLit(ct)
  109. doAssert test_newLit_ComposedType == ComposedType(mt: MyType(a: 123, b:"abc"), arr: [1,2,3,4], data: @[1.byte, 3, 7, 127])
  110. macro test_newLit_empty_seq_string: untyped =
  111. var strSeq = newSeq[string](0)
  112. result = newLit(strSeq)
  113. block:
  114. # x needs to be of type seq[string]
  115. var x = test_newLit_empty_seq_string
  116. x.add("xyz")
  117. type
  118. MyEnum = enum
  119. meA
  120. meB
  121. macro test_newLit_Enum: untyped =
  122. result = newLit(meA)
  123. block:
  124. let tmp: MyEnum = meA
  125. doAssert tmp == test_newLit_Enum
  126. macro test_newLit_set: untyped =
  127. let myset = {MyEnum.low .. MyEnum.high}
  128. result = newLit(myset)
  129. block:
  130. let tmp: set[MyEnum] = {MyEnum.low .. MyEnum.high}
  131. doAssert tmp == test_newLit_set
  132. macro test_newLit_ref_object: untyped =
  133. var x = RefObject(x: 10)
  134. return newLit(x)
  135. block:
  136. let x = test_newLit_ref_object()
  137. doAssert $(x[]) == "(x: 10)"
  138. macro test_newLit_object_ref_alias: untyped =
  139. var x = ObjectRefAlias(x: 10)
  140. return newLit(x)
  141. block:
  142. let x = test_newLit_object_ref_alias()
  143. doAssert $(x[]) == "(x: 10)"