3 test("Basic requirements", function() {
5 ok( Array.prototype.push, "Array.push()" );
6 ok( Function.prototype.apply, "Function.apply()" );
7 ok( document.getElementById, "getElementById" );
8 ok( document.getElementsByTagName, "getElementsByTagName" );
9 ok( RegExp, "RegExp" );
10 ok( jQuery, "jQuery" );
14 test("jQuery()", function() {
17 // Basic constructor's behavior
19 equals( jQuery().length, 0, "jQuery() === jQuery([])" );
20 equals( jQuery(undefined).length, 0, "jQuery(undefined) === jQuery([])" );
21 equals( jQuery(null).length, 0, "jQuery(null) === jQuery([])" );
22 equals( jQuery("").length, 0, "jQuery('') === jQuery([])" );
24 var obj = jQuery("div")
25 equals( jQuery(obj).selector, "div", "jQuery(jQueryObj) == jQueryObj" );
27 // can actually yield more than one, when iframes are included, the window is an array as well
28 equals( jQuery(window).length, 1, "Correct number of elements generated for jQuery(window)" );
31 var main = jQuery("#main");
32 same( jQuery("div p", main).get(), q("sndp", "en", "sap"), "Basic selector with jQuery object as context" );
35 // disabled since this test was doing nothing. i tried to fix it but i'm not sure
36 // what the expected behavior should even be. FF returns "\n" for the text node
37 // make sure this is handled
38 var crlfContainer = jQuery('<p>\r\n</p>');
39 var x = crlfContainer.contents().get(0).nodeValue;
40 equals( x, what???, "Check for \\r and \\n in jQuery()" );
43 /* // Disabled until we add this functionality in
46 jQuery("<div>Testing</div>").appendTo(document.getElementById("iframe").contentDocument.body);
50 ok( pass, "jQuery('<tag>') needs optional document parameter to ease cross-frame DOM wrangling, see #968" );*/
52 var code = jQuery("<code/>");
53 equals( code.length, 1, "Correct number of elements generated for code" );
54 equals( code.parent().length, 0, "Make sure that the generated HTML has no parent." );
55 var img = jQuery("<img/>");
56 equals( img.length, 1, "Correct number of elements generated for img" );
57 equals( img.parent().length, 0, "Make sure that the generated HTML has no parent." );
58 var div = jQuery("<div/><hr/><code/><b/>");
59 equals( div.length, 4, "Correct number of elements generated for div hr code b" );
60 equals( div.parent().length, 0, "Make sure that the generated HTML has no parent." );
62 equals( jQuery([1,2,3]).get(1), 2, "Test passing an array to the factory" );
64 equals( jQuery(document.body).get(0), jQuery('body').get(0), "Test passing an html node to the factory" );
68 var elem = jQuery("<div/>", {
70 css: { paddingLeft:1, paddingRight:1 },
71 click: function(){ ok(exec, "Click executed."); },
77 equals( elem[0].style.width, '10px', 'jQuery() quick setter width');
78 equals( elem[0].style.paddingLeft, '1px', 'jQuery quick setter css');
79 equals( elem[0].style.paddingRight, '1px', 'jQuery quick setter css');
80 equals( elem[0].childNodes.length, 1, 'jQuery quick setter text');
81 equals( elem[0].firstChild.nodeValue, "test", 'jQuery quick setter text');
82 equals( elem[0].className, "test2", 'jQuery() quick setter class');
83 equals( elem[0].id, "test3", 'jQuery() quick setter id');
89 test("selector state", function() {
94 test = jQuery(undefined);
95 equals( test.selector, "", "Empty jQuery Selector" );
96 equals( test.context, undefined, "Empty jQuery Context" );
98 test = jQuery(document);
99 equals( test.selector, "", "Document Selector" );
100 equals( test.context, document, "Document Context" );
102 test = jQuery(document.body);
103 equals( test.selector, "", "Body Selector" );
104 equals( test.context, document.body, "Body Context" );
106 test = jQuery("#main");
107 equals( test.selector, "#main", "#main Selector" );
108 equals( test.context, document, "#main Context" );
110 test = jQuery("#notfoundnono");
111 equals( test.selector, "#notfoundnono", "#notfoundnono Selector" );
112 equals( test.context, document, "#notfoundnono Context" );
114 test = jQuery("#main", document);
115 equals( test.selector, "#main", "#main Selector" );
116 equals( test.context, document, "#main Context" );
118 test = jQuery("#main", document.body);
119 equals( test.selector, "#main", "#main Selector" );
120 equals( test.context, document.body, "#main Context" );
124 equals( test.selector, "#main", "#main Selector" );
125 equals( test.context, document.body, "#main Context" );
127 test = jQuery(document.body).find("#main");
128 equals( test.selector, "#main", "#main find Selector" );
129 equals( test.context, document.body, "#main find Context" );
131 test = jQuery("#main").filter("div");
132 equals( test.selector, "#main.filter(div)", "#main filter Selector" );
133 equals( test.context, document, "#main filter Context" );
135 test = jQuery("#main").not("div");
136 equals( test.selector, "#main.not(div)", "#main not Selector" );
137 equals( test.context, document, "#main not Context" );
139 test = jQuery("#main").filter("div").not("div");
140 equals( test.selector, "#main.filter(div).not(div)", "#main filter, not Selector" );
141 equals( test.context, document, "#main filter, not Context" );
143 test = jQuery("#main").filter("div").not("div").end();
144 equals( test.selector, "#main.filter(div)", "#main filter, not, end Selector" );
145 equals( test.context, document, "#main filter, not, end Context" );
147 test = jQuery("#main").parent("body");
148 equals( test.selector, "#main.parent(body)", "#main parent Selector" );
149 equals( test.context, document, "#main parent Context" );
151 test = jQuery("#main").eq(0);
152 equals( test.selector, "#main.slice(0,1)", "#main eq Selector" );
153 equals( test.context, document, "#main eq Context" );
157 jQuery(d).appendTo(jQuery(d)).selector,
158 jQuery(d).appendTo(d).selector,
159 "manipulation methods make same selector for jQuery objects"
164 test("browser", function() {
167 jQuery.get("data/ua.txt", function(data){
168 var uas = data.split("\n");
169 expect( (uas.length - 1) * 2 );
171 jQuery.each(uas, function(){
172 var parts = this.split("\t");
174 var ua = jQuery.uaMatch( parts[2] );
175 equals( ua.browser, parts[0], "Checking browser for " + parts[2] );
176 equals( ua.version, parts[1], "Checking version string for " + parts[2] );
185 test("noConflict", function() {
190 equals( jQuery, jQuery.noConflict(), "noConflict returned the jQuery object" );
191 equals( jQuery, $$, "Make sure jQuery wasn't touched." );
192 equals( $, original$, "Make sure $ was reverted." );
196 equals( jQuery.noConflict(true), $$, "noConflict returned the jQuery object" );
197 equals( jQuery, originaljQuery, "Make sure jQuery was reverted." );
198 equals( $, original$, "Make sure $ was reverted." );
203 test("trim", function() {
206 var nbsp = String.fromCharCode(160);
208 equals( jQuery.trim("hello "), "hello", "trailing space" );
209 equals( jQuery.trim(" hello"), "hello", "leading space" );
210 equals( jQuery.trim(" hello "), "hello", "space on both sides" );
211 equals( jQuery.trim(" " + nbsp + "hello " + nbsp + " "), "hello", " " );
213 equals( jQuery.trim(), "", "Nothing in." );
214 equals( jQuery.trim( undefined ), "", "Undefined" );
215 equals( jQuery.trim( null ), "", "Null" );
216 equals( jQuery.trim( 5 ), "5", "Number" );
217 equals( jQuery.trim( false ), "false", "Boolean" );
220 test("isPlainObject", function() {
225 // The use case that we want to match
226 ok(jQuery.isPlainObject({}), "{}");
228 // Not objects shouldn't be matched
229 ok(!jQuery.isPlainObject(""), "string");
230 ok(!jQuery.isPlainObject(0) && !jQuery.isPlainObject(1), "number");
231 ok(!jQuery.isPlainObject(true) && !jQuery.isPlainObject(false), "boolean");
232 ok(!jQuery.isPlainObject(null), "null");
233 ok(!jQuery.isPlainObject(undefined), "undefined");
235 // Arrays shouldn't be matched
236 ok(!jQuery.isPlainObject([]), "array");
238 // Instantiated objects shouldn't be matched
239 ok(!jQuery.isPlainObject(new Date), "new Date");
241 var fn = function(){};
243 // Functions shouldn't be matched
244 ok(!jQuery.isPlainObject(fn), "fn");
246 // Again, instantiated objects shouldn't be matched
247 ok(!jQuery.isPlainObject(new fn), "new fn (no methods)");
249 // Makes the function a little more realistic
250 // (and harder to detect, incidentally)
251 fn.prototype = {someMethod: function(){}};
253 // Again, instantiated objects shouldn't be matched
254 ok(!jQuery.isPlainObject(new fn), "new fn");
257 ok(!jQuery.isPlainObject(document.createElement("div")), "DOM Element");
260 ok(!jQuery.isPlainObject(window), "window");
262 var iframe = document.createElement("iframe");
263 document.body.appendChild(iframe);
265 window.iframeDone = function(otherObject){
266 // Objects from other windows should be matched
267 ok(jQuery.isPlainObject(new otherObject), "new otherObject");
268 document.body.removeChild( iframe );
272 var doc = iframe.contentDocument || iframe.contentWindow.document;
274 doc.write("<body onload='window.parent.iframeDone(Object);'>");
278 test("isFunction", function() {
281 // Make sure that false values return false
282 ok( !jQuery.isFunction(), "No Value" );
283 ok( !jQuery.isFunction( null ), "null Value" );
284 ok( !jQuery.isFunction( undefined ), "undefined Value" );
285 ok( !jQuery.isFunction( "" ), "Empty String Value" );
286 ok( !jQuery.isFunction( 0 ), "0 Value" );
289 // Safari uses "(Internal Function)"
290 ok( jQuery.isFunction(String), "String Function("+String+")" );
291 ok( jQuery.isFunction(Array), "Array Function("+Array+")" );
292 ok( jQuery.isFunction(Object), "Object Function("+Object+")" );
293 ok( jQuery.isFunction(Function), "Function Function("+Function+")" );
295 // When stringified, this could be misinterpreted
296 var mystr = "function";
297 ok( !jQuery.isFunction(mystr), "Function String" );
299 // When stringified, this could be misinterpreted
300 var myarr = [ "function" ];
301 ok( !jQuery.isFunction(myarr), "Function Array" );
303 // When stringified, this could be misinterpreted
304 var myfunction = { "function": "test" };
305 ok( !jQuery.isFunction(myfunction), "Function Object" );
307 // Make sure normal functions still work
308 var fn = function(){};
309 ok( jQuery.isFunction(fn), "Normal Function" );
311 var obj = document.createElement("object");
313 // Firefox says this is a function
314 ok( !jQuery.isFunction(obj), "Object Element" );
316 // IE says this is an object
317 // Since 1.3, this isn't supported (#2968)
318 //ok( jQuery.isFunction(obj.getAttribute), "getAttribute Function" );
320 var nodes = document.body.childNodes;
322 // Safari says this is a function
323 ok( !jQuery.isFunction(nodes), "childNodes Property" );
325 var first = document.body.firstChild;
327 // Normal elements are reported ok everywhere
328 ok( !jQuery.isFunction(first), "A normal DOM Element" );
330 var input = document.createElement("input");
332 document.body.appendChild( input );
334 // IE says this is an object
335 // Since 1.3, this isn't supported (#2968)
336 //ok( jQuery.isFunction(input.focus), "A default function property" );
338 document.body.removeChild( input );
340 var a = document.createElement("a");
341 a.href = "some-function";
342 document.body.appendChild( a );
344 // This serializes with the word 'function' in it
345 ok( !jQuery.isFunction(a), "Anchor Element" );
347 document.body.removeChild( a );
349 // Recursive function calls have lengths and array-like properties
350 function callme(callback){
351 function fn(response){
355 ok( jQuery.isFunction(fn), "Recursive Function Call" );
357 fn({ some: "data" });
361 callme(function(){});
365 test("isXMLDoc - HTML", function() {
368 ok( !jQuery.isXMLDoc( document ), "HTML document" );
369 ok( !jQuery.isXMLDoc( document.documentElement ), "HTML documentElement" );
370 ok( !jQuery.isXMLDoc( document.body ), "HTML Body Element" );
372 var iframe = document.createElement("iframe");
373 document.body.appendChild( iframe );
376 var body = jQuery(iframe).contents()[0];
377 ok( !jQuery.isXMLDoc( body ), "Iframe body element" );
379 ok( false, "Iframe body element exception" );
382 document.body.removeChild( iframe );
386 test("isXMLDoc - XML", function() {
389 jQuery.get('data/dashboard.xml', function(xml) {
390 ok( jQuery.isXMLDoc( xml ), "XML document" );
391 ok( jQuery.isXMLDoc( xml.documentElement ), "XML documentElement" );
392 ok( jQuery.isXMLDoc( jQuery("tab", xml)[0] ), "XML Tab Element" );
398 test("jQuery('html')", function() {
403 var s = jQuery("<script>jQuery.foo='test';</script>")[0];
404 ok( s, "Creating a script" );
405 ok( !jQuery.foo, "Make sure the script wasn't executed prematurely" );
406 jQuery("body").append("<script>jQuery.foo='test';</script>");
407 ok( jQuery.foo, "Executing a scripts contents in the right context" );
409 // Test multi-line HTML
410 var div = jQuery("<div>\r\nsome text\n<p>some p</p>\nmore text\r\n</div>")[0];
411 equals( div.nodeName.toUpperCase(), "DIV", "Make sure we're getting a div." );
412 equals( div.firstChild.nodeType, 3, "Text node." );
413 equals( div.lastChild.nodeType, 3, "Text node." );
414 equals( div.childNodes[1].nodeType, 1, "Paragraph." );
415 equals( div.childNodes[1].firstChild.nodeType, 3, "Paragraph text." );
418 ok( jQuery("<link rel='stylesheet'/>")[0], "Creating a link" );
420 ok( !jQuery("<script/>")[0].parentNode, "Create a script" );
422 ok( jQuery("<input/>").attr("type", "hidden"), "Create an input and set the type." );
424 var j = jQuery("<span>hi</span> there <!-- mon ami -->");
425 ok( j.length >= 2, "Check node,textnode,comment creation (some browsers delete comments)" );
427 ok( !jQuery("<option>test</option>")[0].selected, "Make sure that options are auto-selected #2050" );
429 ok( jQuery("<div></div>")[0], "Create a div with closing tag." );
430 ok( jQuery("<table></table>")[0], "Create a table with closing tag." );
433 test("jQuery('html', context)", function() {
436 var $div = jQuery("<div/>")[0];
437 var $span = jQuery("<span/>", $div);
438 equals($span.length, 1, "Verify a span created with a div context works, #1763");
442 test("jQuery(selector, xml).text(str) - Loaded via XML document", function() {
445 jQuery.get('data/dashboard.xml', function(xml) {
446 // tests for #1419 where IE was a problem
447 var tab = jQuery("tab", xml).eq(0);
448 equals( tab.text(), "blabla", "Verify initial text correct" );
450 equals( tab.text(), "newtext", "Verify new text correct" );
456 test("end()", function() {
458 equals( 'Yahoo', jQuery('#yahoo').parent().end().text(), 'Check for end' );
459 ok( jQuery('#yahoo').end(), 'Check for end with nothing to end' );
461 var x = jQuery('#yahoo');
463 equals( 'Yahoo', jQuery('#yahoo').text(), 'Check for non-destructive behaviour' );
466 test("length", function() {
468 equals( jQuery("p").length, 6, "Get Number of Elements Found" );
471 test("size()", function() {
473 equals( jQuery("p").size(), 6, "Get Number of Elements Found" );
476 test("get()", function() {
478 same( jQuery("p").get(), q("firstp","ap","sndp","en","sap","first"), "Get All Elements" );
481 test("toArray()", function() {
483 same( jQuery("p").toArray(),
484 q("firstp","ap","sndp","en","sap","first"),
485 "Convert jQuery object to an Array" )
488 test("get(Number)", function() {
490 equals( jQuery("p").get(0), document.getElementById("firstp"), "Get A Single Element" );
493 test("get(-Number)",function() {
495 equals( jQuery("p").get(-1),
496 document.getElementById("first"),
497 "Get a single element with negative index" )
500 test("each(Function)", function() {
502 var div = jQuery("div");
503 div.each(function(){this.foo = 'zoo';});
505 for ( var i = 0; i < div.size(); i++ ) {
506 if ( div.get(i).foo != "zoo" ) pass = false;
508 ok( pass, "Execute a function, Relative" );
511 test("slice()", function() {
514 var $links = jQuery("#ap a");
516 same( $links.slice(1,2).get(), q("groups"), "slice(1,2)" );
517 same( $links.slice(1).get(), q("groups", "anchor1", "mark"), "slice(1)" );
518 same( $links.slice(0,3).get(), q("google", "groups", "anchor1"), "slice(0,3)" );
519 same( $links.slice(-1).get(), q("mark"), "slice(-1)" );
521 same( $links.eq(1).get(), q("groups"), "eq(1)" );
522 same( $links.eq('2').get(), q("anchor1"), "eq('2')" );
523 same( $links.eq(-1).get(), q("mark"), "eq(-1)" );
526 test("first()/last()", function() {
529 var $links = jQuery("#ap a"), $none = jQuery("asdf");
531 same( $links.first().get(), q("google"), "first()" );
532 same( $links.last().get(), q("mark"), "last()" );
534 same( $none.first().get(), [], "first() none" );
535 same( $none.last().get(), [], "last() none" );
538 test("map()", function() {
539 expect(2);//expect(6);
542 jQuery("#ap").map(function(){
543 return jQuery(this).find("a").get();
545 q("google", "groups", "anchor1", "mark"),
550 jQuery("#ap > a").map(function(){
551 return this.parentNode;
557 return;//these haven't been accepted yet
560 var keys = jQuery.map( {a:1,b:2}, function( v, k ){
564 equals( keys.join(""), "ab", "Map the keys from a hash to an array" );
566 var values = jQuery.map( {a:1,b:2}, function( v, k ){
570 equals( values.join(""), "12", "Map the values from a hash to an array" );
572 var scripts = document.getElementsByTagName("script");
573 var mapped = jQuery.map( scripts, function( v, k ){
577 equals( mapped.length, scripts.length, "Map an array(-like) to a hash" );
579 var flat = jQuery.map( Array(4), function( v, k ){
580 return k % 2 ? k : [k,k,k];//try mixing array and regular returns
583 equals( flat.join(""), "00012223", "try the new flatten technique(#2616)" );
586 test("jQuery.merge()", function() {
589 var parse = jQuery.merge;
591 same( parse([],[]), [], "Empty arrays" );
593 same( parse([1],[2]), [1,2], "Basic" );
594 same( parse([1,2],[3,4]), [1,2,3,4], "Basic" );
596 same( parse([1,2],[]), [1,2], "Second empty" );
597 same( parse([],[1,2]), [1,2], "First empty" );
599 // Fixed at [5998], #3641
600 same( parse([-2,-1], [0,1,2]), [-2,-1,0,1,2], "Second array including a zero (falsy)");
602 // After fixing #5527
603 same( parse([], [null, undefined]), [null, undefined], "Second array including null and undefined values");
604 same( parse({length:0}, [1,2]), {length:2, 0:1, 1:2}, "First array like");
607 test("jQuery.extend(Object, Object)", function() {
610 var settings = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
611 options = { xnumber2: 1, xstring2: "x", xxx: "newstring" },
612 optionsCopy = { xnumber2: 1, xstring2: "x", xxx: "newstring" },
613 merged = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "x", xxx: "newstring" },
614 deep1 = { foo: { bar: true } },
615 deep1copy = { foo: { bar: true } },
616 deep2 = { foo: { baz: true }, foo2: document },
617 deep2copy = { foo: { baz: true }, foo2: document },
618 deepmerged = { foo: { bar: true, baz: true }, foo2: document },
620 nestedarray = { arr: arr };
622 jQuery.extend(settings, options);
623 same( settings, merged, "Check if extended: settings must be extended" );
624 same( options, optionsCopy, "Check if not modified: options must not be modified" );
626 jQuery.extend(settings, null, options);
627 same( settings, merged, "Check if extended: settings must be extended" );
628 same( options, optionsCopy, "Check if not modified: options must not be modified" );
630 jQuery.extend(true, deep1, deep2);
631 same( deep1.foo, deepmerged.foo, "Check if foo: settings must be extended" );
632 same( deep2.foo, deep2copy.foo, "Check if not deep2: options must not be modified" );
633 equals( deep1.foo2, document, "Make sure that a deep clone was not attempted on the document" );
635 ok( jQuery.extend(true, [], arr) !== arr, "Deep extend of array must clone array" );
636 ok( jQuery.extend(true, {}, nestedarray).arr !== arr, "Deep extend of object must clone child array" );
639 var optionsWithLength = { foo: { length: -1 } };
640 jQuery.extend(true, empty, optionsWithLength);
641 same( empty.foo, optionsWithLength.foo, "The length property must copy correctly" );
644 var optionsWithDate = { foo: { date: new Date } };
645 jQuery.extend(true, empty, optionsWithDate);
646 same( empty.foo, optionsWithDate.foo, "Dates copy correctly" );
648 var myKlass = function() {};
649 var customObject = new myKlass();
650 var optionsWithCustomObject = { foo: { date: customObject } };
652 jQuery.extend(true, empty, optionsWithCustomObject);
653 ok( empty.foo && empty.foo.date === customObject, "Custom objects copy correctly (no methods)" );
655 // Makes the class a little more realistic
656 myKlass.prototype = { someMethod: function(){} };
658 jQuery.extend(true, empty, optionsWithCustomObject);
659 ok( empty.foo && empty.foo.date === customObject, "Custom objects copy correctly" );
661 var ret = jQuery.extend(true, { foo: 4 }, { foo: new Number(5) } );
662 ok( ret.foo == 5, "Wrapped numbers copy correctly" );
665 nullUndef = jQuery.extend({}, options, { xnumber2: null });
666 ok( nullUndef.xnumber2 === null, "Check to make sure null values are copied");
668 nullUndef = jQuery.extend({}, options, { xnumber2: undefined });
669 ok( nullUndef.xnumber2 === options.xnumber2, "Check to make sure undefined values are not copied");
671 nullUndef = jQuery.extend({}, options, { xnumber0: null });
672 ok( nullUndef.xnumber0 === null, "Check to make sure null values are inserted");
675 var recursive = { foo:target, bar:5 };
676 jQuery.extend(true, target, recursive);
677 same( target, { bar:5 }, "Check to make sure a recursive obj doesn't go never-ending loop by not copying it over" );
679 var ret = jQuery.extend(true, { foo: [] }, { foo: [0] } ); // 1907
680 equals( ret.foo.length, 1, "Check to make sure a value with coersion 'false' copies over when necessary to fix #1907" );
682 var ret = jQuery.extend(true, { foo: "1,2,3" }, { foo: [1, 2, 3] } );
683 ok( typeof ret.foo != "string", "Check to make sure values equal with coersion (but not actually equal) overwrite correctly" );
685 var ret = jQuery.extend(true, { foo:"bar" }, { foo:null } );
686 ok( typeof ret.foo !== 'undefined', "Make sure a null value doesn't crash with deep extend, for #1908" );
688 var obj = { foo:null };
689 jQuery.extend(true, obj, { foo:"notnull" } );
690 equals( obj.foo, "notnull", "Make sure a null value can be overwritten" );
693 jQuery.extend(func, { key: "value" } );
694 equals( func.key, "value", "Verify a function can be extended" );
696 var defaults = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
697 defaultsCopy = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
698 options1 = { xnumber2: 1, xstring2: "x" },
699 options1Copy = { xnumber2: 1, xstring2: "x" },
700 options2 = { xstring2: "xx", xxx: "newstringx" },
701 options2Copy = { xstring2: "xx", xxx: "newstringx" },
702 merged2 = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "xx", xxx: "newstringx" };
704 var settings = jQuery.extend({}, defaults, options1, options2);
705 same( settings, merged2, "Check if extended: settings must be extended" );
706 same( defaults, defaultsCopy, "Check if not modified: options1 must not be modified" );
707 same( options1, options1Copy, "Check if not modified: options1 must not be modified" );
708 same( options2, options2Copy, "Check if not modified: options2 must not be modified" );
711 test("jQuery.each(Object,Function)", function() {
713 jQuery.each( [0,1,2], function(i, n){
714 equals( i, n, "Check array iteration" );
717 jQuery.each( [5,6,7], function(i, n){
718 equals( i, n - 5, "Check array iteration" );
721 jQuery.each( { name: "name", lang: "lang" }, function(i, n){
722 equals( i, n, "Check object iteration" );
726 jQuery.each([1,2,3], function(i,v){ total += v; });
727 equals( total, 6, "Looping over an array" );
729 jQuery.each([1,2,3], function(i,v){ total += v; if ( i == 1 ) return false; });
730 equals( total, 3, "Looping over an array, with break" );
732 jQuery.each({"a":1,"b":2,"c":3}, function(i,v){ total += v; });
733 equals( total, 6, "Looping over an object" );
735 jQuery.each({"a":3,"b":3,"c":3}, function(i,v){ total += v; return false; });
736 equals( total, 3, "Looping over an object, with break" );
738 var f = function(){};
740 jQuery.each(f, function(i){
743 equals( "baz", f.foo, "Loop over a function" );
746 test("jQuery.makeArray", function(){
749 equals( jQuery.makeArray(jQuery('html>*'))[0].nodeName.toUpperCase(), "HEAD", "Pass makeArray a jQuery object" );
751 equals( jQuery.makeArray(document.getElementsByName("PWD")).slice(0,1)[0].name, "PWD", "Pass makeArray a nodelist" );
753 equals( (function(){ return jQuery.makeArray(arguments); })(1,2).join(""), "12", "Pass makeArray an arguments array" );
755 equals( jQuery.makeArray([1,2,3]).join(""), "123", "Pass makeArray a real array" );
757 equals( jQuery.makeArray().length, 0, "Pass nothing to makeArray and expect an empty array" );
759 equals( jQuery.makeArray( 0 )[0], 0 , "Pass makeArray a number" );
761 equals( jQuery.makeArray( "foo" )[0], "foo", "Pass makeArray a string" );
763 equals( jQuery.makeArray( true )[0].constructor, Boolean, "Pass makeArray a boolean" );
765 equals( jQuery.makeArray( document.createElement("div") )[0].nodeName.toUpperCase(), "DIV", "Pass makeArray a single node" );
767 equals( jQuery.makeArray( {length:2, 0:"a", 1:"b"} ).join(""), "ab", "Pass makeArray an array like map (with length)" );
769 ok( !!jQuery.makeArray( document.documentElement.childNodes ).slice(0,1)[0].nodeName, "Pass makeArray a childNodes array" );
771 // function, is tricky as it has length
772 equals( jQuery.makeArray( function(){ return 1;} )[0](), 1, "Pass makeArray a function" );
774 //window, also has length
775 equals( jQuery.makeArray(window)[0], window, "Pass makeArray the window" );
777 equals( jQuery.makeArray(/a/)[0].constructor, RegExp, "Pass makeArray a regex" );
779 ok( jQuery.makeArray(document.getElementById('form')).length >= 13, "Pass makeArray a form (treat as elements)" );
782 same( jQuery.makeArray({'length': '0'}), [], "Make sure object is coerced properly.");
783 same( jQuery.makeArray({'length': '5'}), [], "Make sure object is coerced properly.");
786 test("jQuery.isEmptyObject", function(){
789 equals(true, jQuery.isEmptyObject({}), "isEmptyObject on empty object literal" );
790 equals(false, jQuery.isEmptyObject({a:1}), "isEmptyObject on non-empty object literal" );
793 // equals(true, jQuery.isEmptyObject(null), "isEmptyObject on null" );
796 test("jQuery.proxy", function(){
799 var test = function(){ equals( this, thisObject, "Make sure that scope is set properly." ); };
800 var thisObject = { foo: "bar", method: test };
802 // Make sure normal works
803 test.call( thisObject );
806 jQuery.proxy( test, thisObject )();
808 // Make sure it doesn't freak out
809 equals( jQuery.proxy( null, thisObject ), undefined, "Make sure no function was returned." );
811 // Use the string shortcut
812 jQuery.proxy( thisObject, "method" )();
815 test("jQuery.parseJSON", function(){
818 equals( jQuery.parseJSON(), null, "Nothing in, null out." );
819 equals( jQuery.parseJSON( null ), null, "Nothing in, null out." );
820 equals( jQuery.parseJSON( "" ), null, "Nothing in, null out." );
822 same( jQuery.parseJSON("{}"), {}, "Plain object parsing." );
823 same( jQuery.parseJSON('{"test":1}'), {"test":1}, "Plain object parsing." );
825 same( jQuery.parseJSON('\n{"test":1}'), {"test":1}, "Make sure leading whitespaces are handled." );
828 jQuery.parseJSON("{a:1}");
829 ok( false, "Test malformed JSON string." );
831 ok( true, "Test malformed JSON string." );
835 jQuery.parseJSON("{'a':1}");
836 ok( false, "Test malformed JSON string." );
838 ok( true, "Test malformed JSON string." );