/
test.html
461 lines (396 loc) · 18.4 KB
/
test.html
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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>JS.Class tests</title>
<script src="prototype.js" type="text/javascript" charset="utf-8"></script>
<script src="unittest.js" type="text/javascript" charset="utf-8"></script>
<script src="build/class.js" type="text/javascript" charset="utf-8"></script>
<link rel="stylesheet" href="test.css" type="text/css" media="screen" title="no title" charset="utf-8">
<script type="text/javascript" charset="utf-8">
var Animal = JS.Class({
extend: {
find: function(thing) { return 'Animal finds ' + thing; },
create: function(thing) { return this.find(thing) + ' and Animal creates ' + thing; }
},
initialize: function(name) {
this.name = String(name);
},
speak: function(stuff) {
return 'My name is ' + this.name + ' and I like ' + stuff;
}
});
var Bear = JS.Class(Animal, {
extend: {
create: function(thing) {
return this._super(thing) + ', but Bear creates other stuff'
}
},
speak: function(stuff) {
return this._super(stuff).toUpperCase();
}
});
var NoSuperBear = JS.Class(Bear, {
speak: function() { return this.name.toUpperCase(); }
});
var Koala = JS.Class(Bear, {
speak: function(stuff) {
return "I'm not really a Bear, but I do like " + stuff;
}
});
var Dog = new JS.Class(Animal, {
bark: function() { return this.name + ' says WOOF!'; }
});
Dog.Interface = new JS.Interface(['speak', 'bark']);
var Pitbull = JS.Class(Dog, {
extend: {
canBiteYourLegsOff: function() {
return this.biting;
},
biting: 'Of course it can'
},
speak: function() {
return this._super();
}
});
var Camel = new JS.Singleton(Animal, {
fillHumpsWithWater: function() {}
});
//--------------------------------------------------------------------------------
//--------------------------------------------------------------------------------
// Let's implement the classes from here: http://www.ajaxpath.com/javascript-inheritance
// and make sure Crockford's problems aren't encountered.
var BaseClass = JS.Class({
getName: function() { return 'BaseClass(' + this.getId() + ')'; },
getId: function() { return 1; }
});
var SubClass = JS.Class(BaseClass, {
getName: function() {
return 'SubClass(' + this.getId() + ') extends ' + this._super();
},
getId: function() { return 2; }
});
var TopClass = JS.Class(SubClass, {
getName: function() {
return 'TopClass(' + this.getId() + ') extends ' + this._super();
},
getId: function() {
return this._super();
}
});
//--------------------------------------------------------------------------------
//--------------------------------------------------------------------------------
var NativeClass = function() {};
NativeClass.prototype = {
getName: function() { return 'Native'; }
};
var ChildOfNativeClass = JS.Class(NativeClass, {
getName: function() {
return this._super();
}
});
//--------------------------------------------------------------------------------
//--------------------------------------------------------------------------------
var Paginator = JS.Class({
initialize: function(n) {
this.count = n;
this.createItems(n);
},
createItems: function(n) {
this.items = this.items || [];
for (var i = 0; i < n; i++)
this.items.push(new this.klass.Item(i));
}
});
Paginator.Item = JS.Class({
initialize: function(n) {
this.name = 'Item';
this.number = n;
},
getName: function() {
return this.name + ' ' + this.getNumber();
},
getNumber: function() {
return this.number;
}
});
var Gallery = JS.Class(Paginator, {
createItems: function(n) { this._super(4 * n); }
});
var ItemInheritor = JS.Class(Gallery);
ItemInheritor.Item = JS.Class(Gallery.Item, {
getNumber: function() { return this._super() + 7; }
});
//--------------------------------------------------------------------------------
//--------------------------------------------------------------------------------
var Enum = JS.Module({
each: function() {},
some: function() {},
included: function() {
Enum.hasBeenIncluded = Enum.hasBeenIncluded || 0;
Enum.hasBeenIncluded++;
}
});
var ActiveRecord = JS.Module({
include: Enum,
save: function() {},
validate: function() {},
attributes: function() {},
extend: {
find: function() {},
create: function() {}
}
});
var BlankClass = JS.Class();
BlankClass.include(ActiveRecord);
//--------------------------------------------------------------------------------
//--------------------------------------------------------------------------------
var InheritanceCatcher = JS.Class({
extend: {
inherited: function(klass) {
this.children.push(klass);
klass.log(this);
},
children: []
}
});
//--------------------------------------------------------------------------------
//--------------------------------------------------------------------------------
// Object similar to Ruby's Comparable module
var Comparable = new JS.Module({
extend: {
compare: function(a,b) {
return a.compareWith(b);
}
},
lt: function(object) {
return this.klass.compare(this, object) == -1;
},
lte: function(object) {
return this.klass.compare(this, object) < 1;
},
gt: function(object) {
return this.klass.compare(this, object) == 1;
},
gte: function(object) {
return this.klass.compare(this, object) > -1;
},
eq: function(object) {
return this.klass.compare(this, object) == 0;
}
});
// A 'comparable' class whose instances can be compared and sorted
var TodoItem = new JS.Class({
include: Comparable,
initialize: function(position) {
this.position = position;
},
compareWith: function(other) {
if (this.position < other.position)
return -1;
else if (this.position > other.position)
return 1;
else
return 0;
}
});
</script>
</head>
<body>
<div id="testlog"></div>
<script type="text/javascript" charset="utf-8">
new Test.Unit.Runner({
testSuperSpeed: function() { with(this) {
var yogi = new Bear('Yogi');
benchmark(function() {
yogi.speak();
}, 3000);
var bobo = new NoSuperBear('Bobo');
benchmark(function() {
bobo.speak();
}, 3000);
}},
testClassDefinition: function() { with(this) {
var frog = new Animal('Kermit');
assertEqual('My name is Kermit and I like Ms. Piggy', frog.speak('Ms. Piggy'));
assertEqual('Animal finds food', Animal.find('food'));
assertEqual('Animal finds shelter and Animal creates shelter', Animal.create('shelter'));
}},
testEmptyClass: function() { with(this) {
var klass = JS.Class();
var instance = new klass;
assert(instance.isA(klass));
}},
testInheritance: function() { with(this) {
var yogi = new Bear('Yogi');
assertEqual('MY NAME IS YOGI AND I LIKE PICNICS', yogi.speak('picnics'));
assertEqual('Animal finds honey', Bear.find('honey'));
assertEqual('Animal finds nothing and Animal creates nothing, but Bear creates other stuff', Bear.create('nothing'));
}},
testSingletonMethodInheritance: function() { with(this) {
var rex = new Bear('rex');
rex.extend({speak: function() {
return 'Guess what? ' + this._super();
}});
assertEqual('Guess what? MY NAME IS REX AND I LIKE WALKS', rex.speak('walks'));
}},
testInterfaces: function() { with(this) {
assertRaise('Error', function() {
JS.Interface.ensure({}, Dog.Interface);
});
assert(Dog.Interface.test(new Dog('Rex')));
assert(!Dog.Interface.test({}));
}},
testImplement: function() { with(this) {
var I = new JS.Interface(['compareWith', 'lt', 'lte', 'gt', 'gte', 'eq']);
assertRaise('Error', function() {
var klass = new JS.Class({implement: I,
initialize: function() {}
});
});
assertRaise('Error', function() {
var klass = new JS.Class({implement: [I],
compareWith: function() {}
});
klass.include(Comparable);
});
var klass = new JS.Class({implement: [I],
include: Comparable,
compareWith: function() {}
});
}},
testSuperWithNoArguments: function() { with(this) {
var brutus = new Pitbull('Brutus');
assertEqual('My name is Brutus and I like biting', brutus.speak('biting'));
}},
// Avoid Crockford's problem: http://www.ajaxpath.com/javascript-inheritance
testReassignedSuper: function() { with(this) {
var top = new TopClass();
assertEqual('TopClass(2) extends SubClass(2) extends BaseClass(2)', top.getName());
}},
testInheritanceFromNativeClass: function() { with(this) {
var nat = new NativeClass();
assertEqual('Native', nat.getName());
var child = new ChildOfNativeClass();
assertEqual('Native', child.getName());
NativeClass.prototype.shout = function() { return 'Shouting!!'; };
assertEqual('Shouting!!', child.shout());
assert(child.isA(ChildOfNativeClass));
assert(child.isA(NativeClass));
assert(child.isA(Object));
assert(!child.isA(Dog));
}},
testBinding: function() { with(this) {
var rex = new Dog('Rex');
var bark = rex.method('bark');
assertEqual('Rex says WOOF!', bark());
var brutus = new Pitbull('Brutus');
assertEqual('Brutus says WOOF!', brutus.bark());
bark = brutus.bark;
assertEqual(' says WOOF!', bark());
var bite = Pitbull.method('canBiteYourLegsOff');
assertEqual('Of course it can', bite());
}},
testMethodMemoization: function() { with(this) {
var dog = new Dog('Oscar');
var bark = dog.method('bark');
assertEqual(bark, dog.method('bark'));
dog.extend({
bark: function() {
return this._super() + ' and has his own bark() method.';
}
});
assertEqual('Oscar says WOOF! and has his own bark() method.', dog.bark());
assert(dog.method('bark') != bark);
assert(dog.method('bark') == dog.method('bark'));
}},
testHierarchy: function() { with(this) {
var rex = new Dog('Rex');
assert(rex.isA(Dog));
assert(rex.isA(Animal));
assert(rex.isA(Object));
assert(!rex.isA(Bear));
var yogi = new rex.klass.superclass.subclasses[0]('Yogi');
assertEqual('Yogi', yogi.name);
assert(yogi.isA(Bear));
assert(!yogi.isA(Dog));
}},
testSubclassUpdating: function() { with(this) {
var kevin = new Koala('Kevin');
Animal.instanceMethod('shout', function(thing) {
return this.name + ' is loving ' + thing + '!!!';
});
assertEqual('Kevin is loving trees!!!', kevin.shout('trees'));
Animal.classMethod('count', function() { return 6; });
assertEqual(4*6, Koala.count() + Bear.count() + Dog.count() + Pitbull.count());
}},
testClassMethodInheritanceSkipsGenerations: function() { with(this) {
Dog.eatFood = function(food) { return 'Dogs eat ' + food; };
assertEqual('Dogs eat biscuits', Dog.eatFood('biscuits'));
Animal.classMethod('eatFood', function(food) { return 'Animals eat ' + food; });
assertEqual('Animals eat people', Pitbull.eatFood('people'));
assertEqual('Dogs eat biscuits', Dog.eatFood('biscuits'));
Pitbull.classMethod('eatFood', function() { return this._super(); });
assertEqual('Dogs eat people', Pitbull.eatFood('people'));
}},
testModularInheritance: function() { with(this) {
var paginator = new Paginator(12);
assert(paginator.isA(Paginator));
assert(paginator.items[6].isA(Paginator.Item));
assert(paginator.items[6].isA(paginator.items[3].klass));
assertEqual('Item 7', paginator.items[7].getName());
var gallery = new Gallery(3);
assertEqual(12, gallery.items.length);
assert(gallery.isA(Gallery));
assert(gallery.isA(Paginator));
assert(gallery.items[5].isA(Gallery.Item));
assert(gallery.items[5].isA(Paginator.Item));
assert(Paginator.Item === Gallery.Item);
assert(ItemInheritor.Item !== Gallery.Item);
assert(ItemInheritor.Item.superclass === Gallery.Item);
var itor = new ItemInheritor(2);
assertEqual('Item 13', itor.items[6].getName());
}},
testModule: function() { with(this) {
assertEqual('function', typeof BlankClass.find);
assertEqual('function', typeof BlankClass.create);
var foo = new BlankClass;
assertEqual('function', typeof foo.save);
assertEqual('function', typeof foo.validate);
assertEqual('function', typeof foo.attributes);
assertEqual('function', typeof foo.each);
assertEqual('function', typeof foo.some);
assertEqual(1, Enum.hasBeenIncluded);
}},
testSingleton: function() { with(this) {
var bear = JS.Singleton(Bear, {
roar: function() { return this.noise; },
noise: 'ROAR'
});
assertEqual(Bear, bear.klass.superclass);
var roar = bear.method('roar');
assertEqual('ROAR', roar());
}},
testSingletonReinstantiation: function() { with(this) {
assertRaise('Error', function() {
var foo = new Camel.klass('Cyril');
});
}},
testInheritedHook: function() { with(this) {
var foo = JS.Class(InheritanceCatcher, {
extend: {
log: function(klass) {}
}
});
assertEqual(foo, InheritanceCatcher.children[0]);
}},
testComparableModule: function() { with(this) {
var items = [8,2,7,5,3,7,6].map(function(id) { return new TodoItem(id) });
var sorted = items.clone().sort(TodoItem.compare);
assertEqual('2,3,5,6,7,7,8', sorted.pluck('position').join(','));
}}
});
</script>
</body>
</html>