-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathmodule_spec.js
540 lines (447 loc) · 18.9 KB
/
module_spec.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
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
JS.ENV.ModuleSpec = JS.Test.describe(JS.Module, function() { with(this) {
sharedBehavior("module", function() { with(this) {
before(function() { with(this) {
this.modA = new JS.Module('A')
this.modB = new JS.Module('B')
this.modC = new JS.Module('C')
this.modD = new JS.Module('D')
}})
it("is a Module", function() { this.assert( this.subjectClass.isA(JS.Module) ) })
it("is a Class", function() { this.assert( this.subjectClass.isA(JS.Class) ) })
it("is an instance of Class", function() { this.assertEqual( JS.Class, this.subjectClass.klass ) })
it("inherits from Kernel", function() { this.assert( this.subjectClass.isA(JS.Kernel) ) })
describe("with singleton methods", function() { with(this) {
before(function() { with(this) {
this.User = new subjectClass({
extend: {
find: function(name) {
return "User " + name
},
create: function() {
return this.className + " not found, so we created it"
}
}
})
}})
it("adds the methods to the class object", function() { with(this) {
assertEqual( "User jcoglan", User.find("jcoglan") )
}})
it("runs class methods in the context of the class", function() { with(this) {
User.className = "UserClass"
assertEqual( "UserClass not found, so we created it", User.create() )
}})
}})
describe("#ancestors", function() { with(this) {
before(function() { with(this) {
this.module = new subjectClass()
}})
describe("with no included modules", function() { with(this) {
it("returns the receiver", function() { with(this) {
assertEqual( ancestors.concat(module), module.ancestors() )
}})
}})
describe("with an included module", function() { with(this) {
before(function() { with(this) {
module.include(modA)
}})
it("returns the included module and the receiver", function() { with(this) {
assertEqual( ancestors.concat(modA, module), module.ancestors() )
}})
}})
describe("with two included modules", function() { with(this) {
before(function() { with(this) {
module.include(modC)
module.include(modD)
}})
it("sorts the modules by inclusion order", function() { with(this) {
assertEqual( ancestors.concat(modC, modD, module), module.ancestors() )
}})
}})
describe("with a tree of included modules", function() { with(this) {
before(function() { with(this) {
// Having includes in this order tests the double inclusion problem
// http://eigenclass.org/hiki/The+double+inclusion+problem
module.include(modB)
modB.include(modA)
}})
it("returns the flattened tree", function() { with(this) {
assertEqual( ancestors.concat(modA, modB, module), module.ancestors() )
}})
// Diamond problem: http://en.wikipedia.org/wiki/Diamond_problem
//
// A
// / \
// B C
// \ /
// D
//
describe("with a repeated reference in the tree", function() { with(this) {
before(function() { with(this) {
modC.include(modA)
module.include(modC)
}})
it("places the repeated module at its earliest possible position", function() { with(this) {
assertEqual( ancestors.concat(modA, modB, modC, module), module.ancestors() )
}})
}})
}})
}})
describe("#displayName", function() { with(this) {
before(function() { with(this) {
this.module = new subjectClass("NameOfModule", {
methodOne: function() {},
methodTwo: function() {}
})
}})
it("returns the name of the module", function() { with(this) {
assertEqual( "NameOfModule", module.displayName )
}})
it("returns the name of each method", function() { with(this) {
assertEqual( "NameOfModule#methodOne", module.__fns__.methodOne.callable.displayName )
assertEqual( "NameOfModule#methodTwo", module.__fns__.methodTwo.callable.displayName )
}})
describe("for nested modules", function() { with(this) {
before(function() { with(this) {
this.module = new subjectClass("Outer", {
extend: {
InnerPublic: new subjectClass({ aMethod: function() {} })
},
InnerPrivate: new subjectClass({
aMethod: function() {},
extend: {
DeepInner: new subjectClass({
aMethod: function() {},
Klass: new subjectClass({ aMethod: function() {} }),
extend: {
Foo: new subjectClass({ aMethod: function() {} })
}
})
}
})
})
}})
it("returns the name of an inner public module", function() { with(this) {
assertEqual( "Outer.InnerPublic", module.InnerPublic.displayName )
assertEqual( "Outer.InnerPublic#aMethod",
module.InnerPublic.__fns__.
aMethod.callable.displayName )
}})
it("returns the name of an inner private module", function() { with(this) {
assertEqual( "Outer#InnerPrivate",
module.__fns__.
InnerPrivate.displayName )
assertEqual( "Outer#InnerPrivate#aMethod",
module.__fns__.
InnerPrivate.__fns__.
aMethod.callable.displayName )
}})
it("returns the name of a deeply nested module", function() { with(this) {
assertEqual( "Outer#InnerPrivate.DeepInner",
module.__fns__.
InnerPrivate.DeepInner.displayName )
assertEqual( "Outer#InnerPrivate.DeepInner#aMethod",
module.__fns__.
InnerPrivate.DeepInner.__fns__.
aMethod.callable.displayName )
assertEqual( "Outer#InnerPrivate.DeepInner.Foo",
module.__fns__.
InnerPrivate.DeepInner.
Foo.displayName )
assertEqual( "Outer#InnerPrivate.DeepInner.Foo#aMethod",
module.__fns__.
InnerPrivate.DeepInner.
Foo.__fns__.
aMethod.callable.displayName )
assertEqual( "Outer#InnerPrivate.DeepInner#Klass",
module.__fns__.
InnerPrivate.DeepInner.__fns__.
Klass.displayName )
assertEqual( "Outer#InnerPrivate.DeepInner#Klass#aMethod",
module.__fns__.
InnerPrivate.DeepInner.__fns__.
Klass.__fns__.
aMethod.callable.displayName )
}})
}})
}})
describe("#extended", function() { with(this) {
before(function() { with(this) {
this.extenders = []
this.module = new subjectClass()
this.module.extend({
extended: function(base) { extenders.push(base) }
})
}})
describe("when the module is included by a module", function() { with(this) {
before(function() { with(this) {
this.hostModule = new JS.Module()
hostModule.include(module)
}})
it("is not called", function() { with(this) {
assertEqual( [], extenders )
}})
}})
describe("when the module is included by a class", function() { with(this) {
before(function() { with(this) {
this.hostClass = new JS.Class()
hostClass.include(module)
}})
it("is not called", function() { with(this) {
assertEqual( [], extenders )
}})
}})
describe("when the module is used to extend a class", function() { with(this) {
before(function() { with(this) {
this.hostClass = new JS.Class()
hostClass.extend(module)
}})
it("is called with the extended class", function() { with(this) {
assertEqual( [hostClass], extenders )
}})
describe("and that class is inherited", function() { with(this) {
before(function() { with(this) {
this.child = new JS.Class(hostClass)
}})
it("is not called with the subclass", function() { with(this) {
assertEqual( [hostClass], extenders )
}})
}})
}})
describe("when the module is used to extend an object", function() { with(this) {
before(function() { with(this) {
this.hostModule = new JS.Module()
hostModule.extend(module)
}})
it("is called with the extended object", function() { with(this) {
assertEqual( [hostModule], extenders )
}})
}})
}})
describe("#included", function() { with(this) {
before(function() { with(this) {
this.includers = []
this.module = new subjectClass({ theMethod: function() { return "an instance method" } })
this.module.extend({
included: function(base) { includers.push(base) }
})
}})
describe("when the module is included by a module", function() { with(this) {
before(function() { with(this) {
this.hostModule = new JS.Module()
hostModule.include(module)
}})
it("is called once with the including module", function() { with(this) {
assertEqual( [hostModule], includers )
}})
describe("and the including module is included somewhere else", function() { with(this) {
before(function() { with(this) {
assertEqual( ancestors.concat([module, hostModule]), hostModule.ancestors() )
this.target = new JS.Module()
target.include(hostModule)
}})
it("is not called with the indirectly including module", function() { with(this) {
assertEqual( [hostModule], includers )
}})
}})
}})
describe("when the module is used to extend an object", function() { with(this) {
before(function() { with(this) {
this.object = new JS.Module()
object.extend(module)
}})
it("is not called", function() { with(this) {
assertEqual( [], includers )
}})
}})
describe("when the module is included by a class", function() { with(this) {
before(function() { with(this) {
this.hostClass = new JS.Class()
hostClass.include(module)
}})
it("is called once with the including class", function() { with(this) {
assertEqual( [hostClass], includers )
}})
}})
describe("when the module is used to extend a class", function() { with(this) {
before(function() { with(this) {
this.object = new JS.Class()
object.extend(module)
}})
it("is not called", function() { with(this) {
assertEqual( [], includers )
}})
}})
describe("when the hook causes the host to extend itself with the same module", function() { with(this) {
before(function() { with(this) {
module.extend({
included: function(base) { base.extend(this) }
})
this.hostClass = new JS.Class()
hostClass.include(module)
}})
it("adds the module's methods as instance methods to the host", function() { with(this) {
assertEqual( "an instance method", (new hostClass()).theMethod() )
}})
it("adds the module's methods as singleton methods to the host", function() { with(this) {
assertEqual( "an instance method", hostClass.theMethod() )
}})
}})
}})
describe("#instanceMethod", function() { with(this) {
before(function() { with(this) {
this.module = new subjectClass({ aMethod: function() {} })
}})
it("returns a Method", function() { with(this) {
assertKindOf( JS.Method, module.instanceMethod("aMethod") )
}})
it("returns a the named method from the module", function() { with(this) {
assertEqual( "aMethod", module.instanceMethod("aMethod").name )
}})
describe("with inherited methods", function() { with(this) {
before(function() { with(this) {
this.includer = new subjectClass({ include: module })
}})
it("returns the named method from its inheritance chain", function() { with(this) {
assertSame( includer.instanceMethod("aMethod"),
module.instanceMethod("aMethod") )
}})
}})
}})
describe("#instanceMethods", function() { with(this) {
before(function() { with(this) {
this.module = new subjectClass()
}})
describe("with no methods", function() { with(this) {
it("returns a list of inherited methods", function() { with(this) {
assertEqual( instanceMethods, module.instanceMethods() )
}})
it("returns an empty list when called with false", function() { with(this) {
assertEqual( [], module.instanceMethods(false) )
}})
}})
describe("with some methods", function() { with(this) {
before(function() { with(this) {
module.define("aMethod", function() {})
module.define("bMethod", function() {})
}})
it("returns the inherited methods and the module's own methods", function() { with(this) {
assertEqual( instanceMethods.concat("aMethod", "bMethod").sort(),
module.instanceMethods().sort() )
}})
it("returns only the module's own methods when called with false", function() { with(this) {
assertEqual( ["aMethod", "bMethod"], module.instanceMethods(false) )
}})
describe("with properties that are not methods", function() { with(this) {
before(function() { with(this) {
module.define("SOMETHING", "foo")
}})
it("does not include properties", function() { with(this) {
assertEqual( ["aMethod", "bMethod"], module.instanceMethods(false) )
}})
}})
}})
}})
}})
before(function() { with(this) {
this.subjectClass = JS.Module
this.ancestors = []
this.instanceMethods = []
}})
behavesLike("module")
it("has Object as its parent class", function() { with(this) {
assertEqual( Object, JS.Module.superclass )
}})
it("has Class as a subclass", function() { with(this) {
assertEqual( 0, JS.indexOf(JS.Module.subclasses, JS.Class) )
}})
describe("#define", function() { with(this) {
before(function() { with(this) {
this.parent = new JS.Module("Parent")
this.child = new JS.Module("Child", {aMethod: function() {}})
child.include(parent)
}})
it("adds the method to modules that depend on the receiver", function() { with(this) {
assertEqual( [child.instanceMethod("aMethod")], child.lookup("aMethod") )
parent.define("aMethod", function() {})
assertEqual( [parent.instanceMethod("aMethod"), child.instanceMethod("aMethod")], child.lookup("aMethod") )
}})
}})
describe("#include", function() { with(this) {
before(function() { with(this) {
this.module = new JS.Module()
this.mixin = new JS.Module({ foo: function() { return "foo" } })
this.plainOldObject = { theMethod: function() { return "the method" } }
this.Class = new JS.Class({ include: module })
this.object = new Class()
}})
describe("taking a module", function() { with(this) {
it("makes the mixin an ancestor of the receiver", function() { with(this) {
assertEqual( [module], module.ancestors() )
module.include(mixin)
assertEqual( [mixin, module], module.ancestors() )
}})
it("makes the mixin an ancestor of downstream classes", function() { with(this) {
assertEqual( [JS.Kernel, module, Class], Class.ancestors() )
module.include(mixin)
assertEqual( [JS.Kernel, mixin, module, Class], Class.ancestors() )
}})
it("adds the mixin's instance methods indirectly to the receiver", function() { with(this) {
assertEqual( [], module.instanceMethods() )
module.include(mixin)
assertEqual( ["foo"], module.instanceMethods() )
assertEqual( [], module.instanceMethods(false) )
}})
it("adds the mixin's instance methods indirectly to downstream classes", function() { with(this) {
assertEqual( JS.Kernel.instanceMethods(), Class.instanceMethods() )
module.include(mixin)
assertEqual( ["foo"].concat(JS.Kernel.instanceMethods()).sort(),
Class.instanceMethods().sort() )
}})
it("adds the method to objects that inherit from the receiver", function() { with(this) {
assertEqual( undefined, object.foo )
module.include(mixin)
assertEqual( "foo", object.foo() )
}})
describe("when the mixin defines methods also defined in the receiver", function() { with(this) {
before(function() { with(this) {
module.define("foo", function() { return "module foo" })
}})
it("does not clobber the method on downstream objects", function() { with(this) {
module.include(mixin)
assertEqual( "module foo", object.foo() )
}})
}})
}})
describe("taking a plain old object", function() { with(this) {
it("does not change the receiver's ancestors", function() { with(this) {
module.include(plainOldObject)
assertEqual( [module], module.ancestors() )
assertEqual( [JS.Kernel, module, Class], Class.ancestors() )
}})
it("adds the object's methods directly to the receiver", function() { with(this) {
module.include(plainOldObject)
assertEqual( ["theMethod"], module.instanceMethods() )
assertEqual( ["theMethod"], module.instanceMethods(false) )
}})
it("adds the object's methods indirectly to downstream classes", function() { with(this) {
module.include(plainOldObject)
assertEqual( [], Class.instanceMethods(false) )
assertEqual( ["theMethod"].concat(JS.Kernel.instanceMethods()).sort(),
Class.instanceMethods().sort() )
}})
it("adds the method to objects that inherit from the receiver", function() { with(this) {
assertEqual( undefined, object.theMethod )
module.include(plainOldObject)
assertEqual( "the method", object.theMethod() )
}})
}})
}})
describe("#instanceMethod", function() { with(this) {
before(function() { with(this) {
this.module = new JS.Module({ bar: function() {} })
}})
it("returns the named instance method", function() { with(this) {
assertSame( module.__fns__.bar, module.instanceMethod("bar") )
}})
}})
}})