-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.nim
685 lines (430 loc) · 14.1 KB
/
test.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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
import ./src/classes
import ExternalClassTest
import macros
import strutils
# Platform specific imports
when defined(js):
# Code for Javascript
const fgBlue = "\u001b[34m"
const fgGreen = "\u001b[32m"
const fgRed = "\u001b[31m"
const fgDefault = "\u001b[0m"
proc styledEcho(v: varargs[string]) = echo v.join("")
const platformName = "js"
else:
# Native code
import asyncdispatch
import terminal
import os
import times
const platformName = "native"
# Helpers for testing
proc group(str: string) = styledEcho "\n", fgBlue, "+ ", fgDefault, str, " (" & platformName & " compiler)"
proc test(str: string) = styledEcho fgGreen, " + ", fgDefault, str
proc warn(str: string) = styledEcho fgRed, " ! ", fgDefault, str
group "Class variables"
test "Empty class definition"
class Empty1
class Empty2 of Empty1
test "Class variables"
class ClassWithVars:
## Comment here
var var0 = 0
var var1 = "hello"
var var2: int = len("hi")
var var3: seq[string]
var var4: seq[string] = @["hi"]
var var5 = true
assert(ClassWithVars().init().var0 == 0)
assert(ClassWithVars.init().var1 == "hello")
assert(newClassWithVars().var2 == 2)
assert(ClassWithVars.init().var5 == true)
test "Class variables overwritten by subclass"
class ClassWithVars2 of ClassWithVars:
var var1 = "hello rewritten"
assert(ClassWithVars2().init().var2 == 2)
assert(ClassWithVars2().init().var1 == "hello rewritten")
test "Data only class"
class DataOnly:
var v1 = 5
var v2: int
var v3: string
assert(DataOnly(v3: "hi", v2: 10).init().v1 == 5)
assert(DataOnly(v3: "hi", v2: 10).init().v2 == 10)
assert(DataOnly(v3: "hi", v2: 10).init().v3 == "hi")
test "Using 'when' inside class body"
warn "Not implemented yet"
# class WhenClass:
# when compileOption("threads"):
# var hasThreads = true
# else:
# var hasThreads = false
# assert(WhenClass.init().hasThreads == compileOption("threads"))
test "Getters and setters"
class CustomGetterSetter:
var v2private = 6
method v2(): int = this.v2private
method `v2=`(v: int) = this.v2private = v
let customGetterSetter = CustomGetterSetter.init()
customGetterSetter.v2 = 7
assert(customGetterSetter.v2 == 7)
let externalGetterSetter = ExternalClass.init()
externalGetterSetter.testCustomSetter = 8
assert(externalGetterSetter.testCustomSetter == 8)
group "Constructors and destructors"
test "Automatic constructors on the base class"
class ClassA
# Our method of creating classes
let classA1 = ClassA.init()
# Nim's method of creating classes
let classA2 = newClassA()
test "Automatic constructors on subclasses"
class C:
var v1 = 5
method init(i: int) = this.v1 = this.v1 + 5 + i
class D of C
assert(C.new(5).v1 == 15)
assert(D.new(5).v1 == 15)
var tmp1 = 5
class E:
method init() =
tmp1 = tmp1 + 5
class F of E
class G of F:
method init() =
super.init()
tmp1 = tmp1 + 5
discard E.init()
assert(tmp1 == 10)
discard F.init()
assert(tmp1 == 15)
discard G.init()
assert(tmp1 == 25)
test "Automatic constructors on subclasses with overwritten vars"
warn "Not implemented yet, will not set variable values"
# class E of C:
# var v1 = 10
# assert(E.new(5).v1 == 20)
test "Constructor with 0 args"
class ClassWithZeroInit:
var v1 = 5
method init() =
this.v1 = this.v1 + 5
assert(ClassWithZeroInit().init().v1 == 10)
test "Constructor with 5 args"
class ClassWith3Init:
var v1: int
method init(a, b, c: int, d: float, e: float) =
this.v1 = a + b + c
assert(ClassWith3Init().init(5, 5, 5, e=4.5, d=3.4).v1 == 15)
assert(newClassWith3Init(5, 5, 5, e=4.5, d=3.4).v1 == 15)
assert(ClassWith3Init.init(5, 5, 5, e=4.5, d=3.4).v1 == 15)
test "Constructor ordering"
class ClassConstr1:
var v1 = 2
method makeCopy(): ClassConstr1 =
let n = ClassConstr1.init()
return n
method init() = discard
assert(ClassConstr1.init().makeCopy().v1 == 2)
test "Factory method"
class ClassWithFactory:
var v1 = 0
method withValue(v: int): ClassWithFactory {.static.} =
let n = ClassWithFactory.init()
n.v1 = v
return n
assert(ClassWithFactory.withValue(3).v1 == 3)
test "Destructors"
when defined(js):
# Not supported
warn "Not supported in Javascript"
else:
# Counter to store destructor call count
var globalDestructorCounter = 1
# Simple destructor
class TestDestructor1:
method deinit() = globalDestructorCounter += 1
# Class with destructor in the superclass
class TestDestructor2 of TestDestructor1
# Class with custom destructor
class TestDestructor3 of TestDestructor1:
method deinit() =
super.deinit()
globalDestructorCounter += 1 # <-- Add again
# Create objects to be cleaned up
proc checkDestructor() =
let cls = TestDestructor1.init() # <-- Destructor adds one
let cls2 = TestDestructor2.init() # <-- Destructor in superclass adds one
let cls3 = TestDestructor3.init() # <-- Destructor adds one + calls super destructor which also adds one
checkDestructor()
# GC takes a while, wait for it to be done
let startedAt = cpuTime()
while true:
if globalDestructorCounter == 5 or startedAt - cpuTime() > 10: break
GC_fullCollect()
sleep(100)
# Ensure the counter is correct
assert(globalDestructorCounter == 5, "Expected 5 but got " & $globalDestructorCounter)
group "Comments"
test "Comment inside method"
class CommentA:
method a() =
## Test
discard
CommentA.init().a()
test "Comment outside method"
class CommentB:
## Test
method b() =
discard
## Test2
method c() =
## Actual
discard
## Test
method d() =
# Not real
discard
CommentB.init().b()
CommentB.init().c()
CommentB.init().d()
# TODO: How do we test for this?
group "Inheritance"
test "Super constructor"
class WithSuper3:
var v1 = 5
method init(a: int) =
this.v1 += a
class WithSuper4 of WithSuper3:
method init() = super.init(20)
assert(WithSuper3().init(5).v1 == 10)
assert(WithSuper4().init().v1 == 25)
test "Super call"
class WithSuper1:
method test(): int = 5
class WithSuper2 of WithSuper1:
method test(): int = super.test() + 5
method test2(): int = super.test()
assert(newWithSuper2().test() == 10)
assert(newWithSuper2().test2() == 5)
test "Constructor inheritance"
class ClsInit1:
var v1 = 1
method init() = this.v1 += 2
class ClsInit2 of ClsInit1:
method init() =
super.init()
this.v1 += 3
class ClsInit3 of ClsInit2:
method init() =
super.init()
this.v1 += 4
assert(ClsInit3.init().v1 == 10)
test "Super call with skipped inheritance"
class TestInheritanceA1:
var v1 = 1
method increase() = this.v1 += 1
class TestInheritanceA2 of TestInheritanceA1
class TestInheritanceA3 of TestInheritanceA2:
method increase() =
super.increase()
this.v1 += 2
let inheritanceA = TestInheritanceA3.init()
inheritanceA.increase()
assert(inheritanceA.v1 == 4)
group "Methods"
test "Abstract methods"
class WithAbstract:
method overrideMe()
class WithAbstract2:
method overrideMe() = discard
# Calling the abstract method directly should fail
doAssertRaises Defect, WithAbstract().overrideMe()
# Calling the overridden abstract method from the subclass should be fine
WithAbstract2().overrideMe()
test "Overridden methods with different param names"
class OverrideTest1:
method overrideMe2(str1 : string, int2 : int) : string = str1 & $int2
class OverrideTest2 of OverrideTest1:
method overrideMe2(str3 : string, int4 : int) : string = super.overrideMe2(str3, int4) & "extra"
# Ensure return value is expected
assert(OverrideTest2.init().overrideMe2("hello", 2) == "hello2extra")
test "Static methods"
class WithStatic:
method staticFunc() {.static.} = discard
method staticFunc2() {.used, static.} = discard
WithStatic.staticFunc()
WithStatic.staticFunc() # <-- Calling it twice
test "Private methods"
warn "Not implemented yet"
group "Interop with Nim's class format"
test "Subclass a Nim class"
type NimClass = ref object of RootObj
var1: string
class NimClass2 of NimClass:
method init() =
this.var1 = "hi"
assert(NimClass2().init().var1 == "hi")
test "Nim subclass of our class"
class NimClass3:
var v1 = "hi"
type NimClass4 = ref object of NimClass3
assert(NimClass4().init().v1 == "hi")
test "Type checking at compile time"
class InheritedFromNimClassA1 of NimClass
class InheritedFromNimClassA2 of InheritedFromNimClassA1
class InheritedFromNimClassA3 of InheritedFromNimClassA2
class InheritedClassB1
class InheritedClassB2 of InheritedClassB1
class InheritedClassB3 of InheritedClassB2
assert(InheritedFromNimClassA3.init() is NimClass == true)
assert(InheritedFromNimClassA3.init() is InheritedFromNimClassA1 == true)
assert(InheritedFromNimClassA3.init() is InheritedFromNimClassA2 == true)
assert(InheritedFromNimClassA3.init() is InheritedFromNimClassA3 == true)
assert(InheritedFromNimClassA3.init() is InheritedClassB1 == false)
assert(InheritedClassB3.init() is InheritedClassB1 == true)
assert(InheritedClassB3.init() is InheritedClassB2 == true)
assert(InheritedClassB3.init() is InheritedClassB3 == true)
assert(InheritedClassB3.init() is InheritedFromNimClassA3 == false)
test "Type checking at run time"
var inheritedInVariable: InheritedClassB1 = InheritedClassB3.init()
assert(inheritedInVariable of InheritedFromNimClassA3 == false)
assert(inheritedInVariable of InheritedClassB1 == true)
assert(inheritedInVariable of InheritedClassB2 == true)
assert(inheritedInVariable of InheritedClassB3 == true)
group "Exported classes"
test "Use a static method"
ExternalClass.testStatic()
test "Use a normal method"
ExternalClass.init().test()
newExternalClass().test()
ExternalClass.new().test
group "Advanced usage"
test "Get class name at runtime"
class Adv1
class Adv2 of Adv1
assert(Adv1.init().className == "Adv1")
assert(Adv2.init().className == "Adv2")
when not defined(js):
test "Run an async function"
class AsyncCls:
method testVoid() {.async.} = discard
method testInt(): Future[int] {.async.} = return 3
waitFor AsyncCls.init().testVoid()
let i: int = waitFor AsyncCls.init().testInt()
assert(i == 3)
test "Proxied macro"
macro class2(head: untyped, body: untyped): untyped =
# TODO: Why does Nim put a `gensym123 suffix only on the injected variable, and not the injected method???
let injectedVar = ident"injectedVar"
# let injectedVar
return quote do:
class `head`:
var `injectedVar`: int = 3
method injectedMethod(): int = 5
`body`
class2 MyProxiedClass:
## Original var
var originalVar = 6
## Original method
method checkMe(): int = 4
assert(MyProxiedClass.init().checkMe() == 4)
assert(MyProxiedClass.init().injectedVar == 3)
assert(MyProxiedClass.init().injectedMethod() == 5)
assert(MyProxiedClass.init().originalVar == 6)
class2 ProxyClass2 of MyProxiedClass:
var v3 = 3
method m4(): int = 4
assert(ProxyClass2.init().v3 == 3)
assert(ProxyClass2.init().m4 == 4)
class2 Cls2Init1:
var v1 = 1
method init() = this.v1 += 2
class2 Cls2Init2 of Cls2Init1:
## Override init
method init() =
super.init()
this.v1 += 3
class2 Cls2Init3 of Cls2Init2:
## Override init again
method init() =
super.init()
this.v1 += 4
assert(Cls2Init3.init().v1 == 10)
group "Mixins"
test "Apply mixin to class"
class Mixed1:
## Comment here
var var0 = 0
var var1 = "hello"
class Mixed2:
mixin Mixed1
## Comment here
var var2 = 0
var var3 = "hello"
assert(Mixed2.init().var1 == "hello")
test "Modify mixin variable from class"
class Mixed3:
method init() =
this.var0 = 3
mixin Mixed1
assert(Mixed3.init().var0 == 3)
test "Modify class variable from mixin"
class Mixed4:
var var0 = 2
method update() =
this.var0 = this.var0 * 2
class Mixed5:
mixin Mixed4
let mixed5 = Mixed5.init()
mixed5.update()
assert(mixed5.var0 == 4)
test "Subclass with mixin"
class Mixed6 of Mixed1:
mixin Mixed2
assert(Mixed6.init().var3 == "hello")
test "Subclassed mixin"
class Mixed7 of Mixed4
class Mixed8 of Mixed2:
mixin Mixed7
let mixed8 = Mixed8.init()
mixed8.update()
assert(mixed8.var0 == 4)
group "Singletons"
test "Definition"
singleton Singleton1:
var v1 = 3
var v2 = 0
method init() =
this.v2 = 4
test "Variables"
assert(Singleton1.shared.v1 == 3)
test "Init"
assert(Singleton1.shared.v2 == 4)
singleton Singleton2 of Singleton1:
var v3 = 0
method init() =
super.init()
this.v3 = 5
method myFunc(): int = 6
## Ensure only one class is created
var singletonCounter = 0
singleton SingleTest:
method init() = singletonCounter += 1
method deinit() = singletonCounter -= 1
method test() = discard
SingleTest.shared.test()
SingleTest.shared.test()
assert(SingleTest.shared == SingleTest.shared, "Only one instance of a singleton should exist.")
assert(singletonCounter == 1, "Singleton constructor should only run once")
test "Subclassing"
assert(Singleton2.shared.v1 == 3)
assert(Singleton2.shared.v2 == 4)
assert(Singleton2.shared.v3 == 5)
test "Methods"
assert(Singleton2.shared.myFunc() == 6)
test "External singleton"
assert(ExternalSingleton.shared.v1 == 3)
# All tests done
echo ""