~bcsaller/juju-gui/update-reductions

« back to all changes in this revision

Viewing changes to lib/yui/tests/oop/tests/oop-test.js

  • Committer: kapil.foss at gmail
  • Date: 2012-07-13 18:45:59 UTC
  • Revision ID: kapil.foss@gmail.com-20120713184559-2xl7be17egsrz0c9
reshape

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
YUI.add('oop-test', function (Y) {
2
 
 
3
 
var Assert       = Y.Assert,
4
 
    ArrayAssert  = Y.ArrayAssert,
5
 
    ObjectAssert = Y.ObjectAssert,
6
 
 
7
 
    suite  = new Y.Test.Suite('oop');
8
 
 
9
 
suite.add(new Y.Test.Case({
10
 
    name: 'Core',
11
 
 
12
 
    test_clone: function () {
13
 
        var a = {
14
 
            'bool' : false,
15
 
            'num'  : 0,
16
 
            'nul' : null,
17
 
            'undef': undefined,
18
 
            'T'    : 'blabber'
19
 
        };
20
 
 
21
 
        Assert.isTrue((a.T === 'blabber'));
22
 
 
23
 
        var b = Y.clone(a);
24
 
 
25
 
        var c = (function() {
26
 
            var _c = 3;
27
 
 
28
 
            return {
29
 
                a: 1,
30
 
                b: 2,
31
 
                c: function() {
32
 
                    return _c;
33
 
                }
34
 
            };
35
 
        }());
36
 
 
37
 
        var d = Y.clone(c);
38
 
 
39
 
        Assert.isTrue((d.a === 1));
40
 
        Assert.isTrue((d.c() === 3));
41
 
 
42
 
    },
43
 
 
44
 
    test_clone_node: function () {
45
 
        var a = {
46
 
            node: Y.one(document.createElement('div')),
47
 
            y: Y,
48
 
            w: window,
49
 
            d: document,
50
 
            el: document.createElement('div')
51
 
        };
52
 
 
53
 
        var b = Y.clone(a);
54
 
        b.node.foo = 'bar';
55
 
 
56
 
        Assert.isFalse(b.node.foo === a.node.foo);
57
 
    },
58
 
 
59
 
    test_each: function () {
60
 
        var count = 0;
61
 
 
62
 
        Y.each(null, function(v, k) {
63
 
            // should not throw
64
 
        });
65
 
 
66
 
        Y.each('string', function(v, k) {
67
 
            // should not throw
68
 
        });
69
 
 
70
 
        Y.each(12345, function(v, k) {
71
 
            // should not throw
72
 
        });
73
 
 
74
 
        Y.each({ foo: 1 }, function(v, k) {
75
 
            count++;
76
 
        });
77
 
 
78
 
        Assert.areEqual(1, count);
79
 
    },
80
 
 
81
 
    test_extend: function () {
82
 
        var firedbase = false;
83
 
        var firedextended = false;
84
 
 
85
 
        var Base = function() {
86
 
            arguments.callee.superclass.constructor.apply(this, arguments);
87
 
 
88
 
            // bind by string in order to allow the subclass
89
 
            this.on('testStringFn', Y.bind('base', this));
90
 
        };
91
 
 
92
 
        Y.extend(Base, Y.EventTarget, {
93
 
            base: function() {
94
 
                firedbase = true;
95
 
            }
96
 
        });
97
 
 
98
 
        var Extended = function() {
99
 
            arguments.callee.superclass.constructor.apply(this, arguments);
100
 
        };
101
 
 
102
 
        Y.extend(Extended, Base, {
103
 
            base: function() {
104
 
                firedextended = true;
105
 
            }
106
 
        });
107
 
 
108
 
        var b = new Extended();
109
 
        b.fire('testStringFn', 1, 2);
110
 
 
111
 
        Y.Assert.isFalse(firedbase);
112
 
        Y.Assert.isTrue(firedextended);
113
 
    },
114
 
 
115
 
    test_merge: function () {
116
 
        Object.prototype.foo = 'hello!';
117
 
 
118
 
        var o1 = { one: "one" },
119
 
            o2 = { two: "two" },
120
 
            o3 = { two: "twofromthree", three: "three" },
121
 
            o4 = { one: "one", two: "twofromthree", three: "three" },
122
 
            o123 = Y.merge(o1, o2, o3);
123
 
 
124
 
        Y.ObjectAssert.areEqual(o123, o4);
125
 
        Assert.areEqual(o123.two, o4.two);
126
 
 
127
 
        Y.Assert.isFalse((o123.hasOwnProperty('foo')), 'prototype properties added to Object should not be iterable');
128
 
        delete Object.prototype.foo;
129
 
    }
130
 
}));
131
 
 
132
 
suite.add(new Y.Test.Case({
133
 
    name: 'augment()',
134
 
 
135
 
    setUp: function () {
136
 
        this.receiver = function () {};
137
 
        this.supplier = function () {};
138
 
    },
139
 
 
140
 
    tearDown: function () {
141
 
        delete this.receiver;
142
 
        delete this.supplier;
143
 
    },
144
 
 
145
 
    "receiver object should be augmented with supplier's prototype properties": function () {
146
 
        var receiver = {},
147
 
            supplier = this.supplier;
148
 
 
149
 
        supplier.prototype.foo = 'foo';
150
 
        supplier.prototype.bar = function () { return 'bar'; };
151
 
 
152
 
        Assert.areSame(receiver, Y.augment(receiver, supplier));
153
 
        Assert.areSame(2, Y.Object.size(receiver));
154
 
        ArrayAssert.itemsAreSame(['foo', 'bar'], Y.Object.keys(receiver));
155
 
        ArrayAssert.itemsAreSame(['foo', supplier.prototype.bar], Y.Object.values(receiver));
156
 
    },
157
 
 
158
 
    "receiver object properties should not be overwritten when `overwrite` is not `true`": function () {
159
 
        var receiver = {foo: 'moo'},
160
 
            supplier = this.supplier;
161
 
 
162
 
        supplier.prototype.foo = 'foo';
163
 
        supplier.prototype.bar = 'bar';
164
 
 
165
 
        Y.augment(receiver, supplier);
166
 
 
167
 
        Assert.areSame('moo', receiver.foo);
168
 
        Assert.areSame('bar', receiver.bar);
169
 
    },
170
 
 
171
 
    "receiver object properties should be overwritten when `overwrite` is `true`": function () {
172
 
        var receiver = {foo: 'moo'},
173
 
            supplier = this.supplier;
174
 
 
175
 
        supplier.prototype.foo = 'foo';
176
 
        supplier.prototype.bar = 'bar';
177
 
 
178
 
        Y.augment(receiver, supplier, true);
179
 
 
180
 
        Assert.areSame('foo', receiver.foo);
181
 
        Assert.areSame('bar', receiver.bar);
182
 
    },
183
 
 
184
 
    "only whitelisted properties should be copied to a receiver object": function () {
185
 
        var receiver = {},
186
 
            supplier = this.supplier;
187
 
 
188
 
        supplier.prototype.foo = 'a';
189
 
        supplier.prototype.bar = 'b';
190
 
        supplier.prototype.baz = 'c';
191
 
 
192
 
        Y.augment(receiver, supplier, false, ['foo', 'baz']);
193
 
 
194
 
        ArrayAssert.itemsAreSame(['foo', 'baz'], Y.Object.keys(receiver));
195
 
    },
196
 
 
197
 
    "supplier constructor should be called immediately when augmenting a receiver object": function () {
198
 
        var calls    = 0,
199
 
            receiver = {};
200
 
 
201
 
        function supplier() { calls += 1; }
202
 
 
203
 
        Y.augment(receiver, supplier);
204
 
        Assert.areSame(1, calls);
205
 
    },
206
 
 
207
 
    "supplier constructor should receive supplied args when augmenting a receiver object": function () {
208
 
        var calls    = 0,
209
 
            receiver = {};
210
 
 
211
 
        function supplier(foo) {
212
 
            calls += 1;
213
 
            Assert.areSame('foo', foo);
214
 
        }
215
 
 
216
 
        function supplierTwo(foo, bar) {
217
 
            calls += 1;
218
 
            Assert.areSame('foo', foo);
219
 
            Assert.areSame('bar', bar);
220
 
        }
221
 
 
222
 
        Y.augment(receiver, supplier, false, null, 'foo');
223
 
 
224
 
        receiver = {};
225
 
        Y.augment(receiver, supplierTwo, false, null, ['foo', 'bar']);
226
 
 
227
 
        Assert.areSame(2, calls);
228
 
    },
229
 
 
230
 
    "receiver function prototype should be augmented with supplier's prototype properties": function () {
231
 
        var receiverCalls = 0,
232
 
            supplierCalls = 0,
233
 
            instance;
234
 
 
235
 
        function receiver() { receiverCalls += 1; }
236
 
        function supplier() { supplierCalls += 1; }
237
 
 
238
 
        supplier.prototype.foo = 'foo';
239
 
        supplier.prototype.bar = function () { return 'bar'; };
240
 
        supplier.prototype.baz = function () { return 'baz'; };
241
 
 
242
 
        Assert.areSame(receiver, Y.augment(receiver, supplier));
243
 
        ArrayAssert.itemsAreSame(['foo', 'bar', 'baz'], Y.Object.keys(receiver.prototype));
244
 
        Assert.areSame('foo', receiver.prototype.foo);
245
 
        Assert.areNotSame(supplier.prototype.bar, receiver.prototype.bar, '`bar()` should be sequestered on `receiver.prototype`');
246
 
        Assert.areNotSame(supplier.prototype.baz, receiver.prototype.baz, '`baz()` should be sequestered on `receiver.prototype`');
247
 
        Assert.isFunction(receiver.prototype.bar);
248
 
        Assert.isFunction(receiver.prototype.baz);
249
 
 
250
 
        instance = new receiver();
251
 
        Assert.areSame(1, receiverCalls, "receiver's constructor should be called once");
252
 
        Assert.areSame(0, supplierCalls, "supplier's constructor should not be called yet");
253
 
 
254
 
        Assert.areNotSame(supplier.prototype.bar, instance.bar, '`bar()` should be sequestered on a new instance of `receiver`');
255
 
        Assert.areNotSame(supplier.prototype.baz, instance.baz, '`baz()` should be sequestered on a new instance of `receiver`');
256
 
        Assert.isFunction(instance.bar);
257
 
        Assert.isFunction(instance.baz);
258
 
        Assert.areSame('bar', instance.bar(), 'calling `bar()` on a new instance of `receiver` should work');
259
 
        Assert.areSame(1, supplierCalls, "supplier's constructor should be called on first use of a sequestered function");
260
 
        Assert.areSame(supplier.prototype.bar, instance.bar, 'after the first call, `instance.bar` and `supplier.prototype.bar` should be the same');
261
 
        Assert.areSame(supplier.prototype.baz, instance.baz, 'after the first call, `instance.baz` and `supplier.prototype.baz` should be the same');
262
 
        Assert.areSame('baz', instance.baz());
263
 
        Assert.areSame(1, supplierCalls, "supplier's constructor should not be called twice");
264
 
    },
265
 
 
266
 
    "receiver function prototype properties should not be overwritten when `overwrite` is not `true`": function () {
267
 
        var receiver = this.receiver,
268
 
            supplier = this.supplier;
269
 
 
270
 
        function quux() {}
271
 
 
272
 
        receiver.prototype.foo  = 'moo';
273
 
        receiver.prototype.quux = quux;
274
 
 
275
 
        supplier.prototype.foo  = 'foo';
276
 
        supplier.prototype.bar  = 'bar';
277
 
        supplier.prototype.quux = function () {};
278
 
 
279
 
        Y.augment(receiver, supplier);
280
 
 
281
 
        Assert.areSame('moo', receiver.prototype.foo);
282
 
        Assert.areSame('bar', receiver.prototype.bar);
283
 
        Assert.areSame(quux, receiver.prototype.quux);
284
 
    },
285
 
 
286
 
    "receiver function prototype properties should be overwritten when `overwrite` is `true`": function () {
287
 
        var receiver = this.receiver,
288
 
            supplier = this.supplier;
289
 
 
290
 
        function quux() {}
291
 
 
292
 
        receiver.prototype.foo  = 'moo';
293
 
        receiver.prototype.quux = quux;
294
 
 
295
 
        supplier.prototype.foo  = 'foo';
296
 
        supplier.prototype.bar  = 'bar';
297
 
        supplier.prototype.quux = function () {};
298
 
 
299
 
        Y.augment(receiver, supplier, true);
300
 
 
301
 
        Assert.areSame('foo', receiver.prototype.foo);
302
 
        Assert.areSame('bar', receiver.prototype.bar);
303
 
        Assert.areNotSame(quux, receiver.prototype.quux);
304
 
    },
305
 
 
306
 
    "only whitelisted properties should be copied to a receiver function": function () {
307
 
        var receiver = this.receiver,
308
 
            supplier = this.supplier;
309
 
 
310
 
        supplier.prototype.foo = 'a';
311
 
        supplier.prototype.bar = 'b';
312
 
        supplier.prototype.baz = 'c';
313
 
 
314
 
        Y.augment(receiver, supplier, false, ['foo', 'baz']);
315
 
 
316
 
        ArrayAssert.itemsAreSame(['foo', 'baz'], Y.Object.keys(receiver.prototype));
317
 
    },
318
 
 
319
 
    "supplier constructor should receive supplied args when augmenting a receiver function": function () {
320
 
        var calls    = 0,
321
 
            receiver = function () {};
322
 
 
323
 
        function supplier(foo) {
324
 
            calls += 1;
325
 
            Assert.areSame('foo', foo);
326
 
        }
327
 
 
328
 
        function supplierTwo(foo, bar) {
329
 
            calls += 1;
330
 
            Assert.areSame('foo', foo);
331
 
            Assert.areSame('bar', bar);
332
 
        }
333
 
 
334
 
        supplier.prototype.foo    = function () {};
335
 
        supplierTwo.prototype.foo = function () {};
336
 
 
337
 
        Y.augment(receiver, supplier, false, null, 'foo');
338
 
        new receiver().foo();
339
 
 
340
 
        receiver = function () {};
341
 
        Y.augment(receiver, supplierTwo, false, null, ['foo', 'bar']);
342
 
        new receiver().foo();
343
 
 
344
 
        Assert.areSame(2, calls);
345
 
    },
346
 
 
347
 
    // http://yuilibrary.com/projects/yui3/ticket/2530501
348
 
    'augmenting a Y.Node instance should not overwrite existing properties by default': function () {
349
 
        var node = Y.one('#test');
350
 
 
351
 
        Assert.isInstanceOf(Y.Node, node.get('parentNode'), 'parentNode attribute should be a Node instance before augment');
352
 
        Y.augment(node, Y.Attribute);
353
 
        Assert.isInstanceOf(Y.Node, node.get('parentNode'), 'parentNode attribute should be a Node instance after augment');
354
 
    }
355
 
}));
356
 
 
357
 
// TODO: mix tests should be moved to the tests for yui-core.js, where mix()
358
 
// lives now. Need to refactor yui-core tests first though.
359
 
 
360
 
suite.add(new Y.Test.Case({
361
 
    name: 'mix: default mode (object to object)',
362
 
 
363
 
    setUp: function () {
364
 
        Object.prototype.foo = "I'm on Object.prototype!";
365
 
        Object.prototype.zoo = "I'm on Object.prototype!";
366
 
    },
367
 
 
368
 
    tearDown: function () {
369
 
        delete Object.prototype.foo;
370
 
        delete Object.prototype.zoo;
371
 
    },
372
 
 
373
 
    'test [mode 0]: missing receiver or supplier': function () {
374
 
        var receiver = {a: 'a'},
375
 
            supplier = {z: 'z'};
376
 
 
377
 
        Assert.areSame(receiver, Y.mix(receiver), 'returns receiver when no supplier is passed');
378
 
        Assert.areSame(Y, Y.mix(null, supplier), 'returns Y when no receiver is passed');
379
 
        Assert.areSame(Y, Y.mix(), 'returns Y when neither receiver nor supplier is passed');
380
 
    },
381
 
 
382
 
    'test [mode 0]: returns receiver': function () {
383
 
        var receiver = {a: 'a'},
384
 
            supplier = {z: 'z'};
385
 
 
386
 
        Assert.areSame(receiver, Y.mix(receiver, supplier));
387
 
    },
388
 
 
389
 
    'test [mode 0]: no overwrite, no whitelist, no merge': function () {
390
 
        var receiver = {a: 'a'},
391
 
            supplier = {a: 'z', foo: 'foo', bar: 'bar', toString: function () {}};
392
 
 
393
 
        Y.mix(receiver, supplier);
394
 
 
395
 
        Assert.areSame(2, Y.Object.size(receiver), 'should own two keys');
396
 
        ObjectAssert.ownsKeys(['a', 'bar'], receiver, 'should own new keys');
397
 
        Assert.areSame('a', receiver.a, '"a" should not be overwritten');
398
 
 
399
 
        receiver = {};
400
 
        Y.mix(receiver, Y.Object(supplier));
401
 
 
402
 
        Assert.areSame(0, Y.Object.size(receiver), 'prototype properties should not get mixed');
403
 
    },
404
 
 
405
 
    'test [mode 0]: overwrite, no whitelist, no merge': function () {
406
 
        var receiver = {a: 'a', obj: {a: 'a', b: 'b'}},
407
 
            supplier = {a: 'z', foo: 'foo', bar: 'bar', obj: {a: 'z'}, toString: function () {}};
408
 
 
409
 
        Y.mix(receiver, supplier, true);
410
 
 
411
 
        Assert.areSame(5, Y.Object.size(receiver), 'should own five keys');
412
 
        ObjectAssert.ownsKeys(['a', 'foo', 'bar', 'obj', 'toString'], receiver, 'should own new keys');
413
 
        Assert.areSame('z', receiver.a, '"a" should be overwritten');
414
 
        Assert.areSame(receiver.obj, supplier.obj, 'objects should be overwritten, not merged');
415
 
        Assert.areSame(supplier.toString, receiver.toString, '"toString" should be the same');
416
 
    },
417
 
 
418
 
    'test [mode 0]: overwrite, whitelist, no merge': function () {
419
 
        var receiver = {a: 'a', bar: 'a', obj: {a: 'a', b: 'b'}},
420
 
            supplier = {a: 'z', foo: 'foo', bar: 'bar', obj: {a: 'z'}};
421
 
 
422
 
        Y.mix(receiver, supplier, true, ['a', 'obj']);
423
 
 
424
 
        Assert.areSame(3, Y.Object.size(receiver), 'should own three keys');
425
 
        ObjectAssert.ownsKeys(['a', 'bar', 'obj'], receiver, 'should own whitelisted keys');
426
 
        Assert.areSame('z', receiver.a, '"a" should be overwritten');
427
 
        Assert.areSame('a', receiver.bar, '"bar" should not be overwritten');
428
 
        Assert.areSame(receiver.obj, supplier.obj, 'objects should be overwritten, not merged');
429
 
    },
430
 
 
431
 
    'test [mode 0]: no overwrite, whitelist, no merge': function () {
432
 
        var receiver = {a: 'a', bar: 'a', obj: {a: 'a', b: 'b'}},
433
 
            supplier = {a: 'z', foo: 'foo', moo: 'cow', bar: 'bar', obj: {a: 'z'}};
434
 
 
435
 
        Y.mix(receiver, supplier, false, ['a', 'obj', 'foo']);
436
 
 
437
 
        Assert.areSame(3, Y.Object.size(receiver), 'should own three keys');
438
 
        ObjectAssert.ownsKeys(['a', 'bar', 'obj'], receiver, 'should own whitelisted keys');
439
 
        Assert.areSame('a', receiver.a, '"a" should not be overwritten');
440
 
        Assert.areNotSame(receiver.obj, supplier.obj, '"obj" should not be overwritten');
441
 
        Assert.areSame('a', receiver.obj.a, '"obj" should not be merged');
442
 
    },
443
 
 
444
 
    'test [mode 0]: no overwrite, no whitelist, merge': function () {
445
 
        var receiver = {a: 'a', fakeout: {a: 'a'}, obj: {a: 'a', b: 'b', deep: {foo: 'foo', deeper: {bar: 'bar'}}}},
446
 
            supplier = {a: 'z', foo: 'foo', bar: 'bar', fakeout: 'moo', obj: {a: 'z', deep: {deeper: {bar: 'z', baz: 'baz'}}}};
447
 
 
448
 
        Y.mix(receiver, supplier, false, null, 0, true);
449
 
 
450
 
        Assert.areSame(4, Y.Object.size(receiver), 'should own four keys');
451
 
        ObjectAssert.ownsKeys(['a', 'bar', 'fakeout', 'obj'], receiver, 'should own new keys');
452
 
        Assert.areSame('a', receiver.a, '"a" should not be overwritten');
453
 
        Assert.areSame(1, Y.Object.size(receiver.fakeout), 'non-objects should not be merged into objects');
454
 
 
455
 
        Assert.areNotSame(receiver.obj, supplier.obj, 'objects should be merged, not overwritten');
456
 
        Assert.areNotSame(receiver.obj.deep, supplier.obj.deep, 'deep objects should be merged, not overwritten');
457
 
        Assert.areNotSame(receiver.obj.deep.deeper, supplier.obj.deep.deeper, 'deeper objects should be merged, not overwritten');
458
 
 
459
 
        Assert.areSame('a', receiver.obj.a, 'merged properties should not be overwritten');
460
 
        Assert.areSame('b', receiver.obj.b, 'objects should be merged');
461
 
        Assert.areSame('foo', receiver.obj.deep.foo, 'deep objects should be merged');
462
 
        Assert.areSame('bar', receiver.obj.deep.deeper.bar, 'deeper merged properties should not be overwritten');
463
 
        Assert.areSame('baz', receiver.obj.deep.deeper.baz, 'deeper objects should be merged');
464
 
 
465
 
        // Array merge (see http://yuilibrary.com/projects/yui3/ticket/2528405)
466
 
        receiver = {a: [{x: 1}, {x: 2}]};
467
 
        supplier = {a: [{y: 99}, {y: 98}]};
468
 
 
469
 
        Y.mix(receiver, supplier, false, null, 0, true);
470
 
 
471
 
        Assert.areSame(1, receiver.a[0].x, 'objects in arrays should be merged');
472
 
        Assert.areSame(99, receiver.a[0].y, 'objects in arrays should be merged');
473
 
    },
474
 
 
475
 
    'test [mode 0]: overwrite, no whitelist, merge': function () {
476
 
        var receiver = {a: 'a', obj: {a: 'a', b: 'b', deep: {foo: 'foo', deeper: {bar: 'bar'}}}},
477
 
            supplier = {a: 'z', foo: 'foo', bar: 'bar', obj: {a: 'z', deep: {deeper: {bar: 'z'}}}};
478
 
 
479
 
        Y.mix(receiver, supplier, true, null, 0, true);
480
 
 
481
 
        Assert.areSame(4, Y.Object.size(receiver), 'should own four keys');
482
 
        ObjectAssert.ownsKeys(['a', 'foo', 'bar', 'obj'], receiver, 'should own new keys');
483
 
        Assert.areSame('z', receiver.a, '"a" should be overwritten');
484
 
        Assert.areSame('foo', receiver.foo, '"foo" should be received');
485
 
 
486
 
        Assert.areNotSame(receiver.obj, supplier.obj, 'objects should be merged, not overwritten');
487
 
        Assert.areNotSame(receiver.obj.deep, supplier.obj.deep, 'deep objects should be merged, not overwritten');
488
 
        Assert.areNotSame(receiver.obj.deep.deeper, supplier.obj.deep.deeper, 'deeper objects should be merged, not overwritten');
489
 
 
490
 
        Assert.areSame('z', receiver.obj.a, 'objects should be merged');
491
 
        Assert.areSame('b', receiver.obj.b, 'objects should be merged');
492
 
        Assert.areSame('foo', receiver.obj.deep.foo, 'deep objects should be merged');
493
 
        Assert.areSame('z', receiver.obj.deep.deeper.bar, 'deeper objects should be merged');
494
 
 
495
 
        // Array merge (see http://yuilibrary.com/projects/yui3/ticket/2528405)
496
 
        receiver = {a: [{x: 1}, {x: 2}]};
497
 
        supplier = {a: [{y: 99}, {y: 98}]};
498
 
 
499
 
        Y.mix(receiver, supplier, true, null, 0, true);
500
 
 
501
 
        Assert.areSame(1, receiver.a[0].x, 'objects in arrays should be merged');
502
 
        Assert.areSame(99, receiver.a[0].y, 'objects in arrays should be merged');
503
 
    },
504
 
 
505
 
    'test [mode 0]: overwrite, whitelist, merge': function () {
506
 
        var receiver = {a: 'a', obj: {a: 'a', b: 'b', deep: {foo: 'foo', deeper: {bar: 'bar'}}}},
507
 
            supplier = {a: 'z', foo: 'foo', bar: 'bar', obj: {a: 'z', deep: {deeper: {bar: 'z'}}}};
508
 
 
509
 
        Y.mix(receiver, supplier, true, ['a', 'foo', 'deeper'], 0, true);
510
 
 
511
 
        Assert.areSame(3, Y.Object.size(receiver), 'should own three keys');
512
 
        ObjectAssert.ownsKeys(['a', 'foo', 'obj'], receiver, 'should own new keys');
513
 
        Assert.areSame('z', receiver.a, '"a" should be overwritten');
514
 
        Assert.areSame('foo', receiver.foo, '"foo" should be received');
515
 
 
516
 
        Assert.areNotSame(receiver.obj, supplier.obj, 'objects should be merged, not overwritten');
517
 
        Assert.areNotSame(receiver.obj.deep, supplier.obj.deep, 'deep objects should be merged, not overwritten');
518
 
        Assert.areNotSame(receiver.obj.deep.deeper, supplier.obj.deep.deeper, 'deeper objects should be merged, not overwritten');
519
 
 
520
 
        Assert.areSame('a', receiver.obj.a, 'non-whitelisted deep properties should not be overwritten');
521
 
        Assert.areSame('b', receiver.obj.b, 'objects should be merged');
522
 
        Assert.areSame('foo', receiver.obj.deep.foo, 'deep objects should be merged');
523
 
        Assert.areSame('bar', receiver.obj.deep.deeper.bar, 'non-whitelisted deeper objects should be merged');
524
 
    },
525
 
 
526
 
    'test [mode 0]: overwrite, whitelist, toplevel merge': function() {
527
 
        var receiver = {bar: true, foo:{a:1, b:2}},
528
 
            supplier = {bar: false, foo:{c:3}};
529
 
 
530
 
        Y.aggregate(receiver, supplier, true, ['foo']);
531
 
        Y.mix(receiver, supplier, true, ['foo'], 0, true);
532
 
 
533
 
        ObjectAssert.hasKeys(['a', 'b', 'c'], receiver.foo, "merge property with an object value from whitelist is missing properties");
534
 
        ObjectAssert.areEqual({a:1, b:2, c:3}, receiver.foo, "merge property with an object value from whitelist doesn't have expected properties/values");
535
 
    }
536
 
}));
537
 
 
538
 
suite.add(new Y.Test.Case({
539
 
    name: 'mix: mode 1 (prototype to prototype)',
540
 
 
541
 
    setUp: function () {
542
 
        this.supplier = function () {};
543
 
        this.supplier.prototype = {a: 'z', foo: 'foo', bar: 'bar', obj: {a: 'z', deep: {deeper: {bar: 'z'}}}};
544
 
        this.supplier.owned = "I'm an owned property!";
545
 
    },
546
 
 
547
 
    tearDown: function () {
548
 
        delete this.supplier;
549
 
    },
550
 
 
551
 
    'test [mode 1]: no overwrite, no whitelist, no merge': function () {
552
 
        var receiver = function () {};
553
 
        receiver.a = 'a';
554
 
 
555
 
        Y.mix(receiver, this.supplier, false, null, 1);
556
 
 
557
 
        Assert.areSame(1, Y.Object.size(receiver), 'should own one key');
558
 
        Assert.areSame('a', receiver.a, '"a" should not be overwritten');
559
 
        ObjectAssert.ownsKeys(['a', 'foo', 'bar', 'obj'], receiver.prototype, 'prototype should own new keys');
560
 
        Assert.areSame('z', receiver.prototype.a, '"a" should exist on prototype');
561
 
    },
562
 
 
563
 
    'test [mode 1]: overwrite, no whitelist, no merge': function () {
564
 
        var receiver = function () {};
565
 
 
566
 
        receiver.a = 'a';
567
 
        receiver.prototype.a = 'a';
568
 
        receiver.prototype.obj = {a: 'a', foo: 'foo'};
569
 
 
570
 
        Y.mix(receiver, this.supplier, true, null, 1);
571
 
 
572
 
        Assert.areSame(1, Y.Object.size(receiver), 'should own one key');
573
 
        Assert.areSame('a', receiver.a, '"a" should not be overwritten on receiver');
574
 
        Assert.areSame('z', receiver.prototype.a, '"a" should be overwritten on receiver\'s prototype');
575
 
        Assert.areSame(this.supplier.prototype.obj, receiver.prototype.obj);
576
 
    },
577
 
 
578
 
    'test [mode 1]: overwrite, whitelist, no merge': function () {
579
 
        var receiver = function () {};
580
 
 
581
 
        receiver.prototype.a = 'a';
582
 
 
583
 
        Y.mix(receiver, this.supplier, true, ['a', 'foo'], 1);
584
 
 
585
 
        ObjectAssert.ownsNoKeys(receiver);
586
 
        Assert.areSame(2, Y.Object.size(receiver.prototype));
587
 
        Assert.areSame('z', receiver.prototype.a);
588
 
        Assert.areSame('foo', receiver.prototype.foo);
589
 
    },
590
 
 
591
 
    'test [mode 1]: no overwrite, whitelist, no merge': function () {
592
 
        var receiver = function () {};
593
 
 
594
 
        receiver.prototype.a = 'a';
595
 
 
596
 
        Y.mix(receiver, this.supplier, false, ['a', 'foo'], 1);
597
 
 
598
 
        ObjectAssert.ownsNoKeys(receiver);
599
 
        Assert.areSame(2, Y.Object.size(receiver.prototype));
600
 
        Assert.areSame('a', receiver.prototype.a);
601
 
        Assert.areSame('foo', receiver.prototype.foo);
602
 
    },
603
 
 
604
 
    'test [mode 1]: no overwrite, no whitelist, merge': function () {
605
 
        var receiver = function () {};
606
 
 
607
 
        receiver.prototype.obj = {a: 'a', foo: 'foo', deep: {deeper: {a: 'a'}}};
608
 
 
609
 
        Y.mix(receiver, this.supplier, false, null, 1, true);
610
 
 
611
 
        Assert.areNotSame(this.supplier.prototype.obj, receiver.prototype.obj);
612
 
        Assert.areSame('a', receiver.prototype.obj.a);
613
 
        Assert.areSame('foo', receiver.prototype.obj.foo);
614
 
        Assert.areSame('a', receiver.prototype.obj.deep.deeper.a);
615
 
        Assert.areSame('z', receiver.prototype.obj.deep.deeper.bar);
616
 
    },
617
 
 
618
 
    'test [mode 1]: overwrite, no whitelist, merge': function () {
619
 
        var receiver = function () {};
620
 
 
621
 
        receiver.prototype.obj = {a: 'a', foo: 'foo', deep: {deeper: {bar: 'a'}}};
622
 
 
623
 
        Y.mix(receiver, this.supplier, true, null, 1, true);
624
 
 
625
 
        Assert.areNotSame(this.supplier.prototype.obj, receiver.prototype.obj);
626
 
        Assert.areSame('z', receiver.prototype.obj.a);
627
 
        Assert.areSame('foo', receiver.prototype.obj.foo);
628
 
        Assert.areSame('z', receiver.prototype.obj.deep.deeper.bar);
629
 
    },
630
 
 
631
 
    'test [mode 1]: overwrite, whitelist, merge': function () {
632
 
        var receiver = function () {};
633
 
 
634
 
        receiver.prototype.obj = {a: 'a', foo: 'foo', deep: {deeper: {bar: 'a'}}};
635
 
 
636
 
        Y.mix(receiver, this.supplier, true, ['a', 'obj', 'deep'], 1, true);
637
 
 
638
 
        Assert.isUndefined(receiver.prototype.bar);
639
 
        Assert.areNotSame(this.supplier.prototype.obj, receiver.prototype.obj);
640
 
        Assert.areSame('z', receiver.prototype.obj.a);
641
 
        Assert.areSame('foo', receiver.prototype.obj.foo);
642
 
    }
643
 
}));
644
 
 
645
 
// The tests for other modes above cover the various mix options exhaustively.
646
 
// From here on, we're just doing sanity checks of the remaining modes.
647
 
 
648
 
suite.add(new Y.Test.Case({
649
 
    name: 'mix: mode 2 (object to object and prototype to prototype)',
650
 
 
651
 
    setUp: function () {
652
 
        this.supplier = function () {};
653
 
        this.supplier.prototype = {a: 'z', foo: 'foo', bar: 'bar', obj: {a: 'z', deep: {deeper: {bar: 'z'}}}};
654
 
        this.supplier.owned = "I'm an owned property!";
655
 
    },
656
 
 
657
 
    tearDown: function () {
658
 
        delete this.supplier;
659
 
    },
660
 
 
661
 
    'test [mode 2]: basic sanity check': function () {
662
 
        var receiver = function () {};
663
 
 
664
 
        Y.mix(receiver, this.supplier, false, null, 2);
665
 
 
666
 
        ObjectAssert.ownsKeys(['a', 'foo', 'bar', 'obj'], receiver.prototype);
667
 
        ObjectAssert.ownsKey('owned', receiver);
668
 
    }
669
 
}));
670
 
 
671
 
suite.add(new Y.Test.Case({
672
 
    name: 'mix: mode 3 (prototype to object)',
673
 
 
674
 
    setUp: function () {
675
 
        this.supplier = function () {};
676
 
        this.supplier.prototype = {a: 'z', foo: 'foo', bar: 'bar', obj: {a: 'z', deep: {deeper: {bar: 'z'}}}};
677
 
        this.supplier.owned = "I'm an owned property!";
678
 
    },
679
 
 
680
 
    tearDown: function () {
681
 
        delete this.supplier;
682
 
    },
683
 
 
684
 
    'test [mode 3]: basic sanity check': function () {
685
 
        var receiver = function () {};
686
 
 
687
 
        Y.mix(receiver, this.supplier, false, null, 3);
688
 
 
689
 
        ObjectAssert.ownsKeys(['a', 'foo', 'bar', 'obj'], receiver);
690
 
        ObjectAssert.ownsNoKeys(receiver.prototype);
691
 
        Assert.isUndefined(receiver.owned);
692
 
    }
693
 
}));
694
 
 
695
 
suite.add(new Y.Test.Case({
696
 
    name: 'mix: mode 4 (object to prototype)',
697
 
 
698
 
    setUp: function () {
699
 
        this.supplier = function () {};
700
 
        this.supplier.prototype = {a: 'z', foo: 'foo', bar: 'bar', obj: {a: 'z', deep: {deeper: {bar: 'z'}}}};
701
 
        this.supplier.owned = "I'm an owned property!";
702
 
    },
703
 
 
704
 
    tearDown: function () {
705
 
        delete this.supplier;
706
 
    },
707
 
 
708
 
    'test [mode 4]: basic sanity check': function () {
709
 
        var receiver = function () {};
710
 
 
711
 
        Y.mix(receiver, this.supplier, false, null, 4);
712
 
 
713
 
        ObjectAssert.ownsKey('owned', receiver.prototype);
714
 
        ObjectAssert.ownsNoKeys(receiver);
715
 
    }
716
 
}));
717
 
 
718
 
Y.Test.Runner.add(suite);
719
 
 
720
 
}, '@VERSION@', {requires: ['attribute', 'test']});