1
YUI.add('array-test', function (Y) {
5
suite = new Y.Test.Suite('Y.Array (core)');
7
suite.add(new Y.Test.Case({
10
testArray: function () {
11
var els = document.getElementById('tester').getElementsByTagName('span'),
12
nodes = Y.all('#tester span');
14
Assert.isArray(Y.Array(els), '');
15
Assert.areSame(3, Y.Array(els).length);
16
Assert.areSame(2, Y.Array(els, 1).length);
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
21
Assert.isArray(Y.Array(nodes));
22
Assert.areSame(1, Y.Array(nodes).length);
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));
31
// Y.Lang.type is broken for HTMLElementCollections in Safari. isObject
32
// returns false because typeof returns function
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);
42
testDedupe: function () {
43
var array = ['foo', 'bar', 'foo', 'baz', 'foo', 'baz'];
45
Y.ArrayAssert.itemsAreSame(['foo', 'bar', 'baz'], Y.Array.dedupe(array));
48
testEach: function () {
50
data = ['a', 'b', 'c', 'd'],
53
Y.Array.each(data, function (item, index, array) {
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');
61
Assert.areSame(calls, 4, 'the callback should be called 4 times');
63
Y.Array.each(data, function () {
64
Assert.areSame(obj, this, 'the `this` object should be overridable');
67
Assert.areSame(Y.Array.each, Y.Array.forEach, 'forEach should be an alias for each');
70
'each() should handle sparse arrays correctly': function () {
76
Y.Array.each(foo, function () {
80
Assert.areSame(1, calls);
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'
90
Y.ObjectAssert.areEqual(
91
{a: true, b: true, c: true},
92
Y.Array.hash(['a', 'b', 'c']),
93
'the values array is optional'
97
'hash() should handle sparse arrays correctly': function () {
98
var keys = ['foo', 'bar'],
105
hash = Y.Array.hash(keys, values);
107
Y.Assert.areSame(2, Y.Object.size(hash));
108
Y.Assert.areSame('baz', hash.foo);
109
Y.Assert.isTrue(hash.bar);
112
testIndexOf: function () {
113
var data = ['a', 'b', 1, 0, false, null, 'a'];
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');
121
// TODO: support fromIndex
124
'indexOf() should handle sparse arrays correctly': function () {
125
var array = ['a', 'b'];
126
array[3] = undefined;
128
Y.Assert.areSame(3, Y.Array.indexOf(array, undefined));
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));
137
testSome: function () {
138
var data = [1, 2, 3],
142
Assert.isTrue(Y.Array.some(data, function (v, index, array) {
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');
149
if (v === 2) { return 'truthy'; }
150
if (v === 3) { Y.fail('truthy value did not stop iteration'); }
151
}), 'should return true');
153
Assert.areSame(2, calls, 'callback should be called twice');
155
Assert.isFalse(Y.Array.some(data, function () {
156
Assert.areSame(obj, this, 'the `this` object should be overridable');
157
}, obj), 'should return false');
160
'some() should handle sparse arrays correctly': function () {
166
Y.Array.some(foo, function () {
170
Assert.areSame(1, calls);
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
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
195
Y.Test.Runner.add(suite);
197
}, '@VERSION@', {requires: ['test']});