1
YUI.add('object-test', function (Y) {
6
suite = new Y.Test.Suite('Y.Object');
8
suite.add(new Y.Test.Case({
25
tearDown: function () {
29
test_create: function () {
30
var a = {o: {a: 1}, y: {a: 1}},
31
b = {o: {b: 1}, x: {b: 1}},
36
Y.ObjectAssert.areEqual({a: 1}, c.o);
37
Y.ObjectAssert.areEqual({a: 1}, c.y);
38
Assert.isUndefined(c.x);
40
Y.ObjectAssert.areEqual({b: 1}, d.o);
41
Y.ObjectAssert.areEqual({b: 1}, d.x);
42
Assert.isUndefined(d.y);
44
// Verify the prototype in browsers that support it.
45
if (Object.getPrototypeOf) {
46
cProto = Object.getPrototypeOf(c);
47
dProto = Object.getPrototypeOf(d);
48
} else if (c.__proto__) {
53
if (cProto && dProto) {
54
Assert.areSame(a, cProto, 'Prototype of object `a` should be `c`');
55
Assert.areSame(b, dProto, 'Prototype of object `b` should be `d`');
59
test_each: function () {
61
data = Y.Object(this.o),
62
thisObj = {foo: 'bar'};
69
Y.Object.each(data, function (value, key, obj) {
72
Assert.areSame(data[key], value, 'the current value should be passed to the callback');
73
Assert.areSame(data, obj, 'the object should be passed to the callback');
74
Assert.areSame(Y, this, 'the `this` object should default to the YUI instance');
77
Assert.areSame(calls, 4, 'the callback should be called 4 times');
79
Y.Object.each(data, function () {
80
Assert.areSame(thisObj, this, 'the `this` object should be overridable');
85
Y.Object.each(data, function () {
89
Assert.areSame(calls, 7, 'when the _proto_ argument is `true`, prototype properties should be iterated');
92
test_getValue: function () {
93
Assert.areEqual('c', Y.Object.getValue(this.o, 'c1'));
94
Assert.areEqual('a', Y.Object.getValue(this.o, ['a1', 'a2', 'a3']));
95
Assert.areEqual(undefined, Y.Object.getValue(this.o, ['b1', 'b2', 'b3']));
96
Assert.areEqual(undefined, Y.Object.getValue(null, ['b1', 'b2', 'b3']));
99
test_hasKey: function () {
100
Assert.areSame(Y.Object.owns, Y.Object.hasKey, 'should be an alias for owns()');
103
test_hasValue: function () {
104
Assert.isTrue(Y.Object.hasValue(this.o, 'c'), 'should return true if a value is present');
105
Assert.isFalse(Y.Object.hasValue(this.o, 'z'), 'should return false if a value is not present');
108
test_isEmpty: function () {
109
Assert.isTrue(Y.Object.isEmpty({}), 'should return true for empty objects');
110
Assert.isFalse(Y.Object.isEmpty(this.o), 'should return false for non-empty objects');
111
Assert.isTrue(Y.Object.isEmpty(Y.Object(this.o)), 'should return true for objects with no own properties');
114
test_keys: function () {
115
var el = doc.createElement('span');
117
Y.ArrayAssert.itemsAreSame(['a1', 'b1', 'c1'], Y.Object.keys(this.o), 'should return an array of keys');
120
Assert.areSame(Object.keys, Y.Object.keys, 'when native Object.keys is present, Y.Object.keys should be an alias');
124
// - http://whattheheadsaid.com/2010/10/a-safer-object-keys-compatibility-implementation
125
// - http://groups.google.com/group/prototype-core/browse_thread/thread/48400dbed4c1dd62?pli=1
126
Y.ArrayAssert.itemsAreSame(['toString', 'valueOf'], Y.Object.keys({toString: 1, valueOf: 1}), 'should include toString, valueOf, etc.');
129
Y.assert(Y.Object.keys(el).length > 0, 'should return enumerable keys from DOM elements');
132
test_owns: function () {
133
Assert.isTrue(Y.Object.owns(this.o, 'a1'), 'should return true if the key is owned by the object');
134
Assert.isFalse(Y.Object.owns(this.o, 'z'), 'should return false if the key is not owned by the object');
135
Assert.isFalse(Y.Object.owns(Y.Object(this.o), 'a1'), 'should return false for prototype properties');
138
test_setValue: function () {
139
Y.Object.setValue(this.o, 'c1', 'changed_c');
140
Y.Object.setValue(this.o, ['a1', 'a2', 'a3'], 'changed_a');
141
Y.Object.setValue(this.o, ['b1', 'b2', 'b3'], 'changed_b');
143
Assert.areEqual('changed_c', Y.Object.getValue(this.o, 'c1'));
144
Assert.areEqual('changed_a', Y.Object.getValue(this.o, ['a1', 'a2', 'a3']));
145
Assert.areEqual(undefined, Y.Object.getValue(this.o, ['b1', 'b2', 'b3']));
148
test_size: function () {
149
Assert.areSame(3, Y.Object.size(this.o), 'should return the number of keys owned by an object');
152
'size() should return 0 for non-objects [legacy behavior]': function () {
153
Assert.areSame(0, Y.Object.size('foo'));
154
Assert.areSame(0, Y.Object.size(42));
155
Assert.areSame(0, Y.Object.size(false));
156
Assert.areSame(0, Y.Object.size(true));
157
Assert.areSame(0, Y.Object.size(null));
158
Assert.areSame(0, Y.Object.size(undefined));
159
Assert.areSame(0, Y.Object.size(NaN));
162
test_some: function () {
164
data = Y.Object(this.o),
165
thisObj = {foo: 'bar'};
172
Assert.isTrue(Y.Object.some(data, function (value, key, obj) {
175
Assert.areSame(data[key], value, 'the current value should be passed to the callback');
176
Assert.areSame(data, obj, 'the object should be passed to the callback');
177
Assert.areSame(Y, this, 'the `this` object should default to the YUI instance');
179
if (calls === 3) { return 'truthy'; }
180
if (calls > 3) { Y.fail('truthy value did not stop iteration'); }
181
}), 'should return true');
183
Assert.areSame(3, calls, 'callback should be called 3 times');
185
Assert.isFalse(Y.Object.some(data, function () {
186
Assert.areSame(thisObj, this, 'the `this` object should be overridable');
187
}, thisObj), 'should return false');
191
Y.Object.some(data, function () {
195
Assert.areSame(4, calls, 'prototype properties should not be iterated by default');
199
Y.Object.some(data, function () {
203
Assert.areSame(7, calls, 'prototype properties should be iterated when the _proto_ parameter is `true`');
206
test_values: function () {
207
var data = {a: 'foo', b: 'bar', c: null, d: 'baz', e: undefined};
208
Y.ArrayAssert.itemsAreSame(['foo', 'bar', null, 'baz', undefined], Y.Object.values(data), 'should return an array of values');
211
test_people_messing_up_object_prototype: function () {
214
Object.prototype.foo = 'hello!';
216
Assert.isFalse(Y.Object.hasKey({}, 'foo'), 'hasKey should not find proto props');
217
Assert.areEqual(0, Y.Object.size({}), 'size should not count proto additions');
218
Assert.areEqual(0, Y.Object.keys({}), 'keys should not include proto additions');
219
Assert.areEqual(0, Y.Object.values({}), 'values should not count proto additions');
220
Assert.isFalse(Y.Object.hasValue({}, 'hello!'), 'hasValue should not look at proto additions');
222
Y.Object.each({}, function () {
226
Assert.areEqual(0, count, 'each should not iterate proto props unless asked to do so.');
230
Y.Test.Runner.add(suite);
232
}, '@VERSION@', {requires: ['test']});