This repository has been archived by the owner on May 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
289 lines (241 loc) · 8.53 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
'use strict';
let assert = require('chai').assert;
let KindaObject = require('./src');
suite('KindaObject', function() {
suite('Simple classes hierarchy', function() {
let Foo = KindaObject.extend('Foo', function() {
this.cool = 'very';
});
Foo.hello = 'Hello';
Foo._bye = 'Bye';
let Bar = Foo.extend('Bar', function() {
this.isCold = function() {
return this.cool === 'very' ? 'yes' : 'no';
};
});
let Baz = KindaObject.extend('Baz', function() {
this.include(Bar);
});
let UnnamedClass = Foo.extend();
suite('Class methods', function() {
test('extend()', function() {
assert.strictEqual(Bar.hello, 'Hello');
assert.isUndefined(Bar._bye);
});
test('name', function() {
assert.strictEqual(Foo.name, 'Foo');
assert.strictEqual(UnnamedClass.name, 'SubFoo');
});
test('instantiate()', function() {
let bar = Bar.instantiate();
assert.strictEqual(bar.cool, 'very');
assert.isFunction(bar.isCold);
});
test('create() and new', function() {
let Person = KindaObject.extend('Person', function() {
this.creator = function(firstname, lastname) {
this.firstname = firstname;
this.lastname = lastname;
};
});
let person1 = Person.create('Jean', 'Dupont');
assert.strictEqual(person1.firstname, 'Jean');
assert.strictEqual(person1.lastname, 'Dupont');
let person2 = new Person('Pierre', 'Durand');
assert.strictEqual(person2.firstname, 'Pierre');
assert.strictEqual(person2.lastname, 'Durand');
});
test('prototype', function() {
let bar = Bar.instantiate();
assert.strictEqual(Bar.prototype.isCold, bar.isCold);
});
test('isKindaClass', function() {
assert.isTrue(Bar.isKindaClass);
});
test('isClassOf()', function() {
let baz = Baz.instantiate();
assert.isTrue(Baz.isClassOf(baz));
assert.isTrue(Bar.isClassOf(baz));
assert.isTrue(Foo.isClassOf(baz));
assert.isTrue(KindaObject.isClassOf(baz));
let Qux = KindaObject.extend('Qux');
assert.isFalse(Qux.isClassOf(baz));
});
test('unserialize()', function() {
let Person = KindaObject.extend('Person', function() {
this.unserializer = function(json) {
this.firstname = json.firstname;
this.lastname = json.lastname;
};
});
let person = Person.unserialize({ firstname: 'Jean', lastname: 'Dupont' });
assert.strictEqual(person.firstname, 'Jean');
assert.strictEqual(person.lastname, 'Dupont');
});
});
suite('Instance methods', function() {
test('include()', function() {
let baz = Baz.instantiate();
assert.strictEqual(baz.cool, 'very');
assert.isFunction(baz.isCold);
});
test('class', function() {
let foo = Foo.instantiate();
assert.strictEqual(foo.class, Foo);
});
test('superclasses', function() {
let foo = Foo.instantiate();
assert.deepEqual(foo.superclasses, [KindaObject]);
let bar = Bar.instantiate();
assert.deepEqual(bar.superclasses, [KindaObject, Foo]);
let baz = Baz.instantiate();
assert.deepEqual(baz.superclasses, [KindaObject, Foo, Bar]);
});
test('isInstanceOf()', function() {
let foo = Foo.instantiate();
assert.isTrue(foo.isInstanceOf(Foo));
assert.isFalse(foo.isInstanceOf(Bar));
});
test('serialize()', function() {
let Person = KindaObject.extend('Person', function() {
this.serializer = function() {
return { firstname: this.firstname, lastname: this.lastname };
};
});
let person = Person.create();
person.firstname = 'Jean';
person.lastname = 'Dupont';
let json = person.serialize();
assert.deepEqual(
json,
{ firstname: 'Jean', lastname: 'Dupont' }
);
});
test('clone()', function() {
let Person = KindaObject.extend('Person', function() {
this.serializer = function() {
return { firstname: this.firstname, lastname: this.lastname };
};
this.unserializer = function(json) {
this.firstname = json.firstname;
this.lastname = json.lastname;
};
});
let person = Person.create();
person.firstname = 'Jean';
person.lastname = 'Dupont';
let copy = person.clone();
assert.notEqual(copy, person);
assert.equal(copy.firstname, person.firstname);
assert.equal(copy.lastname, person.lastname);
});
});
});
suite('Object constructor', function() {
test('simple class', function() {
let French = KindaObject.extend('French', {
hello: 'Bonjour',
bye: 'Au revoir'
});
assert.strictEqual(French.prototype.hello, 'Bonjour');
assert.strictEqual(French.prototype.bye, 'Au revoir');
});
});
suite('Name conflict', function() {
test('extend a class with the same name', function() {
let Person = KindaObject.extend('Person', function() {
this.isNice = 'yes';
this.isCool = 'yes';
});
Person = Person.extend('Person', function() {
this.isCool = 'absolutely';
});
let person = Person.instantiate();
assert.strictEqual(person.isNice, 'yes');
assert.strictEqual(person.isCool, 'absolutely');
Person = Person.extend('Person', function() {
this.isCool = 'definitely';
});
person = Person.instantiate();
assert.strictEqual(person.isNice, 'yes');
assert.strictEqual(person.isCool, 'definitely');
});
});
suite('Diamond problem', function() {
test('top constructor should only be called once', function() {
let count = 0;
let Top = KindaObject.extend('Top', function() {
count++;
});
assert.strictEqual(count, 1);
let Left = Top.extend('Left');
assert.strictEqual(count, 2);
let Right = Top.extend('Right');
assert.strictEqual(count, 3);
Top.extend('Bottom', function() {
this.include(Left);
this.include(Right);
});
assert.strictEqual(count, 4);
});
});
suite('Versioning', function() {
test('version', function() {
let A = KindaObject.extend('A');
assert.isUndefined(A.version);
let B = KindaObject.extend('B', '0.1.0');
assert.strictEqual(B.version, '0.1.0');
});
test('isInstanceOf()', function() {
let A010 = KindaObject.extend('A', '0.1.0');
assert.isTrue(A010.prototype.isInstanceOf(A010));
let A015 = KindaObject.extend('A', '0.1.5');
assert.isTrue(A010.prototype.isInstanceOf(A015));
assert.isFalse(A015.prototype.isInstanceOf(A010));
let B015 = KindaObject.extend('B', '0.1.5');
assert.isFalse(A010.prototype.isInstanceOf(B015));
let A024 = KindaObject.extend('A', '0.2.4');
assert.isFalse(A015.prototype.isInstanceOf(A024));
assert.isFalse(A024.prototype.isInstanceOf(A015));
let A117 = KindaObject.extend('A', '1.1.7');
let A141 = KindaObject.extend('A', '1.4.1');
assert.isTrue(A117.prototype.isInstanceOf(A141));
assert.isFalse(A141.prototype.isInstanceOf(A117));
let A202 = KindaObject.extend('A', '2.0.2');
assert.isFalse(A117.prototype.isInstanceOf(A202));
assert.isFalse(A202.prototype.isInstanceOf(A117));
});
test('include()', function() {
let a010Constructed, a015Constructed;
let A010 = KindaObject.extend('A', '0.1.0', function() {
a010Constructed = true;
});
let A015 = KindaObject.extend('A', '0.1.5', function() {
a015Constructed = true;
});
let A020 = KindaObject.extend('A', '0.2.0');
a010Constructed = false;
a015Constructed = false;
(KindaObject.extend(function() {
this.include(A010);
this.include(A015);
})).instantiate();
assert.isTrue(a010Constructed);
assert.isTrue(a015Constructed);
a010Constructed = false;
a015Constructed = false;
(KindaObject.extend(function() {
this.include(A015);
this.include(A010);
})).instantiate();
assert.isTrue(a015Constructed);
assert.isFalse(a010Constructed);
assert.throws(function() {
(KindaObject.extend(function() {
this.include(A015);
this.include(A020);
})).instantiate();
});
});
});
});