/
test_objutils.nim
149 lines (131 loc) · 3.35 KB
/
test_objutils.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import boost.objutils,
unittest,
macros
type GlobalX* = object
a*: int
b*: seq[string]
when compiles(GlobalX.genConstructor):
GlobalX.genConstructor createGlobalX, exported
suite "objutils - constructor":
test "simple object":
type
X1 = object
a: int
b: seq[string]
check: compiles(X1.genConstructor)
when compiles(X1.genConstructor):
genConstructor X1
check: declared(initX1)
when declared(initX1):
let x1 = initX1(1, @["2"])
check: x1.a == 1
check: x1.b == @["2"]
type
X2 = ref object
a: int
b: seq[string]
check: compiles(X2.genConstructor)
when compiles(X2.genConstructor):
genConstructor X2
check: declared(newX2)
when declared(newX2):
let x2 = newX2(1, @["2"])
check: x2.a == 1
check: x2.b == @["2"]
test "exported object":
check: declared(createGlobalX)
when declared(createGlobalX):
let x3 = createGlobalX(1, @["2"])
check: x3.a == 1
check: x3.b == @["2"]
test "generic fields":
type A[T] = object
x: T
check: compiles(A.genConstructor)
when compiles(A.genConstructor):
genConstructor A
check: declared(initA)
when declared(initA):
let a = initA(1)
check: a.x == 1
type B[T] = ref object
x: T
check: compiles(B.genConstructor)
when compiles(B.genConstructor):
genConstructor B
check: declared(newB)
when declared(newB):
let b = newB(1)
check: b.x == 1
test "ref object":
type XObj = object
x: int
type X = ref XObj
check: compiles(X.genConstructor)
when compiles(X.genConstructor):
genConstructor X
check: declared(newX)
when declared(newX):
let x = newX(1)
check: x.x == 1
type YObj[T,U] = object
y1: T
y2: U
type Y[T] = ref YObj[T, int]
check: compiles(Y.genConstructor)
when compiles(Y.genConstructor):
genConstructor Y
check: declared(newY)
when declared(newY):
let y = newY(1, 2)
check: y.y1 == 1
check: y.y2 == 2
test "complex fields":
type A = object
x: int
type B = object
a: A
check: compiles(B.genConstructor)
when compiles(B.genConstructor):
genConstructor B
check: declared(initB)
when declared(initB):
let b = initB(A(x: 1))
check: b.a.x == 1
type C[D] = object
x: D
type E = object
c: C[int]
check: compiles(E.genConstructor)
when compiles(E.genConstructor):
genConstructor E
check: declared(initE)
when declared(initE):
let e = initE(C[int](x: 1))
check: e.c.x == 1
suite "objutils - data keyword":
data DataA:
a: int
let b = "a"
var c: seq[int] = @[1,2,3]
test "simple type":
check compiles(DataA)
when compiles(DataA):
check compiles(initDataA)
when compiles(initDataA):
let a = initDataA(1, c = @[1,2])
check: a.a == 1
check: a.b == "a"
check: a.c == @[1,2]
data DataB of DataA:
d: string
test "simple types inheritance":
check compiles(DataB)
# data X[T: int|string,U] ref of RootObj(exported, constructor(mkX)):
# r: bool
# case r
# of true:
# let x = T()
# else:
# y: U
# var z = 12