1 | /* Javascript Object Inheritance Implementation ______ ________ |
||
2 | * (c) 2016 <[email protected]> __ / / __ \/ _/ _/ |
||
3 | * Licensed under MIT. / // / /_/ // /_/ / |
||
4 | * ------------------------------------------------------ \___/\____/___/__*/ |
||
5 | var JOII = require('../../dist/joii').JOII; |
||
6 | |||
7 | /** |
||
8 | * Tests getters, setters and inheritance of instantiated class defintions. |
||
9 | */ |
||
10 | test('ClassBuilder:SerializeTest', function(assert) { |
||
11 | |||
12 | // BaseClass declaration with 3 public properties. We'll be extending on |
||
13 | // this class to see if the integrity of these values stay intact within |
||
14 | // the correct context. |
||
15 | var BaseClass = JOII.ClassBuilder('BaseClass', {}, { |
||
0 ignored issues
–
show
|
|||
16 | 'public a': 1, |
||
17 | 'public notserializable b': 'foo', |
||
18 | 'protected serializable c': 'bar', |
||
19 | 'protected d': '2', |
||
20 | 'public verifyDefaultValues': function (name) |
||
21 | { |
||
22 | if (!name) |
||
23 | { |
||
24 | name = 'Base'; |
||
25 | } |
||
26 | assert.equal(this.getA(), 1, name + '.getA() == 1'); |
||
27 | assert.equal(this.getB(), 'foo', name + '.getB() == foo'); |
||
28 | assert.equal(this.getC(), 'bar', name + '.getC() == bar'); |
||
29 | assert.equal(this.getD(), 2, name + '.getD() == 2'); |
||
30 | }, |
||
31 | 'public changeValues': function () |
||
32 | { |
||
33 | this.setA(100); |
||
34 | this.setB('foo2'); |
||
35 | this.setC('bar2'); |
||
36 | this.setD(200); |
||
37 | }, |
||
38 | 'public verifyChangedValues': function () |
||
39 | { |
||
40 | assert.equal(this.getA(), 100, 'Base.getA() == 100'); |
||
41 | assert.equal(this.getB(), 'foo', 'Base.getB() == foo'); // not serialized, so should still be the original |
||
42 | assert.equal(this.getC(), 'bar2', 'Base.getC() == bar2'); |
||
43 | assert.equal(this.getD(), 2, 'Base.getD() == 2'); // not serialized, so should still be the original |
||
44 | } |
||
45 | }); |
||
46 | var Child1 = JOII.ClassBuilder('Child1', { 'extends': BaseClass }, { |
||
47 | 'public e': 3, |
||
48 | 'public verifyDefaultValues': function () |
||
49 | { |
||
50 | this['super']('verifyDefaultValues', 'Child1'); |
||
51 | assert.equal(this.getE(), 3, 'Base.getE() == 3'); |
||
52 | }, |
||
53 | 'public changeValues': function () { |
||
54 | this.setA(1000); |
||
55 | this.setB('foo21'); |
||
56 | this.setC('bar21'); |
||
57 | this.setD(2000); |
||
58 | this.setE(3000); |
||
59 | }, |
||
60 | 'public verifyChangedValues': function () { |
||
61 | assert.equal(this.getA(), 1000, 'Base.getA() == 1000'); |
||
62 | assert.equal(this.getB(), 'foo', 'Base.getB() == foo'); // not serialized, so should still be the original |
||
63 | assert.equal(this.getC(), 'bar21', 'Base.getC() == bar21'); |
||
64 | assert.equal(this.getD(), 2, 'Base.getD() == 2'); // not serialized, so should still be the original |
||
65 | assert.equal(this.getE(), 3000, 'Base.getE() == 3000'); |
||
66 | } |
||
67 | }); |
||
68 | var Child2 = JOII.ClassBuilder('Child2', { 'extends': BaseClass }, { |
||
69 | 'protected f': 4, |
||
70 | 'public verifyDefaultValues': function () { |
||
71 | this['super']('verifyDefaultValues', 'Child2'); |
||
72 | assert.equal(this.getF(), 4, 'Base.getF() == 4'); |
||
73 | }, |
||
74 | 'public changeValues': function () { |
||
75 | this.setA(10000); |
||
76 | this.setB('foo22'); |
||
77 | this.setC('bar22'); |
||
78 | this.setD(20000); |
||
79 | this.setF(40000); |
||
80 | }, |
||
81 | 'public verifyChangedValues': function () { |
||
82 | assert.equal(this.getA(), 10000, 'Base.getA() == 10000'); |
||
83 | assert.equal(this.getB(), 'foo', 'Base.getB() == foo'); // not serialized, so should still be the original |
||
84 | assert.equal(this.getC(), 'bar22', 'Base.getC() == bar22'); |
||
85 | assert.equal(this.getD(), 2, 'Base.getD() == 2'); // not serialized, so should still be the original |
||
86 | assert.equal(this.getF(), 4, 'Base.getF() == 4'); // not serialized, so should still be the original |
||
87 | } |
||
88 | }); |
||
89 | |||
90 | var HolderClass = JOII.ClassBuilder({}, { |
||
91 | 'public BaseClass base': null, |
||
92 | 'public BaseClass c1': null, |
||
93 | 'public BaseClass c2': null, |
||
94 | construct: function() { |
||
95 | var base1 = new BaseClass(); |
||
96 | var c1 = new Child1(); |
||
97 | var c2 = new Child2(); |
||
98 | |||
99 | this.setBase(base1); |
||
100 | this.setC1(c1); |
||
101 | this.setC2(c2); |
||
102 | } |
||
103 | }); |
||
104 | |||
105 | var h1 = new HolderClass(); |
||
106 | var base1 = h1.getBase(); |
||
107 | var c1 = h1.getC1(); |
||
108 | var c2 = h1.getC2(); |
||
109 | |||
110 | var json = h1.serialize(); |
||
111 | |||
112 | var h2 = HolderClass.deserialize(json); |
||
113 | |||
114 | |||
115 | // change the values of the original objects, to make sure our data was actually restored from a serialized object, resulting in new data |
||
116 | base1.changeValues(); |
||
117 | c1.changeValues(); |
||
118 | c2.changeValues(); |
||
119 | |||
120 | // verify the default data |
||
121 | h2.getBase().verifyDefaultValues(); |
||
122 | h2.getC1().verifyDefaultValues(); |
||
123 | h2.getC2().verifyDefaultValues(); |
||
124 | |||
125 | // re-serialize with the changed values |
||
126 | var json2 = h1.serialize(); |
||
127 | var h3 = HolderClass.deserialize(json2); |
||
128 | |||
129 | // verify the changed values, for inheritance and expected changes based on serialized fields |
||
130 | h3.getBase().verifyChangedValues(); |
||
131 | h3.getC1().verifyChangedValues(); |
||
132 | h3.getC2().verifyChangedValues(); |
||
133 | |||
134 | |||
135 | var h4 = new HolderClass(); |
||
136 | base1 = h4.getBase(); |
||
137 | c1 = h4.getC1(); |
||
138 | c2 = h4.getC2(); |
||
139 | |||
140 | h4.deserialize(json2); |
||
141 | |||
142 | // verify the changed values, to make sure that the object references were kept intact |
||
143 | base1.verifyChangedValues(); |
||
144 | c1.verifyChangedValues(); |
||
145 | c2.verifyChangedValues(); |
||
146 | |||
147 | |||
148 | |||
149 | var NewClass = JOII.ClassBuilder('NewClass', {}, { |
||
150 | 'public object normal_object': null |
||
151 | }); |
||
152 | |||
153 | var nc = new NewClass(); |
||
154 | |||
155 | nc.setNormalObject([ |
||
156 | { |
||
157 | name: 'Some Name', |
||
158 | value: c1 |
||
159 | }, |
||
160 | c2 |
||
161 | ]); |
||
162 | |||
163 | var json3 = nc.serialize(); |
||
164 | |||
165 | var nc2 = NewClass.deserialize(json3); |
||
166 | |||
167 | nc2.getNormalObject()[0].value.verifyChangedValues(); |
||
168 | nc2.getNormalObject()[1].verifyChangedValues(); |
||
169 | |||
170 | }); |
||
171 |
Strict mode is a way to opt-in to a restricted variant of JavaScript. It eliminates some common pitfalls by being less lenient and raising more errors.
Besides, it is also used to fix certain mistakes which made it difficult for JavaScript runtimes to perform certain optimizations.
We generally recommend to only enable strict mode on the function scope and not in the global scope as that might break third-party scripts on your website.