-
Notifications
You must be signed in to change notification settings - Fork 24
/
tests.js
299 lines (222 loc) · 5.42 KB
/
tests.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
290
291
292
293
294
295
296
297
298
299
var isBrowser = typeof window != "undefined";
if (!isBrowser){
Model = require("./fishbone");
}
var tests = {
"Class based instance property.": function(){
var Klass = Model({
foo: "foo"
});
var instance = new Klass();
return instance.foo == "foo";
},
"Instance property on init.": function(){
var Klass = Model({
init: function(){
this.foo = "foo";
}
});
var instance = new Klass();
return instance.foo == "foo";
},
"Instance property via options.": function(){
var Klass = Model({
init: function(foo){
this.foo = foo;
}
});
var instance = new Klass("foo");
return instance.foo == "foo";
},
"Multiple arguments in constructor.": function(){
var Klass = Model({
init: function(foo, bar){
this.foo = foo;
this.bar = bar;
}
});
var instance = new Klass("foo", "bar");
return instance.foo == "foo" && instance.bar == "bar";
},
"Instance property via method.": function(){
var Klass = Model({
setFoo: function(foo){
this.foo = foo;
}
});
var instance = new Klass();
instance.setFoo("foo");
return instance.foo == "foo";
},
"Automatic method chaining.": function(){
var Klass = Model({
foo: function(){ },
bar: function(){ }
});
var instance = new Klass();
return instance.foo().foo().bar().bar() == instance;
},
"No chaining for methods with return values.": function(){
var Klass = Model({
foo: function(){ },
bar: function(){ return "foo"; }
});
var instance = new Klass();
return instance.foo().foo().bar() == "foo";
},
"Add event handler.": function(){
var Klass = Model({
triggerEvent: function(){
this.trigger("event", "foo");
}
});
var called = false;
var instance = new Klass();
instance.on("event", function(data){
called = data;
});
instance.triggerEvent();
return called == "foo";
},
"Multiple event handlers.": function(){
var first = false;
var second = false;
var Klass = Model({
first: function(data){
first = data;
}
});
var instance = new Klass();
instance.on("event", instance.first);
instance.on("event", function(data){
second = data;
});
instance.trigger("event", true);
return first && second;
},
"Remove single event handler.": function(){
var Klass = Model({
triggerEvent: function(){
this.trigger("event");
}
});
var called = false;
var instance = new Klass();
var callback = function(){
called = true;
};
instance.on("event", callback);
instance.off("event", callback);
instance.triggerEvent();
return !called;
},
"Remove all handlers for a single event type.": function(){
var Klass = Model({
triggerEvent: function(){
this.trigger("event");
}
});
var called = false;
var instance = new Klass();
var callback = function(){
called = true;
};
instance.on("event", callback);
instance.on("event", callback);
instance.off("event");
instance.triggerEvent();
return !called;
},
"Simple inheritance.": function(){
var Klass1 = Model({
foo: "foo"
});
var Klass2 = Klass1.extend({
bar: "bar"
});
var instance = new Klass2();
return instance.foo == "foo" && instance.bar == "bar";
},
"Override property.": function(){
var Klass1 = Model({
foo: "bar"
});
var Klass2 = Klass1.extend({
foo: "foo"
});
var instance = new Klass2();
return instance.foo == "foo";
},
"Override method.": function(){
var Klass1 = Model({
foo: "foo",
bar: function(){
return false;
}
});
var Klass2 = Klass1.extend({
bar: function(){
return this.foo;
}
});
var instance = new Klass2();
return instance.bar() == "foo";
},
"Multiple inheritance": function(){
var Klass1 = Model({ a: "a" });
var Klass2 = Klass1.extend({ b: "b" });
var Klass3 = Klass2.extend({ c: "c" });
var instance = new Klass3();
return instance.a == "a" && instance.b == "b" && instance.c == "c";
},
"Super methods": function(){
var Klass1 = Model({
bar: "bar",
foo: function(){
return this.bar;
}
});
var Klass2 = Klass1.extend({
foo: function(){
return "foo" + this.__foo();
}
});
var instance = new Klass2();
return instance.foo() == "foobar";
},
"Super properties": function(){
var Klass1 = Model({
bar: "bar",
foo: function(){
return this.bar;
}
});
var Klass2 = Klass1.extend({
bar: "foo",
foo: function(){
return this.bar + this.__bar;
}
});
var instance = new Klass2();
return instance.foo() == "foobar";
},
"Second level super properties": function(){
var Klass1 = Model({
bar: "bar"
});
var Klass2 = Klass1.extend();
var Klass3 = Klass2.extend({
bar: function(){
return this.__bar;
}
});
var instance = new Klass3();
return instance.bar() == "bar";
}
};
var log = isBrowser ? function(message){
document.write(message + "<br>");
} : console.log;
for (var description in tests){
log((tests[description]() ? "OK" : "ERROR") + " - " + description);
}