-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
929 lines (748 loc) · 29.9 KB
/
test.js
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
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
"use strict";
function dump(data) {
data = (data === null ? 'null' : data);
var s = QUnit.jsDump.parse(data, typeof data);
return s.replace(/[\&<>]/g, function(s) {
switch(s) {
case "&": return "&";
case "<": return "<";
case ">": return ">";
default: return s;
}
});
}
var Class = Jassino.Class,
Mixin = Jassino.Mixin,
$$ = Jassino.$$,
ns, _ns = {},
default_up_down = {
beforeEach: function () {
ns = Jassino.setDefaultNS({});
},
afterEach: function () {
ns = Jassino.setDefaultNS({});
}
};
var eq = strictEqual;
//========================================================================================================================
module("Basic definitions", default_up_down);
test("Class and Mixin definitions, typeof Class/Mixin === function", 2, function() {
eq(typeof Class, "function", 'Class');
eq(typeof Mixin, "function", 'Mixin')
});
//========================================================================================================================
module("Basic namespace tests --", {});
test("default", function() {
Class('A', {});
Mixin('T', {});
var N = {};
Jassino.setDefaultNS(N);
eq(N.A, undefined, 'not added');
eq(N.T, undefined, 'not added');
var A = Class('A', {});
var T = Mixin('T', {});
eq(A, N.A, 'for Class');
eq(T, N.T, 'for Mixin');
});
module("Class/Mixin creation, NameSpaces --", default_up_down);
test("Class/Mixin creation - default namespace", 2, function() {
var A = Class('A', {});
var T = Mixin('T', {});
eq(A, ns.A, 'for Class');
eq(T, ns.T, 'for Mixin');
});
test("Class/Mixin creation -> namespace test", 2, function() {
var ns = {};
var A = Class(ns, 'A', {});
var T = Mixin(ns, 'T', {});
eq(A, ns.A, 'for Class');
eq(T, ns.T, 'for Mixin');
});
test("Duplicate Class/Mixin creation)", 2, function() {
var n = {};
Class(n, 'A', {});
throws(function(){Class(n, 'A', {})}, Jassino.DuplicationError, 'for Class: ' + dump(n));
Mixin(n, 'T', {});
throws(function(){Mixin(n, 'T', {})}, Jassino.DuplicationError, 'for Mixin');
});
test("Duplicate Class/Mixin creation - default NS)", 2, function() {
Class('A', {});
throws(function(){Class('A', {})}, Jassino.DuplicationError, 'for Class: ' + dump(ns));
Mixin('T', {});
throws(function(){Mixin('T', {})}, Jassino.DuplicationError, 'for Mixin');
});
test("Invalid namespace object test on Class/Mixin creation)", function() {
throws(function(){Class(null, 'A', {})}, Jassino.ArgumentsError, 'null ns for Class')
throws(function(){Mixin(null, 'T', {})}, Jassino.ArgumentsError, 'null ns for Mixin')
throws(function(){Class("", {})}, Jassino.ArgumentsError, 'empty name for Class')
throws(function(){Mixin("", {})}, Jassino.ArgumentsError, 'empty for Mixin')
throws(function(){Class("ff")}, Jassino.ArgumentsError, 'empty name for Class')
throws(function(){Mixin("ff")}, Jassino.ArgumentsError, 'empty for Mixin')
throws(function(){Class()}, Jassino.ArgumentsError, 'empty name for Class')
throws(function(){Mixin()}, Jassino.ArgumentsError, 'empty for Mixin')
throws(function(){Class(['a'], {})})
throws(function(){Mixin(['a'], {})})
Mixin('T', {})
throws(function(){Class(ns, [ns.T], {})})
throws(function(){Mixin(ns, ['a'], {})})
});
//========================================================================================================================
module("Basic Mixin operations", default_up_down);
test("Members setup", 1, function() {
Mixin('T', {
a: 5,
f: function(){return this.a}
})
eq(ns.T.f(), 5, 'member call')
});
//========================================================================================================================
module("Basic Class definitions and operations", default_up_down);
//-------------------------------------------------------------------------------------------------------------------
test("Object members should not become class members", 2, function() {
Class('T', {
a: 5,
f: function(){return this.a}
})
eq(ns.T.f, undefined)
eq(ns.T.a, undefined)
});
//-------------------------------------------------------------------------------------------------------------------
test("Simple instantiation with implicit constructor", 1, function() {
Class('T', {
a: 5,
f: function(){return this.a}
})
var t = new ns.T()
eq(t.f(), 5, 'member call')
});
//========================================================================================================================
module("Class constructors", default_up_down);
//-------------------------------------------------------------------------------------------------------------------
test("Explicit constructor", 1, function() {
Class('T', {
_: function(_cls, num){
this.num = num;
},
a: "zz",
f: function(){
return this.a + this.num
}
});
var t = new ns.T("xx");
eq(t.f(), "zzxx", 'member call test');
});
//-------------------------------------------------------------------------------------------------------------------
test("Implicit constructor", 1, function() {
Class('A', {
_:function(_cls, ancestor_name){
this.a = "ancestor";
this.b = ancestor_name;
}
});
Class('T', ns.A, {
res: function(){return this.a + " " + this.b}
});
var t = new ns.T("Sam", "Guy");
eq(t.res(), "ancestor Sam", 'work properly with super classes, ignores extra parameters')
});
//-------------------------------------------------------------------------------------------------------------------
test("Explicit shortcut - no SuperClass", 3, function() {
Class('T', {
_: 'country, flag_color'
});
var t = new ns.T("China", "Red");
ok(true, dump(t));
eq(t.country, "China", 'initializes parameter');
eq(t.flag_color, "Red", 'initializes parameter')
});
//-------------------------------------------------------------------------------------------------------------------
test("Explicit shortcut with super args but no SuperClass (Error)", function() {
throws(function(){
Class('T', {
_: 'country, flag_color',
__:1
})
},
Jassino.ConstructorError,
"not allowed"
);
throws(function(){
Class('T', {
_: ['blabla', '']
})
},
Jassino.ConstructorError,
"not allowed"
);
throws(function(){
Class('T', {
_: ['', '']
})
},
Jassino.ConstructorError,
"not allowed"
);
});
//-------------------------------------------------------------------------------------------------------------------
test("Explicit shortcut constructor with super class", function() {
Class('A', {
protovar: "proto",
_:function(_cls, ancestorName){
this.rootSuper = _cls.__super__;
this.a = "ancestor";
this.a1 = ancestorName;
}
});
Class('B', ns.A, {
_: 'anynameX, b',
__: 1
});
Class('T', ns.B, {
_:'anyname1, anyname2, c',
__: 2,
res: function(){return this.protovar + " " +
this.a + " " + this.a1 + " " + this.b + " " + this.c}
});
var t = new ns.T("Sam", "b", "c");
eq(t.rootSuper, undefined, '1st cclass has no super');
eq(t.anyname1, undefined, 'ancestor arguments are not defined');
eq(t.anyname2, undefined, 'ancestor arguments are not defined');
eq(t.anynameX, undefined, 'ancestor arguments are not defined');
eq(t.res(), "proto ancestor Sam b c", 'work properly with super classes, ignores extra parameters')
});
test("Explicit shortcut constructor, explicit full constructor, __super__ and super method call", function() {
Class('A', {
_: 'a, b'
});
Class('T', ns.A, {
_: function(_cls, a, b, c){
this.super(_cls, a, b);
this.c = c;
},
res: function(additional){
return this.a + this.b + this.c + additional;
}
});
Class('X', ns.T, {
_:'1, 2, 3, d',
__:3,
res: [$$.clsArg, function(_cls, additional){
return this.supercall(_cls, "res", additional);
}]
});
var t = new ns.X("a", "b", "c", "d");
eq(t.res("ZZZ"), "abcZZZ", 'super method call works');
eq(t.d, "d", 'adding parameters to child constructor works')
});
//========================================================================================================================
module("Class instantiation consistency", default_up_down);
test("Multiple instantiation test", function() {
Class('A', {
_: 'x, y',
cls: {
Z: 78
},
a: 4,
af: function(){return this.a + this.x + this.y}
});
var a = new ns.A(1, 2);
var b = new ns.A(8, 16);
eq(a.a, 4);
eq(b.a, 4);
eq(b.x + b.y, 8 + 16);
eq(a.af(), 1 + 2 + 4);
eq(b.af(), 8 + 16 + 4);
eq(ns.A.cls.Z, 78);
});
test("Don't forgot the'new' !!!", function() {
Class('A', {});
Class('B', ns.A, {});
Class('C', {_:function(_cls, x){}});
Class('D', ns.C, {
_:'x, y',
__:1
});
Class('E', {_:'a, b'});
throws(function(){ns.A()}, Jassino.InstantiationError, "Instantiation error raised");
throws(function(){ns.B()}, Jassino.InstantiationError, "Instantiation error raised 2");
throws(function(){ns.C(5)}, Jassino.InstantiationError, "Instantiation error raised 3");
throws(function(){ns.D(6,7)}, Jassino.InstantiationError, "Instantiation error raised 4");
throws(function(){ns.E(8,9)}, Jassino.InstantiationError, "Instantiation error raised 5");
new ns.A();
new ns.B();
new ns.C(5);
new ns.D(6,7);
new ns.E(8,9);
});
//========================================================================================================================
module("Class meta definitions", default_up_down);
test("Multiple instantiation, __child__, __name__, __super__", function() {
Class('A', {
m:function(a ,b){return 1 + a + b;},
theAMethod: function () {
return this.__child__;
}
});
Class('B', ns.A, {
m: [$$.clsArg, function(_cls, a, b){
return this.supercall(_cls, "m", 10, 20);
}],
theBMethod: function () {
return this.__child__;
}
});
Class('C', ns.B, {
});
eq(ns.A.__name__, 'A', 'A name');
eq(ns.B.__name__, 'B', 'B name');
eq(ns.B.__super__, ns.A, 'A instance');
eq(ns.B.__super__.__name__, 'A', 'B super name is A');
var b = new ns.B();
eq(b.m(), 1 + 10 + 20, "super method test: regular method");
eq(b.theAMethod(), ns.B, 'B');
eq(b.__child__, ns.B, 'B');
eq(b.__child__.__name__, 'B', 'B name');
var c = new ns.C();
eq(c.theAMethod(), ns.C, 'C');
eq(c.theBMethod(), ns.C, 'C');
eq(c.__child__, ns.C, 'C');
eq(c.__child__.__name__, 'C', 'C name');
});
//========================================================================================================================
module("Mixin inheritance", default_up_down);
test("Single inheritance", 15, function() {
Mixin('A', {
a: 'a',
ov: 'ov',
af: function(){return [this.a, this.ov]},
ovf: function(){ return [this.a, this.ov];}
});
Mixin('T', [ns.A], {
t: 'T',
ov: 'T_ov',
tf: function(){return [this.t, this.ov, this.a];},
ovf: function(){ return [this.t, this.ov, this.a];}
});
eq(ns.A.af()[0], 'a', 'A.af() - ancestor\'s members do not corrupted')
eq(ns.A.af()[1], 'ov', 'A.af() - ancestor\'s members do not corrupted 2')
eq(ns.A.ovf()[0], 'a', 'A.ovf() - ancestor\'s members do not corrupted')
eq(ns.A.ovf()[1], 'ov', 'A.ovf() - ancestor\'s members do not corrupted 2')
eq(ns.T.t, 'T', 't = "T" - own members not corrupted')
eq(ns.T.ov, 'T_ov', 't = "T" - own overriden members are correct')
eq(ns.T.a, 'a', 'T.a = "a" - inherited variable')
eq(ns.T.tf()[0], 'T', 'Own T.tf(), should correctly access ancestor/overriden fields')
eq(ns.T.tf()[1], 'T_ov', 'Own T.tf(), should correctly access ancestor/overriden fields 2')
eq(ns.T.tf()[2], 'a', 'Own T.tf(), should correctly access ancestor/overriden fields 3')
eq(ns.T.af()[0], 'a' , 'Virtual function effect: Inherited A.af() -> T.af(), overriden variables should be changed')
eq(ns.T.af()[1], 'T_ov' , 'Virtual function effect: Inherited A.af() -> T.af(), overriden variables should be changed 2')
eq(ns.T.ovf()[0], 'T', 'Overriden A.ovf() -> T.ovf() - output should be as in T.tf()')
eq(ns.T.ovf()[1], 'T_ov', 'Overriden A.ovf() -> T.ovf() - output should be as in T.tf() 2')
eq(ns.T.ovf()[2], 'a', 'Overriden A.ovf() -> T.ovf() - output should be as in T.tf() 3')
});
//-------------------------------------------------------------------------------------------------------------------
test("Multiple inheritance", 6, function() {
Mixin('A', {
a: 'a',
ovf: function(){return 'A'},
anc_ovf: function(){ return 'anc_A';},
anc_ovf2: function(){ return 'anc_A_2';}
});
Mixin('B', {
b: 'b',
ovf: function(){return 'B'},
anc_ovf: function(){ return 'anc_B';},
anc_ovf2: function(){ return 'anc_B_2';}
});
Mixin('C', {
c: 'c',
anc_ovf: function(){ return 'anc_C';}
});
Mixin('T', [ns.A, ns.B, ns.C], {
ovf: function(){ return 'T';}
});
eq(ns.T.a, 'a', 'inherited variable');
eq(ns.T.b, 'b', 'inherited variable 2');
eq(ns.T.c, 'c', 'inherited variable 3');
eq(ns.T.ovf(), 'T', 'Overriden ovf() -> T.ovf()');
eq(ns.T.anc_ovf(), 'anc_C', 'Inheritance order: override 1 - last override in C');
eq(ns.T.anc_ovf2(), 'anc_B_2', 'Inheritance order: override 2 - last override in B');
});
//========================================================================================================================
test("Inheritance transitive law", 4, function() {
Mixin('A', {
a: 'a',
ovf: function(){return 'A'},
anc_ovf: function(){ return 'anc_A';}
});
Mixin('B', [ns.A], {
b: 'b',
ovf: function(){return 'B'},
anc_ovf: function(){ return 'anc_B';}
});
Mixin('T', [ns.B], {
ovf: function(){ return 'T';}
});
eq(ns.T.a, 'a', 'inherited variable');
eq(ns.T.b, 'b', 'inherited variable 2');
eq(ns.T.ovf(), 'T', 'Overriden ovf() -> T.ovf()');
eq(ns.T.anc_ovf(), 'anc_B', 'Inheritance override stack: last override happened in B')
});
//========================================================================================================================
module("Classes mixed-in with Mixins", default_up_down);
test("Single mixin", 1, function() {
Mixin('T1', {t1: "a"});
Class('C1', [ns.T1], {
a: "CLS",
f: function(){return this.a + this.t1}
});
eq((new ns.C1()).f(), "CLSa")
});
//-------------------------------------------------------------------------------------------------------------------
test("Multiple mixins", 2, function() {
Mixin('T1', {t1: "a", xf: function(){return "1"}});
Mixin('T2', {t2: "b"});
Mixin('T3', [ns.T2], {t3: function(){return this.t2 + "c"}});
Class('C', [ns.T1, ns.T2, ns.T3], {
a: "CLS",
xf: function(){return "2"},
f: function(){return this.a + this.t1 + this.t3()}
});
var inst = new ns.C();
eq(inst.f(), "CLSabc", "accessing members");
eq(inst.xf(), "2", "overriding mixin members");
});
//========================================================================================================================
module("Class inheritance", default_up_down);
//-------------------------------------------------------------------------------------------------------------------
test("Inheritance: members test", function() {
Class('A', {
a: 'a',
ov: 'ov',
ex2: 78,
_:'c1, c2',
af: function(){return [this.a, this.ov]},
ovf: function(){ return [this.a, this.ov];}
});
Class('T', ns.A, {
t: 'T',
ov: 'T_ov',
_: 'c1, c2, c_ov',
__: 2,
tf: function(){return [this.t, this.ov, this.a];},
ovf: function(){ return [this.t, this.ov, this.a];}
});
var a = new ns.A(1, 2), t = new ns.T(3, 4, 5);
eq(a.af()[0], 'a', 'A.af() - ancestor\'s members do not corrupted');
eq(a.af()[1], 'ov', 'A.af() - ancestor\'s members do not corrupted 2');
eq(a.ovf()[0], 'a', 'A.ovf() - ancestor\'s members do not corrupted');
eq(a.ovf()[1], 'ov', 'A.ovf() - ancestor\'s members do not corrupted 2');
eq(a.ex2, 78, 'A().c_ov - ancestor\'s members do not corrupted 2');
eq(t.t, 'T', 't = "T" - own members not corrupted');
eq(t.ov, 'T_ov', 't = "T" - own overriden members are correct');
eq(t.a, 'a', 'T.a = "a" - inherited variable');
eq(t.tf()[0], 'T', 'Own T.tf(), should correctly access ancestor/overriden fields');
eq(t.tf()[1], 'T_ov', 'Own T.tf(), should correctly access ancestor/overriden fields 2');
eq(t.tf()[2], 'a', 'Own T.tf(), should correctly access ancestor/overriden fields 3');
eq(t.af()[0], 'a' , 'Virtual function effect: Inherited A.af() -> T.af(), overriden variables should be changed');
eq(t.af()[1], 'T_ov' , 'Virtual function effect: Inherited A.af() -> T.af(), overriden variables should be changed 2');
eq(t.ovf()[0], 'T', 'Overriden A.ovf() -> T.ovf() - output should be as in T.tf()');
eq(t.ovf()[1], 'T_ov', 'Overriden A.ovf() -> T.ovf() - output should be as in T.tf() 2');
eq(t.ovf()[2], 'a', 'Overriden A.ovf() -> T.ovf() - output should be as in T.tf() 3');
eq(t.c_ov, 5, 'T().c_ov - overridden')
});
//-------------------------------------------------------------------------------------------------------------------
test("Inheritance from usual Prototype-Based pseudo class)", 12, function() {
function A(constr_var){
this.constr_var = constr_var
}
A.prototype.a = 'a';
A.prototype.ov = 'ov';
A.prototype.af = function(){return [this.a, this.ov]};
A.prototype.ovf = function(){ return [this.a, this.ov];};
Class('T', A, {
_:'constr_var',
__: 1,
t: 'T',
ov: 'T_ov',
tf: function(){return [this.t, this.ov, this.a];},
ovf: function(){ return [this.t, this.ov, this.a];}
});
var t = new ns.T('CONSTRUCTION');
eq(t.constr_var, 'CONSTRUCTION', 't = super constructor works');
eq(t.t, 'T', 't = "T" - own members not corrupted');
eq(t.ov, 'T_ov', 't = "T" - own overridden members are correct');
eq(t.a, 'a', 'T.a = "a" - inherited variable');
eq(t.tf()[0], 'T', 'Own T.tf(), should correctly access ancestor/overriden fields');
eq(t.tf()[1], 'T_ov', 'Own T.tf(), should correctly access ancestor/overriden fields 2');
eq(t.tf()[2], 'a', 'Own T.tf(), should correctly access ancestor/overriden fields 3');
eq(t.af()[0], 'a' ,
'Virtual function effect: Inherited A.af() -> T.af(), overriden variables should be changed');
eq(t.af()[1], 'T_ov' ,
'Virtual function effect: Inherited A.af() -> T.af(), overriden variables should be changed 2');
eq(t.ovf()[0], 'T', 'Overridden A.ovf() -> T.ovf() - output should be as in T.tf()');
eq(t.ovf()[1], 'T_ov', 'Overridden A.ovf() -> T.ovf() - output should be as in T.tf() 2');
eq(t.ovf()[2], 'a', 'Overridden A.ovf() -> T.ovf() - output should be as in T.tf() 3');
});
//-------------------------------------------------------------------------------------------------------------------
test("Inheritance transitive law", 4, function() {
Class('A', {
a: 'a',
ovf: function(){return 'A'},
anc_ovf: function(){ return 'anc_A';}
});
Class('B', ns.A, {
b: 'b',
ovf: function(){return 'B'},
anc_ovf: function(){ return 'anc_B';}
});
Class('T', ns.B, {
ovf: function(){ return 'T';}
});
var t = new ns.T();
eq(t.a, 'a', 'inherited variable');
eq(t.b, 'b', 'inherited variable 2');
eq(t.ovf(), 'T', 'Overriden ovf() -> T.ovf()');
eq(t.anc_ovf(), 'anc_B', 'Inheritance override stack: last override happened in B');
});
//-------------------------------------------------------------------------------------------------------------------
test("DEEP inheritance", 1, function() {
var LEVEL = 100;
var nspace = {};
Class(nspace, 'C0', {
get_accum: function(){ return this.accum },
_: function(_cls, i){
this.accum += "{C0: {}}"
} //constructor
});
var self, i;
for (i=1; i < LEVEL; i++){
self = 'C' + i.toString();
var ancestor = 'C' + (i - 1).toString();
Class(nspace, self, nspace[ancestor], {
_: function(_cls, i){
this.accum = '{C' + i.toString() + ": " + (this.accum || "");
this.super(_cls, i - 1);
this.accum += "}"
} //constructor
})
}
var accum = (new nspace[self](i - 1)).get_accum();
eq( accum.replace(/[\w{}\s]+/g, "").length, i, "Here is chain: " + dump(accum))
});
//========================================================================================================================
module("Class members (aka static)", default_up_down);
test("basic test", function() {
Class('T', {
_: 'a',
b: function(){ return "Hello!"},
cls: {
a: 8,
b: function () {
return (this.a + 12) + this.__child__.__name__;
},
c: null,
d: undefined
}
});
eq(ns.T.cls.a, 8, 'static variable');
eq(ns.T.cls.b(), '20T', 'static method');
eq(ns.T.cls.c, null, 'robustness test: null');
eq(ns.T.cls.d, undefined, 'robustness test: undefined');
var t = new ns.T(10);
eq(ns.T.cls.a, 8, 'static variable (not changed by inst.)');
eq(ns.T.cls.c, null, 'robustness test: null (not changed by inst.)');
eq(ns.T.cls.d, undefined, 'robustness test: undefined (not changed by inst.)');
eq(t.a, 10, 'instance var is not overridden');
eq(t.b(), 'Hello!', 'instance member working ok');
});
//========================================================================================================================
module("Decorators test", default_up_down);
test("cached test", function() {
var counter = 0;
Class('T', {
memento: [$$.cached, function(){
counter++;
return "Hello" + counter;
}]
});
var t = new ns.T();
eq(t.memento(), "Hello1", 'first call leads to execution');
eq(t.memento(), "Hello1", 'second call - is just a cache');
eq(counter, 1, 'counter unchanged');
});
test("multiple decorators", function() {
var counter = 0;
Class('TheClass', {
memento: [$$.cached, $$.clsArg, function(_cls, arg1){
counter++;
return "Hello" + counter + _cls.__name__ + arg1;
}]
});
var t = new ns.TheClass();
eq(t.memento('X'), "Hello1TheClassX", 'first call leads to execution');
eq(t.memento('Y'), "Hello1TheClassX", 'second call - is just a cache, X instead of Y');
eq(counter, 1, 'counter unchanged');
});
//========================================================================================================================
module("Class/Mixin combined tests", default_up_down);
//-------------------------------------------------------------------------------------------------------------------
test("Rewritten example from my-class (http://myjs.fr/my-class/) - NO INFINITE RECURSION!", 1, function() {
Class('Person', {
old_method: function(){ return "Hey!, "},
_: function(_cls, name){
this.name=name
} //constructor
});
Class('Dreamer', ns.Person, {
_:'name, dream', //constructor shortcut: name -> super call, dream -> this.dream
__: 1
});
var custom_ns = {};
Class(custom_ns, 'Nightmarer', ns.Dreamer, {
field: "dreams about", //"instance constant" - added to every instance, may be overridden in constructor
old_method: function(){ return "Okay, "}, //overriding
_:function(_cls, name, dream){
this.super(_cls, name, dream);
this.field = this.field.toUpperCase(); //control flow should be reached and field created
},
test: [$$.clsArg, function(_cls){ return this.supercall(_cls, "old_method") +
this.old_method() +
this.name + " " + this.field + " " + this.dream
}]
});
var nm = new custom_ns.Nightmarer("Lissa", "Pie");
strictEqual(nm.test(), "Hey!, Okay, Lissa DREAMS ABOUT Pie", "test to not go into infinite recursion!")
});
//========================================================================================================================
module("COMPLEX EXAMPLE", default_up_down);
test("Bees Simplified [not finished]", function() {
/***************************************************************************************
* Full featured Example
*************************************************************************************/
//convenient variables
Jassino.setDefaultNS(window);
//Namespace. It may contain members as well
var Bees = {};
//---------------------------------------------------------------
Mixin('Movable', {
total_distance: 0,
location: {x:0, y:0}, //var members also possible in mixins,
move_to: function(loc){
var t = this;
t.total_distance = t.total_distance + Math.sqrt( Math.pow(loc.x - t.location.x, 2)
+ Math.pow(loc.y - t.location.y, 2) );
t.location = loc
}
});
Mixin(Bees, 'Flyable', [Movable], { //explicit namespace
flight_hours: 0,
fly_to: function(loc){
this.move_to(loc);
this.flight_hours = this.total_distance * 0.001 // hours per meter
}
});
Mixin('Workable', {
work_on: function(place){return "Working on " + place.toString()}
});
//---------------------------------------------------------------
Class('Bee', {
_: 'name, lifespan',
cls: {
WINGS: {
oscillating: 1
}
}
});
Class(Bees, 'FemaleBee', Bee, {
_: function(_cls, name){ //Explicit constructor
this.super(_cls,'F'); //super constructor call
this.name=name;
} //constructor
});
Class(Bees, 'MaleBee', Bee, [Bees.Flyable], {
_: 'name, lifespan',
__: 2
});
Class(Bees, 'Queen', Bees.FemaleBee, {
_: function(_cls, name){ //Full form of constructor
this.super(_cls, 'F'); //super constructor call
this.name=name;
this.wingType = _cls.cls.WINGS.oscillating;
} //constructor
});
Class(Bees, 'Worker', Bees.FemaleBee, [Workable, Bees.Flyable], {
//Sort Constructor form, means:
// On declaration time, generate constructor accepting 2 parameters,
// first parameter pass to super constructor
// second parameter write to this.name
_: 'gender, name',
__:1,
get_productiveness: function(){ return this.productiveness }
});
//----------------------------------------------------------------------------------------------
var Places = {};
function Place(visitors_capacity){ //native prototype-based class
this.visitors_capacity = visitors_capacity
this.visitors = []
}
Place.prototype.add_visitor = function(vis){
if (this.visitors.length < this.visitors_capacity){
this.visitors_capacity.push(vis);
return true
}else{
return false
}
};
Place.prototype.remove_visitor = function(vis){
if (this.visitors.length < this.visitors_capacity){
this.visitors_capacity.push(vis);
return true
}else{
return false
}
};
Class(Places, 'Plant', Place, {
$: 'Place' //inheriting from native pseudo class requires its explicit name
});
Class(Places, 'Flower', Places.Plant, {
nectar: 5,
full_amount: 5,
restore_point: -5,
get_nectar: function(){
var t = this;
if (t.nectar < t.restore_point){
t.nectar = t.full_amount
}
this.nectar > 0 ? (this.nectar--, 1) : 0 }
});
Class(Places, 'Weed', Places.Plant, {
nectar: 0,
get_nectar: function(){ this.nectar ? (this.nectar--, 1) : 0 }
});
//----------------------------------------------------------------------------------------------
Class('BeeKeeper', [Workable], {
});
//---------------------------------------------------------------
Class(Places, 'Hive', Place, [Movable], {
//Constructor shortcut (SuperClass-less form)
//means: take first argument from constructor and place it into this.bees
_: 'visitors_capacity, bees',
__: 1,
get_most_productive: function(){
if ( ! this.bees) return null;s
var most = bees[0];
for (var bee in this.bees){
if (bee.get_productiveness() > most.get_productiveness())
most = bee;
}
return most
}
});
//***************************************************************************
var hive = new Places.Hive(20, //20 visitors max
[new Bees.Queen('')])
//hive.get_most_productive().gather(place)
var queen = new Bees.Queen('Victoria');
eq(queen.wingType, 1);
eq(1,1, "stub")
});