array.html revision 43692b3f865f1b1c970aedb2b96392482bd1fe3e
0N/A<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
2362N/A<html>
0N/A<head>
0N/A<title>YUI Array Tests</title>
0N/A</head>
0N/A
2362N/A<body class="yui-skin-sam">
0N/A<h1>Array Tests</h1>
2362N/A<span>test</span>
0N/A<span>test</span>
0N/A<span>test</span>
0N/A<p><input type="button" value="Run Tests" id="btnRun" disabled="true" /></p>
0N/A
0N/A<script type="text/javascript" src="/build/yui/yui.js"></script>
0N/A<script type="text/javascript">
0N/A
0N/A(function() {
0N/A
0N/A YUI({
0N/A base: "/build/",
2362N/A logExclude: {
2362N/A attribute: true,
2362N/A dom: true,
0N/A node: true,
0N/A event: true,
0N/A base: true,
0N/A widget: true,
0N/A selector: true
0N/A },
0N/A useConsole: true
0N/A }).use("dump", "test", "console", "event-custom", function(Y) {
0N/A
0N/A var button = Y.get('#btnRun');
0N/A
0N/A // Set up the page
0N/A button.set("disabled", false);
0N/A Y.on("click", function() {
0N/A Y.Test.Runner.run();
0N/A }, button);
0N/A
0N/A var myConsole = new Y.Console().render();
0N/A
0N/A var testArray = new Y.Test.Case({
0N/A name: "Array tests",
0N/A
0N/A '[3, 100, 1, 2] should produce [1, 2, 3, 100] when sorted': function() {
0N/A
0N/A // the stock sort behavior should fail to produce desired result
0N/A Y.ArrayAssert.itemsAreEqual([1, 100, 2, 3], [3, 100, 1, 2].sort());
0N/A
0N/A Y.ArrayAssert.itemsAreEqual([1, 2, 3, 100], [3, 100, 1, 2].sort(Y.Array.numericSort));
0N/A
0N/A },
0N/A
0N/A test_some: function() {
0N/A var a = [1, 2, 3];
0N/A
0N/A Y.Array.some(a, function(v) {
0N/A if (v == 2) {
0N/A return 'truthy';
0N/A }
0N/A
0N/A if (v == 3) {
0N/A Y.fail('truthy value did not stop iteration');
0N/A }
0N/A });
0N/A
0N/A if (a.some) {
0N/A Y.log('Using native Array.some support.');
0N/A a.some(function(v) {
0N/A if (v == 2) {
0N/A return v;
0N/A }
0N/A
0N/A if (v == 3) {
0N/A Y.fail('truthy value did not stop iteration');
0N/A }
0N/A });
0N/A } else {
0N/A Y.log('No native Array.some support.');
0N/A }
0N/A },
0N/A
0N/A test_array: function() {
0N/A
0N/A var els = document.getElementsByTagName('span'),
0N/A a = Y.Array(els, 1),
0N/A nodes = Y.all('span'),
0N/A b = Y.Array(nodes, 1);
0N/A
0N/A Y.log('els length:' + els.length);
0N/A Y.log('a length:' + a.length);
0N/A Y.log('els tagName:' + els.tagName);
0N/A Y.log('els alert:' + els.alert);
0N/A Y.log('els size:' + els.size);
0N/A Y.log('els array test:' + Y.Array.test(els));
0N/A
0N/A // Y.Lang.type is broken for HTMLElementCollections in Safari. isObject
0N/A // returns false because typeof returns function
0N/A
0N/A Y.log('els isObject:' + Y.Lang.isObject(els)); // false in Safari
0N/A Y.log('els type:' + Y.Lang.type(els) + '+'); // object
0N/A Y.log('els typeof:' + typeof els); // function
0N/A Y.log('els isFunction:' + Y.Lang.isFunction(els)); // false
0N/A Y.log('els.call:' + els.call);
0N/A Y.log('els.apply:' + els.apply);
0N/A
0N/A Y.Assert.areEqual(2, a.length);
0N/A
0N/A // @TODO NodeList will be wrapped rather than have the items coerced into an
0N/A // array. If Array.test is adapted to handle NodeLists this could be made to
0N/A // work.
0N/A // Y.Assert.areEqual(2, b.length);
0N/A
0N/A },
0N/A
0N/A test_array_test: function(arg1, arg2) {
0N/A
0N/A Y.Assert.areEqual(0, Y.Array.test(function(){})); // functions should fail
0N/A Y.Assert.areEqual(0, Y.Array.test(Y.one('#btnRun'))); // single nodes should fail
0N/A Y.Assert.areEqual(0, Y.Array.test('string'));
0N/A Y.Assert.areEqual(0, Y.Array.test(12345));
0N/A Y.Assert.areEqual(0, Y.Array.test(null));
0N/A Y.Assert.areEqual(0, Y.Array.test(undefined));
0N/A Y.Assert.areEqual(0, Y.Array.test(/regexp/));
0N/A Y.Assert.areEqual(0, Y.Array.test(new Date()));
0N/A Y.Assert.areEqual(1, Y.Array.test([1, 2]));
0N/A Y.Assert.areEqual(1, Y.Array.test([]));
0N/A // Y.Assert.areEqual(1, Y.Array.test('string'.toCharArray()));
0N/A Y.Assert.areEqual(2, Y.Array.test(arguments), 'arguments should be arraylike'); // arguments collection
0N/A Y.Assert.areEqual(2, Y.Array.test(document.getElementsByTagName('span')), 'htmlelement collections should be arraylike'); // HTMLElementsCollection
0N/A
0N/A // @TODO figure out what to do with this. A NodeList does not contain a collection of Nodes; it
0N/A // contains a collection of HTMLElements.
0N/A // Y.Assert.areEqual(3, Y.Array.test(Y.all('#btnRun')), 'nodelists should be specifically identified as a special collection'); // NodeList
0N/A Y.Assert.areEqual(0, Y.Array.test(Y.all('span')), 'nodelists are not currently considered arraylike'); // NodeList
0N/A }
0N/A
0N/A });
0N/A
0N/A
0N/A Y.Test.Runner.add(testArray);
0N/A Y.Test.Runner.run();
0N/A });
0N/A})();
0N/A</script>
0N/A</body>
0N/A</html>
0N/A