array.html revision 7dc268cc10f78c1c8a5df1d94ce93680e20a6f09
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>YUI Array Tests</title>
</head>
<body class="yui-skin-sam">
<h1>Array Tests</h1>
<span>test</span>
<span>test</span>
<span>test</span>
<p><input type="button" value="Run Tests" id="btnRun" disabled="true" /></p>
<script type="text/javascript" src="/build/yui/yui.js"></script>
<script type="text/javascript">
(function() {
YUI({
base: "/build/",
logExclude: {
attribute: true,
dom: true,
node: true,
event: true,
base: true,
widget: true,
selector: true
},
useConsole: true
}).use("dump", "test", "console", "event-custom", function(Y) {
var button = Y.get('#btnRun');
// Set up the page
button.set("disabled", false);
Y.on("click", function() {
Y.Test.Runner.run();
}, button);
var myConsole = new Y.Console().render();
var testArray = new Y.Test.Case({
name: "Array tests",
'[3, 100, 1, 2] should produce [1, 2, 3, 100] when sorted': function() {
// the stock sort behavior should fail to produce desired result
Y.ArrayAssert.itemsAreEqual([1, 100, 2, 3], [3, 100, 1, 2].sort());
Y.ArrayAssert.itemsAreEqual([1, 2, 3, 100], [3, 100, 1, 2].sort(Y.Array.numericSort));
},
test_some: function() {
var a = [1, 2, 3];
Y.Array.some(a, function(v) {
if (v == 2) {
return 'truthy';
}
if (v == 3) {
Y.fail('truthy value did not stop iteration');
}
});
if (a.some) {
Y.log('Using native Array.some support.');
a.some(function(v) {
if (v == 2) {
return v;
}
if (v == 3) {
Y.fail('truthy value did not stop iteration');
}
});
} else {
Y.log('No native Array.some support.');
}
},
test_array: function() {
var els = document.getElementsByTagName('span'),
a = Y.Array(els, 1);
Y.log('els length:' + els.length);
Y.log('a length:' + a.length);
Y.log('els tagName:' + els.tagName);
Y.log('els alert:' + els.alert);
Y.log('els size:' + els.size);
Y.log('els array test:' + Y.Array.test(els));
// Y.Lang.type is broken for HTMLElementCollections in Safari. isObject
// returns false because typeof returns function
Y.log('els isObject:' + Y.Lang.isObject(els)); // false in Safari
Y.log('els type:' + Y.Lang.type(els) + '+'); // object
Y.log('els typeof:' + typeof els); // function
Y.log('els isFunction:' + Y.Lang.isFunction(els)); // false
Y.log('els.call:' + els.call);
Y.log('els.apply:' + els.apply);
Y.Assert.areEqual(2, a.length);
},
test_array_test: function() {
Y.Assert.areEqual(0, Y.Array.test(function(){}));
}
});
Y.Test.Runner.add(testArray);
Y.Test.Runner.run();
});
})();
</script>
</body>
</html>