~ubuntu-branches/ubuntu/precise/maas/precise-security

« back to all changes in this revision

Viewing changes to src/maasserver/static/jslibs/yui/3.4.1/tests/yui/tests/object-test.js

Tags: 1.2+bzr1373+dfsg-0ubuntu1~12.04.4
* SECURITY UPDATE: failure to authenticate downloaded content (LP: #1039513)
  - debian/patches/CVE-2013-1058.patch: Authenticate downloaded files with
    GnuPG and MD5SUM files. Thanks to Julian Edwards.
  - CVE-2013-1058
* SECURITY UPDATE: configuration options may be loaded from current working
  directory (LP: #1158425)
  - debian/patches/CVE-2013-1057-1-2.patch: Do not load configuration
    options from the current working directory. Thanks to Julian Edwards.
  - CVE-2013-1057

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
YUI.add('object-test', function (Y) {
2
 
 
3
 
var Assert = Y.Assert,
4
 
    doc    = Y.config.doc,
5
 
 
6
 
    suite  = new Y.Test.Suite('Y.Object');
7
 
 
8
 
suite.add(new Y.Test.Case({
9
 
    name: 'Core',
10
 
 
11
 
    setUp: function () {
12
 
        this.o = {
13
 
            a1: {
14
 
                a2: {
15
 
                    a3: 'a'
16
 
                }
17
 
            },
18
 
            b1: {
19
 
                b2: 'b'
20
 
            },
21
 
            c1: 'c'
22
 
        };
23
 
    },
24
 
 
25
 
    tearDown: function () {
26
 
        delete this.o;
27
 
    },
28
 
 
29
 
    test_create: function () {
30
 
        var a = {o: {a: 1}, y: {a: 1}},
31
 
            b = {o: {b: 1}, x: {b: 1}},
32
 
            c = Y.Object(a),
33
 
            d = Y.Object(b),
34
 
            cProto, dProto;
35
 
 
36
 
        Y.ObjectAssert.areEqual({a: 1}, c.o);
37
 
        Y.ObjectAssert.areEqual({a: 1}, c.y);
38
 
        Assert.isUndefined(c.x);
39
 
 
40
 
        Y.ObjectAssert.areEqual({b: 1}, d.o);
41
 
        Y.ObjectAssert.areEqual({b: 1}, d.x);
42
 
        Assert.isUndefined(d.y);
43
 
 
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__) {
49
 
            cProto = c.__proto__;
50
 
            dProto = d.__proto__;
51
 
        }
52
 
 
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`');
56
 
        }
57
 
    },
58
 
 
59
 
    test_each: function () {
60
 
        var calls   = 0,
61
 
            data    = Y.Object(this.o),
62
 
            thisObj = {foo: 'bar'};
63
 
 
64
 
        data.a = 'foo';
65
 
        data.b = 'bar';
66
 
        data.c = null;
67
 
        data.d = undefined;
68
 
 
69
 
        Y.Object.each(data, function (value, key, obj) {
70
 
            calls += 1;
71
 
 
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');
75
 
        });
76
 
 
77
 
        Assert.areSame(calls, 4, 'the callback should be called 4 times');
78
 
 
79
 
        Y.Object.each(data, function () {
80
 
            Assert.areSame(thisObj, this, 'the `this` object should be overridable');
81
 
        }, thisObj);
82
 
 
83
 
        calls = 0;
84
 
 
85
 
        Y.Object.each(data, function () {
86
 
            calls += 1;
87
 
        }, null, true);
88
 
 
89
 
        Assert.areSame(calls, 7, 'when the _proto_ argument is `true`, prototype properties should be iterated');
90
 
    },
91
 
 
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']));
97
 
    },
98
 
 
99
 
    test_hasKey: function () {
100
 
        Assert.areSame(Y.Object.owns, Y.Object.hasKey, 'should be an alias for owns()');
101
 
    },
102
 
 
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');
106
 
    },
107
 
 
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');
112
 
    },
113
 
 
114
 
    test_keys: function () {
115
 
        var el = doc.createElement('span');
116
 
 
117
 
        Y.ArrayAssert.itemsAreSame(['a1', 'b1', 'c1'], Y.Object.keys(this.o), 'should return an array of keys');
118
 
 
119
 
        if (Object.keys) {
120
 
            Assert.areSame(Object.keys, Y.Object.keys, 'when native Object.keys is present, Y.Object.keys should be an alias');
121
 
        }
122
 
 
123
 
        // IE bugs.
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.');
127
 
 
128
 
        el.foo = 'bar';
129
 
        Y.assert(Y.Object.keys(el).length > 0, 'should return enumerable keys from DOM elements');
130
 
    },
131
 
 
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');
136
 
    },
137
 
 
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');
142
 
 
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']));
146
 
    },
147
 
 
148
 
    test_size: function () {
149
 
        Assert.areSame(3, Y.Object.size(this.o), 'should return the number of keys owned by an object');
150
 
    },
151
 
 
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));
160
 
    },
161
 
 
162
 
    test_some: function () {
163
 
        var calls   = 0,
164
 
            data    = Y.Object(this.o),
165
 
            thisObj = {foo: 'bar'};
166
 
 
167
 
        data.a = 'foo';
168
 
        data.b = 'bar';
169
 
        data.c = null;
170
 
        data.d = undefined;
171
 
 
172
 
        Assert.isTrue(Y.Object.some(data, function (value, key, obj) {
173
 
            calls += 1;
174
 
 
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');
178
 
 
179
 
            if (calls === 3) { return 'truthy'; }
180
 
            if (calls > 3) { Y.fail('truthy value did not stop iteration'); }
181
 
        }), 'should return true');
182
 
 
183
 
        Assert.areSame(3, calls, 'callback should be called 3 times');
184
 
 
185
 
        Assert.isFalse(Y.Object.some(data, function () {
186
 
            Assert.areSame(thisObj, this, 'the `this` object should be overridable');
187
 
        }, thisObj), 'should return false');
188
 
 
189
 
        calls = 0;
190
 
 
191
 
        Y.Object.some(data, function () {
192
 
            calls += 1;
193
 
        });
194
 
 
195
 
        Assert.areSame(4, calls, 'prototype properties should not be iterated by default');
196
 
 
197
 
        calls = 0;
198
 
 
199
 
        Y.Object.some(data, function () {
200
 
            calls += 1;
201
 
        }, null, true);
202
 
 
203
 
        Assert.areSame(7, calls, 'prototype properties should be iterated when the _proto_ parameter is `true`');
204
 
    },
205
 
 
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');
209
 
    },
210
 
 
211
 
    test_people_messing_up_object_prototype: function () {
212
 
        var count = 0;
213
 
 
214
 
        Object.prototype.foo = 'hello!';
215
 
 
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');
221
 
 
222
 
        Y.Object.each({}, function () {
223
 
            count++;
224
 
        });
225
 
 
226
 
        Assert.areEqual(0, count, 'each should not iterate proto props unless asked to do so.');
227
 
    }
228
 
}));
229
 
 
230
 
Y.Test.Runner.add(suite);
231
 
 
232
 
}, '@VERSION@', {requires: ['test']});