This repository has been archived by the owner on Jun 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
249 lines (197 loc) · 7.22 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
'use strict';
import { assert } from 'chai';
import KindaExtendable from './src';
suite('KindaExtendable', function() {
test('simple extend', function() {
let Class = KindaExtendable.extend('Class', function() {
this.staticMethod = function() {
return 'Hello!';
};
});
assert.strictEqual(Class.name, 'Class');
assert.strictEqual(Class.staticMethod(), 'Hello!');
assert.deepEqual(Class.superclasses, [KindaExtendable]);
let Subclass = Class.extend('Subclass', function() {
this.otherStaticMethod = function() {
return 'Hello again!';
};
});
assert.strictEqual(Subclass.name, 'Subclass');
assert.strictEqual(Subclass.staticMethod(), 'Hello!');
assert.strictEqual(Subclass.otherStaticMethod(), 'Hello again!');
assert.deepEqual(Subclass.superclasses, [KindaExtendable, Class]);
});
test('simple include', function() {
let Mixin = KindaExtendable.extend('Mixin', function() {
this.mixinMethod = function() {
return 'Hello from Mixin!';
};
});
let Class = KindaExtendable.extend('Class', function() {
this.include(Mixin);
this.staticMethod = function() {
return 'Hello from Class!';
};
});
assert.strictEqual(Class.name, 'Class');
assert.strictEqual(Class.staticMethod(), 'Hello from Class!');
assert.strictEqual(Class.mixinMethod(), 'Hello from Mixin!');
assert.deepEqual(Class.superclasses, [KindaExtendable, Mixin]);
let Subclass = Class.extend('Subclass', function() {
this.otherStaticMethod = function() {
return 'Hello from Subclass!';
};
});
assert.strictEqual(Subclass.name, 'Subclass');
assert.strictEqual(Subclass.staticMethod(), 'Hello from Class!');
assert.strictEqual(Subclass.mixinMethod(), 'Hello from Mixin!');
assert.strictEqual(Subclass.otherStaticMethod(), 'Hello from Subclass!');
assert.deepEqual(Subclass.superclasses, [KindaExtendable, Mixin, Class]);
});
test('version', function() {
let Class1 = KindaExtendable.extend('Class1');
assert.isUndefined(Class1.version);
let Class2 = KindaExtendable.extend('Class2', '0.1.0');
assert.strictEqual(Class2.version, '0.1.0');
});
test('compare classes', function() {
let Class1 = KindaExtendable.extend('Class', '0.1.0');
assert.isTrue(Class1.isSameAsOrNewerThan(Class1, true));
assert.isFalse(Class1.isSameAsOrNewerThan(undefined, true));
let Class2 = KindaExtendable.extend('Class', '0.1.1');
assert.isTrue(Class2.isSameAsOrNewerThan(Class1, true));
assert.isFalse(Class1.isSameAsOrNewerThan(Class2, true));
let Class3 = KindaExtendable.extend('Class', '0.2.0');
assert.isFalse(Class3.isSameAsOrNewerThan(Class1, false));
assert.isFalse(Class1.isSameAsOrNewerThan(Class3, false));
assert.throws(function() {
Class3.isSameAsOrNewerThan(Class1, true);
});
});
test('prototype', function() {
let Class = KindaExtendable.extend('Class', function() {
this.prototype.method = function() {
return 'Hello!';
};
});
assert.strictEqual(Class.prototype.method(), 'Hello!');
});
test('extend with an object as builder', function() {
let French = KindaExtendable.extend('French', {
hello: 'Bonjour', // should go in the prototype
bye: 'Au revoir'
});
assert.strictEqual(French.prototype.hello, 'Bonjour');
assert.strictEqual(French.prototype.bye, 'Au revoir');
});
test('extend a class with the same name', function() {
let Class = KindaExtendable.extend('Class', function() {
this.nice = 'yes';
this.cool = 'yes';
});
assert.strictEqual(Class.nice, 'yes');
assert.strictEqual(Class.cool, 'yes');
Class = Class.extend('Class', function() {
this.cool = 'always';
});
assert.strictEqual(Class.nice, 'yes');
assert.strictEqual(Class.cool, 'always');
Class = Class.extend('Class', function() {
this.cool = 'definitely';
});
assert.strictEqual(Class.nice, 'yes');
assert.strictEqual(Class.cool, 'definitely');
});
test('include a class of the same name', function() {
let Class = KindaExtendable.extend('Class', function() {
this.nice = 'yes';
this.cool = 'yes';
});
assert.strictEqual(Class.nice, 'yes');
assert.strictEqual(Class.cool, 'yes');
let Class2 = KindaExtendable.extend('Class', function() {
this.cool = 'always';
});
let Subclass = Class.extend('Subclass', function() {
this.include(Class2); // should do nothing
});
assert.strictEqual(Subclass.nice, 'yes');
assert.strictEqual(Subclass.cool, 'yes');
});
test('diamond problem', function() {
let count = 0;
let Top = KindaExtendable.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);
let Bottom = Top.extend('Bottom', function() { // eslint-disable-line no-unused-vars
this.include(Left); // should not run the Top builder
this.include(Right); // should not run the Top builder
});
assert.strictEqual(count, 4);
});
test('include compatible and incompatible versions', function() {
let a010Built, a015Built;
let A010 = KindaExtendable.extend('A', '0.1.0', function() {
a010Built = true;
});
let A015 = KindaExtendable.extend('A', '0.1.5', function() {
a015Built = true;
});
let A020 = KindaExtendable.extend('A', '0.2.0');
a010Built = false;
a015Built = false;
KindaExtendable.extend(function() {
this.include(A010);
this.include(A015);
});
assert.isTrue(a010Built);
assert.isTrue(a015Built);
a010Built = false;
a015Built = false;
KindaExtendable.extend(function() {
this.include(A015);
this.include(A010); // should not be included
});
assert.isTrue(a015Built);
assert.isFalse(a010Built);
assert.throws(function() {
KindaExtendable.extend(function() {
this.include(A015);
this.include(A020); // should throw an error
});
});
});
test('patching', function() {
let A = KindaExtendable.extend('A', '0.1.0', function() {
this.greeting = 'Hi';
});
let isPatchingCount = 0;
let isPatchingToVersion010Count = 0;
let isPatchingToVersion011Count = 0;
let A2 = KindaExtendable.extend('A', '0.1.1', function() {
this.greeting = 'Hello';
if (this.isPatching()) isPatchingCount++;
if (this.isPatching('0.1.0')) isPatchingToVersion010Count++;
if (this.isPatching('0.1.1')) isPatchingToVersion011Count++;
});
assert.strictEqual(isPatchingCount, 0);
assert.strictEqual(isPatchingToVersion010Count, 0);
assert.strictEqual(isPatchingToVersion011Count, 0);
let M = A2.extend('M');
assert.strictEqual(isPatchingCount, 0);
assert.strictEqual(isPatchingToVersion010Count, 0);
assert.strictEqual(isPatchingToVersion011Count, 0);
let B = A.extend('B', function() {
this.include(M);
});
assert.strictEqual(isPatchingCount, 1);
assert.strictEqual(isPatchingToVersion010Count, 0);
assert.strictEqual(isPatchingToVersion011Count, 1);
assert.strictEqual(B.greeting, 'Hello');
});
});