-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathtest_jni_generator.nim
451 lines (386 loc) · 13.3 KB
/
test_jni_generator.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
import ../jnim/private/jni_generator,
../jnim/private/jni_api,
./common,
macros,
unittest
jclass java.lang.String2* of JVMObject:
proc new
jclass java.lang.String as JVMString2* of JVMObject:
proc new
# These classes are not used in actual tests -
# but they should compile.
jclass java.util.Map[K,V] of JVMObject:
proc get(k: K): V
# Class with a method that returns a generic with two arguments
jclass java.lang.ProcessBuilder of JVMObject:
proc environment: Map[string, string]
# Class that inherits from another with get() method
jclass java.util.Properties of Map[JVMObject, JVMObject]:
proc new
jclass java.lang.Object* of JVMObject:
proc new
jclass java.lang.String* of Object:
proc new
jclass java.lang.Integer* of Object:
proc new(v: jint)
suite "jni_generator":
setup:
if not isJNIThreadInitialized():
initJNIForTests()
test "jni_generator - proc def - constructors":
var pd: ProcDef
parseProcDefTest pd:
proc new
check: pd.name == "new"
check: pd.jName == "<init>"
check: pd.retType == "void"
check: pd.params.len == 0
check: pd.isConstructor
check: not pd.isStatic
check: not pd.isProp
check: not pd.isFinal
check: not pd.isExported
parseProcDefTest pd:
proc new*
check: pd.name == "new"
check: pd.jName == "<init>"
check: pd.retType == "void"
check: pd.params.len == 0
check: pd.isConstructor
check: not pd.isStatic
check: not pd.isProp
check: not pd.isFinal
check: pd.isExported
parseProcDefTest pd:
proc new(o: JVMObject)
check: pd.name == "new"
check: pd.jName == "<init>"
check: pd.retType == "void"
check: pd.params == @[("o", "JVMObject")]
check: pd.isConstructor
check: not pd.isStatic
check: not pd.isProp
check: not pd.isFinal
check: not pd.isExported
parseProcDefTest pd:
proc new*(i: jint, s: string)
check: pd.name == "new"
check: pd.jName == "<init>"
check: pd.retType == "void"
check: pd.params == @[("i", "jint"), ("s", "string")]
check: pd.isConstructor
check: not pd.isStatic
check: not pd.isProp
check: not pd.isFinal
check: pd.isExported
test "jni_generator - proc def - methods":
var pd: ProcDef
parseProcDefTest pd:
proc getStrings: seq[string]
check: pd.name == "getStrings"
check: pd.jName == "getStrings"
check: pd.retType == "seq[string]"
check: pd.params.len == 0
check: not pd.isConstructor
check: not pd.isStatic
check: not pd.isProp
check: not pd.isFinal
check: not pd.isExported
parseProcDefTest pd:
proc `method`*(i: jint): jshort {.importc: "jmethod".}
check: pd.name == "method"
check: pd.jName == "jmethod"
check: pd.retType == "jshort"
check: pd.params == @[("i", "jint")]
check: not pd.isConstructor
check: not pd.isStatic
check: not pd.isProp
check: not pd.isFinal
check: pd.isExported
parseProcDefTest pd:
proc `method`*(i, j: jint): jshort {.importc: "jmethod".}
check: pd.name == "method"
check: pd.jName == "jmethod"
check: pd.retType == "jshort"
check: pd.params == @[("i", "jint"), ("j", "jint")]
check: not pd.isConstructor
check: not pd.isStatic
check: not pd.isProp
check: not pd.isFinal
check: pd.isExported
parseProcDefTest pd:
proc staticMethod(i: jint): jshort {.`static`.}
check: pd.name == "staticMethod"
check: pd.jName == "staticMethod"
check: pd.retType == "jshort"
check: pd.params == @[("i", "jint")]
check: not pd.isConstructor
check: pd.isStatic
check: not pd.isProp
check: not pd.isFinal
check: not pd.isExported
parseProcDefTest pd:
proc `method`*: Map[string,jint]
check: pd.name == "method"
check: pd.jName == "method"
check: pd.retType == "Map[string,jint]"
check: pd.params == newSeq[ProcParam]()
check: not pd.isConstructor
check: not pd.isStatic
check: not pd.isProp
check: not pd.isFinal
check: pd.isExported
parseProcDefTest pd:
proc `method`*(m: Map[string,jint]): jshort
check: pd.name == "method"
check: pd.jName == "method"
check: pd.retType == "jshort"
check: pd.params == @[("m", "Map[string,jint]")]
check: not pd.isConstructor
check: not pd.isStatic
check: not pd.isProp
check: not pd.isFinal
check: pd.isExported
test "jni_generator - proc def - properties":
var pd: ProcDef
parseProcDefTest pd:
proc `out`*(): JVMObject {.prop, final, `static`.}
check: pd.name == "out"
check: pd.jName == "out"
check: pd.retType == "JVMObject"
check: pd.params.len == 0
check: not pd.isConstructor
check: pd.isStatic
check: pd.isProp
check: pd.isFinal
check: pd.isExported
test "jni_generator - proc def - generics":
var pd: ProcDef
parseProcDefTest pd:
proc setAt[K,V](k: K, v: V)
check: pd.name == "setAt"
check: pd.genericTypes == @["K", "V"]
parseProcDefTest pd:
proc genericProp[V]: V {.prop.}
check: pd.name == "genericProp"
check: pd.genericTypes == @["V"]
check: pd.isProp
check: not pd.isStatic
parseProcDefTest pd:
proc genericProp*[V]: V {.prop.}
check: pd.name == "genericProp"
check: pd.genericTypes == @["V"]
check: pd.isProp
check: pd.isExported
check: not pd.isStatic
test "jni_generator - class def - header":
var cd: ClassDef
parseClassDefTest cd:
java.lang.String of JVMObject
check: cd.name == "String"
check: cd.jName == "java.lang.String"
check: cd.parent == "JVMObject"
check: not cd.isExported
parseClassDefTest cd:
java.lang.String as JVMString of JVMObject
check: cd.name == "JVMString"
check: cd.jName == "java.lang.String"
check: cd.parent == "JVMObject"
check: not cd.isExported
parseClassDefTest cd:
java.lang.String* of JVMObject
check: cd.name == "String"
check: cd.jName == "java.lang.String"
check: cd.parent == "JVMObject"
check: cd.isExported
parseClassDefTest cd:
java.lang.String as JVMString* of JVMObject
check: cd.name == "JVMString"
check: cd.jName == "java.lang.String"
check: cd.parent == "JVMObject"
check: cd.isExported
parseClassDefTest cd:
InnerTestClass$InnerClass of JVMObject
check: cd.name == "InnerClass"
check: cd.jName == "InnerTestClass$InnerClass"
check: cd.parent == "JVMObject"
check: not cd.isExported
test "jni_generator - class def - generic header":
var cd: ClassDef
parseClassDefTest cd:
java.util.List[T] of JVMObject
check: cd.name == "List"
check: cd.jName == "java.util.List"
check: cd.genericTypes == @["T"]
parseClassDefTest cd:
java.util.Map[K,V] of JVMObject
check: cd.name == "Map"
check: cd.jName == "java.util.Map"
check: cd.genericTypes == @["K", "V"]
parseClassDefTest cd:
java.util.HashMap[K,V] of Map[K,V]
check: cd.name == "HashMap"
check: cd.jName == "java.util.HashMap"
check: cd.genericTypes == @["K", "V"]
check: cd.parentGenericTypes == @["K", "V"]
check: cd.parent == "Map"
parseClassDefTest cd:
java.util.Map2*[K,V] of JVMObject
check: cd.name == "Map2"
check: cd.jName == "java.util.Map2"
check: cd.genericTypes == @["K", "V"]
check: cd.isExported
parseClassDefTest cd:
java.util.HashMap2*[K,V] of Map2[K,V]
check: cd.name == "HashMap2"
check: cd.jName == "java.util.HashMap2"
check: cd.genericTypes == @["K", "V"]
check: cd.parentGenericTypes == @["K", "V"]
check: cd.parent == "Map2"
check: cd.isExported
parseClassDefTest cd:
java.util.Map$Entry*[K,V] as MapEntry of JVMObject
check: cd.name == "MapEntry"
check: cd.jName == "java.util.Map$Entry"
check: cd.genericTypes == @["K", "V"]
check: cd.isExported
test "jni_generator - import class":
jclass java.lang.String1 of JVMObject:
proc new
check: declared(String1)
check: String1.jniSig == sigForClass"java.lang.String1"
jclass java.lang.String as JVMString1 of JVMObject:
proc new
check: declared(JVMString1)
check: JVMString1.jniSig == sigForClass"java.lang.String"
check: declared(String2)
check: String2.jniSig == sigForClass"java.lang.String2"
check: declared(JVMString2)
check: JVMString2.jniSig == sigForClass"java.lang.String"
jclass ConstructorTestClass of JVMObject:
proc new
proc new(i: jint)
proc new(s: string)
proc new(i: jint, d: jdouble, s: string)
proc new(ints: openarray[jint])
proc new(strings: openarray[string])
proc new(c: ConstructorTestClass)
proc new(c: openarray[ConstructorTestClass])
test "jni_generator - TestClass - constructors":
var o = ConstructorTestClass.new
check: o.toStringRaw == "Empty constructor called"
o = ConstructorTestClass.new(1.jint)
check: o.toStringRaw == "Int constructor called, 1"
o = ConstructorTestClass.new("hi!")
check: o.toStringRaw == "String constructor called, hi!"
o = ConstructorTestClass.new(1, 2.0, "str")
check: o.toStringRaw == "Multiparameter constructor called, 1, 2.0, str"
o = ConstructorTestClass.new(@[1.jint,2,3])
check: o.toStringRaw == "Int array constructor called, 1, 2, 3"
o = ConstructorTestClass.new(@["a", "b", "c"])
check: o.toStringRaw == "String array constructor called, a, b, c"
o = ConstructorTestClass.new(o)
check: o.toStringRaw == "String array constructor called, a, b, c"
let cc = [ConstructorTestClass.new(), ConstructorTestClass.new(1)]
o = ConstructorTestClass.new(cc)
check: o.toStringRaw == "Empty constructor called\nInt constructor called, 1\n"
jclass MethodTestClass of JVMObject:
proc new
proc add(x, y: jint): jint {.`static`, importc: "addStatic".}
proc addToMem(x: jint): jint {.importc: "addToMem".}
proc factory(i: jint): MethodTestClass {.`static`.}
proc getStrings: seq[string]
proc readBytes(buf: JVMByteArray): jint
test "jni_generator - TestClass - methods":
check: MethodTestClass.add(1, 2) == 3
let o = MethodTestClass.new
check: o.addToMem(2) == 2
check: o.addToMem(3) == 5
check: MethodTestClass.factory(5).addToMem(1) == 6
check: o.getStrings == @["Hello", "world!"]
let buf = newArray(jbyte, 10)
check: o.readBytes(buf) == 4
for i in countup(0, 3):
check: buf[i] == i
let buf2 = newArray(jbyte, 2)
check: o.readBytes(buf2) == 2
for i in countup(0, 1):
check: buf2[i] == i
jclassDef PropsTestClass of JVMObject
jclassImpl PropsTestClass of JVMObject:
proc new
proc staticInt: jint {.prop, `static`.}
proc instanceInt: jint {.prop.}
proc inst: PropsTestClass {.prop, `static`, final.}
proc instanceString: string {.prop.}
proc staticBool: bool {.prop, `static`.}
test "jni_generator - TestClass - properties":
check: PropsTestClass.staticInt == 100
PropsTestClass.staticInt = 200
check: PropsTestClass.staticInt == 200
let o = PropsTestClass.new
check: o.instanceInt == 100
o.instanceInt = 300
check: o.instanceInt == 300
check PropsTestClass.inst.instanceInt == 100
PropsTestClass.inst.instanceString = "Hello, world!"
check: PropsTestClass.inst.instanceString == "Hello, world!"
check: not PropsTestClass.staticBool
PropsTestClass.staticBool = true
check: PropsTestClass.staticBool
jclass InnerTestClass of JVMObject:
proc new
jclass InnerTestClass$InnerClass of JVMObject:
proc new(p: InnerTestClass)
proc intField: jint {.prop.}
test "jni_generator - TestClass - inner classes":
let p = InnerTestClass.new
let o = InnerClass.new(p)
check: o.intField == 100
jclass java.util.List[V] of JVMObject:
proc get[V](i: jint): V
jclass GenericsTestClass[V] of JVMObject:
proc new[V](v: V)
proc genericProp[V]: V {.prop.}
proc getGenericValue[V]: V
proc setGenericValue[V](v: V)
proc getListOfValues[V](count: jint): List[V]
test "jni_generator - TestClass - generics":
let o = GenericsTestClass[string].new("hello")
o.genericProp = "hello, world!"
check: o.genericProp == "hello, world!"
o.setGenericValue("hi!")
check: o.getGenericValue == "hi!"
let l = o.getListOfValues(3)
for i in 0..2:
check: l.get(i.jint) == "hi!"
jclass BaseClass[V] of JVMObject:
proc new[V](v: V)
proc baseMethod[V]: V
proc overridedMethod[V]: V
jclass ChildClass[V] of BaseClass[V]:
proc new[V](base, ch: V)
proc childMethod[V]: V
test "jni_generator - TestClass - inheritance":
let b = BaseClass[string].new("Base")
let c = ChildClass[string].new("Base", "Child")
check: b.baseMethod == b.overridedMethod
check: c.childMethod == c.overridedMethod
check: b.overridedMethod != c.overridedMethod
test "jni_generator - instanceOf":
let
i = newJVMObject(Integer.new(1).get)
s = newJVMObject(String.new().get)
check: i.instanceOf(Integer)
check: i.instanceOf(Object)
check: s.instanceOf(String)
check: s.instanceOf(Object)
check: not i.instanceOf(String)
check: not s.instanceOf(Integer)
test "jni_generator - jcast":
let
s = newJVMObject(String.new().get)
check: jcast[String](s) is String
check: jcast[Object](s) is Object
expect ObjectConversionDefect:
discard jcast[Integer](s)