-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtcinterop.nim
267 lines (186 loc) · 8 KB
/
tcinterop.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
discard """
action: "run"
matrix: "--gc:orc --cc:vcc --passC:-std:c++17 --forceBuild:on --outdir:tests"
target: "c++"
"""
import ./tcinterop/contained
import ./tcinterop/simple
import cinterop/[exprs, pragmas]
template should(description: static string, body: untyped) =
proc test() {.genSym.} = body
test()
should "access value macro":
doAssert DEFINE1 == 1
should "access const global":
doAssert ConstGlobal1 == 1
should "access function":
let instance = CppClass.init()
doAssert function1(instance) == 2
should "access undeclared field of nested class":
let nestedInstance = CppClass.CppNestedClass.init()
doAssert cexpr[cint]^nestedInstance.nestedField1 == 1
should "compare undeclared field with value":
let instance = CppClass.init()
doAssert cexpr[cint]^instance.field1 == 1
doAssert cexpr[cint]^instance.field1 != 2
doAssert cexpr[cint]^instance.field1 > 0
doAssert cexpr[cint]^instance.field1 < 2
doAssert cexpr[cint]^instance.field1 >= 1
doAssert cexpr[cint]^instance.field1 >= 0
doAssert cexpr[cint]^instance.field1 <= 1
doAssert cexpr[cint]^instance.field1 <= 2
doAssert 1 == cexpr[cint]^instance.field1
doAssert 2 != cexpr[cint]^instance.field1
doAssert 2 > cexpr[cint]^instance.field1
doAssert 0 < cexpr[cint]^instance.field1
doAssert 1 >= cexpr[cint]^instance.field1
doAssert 2 >= cexpr[cint]^instance.field1
doAssert 1 <= cexpr[cint]^instance.field1
doAssert 0 <= cexpr[cint]^instance.field1
should "perform binary operations with undeclared field and value":
let instance = CppClass.init()
doAssert cexpr[cint]^instance.field2 + 1 == 3
doAssert cexpr[cint]^instance.field2 - 1 == 1
doAssert cexpr[cint]^instance.field2 * 2 == 4
doAssert cexpr[cint]^instance.field2 / 2 == 1
doAssert 1 + cexpr[cint]^instance.field2 == 3
doAssert 1 - cexpr[cint]^instance.field2 == -1
doAssert 2 * cexpr[cint]^instance.field2 == 4
doAssert 2 / cexpr[cint]^instance.field2 == 1
should "perform `+=` operation on undeclared field with value":
let instance = CppClass.init()
cexpr[cint]^instance.field2 += 1
doAssert cexpr[cint]^instance.field2 == 3
should "perform `-=` operation on undeclared field with value":
let instance = CppClass.init()
cexpr[cint]^instance.field2 -= 1
doAssert cexpr[cint]^instance.field2 == 1
should "perform `*=` operation on undeclared field with value":
let instance = CppClass.init()
cexpr[cint]^instance.field2 *= 2
doAssert cexpr[cint]^instance.field2 == 4
should "perform `/=` operation on undeclared field with value":
let instance = CppClass.init()
cexpr[cint]^instance.field2 /= 2
doAssert cexpr[cint]^instance.field2 == 1
should "assign value to undeclared field":
var instance = CppClass.init()
cexpr[cint]^instance.field1 = 2
doAssert cexpr[cint]^instance.field1 == 2
should "mutate undeclared field through var param":
var instance = CppClass.init()
proc assign2(i: var cint) = i = 2
assign2(cexpr[cint]^instance.field1)
doAssert cexpr[cint]^instance.field1 == 2
should "assign undeclared field to undeclared field":
var instance = CppClass.init()
cexpr[cint]^instance.field1 = cexpr[cint]^instance.field2
doAssert cexpr[cint]^instance.field1 == 2
should "compare undeclared field with undeclared field":
let instance = CppClass.init()
doAssert cexpr[cint]^instance.field1 == cexpr[cint]^instance.field1
doAssert cexpr[cint]^instance.field1 != cexpr[cint]^instance.field2
doAssert cexpr[cint]^instance.field2 > cexpr[cint]^instance.field1
doAssert cexpr[cint]^instance.field1 < cexpr[cint]^instance.field2
doAssert cexpr[cint]^instance.field1 >= cexpr[cint]^instance.field1
doAssert cexpr[cint]^instance.field2 >= cexpr[cint]^instance.field1
doAssert cexpr[cint]^instance.field1 <= cexpr[cint]^instance.field1
doAssert cexpr[cint]^instance.field1 <= cexpr[cint]^instance.field2
should "perform binary operations with undeclared fields":
let instance = CppClass.init()
doAssert cexpr[cint]^instance.field2 + cexpr[cint]^instance.field1 == 3
doAssert cexpr[cint]^instance.field2 - cexpr[cint]^instance.field1 == 1
doAssert cexpr[cint]^instance.field2 * cexpr[cint]^instance.field1 == 2
doAssert cexpr[cint]^instance.field2 / cexpr[cint]^instance.field1 == 2
should "perform `+=` operation on undeclared fields":
let instance = CppClass.init()
cexpr[cint]^instance.field2 += cexpr[cint]^instance.field1
doAssert cexpr[cint]^instance.field2 == 3
should "perform `-=` operation on undeclared fields":
let instance = CppClass.init()
cexpr[cint]^instance.field2 -= cexpr[cint]^instance.field1
doAssert cexpr[cint]^instance.field2 == 1
should "perform `*=` operation on undeclared fields":
let instance = CppClass.init()
cexpr[cint]^instance.field2 *= cexpr[cint]^instance.field1
doAssert cexpr[cint]^instance.field2 == 2
should "perform `/=` operation on undeclared fields":
let instance = CppClass.init()
cexpr[cint]^instance.field2 /= cexpr[cint]^instance.field1
doAssert cexpr[cint]^instance.field2 == 2
should "access undeclared pointer field":
let instance = CppClass.init()
doAssert cexpr[cint]^instance.field3[] == 3
should "assign value to undeclared pointer field":
var instance = CppClass.init()
cexpr[cint]^instance.field3[] = 4
doAssert cexpr[cint]^instance.field3[] == 4
should "access undeclared array field":
let instance = CppClass.init()
doAssert cexpr[cint]^instance.field4[0] == 4
should "assign value to undeclared array field element":
var instance = CppClass.init()
cexpr[cint]^instance.field4[0] = 5
doAssert cexpr[cint]^instance.field4[0] == 5
should "access method":
let instance = CppClass.init()
doAssert instance.method1(1) == 2
should "access undeclared method":
let instance = CppClass.init()
doAssert cexpr[cint]^instance.method2(1) == 3
should "access converter":
let instance = CppClass.init()
doAssert instance.method3() == 3
should "access undeclared method that returns void":
let instance = CppClass.init()
var value: cint
cexpr^instance.method4(value)
doAssert value == 4
should "access field of a class instance in a nim object":
var container: Container
container.subContainer.cppClass = CppClass.init()
doAssert cexpr[cint]^container.subContainer.cppClass.field2 == 2
doAssert cexpr[cint]^container.getSubContainer().cppClass.field2 == 2
should "access private field":
let instance = CppClass.init()
doAssert instance.field3Value == 3
should "access member of an enum":
doAssert ord(cauto^CPP_ENUM.MEMBER_1) == 1
doAssert cexpr[cint]^CPP_ENUM.MEMBER_1 == 1
should "new and delete class instance":
let newInstance = cnew CppClass.init()
doAssert newInstance != nil
cdelete newInstance
should "allow comparison of `auto` with value":
let instance = CppClass.init()
doAssert cauto^instance.field1 == 1
should "allow comparison of `auto` with `auto`":
let instance = CppClass.init()
doAssert cauto^instance.field1 < cauto^instance.field2
should "allow assignment of value to `auto`":
var instance = CppClass.init()
cauto^instance.field1 = 2
doAssert cexpr[cint]^instance.field1 == 2
should "allow assignment of `auto` to `auto`":
var instance = CppClass.init()
cauto^instance.field1 = cauto^instance.field2
doAssert cexpr[cint]^instance.field1 == 2
should "perform unary operation on `auto`":
let instance = CppClass.init()
doAssert -(cauto^instance.field1) == -1
doAssert +(cauto^instance.field1) == +1
should "allow storage of `auto` to temporary variable":
let instance = CppClass.init()
let value = cauto^instance.field2
doAssert value == 2
when (NimMajor, NimMinor, NimPatch) >= (1, 7, 1):
should "mutate `cref` variable":
var instance = CppClass.init()
let value {.cref.} = cauto^instance.field1
cauto^instance.field1 = 2
doAssert value == 2
should "have bitwise operators for enum flags":
doAssert ord(not (cauto^CPP_ENUM.MEMBER_1)) == -2
doAssert ord(cauto^CPP_ENUM.MEMBER_1 and cauto^CPP_ENUM.MEMBER_2) == 0
doAssert ord(cauto^CPP_ENUM.MEMBER_1 or cauto^CPP_ENUM.MEMBER_2) == 3
doAssert ord(not (cauto^CPP_ENUM.MEMBER_1) xor cauto^CPP_ENUM.MEMBER_2) == -4