/
class_test.js
130 lines (109 loc) · 4.24 KB
/
class_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
new Test.Unit.Runner({
testClassCreate: function() {
this.assert(Object.isFunction(Animal), 'Animal is not a constructor');
this.assertEnumEqual([Cat, Mouse, Dog, Ox], Animal.subclasses);
Animal.subclasses.each(function(subclass) {
this.assertEqual(Animal, subclass.superclass);
}, this);
var Bird = Class.create(Animal);
this.assertEqual(Bird, Animal.subclasses.last());
// for..in loop (for some reason) doesn't iterate over the constructor property in top-level classes
this.assertEnumEqual(Object.keys(new Animal).sort(), Object.keys(new Bird).without('constructor').sort());
},
testClassInstantiation: function() {
var pet = new Animal("Nibbles");
this.assertEqual("Nibbles", pet.name, "property not initialized");
this.assertEqual('Nibbles: Hi!', pet.say('Hi!'));
this.assertEqual(Animal, pet.constructor, "bad constructor reference");
this.assertUndefined(pet.superclass);
var Empty = Class.create();
this.assert('object', typeof new Empty);
},
testInheritance: function() {
var tom = new Cat('Tom');
this.assertEqual(Cat, tom.constructor, "bad constructor reference");
this.assertEqual(Animal, tom.constructor.superclass, 'bad superclass reference');
this.assertEqual('Tom', tom.name);
this.assertEqual('Tom: meow', tom.say('meow'));
this.assertEqual('Tom: Yuk! I only eat mice.', tom.eat(new Animal));
},
testSuperclassMethodCall: function() {
var tom = new Cat('Tom');
this.assertEqual('Tom: Yum!', tom.eat(new Mouse));
// augment the constructor and test
var Dodo = Class.create(Animal, {
initialize: function($super, name) {
$super(name);
this.extinct = true;
},
say: function($super, message) {
return $super(message) + " honk honk";
}
});
var gonzo = new Dodo('Gonzo');
this.assertEqual('Gonzo', gonzo.name);
this.assert(gonzo.extinct, 'Dodo birds should be extinct');
this.assertEqual("Gonzo: hello honk honk", gonzo.say("hello"));
},
testClassAddMethods: function() {
var tom = new Cat('Tom');
var jerry = new Mouse('Jerry');
Animal.addMethods({
sleep: function() {
return this.say('ZZZ');
}
});
Mouse.addMethods({
sleep: function($super) {
return $super() + " ... no, can't sleep! Gotta steal cheese!";
},
escape: function(cat) {
return this.say('(from a mousehole) Take that, ' + cat.name + '!');
}
});
this.assertEqual('Tom: ZZZ', tom.sleep(), "added instance method not available to subclass");
this.assertEqual("Jerry: ZZZ ... no, can't sleep! Gotta steal cheese!", jerry.sleep());
this.assertEqual("Jerry: (from a mousehole) Take that, Tom!", jerry.escape(tom));
// insure that a method has not propagated *up* the prototype chain:
this.assertUndefined(tom.escape);
this.assertUndefined(new Animal().escape);
Animal.addMethods({
sleep: function() {
return this.say('zZzZ');
}
});
this.assertEqual("Jerry: zZzZ ... no, can't sleep! Gotta steal cheese!", jerry.sleep());
},
testBaseClassWithMixin: function() {
var grass = new Plant('grass', 3);
this.assertRespondsTo('getValue', grass);
this.assertEqual('#<Plant: grass>', grass.inspect());
},
testSubclassWithMixin: function() {
var snoopy = new Dog('Snoopy', 12, 'male');
this.assertRespondsTo('reproduce', snoopy);
},
testSubclassWithMixins: function() {
var cow = new Ox('cow', 400, 'female');
this.assertEqual('#<Ox: cow>', cow.inspect());
this.assertRespondsTo('reproduce', cow);
this.assertRespondsTo('getValue', cow);
},
testClassWithToStringAndValueOfMethods: function() {
var Foo = Class.create({
toString: function() { return "toString" },
valueOf: function() { return "valueOf" }
});
var Parent = Class.create({
m1: function(){ return 'm1' },
m2: function(){ return 'm2' }
});
var Child = Class.create(Parent, {
m1: function($super) { return 'm1 child' },
m2: function($super) { return 'm2 child' }
});
this.assert(new Child().m1.toString().indexOf('m1 child') > -1);
this.assertEqual("toString", new Foo().toString());
this.assertEqual("valueOf", new Foo().valueOf());
}
});