~lutostag/ubuntu/utopic/maas/1.5.2

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): Andres Rodriguez
  • Date: 2012-03-15 18:14:08 UTC
  • mfrom: (1.1.3)
  • Revision ID: package-import@ubuntu.com-20120315181408-zgl94hzo0x4n99an
Tags: 0.1+bzr295+dfsg-0ubuntu2
* debian/patches:
  - 01-fix-database-settings.patch: Update to set PSERV_URL.
  - 02-pserv-config.patch: Set port to 8001.
* debian/maas.postinst: Run maas-import-isos on install.
* debian/control: Depends on rabbitmq-server.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
YUI.add('array-test', function (Y) {
 
2
 
 
3
var Assert = Y.Assert,
 
4
 
 
5
    suite = new Y.Test.Suite('Y.Array (core)');
 
6
 
 
7
suite.add(new Y.Test.Case({
 
8
    name: 'Array tests',
 
9
 
 
10
    testArray: function () {
 
11
        var els   = document.getElementById('tester').getElementsByTagName('span'),
 
12
            nodes = Y.all('#tester span');
 
13
 
 
14
        Assert.isArray(Y.Array(els), '');
 
15
        Assert.areSame(3, Y.Array(els).length);
 
16
        Assert.areSame(2, Y.Array(els, 1).length);
 
17
 
 
18
        // @TODO NodeList will be wrapped rather than have the items coerced into an
 
19
        // array.  If Array.test is adapted to handle NodeLists this could be made to
 
20
        // work.
 
21
        Assert.isArray(Y.Array(nodes));
 
22
        Assert.areSame(1, Y.Array(nodes).length);
 
23
 
 
24
        // Y.log('els length:' + els.length);
 
25
        // Y.log('a length:' + a.length);
 
26
        // Y.log('els tagName:' + els.tagName);
 
27
        // Y.log('els alert:' + els.alert);
 
28
        // Y.log('els size:' + els.size);
 
29
        // Y.log('els array test:' + Y.Array.test(els));
 
30
 
 
31
        // Y.Lang.type is broken for HTMLElementCollections in Safari.  isObject
 
32
        // returns false because typeof returns function
 
33
 
 
34
        // Y.log('els isObject:' + Y.Lang.isObject(els)); // false in Safari
 
35
        // Y.log('els type:' + Y.Lang.type(els) + '+'); // object
 
36
        // Y.log('els typeof:' + typeof els); // function
 
37
        // Y.log('els isFunction:' + Y.Lang.isFunction(els)); // false
 
38
        // Y.log('els.call:' + els.call);
 
39
        // Y.log('els.apply:' + els.apply);
 
40
    },
 
41
 
 
42
    testDedupe: function () {
 
43
        var array = ['foo', 'bar', 'foo', 'baz', 'foo', 'baz'];
 
44
 
 
45
        Y.ArrayAssert.itemsAreSame(['foo', 'bar', 'baz'], Y.Array.dedupe(array));
 
46
    },
 
47
 
 
48
    testEach: function () {
 
49
        var calls = 0,
 
50
            data  = ['a', 'b', 'c', 'd'],
 
51
            obj   = {foo: 'bar'};
 
52
 
 
53
        Y.Array.each(data, function (item, index, array) {
 
54
            calls += 1;
 
55
 
 
56
            Assert.areSame(data[index], item, 'the current item should be passed to the callback');
 
57
            Assert.areSame(data, array, 'the array should be passed to the callback');
 
58
            Assert.areSame(Y, this, 'the `this` object should default to the YUI instance');
 
59
        });
 
60
 
 
61
        Assert.areSame(calls, 4, 'the callback should be called 4 times');
 
62
 
 
63
        Y.Array.each(data, function () {
 
64
            Assert.areSame(obj, this, 'the `this` object should be overridable');
 
65
        }, obj);
 
66
 
 
67
        Assert.areSame(Y.Array.each, Y.Array.forEach, 'forEach should be an alias for each');
 
68
    },
 
69
 
 
70
    'each() should handle sparse arrays correctly': function () {
 
71
        var calls = 0,
 
72
            foo   = Array(5);
 
73
 
 
74
        foo.push('foo');
 
75
 
 
76
        Y.Array.each(foo, function () {
 
77
            calls += 1;
 
78
        });
 
79
 
 
80
        Assert.areSame(1, calls);
 
81
    },
 
82
 
 
83
    testHash: function () {
 
84
        Y.ObjectAssert.areEqual(
 
85
            {a: 'foo', b: 'bar', c: true},
 
86
            Y.Array.hash(['a', 'b', 'c'], ['foo', 'bar']),
 
87
            'keys should be mapped to values; true is used for missing values'
 
88
        );
 
89
 
 
90
        Y.ObjectAssert.areEqual(
 
91
            {a: true, b: true, c: true},
 
92
            Y.Array.hash(['a', 'b', 'c']),
 
93
            'the values array is optional'
 
94
        );
 
95
    },
 
96
 
 
97
    'hash() should handle sparse arrays correctly': function () {
 
98
        var keys   = ['foo', 'bar'],
 
99
            values = ['baz'],
 
100
            hash;
 
101
 
 
102
        keys.length   = 5;
 
103
        values.length = 5;
 
104
 
 
105
        hash = Y.Array.hash(keys, values);
 
106
 
 
107
        Y.Assert.areSame(2, Y.Object.size(hash));
 
108
        Y.Assert.areSame('baz', hash.foo);
 
109
        Y.Assert.isTrue(hash.bar);
 
110
    },
 
111
 
 
112
    testIndexOf: function () {
 
113
        var data = ['a', 'b', 1, 0, false, null, 'a'];
 
114
 
 
115
        Y.Assert.areSame(0, Y.Array.indexOf(data, 'a'), 'should find the first match');
 
116
        Y.Assert.areSame(-1, Y.Array.indexOf(data, 'z'), 'should return -1 on no match');
 
117
        Y.Assert.areSame(2, Y.Array.indexOf(data, 1), 'should find numbers');
 
118
        Y.Assert.areSame(4, Y.Array.indexOf(data, false), 'should perform strict equality checks');
 
119
        Y.Assert.areSame(5, Y.Array.indexOf(data, null), 'should find null');
 
120
 
 
121
        // TODO: support fromIndex
 
122
    },
 
123
 
 
124
    'indexOf() should handle sparse arrays correctly': function () {
 
125
        var array = ['a', 'b'];
 
126
        array[3] = undefined;
 
127
 
 
128
        Y.Assert.areSame(3, Y.Array.indexOf(array, undefined));
 
129
    },
 
130
 
 
131
    testNumericSort: function () {
 
132
        // the stock sort behavior should fail to produce desired result
 
133
        Y.ArrayAssert.itemsAreEqual([1, 100, 2, 3], [3, 100, 1, 2].sort());
 
134
        Y.ArrayAssert.itemsAreEqual([1, 2, 3, 100], [3, 100, 1, 2].sort(Y.Array.numericSort));
 
135
    },
 
136
 
 
137
    testSome: function () {
 
138
        var data  = [1, 2, 3],
 
139
            obj   = {foo: 'bar'},
 
140
            calls = 0;
 
141
 
 
142
        Assert.isTrue(Y.Array.some(data, function (v, index, array) {
 
143
            calls += 1;
 
144
 
 
145
            Assert.areSame(data[index], v, 'the current item should be passed to the callback');
 
146
            Assert.areSame(data, array, 'the array should be passed to the callback');
 
147
            Assert.areSame(Y.config.win, this, 'the `this` object should default to the global object');
 
148
 
 
149
            if (v === 2) { return 'truthy'; }
 
150
            if (v === 3) { Y.fail('truthy value did not stop iteration'); }
 
151
        }), 'should return true');
 
152
 
 
153
        Assert.areSame(2, calls, 'callback should be called twice');
 
154
 
 
155
        Assert.isFalse(Y.Array.some(data, function () {
 
156
            Assert.areSame(obj, this, 'the `this` object should be overridable');
 
157
        }, obj), 'should return false');
 
158
    },
 
159
 
 
160
    'some() should handle sparse arrays correctly': function () {
 
161
        var calls = 0,
 
162
            foo   = Array(5);
 
163
 
 
164
        foo.push('foo');
 
165
 
 
166
        Y.Array.some(foo, function () {
 
167
            calls += 1;
 
168
        });
 
169
 
 
170
        Assert.areSame(1, calls);
 
171
    },
 
172
 
 
173
    testTest: function () {
 
174
        Y.Assert.areEqual(0, Y.Array.test(function(){})); // functions should fail
 
175
        Y.Assert.areEqual(0, Y.Array.test(Y.one('#tester'))); // single nodes should fail
 
176
        Y.Assert.areEqual(0, Y.Array.test('string'));
 
177
        Y.Assert.areEqual(0, Y.Array.test(12345));
 
178
        Y.Assert.areEqual(0, Y.Array.test(null));
 
179
        Y.Assert.areEqual(0, Y.Array.test(undefined));
 
180
        Y.Assert.areEqual(0, Y.Array.test(/regexp/));
 
181
        Y.Assert.areEqual(0, Y.Array.test(new Date()));
 
182
        Y.Assert.areEqual(1, Y.Array.test([1, 2]));
 
183
        Y.Assert.areEqual(1, Y.Array.test([]));
 
184
        // Y.Assert.areEqual(1, Y.Array.test('string'.toCharArray()));
 
185
        Y.Assert.areEqual(2, Y.Array.test(arguments), 'arguments should be arraylike'); // arguments collection
 
186
        Y.Assert.areEqual(2, Y.Array.test(document.getElementsByTagName('span')), 'htmlelement collections should be arraylike'); // HTMLElementsCollection
 
187
 
 
188
        // @TODO figure out what to do with this.  A NodeList does not contain a collection of Nodes; it
 
189
        // contains a collection of HTMLElements.
 
190
        // Y.Assert.areEqual(3, Y.Array.test(Y.all('#btnRun')), 'nodelists should be specifically identified as a special collection'); // NodeList
 
191
        Y.Assert.areEqual(0, Y.Array.test(Y.all('span')), 'nodelists are not currently considered arraylike'); // NodeList
 
192
    }
 
193
}));
 
194
 
 
195
Y.Test.Runner.add(suite);
 
196
 
 
197
}, '@VERSION@', {requires: ['test']});