Add some tests for jQuery.isWindow and make sure that we're operating against an...
[jquery.git] / test / unit / core.js
1 module("core");
2
3 test("Basic requirements", function() {
4         expect(7);
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" );
11         ok( $, "$" );
12 });
13
14 test("jQuery()", function() {
15         expect(23);
16
17         // Basic constructor's behavior
18
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([])" );
23
24         var obj = jQuery("div")
25         equals( jQuery(obj).selector, "div", "jQuery(jQueryObj) == jQueryObj" );
26
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)" );
29
30
31         var main = jQuery("#main");
32         same( jQuery("div p", main).get(), q("sndp", "en", "sap"), "Basic selector with jQuery object as context" );
33
34 /*
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()" );
41 */
42
43         /* // Disabled until we add this functionality in
44         var pass = true;
45         try {
46                 jQuery("<div>Testing</div>").appendTo(document.getElementById("iframe").contentDocument.body);
47         } catch(e){
48                 pass = false;
49         }
50         ok( pass, "jQuery('&lt;tag&gt;') needs optional document parameter to ease cross-frame DOM wrangling, see #968" );*/
51
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." );
61
62         equals( jQuery([1,2,3]).get(1), 2, "Test passing an array to the factory" );
63
64         equals( jQuery(document.body).get(0), jQuery('body').get(0), "Test passing an html node to the factory" );
65
66         var exec = false;
67
68         var elem = jQuery("<div/>", {
69                 width: 10,
70                 css: { paddingLeft:1, paddingRight:1 },
71                 click: function(){ ok(exec, "Click executed."); },
72                 text: "test",
73                 "class": "test2",
74                 id: "test3"
75         });
76
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');
84
85         exec = true;
86         elem.click();
87 });
88
89 test("selector state", function() {
90         expect(31);
91
92         var test;
93
94         test = jQuery(undefined);
95         equals( test.selector, "", "Empty jQuery Selector" );
96         equals( test.context, undefined, "Empty jQuery Context" );
97
98         test = jQuery(document);
99         equals( test.selector, "", "Document Selector" );
100         equals( test.context, document, "Document Context" );
101
102         test = jQuery(document.body);
103         equals( test.selector, "", "Body Selector" );
104         equals( test.context, document.body, "Body Context" );
105
106         test = jQuery("#main");
107         equals( test.selector, "#main", "#main Selector" );
108         equals( test.context, document, "#main Context" );
109
110         test = jQuery("#notfoundnono");
111         equals( test.selector, "#notfoundnono", "#notfoundnono Selector" );
112         equals( test.context, document, "#notfoundnono Context" );
113
114         test = jQuery("#main", document);
115         equals( test.selector, "#main", "#main Selector" );
116         equals( test.context, document, "#main Context" );
117
118         test = jQuery("#main", document.body);
119         equals( test.selector, "#main", "#main Selector" );
120         equals( test.context, document.body, "#main Context" );
121
122         // Test cloning
123         test = jQuery(test);
124         equals( test.selector, "#main", "#main Selector" );
125         equals( test.context, document.body, "#main Context" );
126
127         test = jQuery(document.body).find("#main");
128         equals( test.selector, "#main", "#main find Selector" );
129         equals( test.context, document.body, "#main find Context" );
130
131         test = jQuery("#main").filter("div");
132         equals( test.selector, "#main.filter(div)", "#main filter Selector" );
133         equals( test.context, document, "#main filter Context" );
134
135         test = jQuery("#main").not("div");
136         equals( test.selector, "#main.not(div)", "#main not Selector" );
137         equals( test.context, document, "#main not Context" );
138
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" );
142
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" );
146
147         test = jQuery("#main").parent("body");
148         equals( test.selector, "#main.parent(body)", "#main parent Selector" );
149         equals( test.context, document, "#main parent Context" );
150
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" );
154         
155         var d = "<div />";
156         equals(
157                 jQuery(d).appendTo(jQuery(d)).selector,
158                 jQuery(d).appendTo(d).selector,
159                 "manipulation methods make same selector for jQuery objects"
160         );
161 });
162
163 if ( !isLocal ) {
164 test("browser", function() {
165         stop();
166
167         jQuery.get("data/ua.txt", function(data){
168                 var uas = data.split("\n");
169                 expect( (uas.length - 1) * 2 );
170
171                 jQuery.each(uas, function(){
172                         var parts = this.split("\t");
173                         if ( parts[2] ) {
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] );
177                         }
178                 });
179
180                 start();
181         });
182 });
183 }
184
185 test("noConflict", function() {
186         expect(7);
187
188         var $$ = jQuery;
189
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." );
193
194         jQuery = $ = $$;
195
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." );
199         ok( $$("#main").html("test"), "Make sure that jQuery still works." );
200
201         jQuery = $$;
202 });
203
204 test("trim", function() {
205         expect(9);
206
207         var nbsp = String.fromCharCode(160);
208
209         equals( jQuery.trim("hello  "), "hello", "trailing space" );
210         equals( jQuery.trim("  hello"), "hello", "leading space" );
211         equals( jQuery.trim("  hello   "), "hello", "space on both sides" );
212         equals( jQuery.trim("  " + nbsp + "hello  " + nbsp + " "), "hello", "&nbsp;" );
213
214         equals( jQuery.trim(), "", "Nothing in." );
215         equals( jQuery.trim( undefined ), "", "Undefined" );
216         equals( jQuery.trim( null ), "", "Null" );
217         equals( jQuery.trim( 5 ), "5", "Number" );
218         equals( jQuery.trim( false ), "false", "Boolean" );
219 });
220
221 test("type", function() {
222         expect(18);
223
224         equals( jQuery.type(null), "null", "null" );
225         equals( jQuery.type(undefined), "undefined", "undefined" );
226         equals( jQuery.type(true), "boolean", "Boolean" );
227         equals( jQuery.type(false), "boolean", "Boolean" );
228         equals( jQuery.type(Boolean(true)), "boolean", "Boolean" );
229         equals( jQuery.type(0), "number", "Number" );
230         equals( jQuery.type(1), "number", "Number" );
231         equals( jQuery.type(Number(1)), "number", "Number" );
232         equals( jQuery.type(""), "string", "String" );
233         equals( jQuery.type("a"), "string", "String" );
234         equals( jQuery.type(String("a")), "string", "String" );
235         equals( jQuery.type({}), "object", "Object" );
236         equals( jQuery.type(/foo/), "regexp", "RegExp" );
237         equals( jQuery.type(new RegExp("asdf")), "regexp", "RegExp" );
238         equals( jQuery.type([1]), "array", "Array" );
239         equals( jQuery.type(new Date()), "date", "Date" );
240         equals( jQuery.type(new Function("return;")), "function", "Function" );
241         equals( jQuery.type(function(){}), "function", "Function" );
242 });
243
244 test("isPlainObject", function() {
245         expect(14);
246
247         stop();
248
249         // The use case that we want to match
250         ok(jQuery.isPlainObject({}), "{}");
251         
252         // Not objects shouldn't be matched
253         ok(!jQuery.isPlainObject(""), "string");
254         ok(!jQuery.isPlainObject(0) && !jQuery.isPlainObject(1), "number");
255         ok(!jQuery.isPlainObject(true) && !jQuery.isPlainObject(false), "boolean");
256         ok(!jQuery.isPlainObject(null), "null");
257         ok(!jQuery.isPlainObject(undefined), "undefined");
258         
259         // Arrays shouldn't be matched
260         ok(!jQuery.isPlainObject([]), "array");
261  
262         // Instantiated objects shouldn't be matched
263         ok(!jQuery.isPlainObject(new Date), "new Date");
264  
265         var fn = function(){};
266  
267         // Functions shouldn't be matched
268         ok(!jQuery.isPlainObject(fn), "fn");
269  
270         // Again, instantiated objects shouldn't be matched
271         ok(!jQuery.isPlainObject(new fn), "new fn (no methods)");
272  
273         // Makes the function a little more realistic
274         // (and harder to detect, incidentally)
275         fn.prototype = {someMethod: function(){}};
276  
277         // Again, instantiated objects shouldn't be matched
278         ok(!jQuery.isPlainObject(new fn), "new fn");
279
280         // DOM Element
281         ok(!jQuery.isPlainObject(document.createElement("div")), "DOM Element");
282         
283         // Window
284         ok(!jQuery.isPlainObject(window), "window");
285
286         try {
287                 var iframe = document.createElement("iframe");
288                 document.body.appendChild(iframe);
289
290                 window.iframeDone = function(otherObject){
291                         // Objects from other windows should be matched
292                         ok(jQuery.isPlainObject(new otherObject), "new otherObject");
293                         document.body.removeChild( iframe );
294                         start();
295                 };
296  
297                 var doc = iframe.contentDocument || iframe.contentWindow.document;
298                 doc.open();
299                 doc.write("<body onload='window.parent.iframeDone(Object);'>");
300                 doc.close();
301         } catch(e) {
302                 document.body.removeChild( iframe );
303
304                 ok(true, "new otherObject - iframes not supported");
305                 start();
306         }
307 });
308
309 test("isFunction", function() {
310         expect(19);
311
312         // Make sure that false values return false
313         ok( !jQuery.isFunction(), "No Value" );
314         ok( !jQuery.isFunction( null ), "null Value" );
315         ok( !jQuery.isFunction( undefined ), "undefined Value" );
316         ok( !jQuery.isFunction( "" ), "Empty String Value" );
317         ok( !jQuery.isFunction( 0 ), "0 Value" );
318
319         // Check built-ins
320         // Safari uses "(Internal Function)"
321         ok( jQuery.isFunction(String), "String Function("+String+")" );
322         ok( jQuery.isFunction(Array), "Array Function("+Array+")" );
323         ok( jQuery.isFunction(Object), "Object Function("+Object+")" );
324         ok( jQuery.isFunction(Function), "Function Function("+Function+")" );
325
326         // When stringified, this could be misinterpreted
327         var mystr = "function";
328         ok( !jQuery.isFunction(mystr), "Function String" );
329
330         // When stringified, this could be misinterpreted
331         var myarr = [ "function" ];
332         ok( !jQuery.isFunction(myarr), "Function Array" );
333
334         // When stringified, this could be misinterpreted
335         var myfunction = { "function": "test" };
336         ok( !jQuery.isFunction(myfunction), "Function Object" );
337
338         // Make sure normal functions still work
339         var fn = function(){};
340         ok( jQuery.isFunction(fn), "Normal Function" );
341
342         var obj = document.createElement("object");
343
344         // Firefox says this is a function
345         ok( !jQuery.isFunction(obj), "Object Element" );
346
347         // IE says this is an object
348         // Since 1.3, this isn't supported (#2968)
349         //ok( jQuery.isFunction(obj.getAttribute), "getAttribute Function" );
350
351         var nodes = document.body.childNodes;
352
353         // Safari says this is a function
354         ok( !jQuery.isFunction(nodes), "childNodes Property" );
355
356         var first = document.body.firstChild;
357
358         // Normal elements are reported ok everywhere
359         ok( !jQuery.isFunction(first), "A normal DOM Element" );
360
361         var input = document.createElement("input");
362         input.type = "text";
363         document.body.appendChild( input );
364
365         // IE says this is an object
366         // Since 1.3, this isn't supported (#2968)
367         //ok( jQuery.isFunction(input.focus), "A default function property" );
368
369         document.body.removeChild( input );
370
371         var a = document.createElement("a");
372         a.href = "some-function";
373         document.body.appendChild( a );
374
375         // This serializes with the word 'function' in it
376         ok( !jQuery.isFunction(a), "Anchor Element" );
377
378         document.body.removeChild( a );
379
380         // Recursive function calls have lengths and array-like properties
381         function callme(callback){
382                 function fn(response){
383                         callback(response);
384                 }
385
386                 ok( jQuery.isFunction(fn), "Recursive Function Call" );
387
388                 fn({ some: "data" });
389         };
390
391         callme(function(){
392                 callme(function(){});
393         });
394 });
395
396 test("isXMLDoc - HTML", function() {
397         expect(4);
398
399         ok( !jQuery.isXMLDoc( document ), "HTML document" );
400         ok( !jQuery.isXMLDoc( document.documentElement ), "HTML documentElement" );
401         ok( !jQuery.isXMLDoc( document.body ), "HTML Body Element" );
402
403         var iframe = document.createElement("iframe");
404         document.body.appendChild( iframe );
405
406         try {
407                 var body = jQuery(iframe).contents()[0];
408
409                 try {
410                         ok( !jQuery.isXMLDoc( body ), "Iframe body element" );
411                 } catch(e) {
412                         ok( false, "Iframe body element exception" );
413                 }
414
415         } catch(e) {
416                 ok( true, "Iframe body element - iframe not working correctly" );
417         }
418
419         document.body.removeChild( iframe );
420 });
421
422 if ( !isLocal ) {
423 test("isXMLDoc - XML", function() {
424         expect(3);
425         stop();
426         jQuery.get('data/dashboard.xml', function(xml) {
427                 ok( jQuery.isXMLDoc( xml ), "XML document" );
428                 ok( jQuery.isXMLDoc( xml.documentElement ), "XML documentElement" );
429                 ok( jQuery.isXMLDoc( jQuery("tab", xml)[0] ), "XML Tab Element" );
430                 start();
431         });
432 });
433 }
434
435 test("isWindow", function() {
436         expect( 12 );
437
438         ok( jQuery.isWindow(window), "window" );
439         ok( !jQuery.isWindow(), "empty" );
440         ok( !jQuery.isWindow(null), "null" );
441         ok( !jQuery.isWindow(undefined), "undefined" );
442         ok( !jQuery.isWindow(document), "document" );
443         ok( !jQuery.isWindow(document.documentElement), "documentElement" );
444         ok( !jQuery.isWindow(""), "string" );
445         ok( !jQuery.isWindow(1), "number" );
446         ok( !jQuery.isWindow(true), "boolean" );
447         ok( !jQuery.isWindow({}), "object" );
448         // HMMM
449         // ok( !jQuery.isWindow({ setInterval: function(){} }), "fake window" );
450         ok( !jQuery.isWindow(/window/), "regexp" );
451         ok( !jQuery.isWindow(function(){}), "function" );
452 });
453
454 test("jQuery('html')", function() {
455         expect(15);
456
457         QUnit.reset();
458         jQuery.foo = false;
459         var s = jQuery("<script>jQuery.foo='test';</script>")[0];
460         ok( s, "Creating a script" );
461         ok( !jQuery.foo, "Make sure the script wasn't executed prematurely" );
462         jQuery("body").append("<script>jQuery.foo='test';</script>");
463         ok( jQuery.foo, "Executing a scripts contents in the right context" );
464
465         // Test multi-line HTML
466         var div = jQuery("<div>\r\nsome text\n<p>some p</p>\nmore text\r\n</div>")[0];
467         equals( div.nodeName.toUpperCase(), "DIV", "Make sure we're getting a div." );
468         equals( div.firstChild.nodeType, 3, "Text node." );
469         equals( div.lastChild.nodeType, 3, "Text node." );
470         equals( div.childNodes[1].nodeType, 1, "Paragraph." );
471         equals( div.childNodes[1].firstChild.nodeType, 3, "Paragraph text." );
472
473         QUnit.reset();
474         ok( jQuery("<link rel='stylesheet'/>")[0], "Creating a link" );
475
476         ok( !jQuery("<script/>")[0].parentNode, "Create a script" );
477
478         ok( jQuery("<input/>").attr("type", "hidden"), "Create an input and set the type." );
479
480         var j = jQuery("<span>hi</span> there <!-- mon ami -->");
481         ok( j.length >= 2, "Check node,textnode,comment creation (some browsers delete comments)" );
482
483         ok( !jQuery("<option>test</option>")[0].selected, "Make sure that options are auto-selected #2050" );
484
485         ok( jQuery("<div></div>")[0], "Create a div with closing tag." );
486         ok( jQuery("<table></table>")[0], "Create a table with closing tag." );
487 });
488
489 test("jQuery('html', context)", function() {
490         expect(1);
491
492         var $div = jQuery("<div/>")[0];
493         var $span = jQuery("<span/>", $div);
494         equals($span.length, 1, "Verify a span created with a div context works, #1763");
495 });
496
497 if ( !isLocal ) {
498 test("jQuery(selector, xml).text(str) - Loaded via XML document", function() {
499         expect(2);
500         stop();
501         jQuery.get('data/dashboard.xml', function(xml) {
502                 // tests for #1419 where IE was a problem
503                 var tab = jQuery("tab", xml).eq(0);
504                 equals( tab.text(), "blabla", "Verify initial text correct" );
505                 tab.text("newtext");
506                 equals( tab.text(), "newtext", "Verify new text correct" );
507                 start();
508         });
509 });
510 }
511
512 test("end()", function() {
513         expect(3);
514         equals( 'Yahoo', jQuery('#yahoo').parent().end().text(), 'Check for end' );
515         ok( jQuery('#yahoo').end(), 'Check for end with nothing to end' );
516
517         var x = jQuery('#yahoo');
518         x.parent();
519         equals( 'Yahoo', jQuery('#yahoo').text(), 'Check for non-destructive behaviour' );
520 });
521
522 test("length", function() {
523         expect(1);
524         equals( jQuery("p").length, 6, "Get Number of Elements Found" );
525 });
526
527 test("size()", function() {
528         expect(1);
529         equals( jQuery("p").size(), 6, "Get Number of Elements Found" );
530 });
531
532 test("get()", function() {
533         expect(1);
534         same( jQuery("p").get(), q("firstp","ap","sndp","en","sap","first"), "Get All Elements" );
535 });
536
537 test("toArray()", function() {
538         expect(1);
539         same( jQuery("p").toArray(),
540                 q("firstp","ap","sndp","en","sap","first"),
541                 "Convert jQuery object to an Array" )
542 })
543
544 test("get(Number)", function() {
545         expect(1);
546         equals( jQuery("p").get(0), document.getElementById("firstp"), "Get A Single Element" );
547 });
548
549 test("get(-Number)",function() {
550         expect(1);
551         equals( jQuery("p").get(-1),
552                 document.getElementById("first"),
553                 "Get a single element with negative index" )
554 })
555
556 test("each(Function)", function() {
557         expect(1);
558         var div = jQuery("div");
559         div.each(function(){this.foo = 'zoo';});
560         var pass = true;
561         for ( var i = 0; i < div.size(); i++ ) {
562                 if ( div.get(i).foo != "zoo" ) pass = false;
563         }
564         ok( pass, "Execute a function, Relative" );
565 });
566
567 test("slice()", function() {
568         expect(7);
569
570         var $links = jQuery("#ap a");
571
572         same( $links.slice(1,2).get(), q("groups"), "slice(1,2)" );
573         same( $links.slice(1).get(), q("groups", "anchor1", "mark"), "slice(1)" );
574         same( $links.slice(0,3).get(), q("google", "groups", "anchor1"), "slice(0,3)" );
575         same( $links.slice(-1).get(), q("mark"), "slice(-1)" );
576
577         same( $links.eq(1).get(), q("groups"), "eq(1)" );
578         same( $links.eq('2').get(), q("anchor1"), "eq('2')" );
579         same( $links.eq(-1).get(), q("mark"), "eq(-1)" );
580 });
581
582 test("first()/last()", function() {
583         expect(4);
584
585         var $links = jQuery("#ap a"), $none = jQuery("asdf");
586
587         same( $links.first().get(), q("google"), "first()" );
588         same( $links.last().get(), q("mark"), "last()" );
589
590         same( $none.first().get(), [], "first() none" );
591         same( $none.last().get(), [], "last() none" );
592 });
593
594 test("map()", function() {
595         expect(2);//expect(6);
596
597         same(
598                 jQuery("#ap").map(function(){
599                         return jQuery(this).find("a").get();
600                 }).get(),
601                 q("google", "groups", "anchor1", "mark"),
602                 "Array Map"
603         );
604
605         same(
606                 jQuery("#ap > a").map(function(){
607                         return this.parentNode;
608                 }).get(),
609                 q("ap","ap","ap"),
610                 "Single Map"
611         );
612
613         return;//these haven't been accepted yet
614
615         //for #2616
616         var keys = jQuery.map( {a:1,b:2}, function( v, k ){
617                 return k;
618         }, [ ] );
619
620         equals( keys.join(""), "ab", "Map the keys from a hash to an array" );
621
622         var values = jQuery.map( {a:1,b:2}, function( v, k ){
623                 return v;
624         }, [ ] );
625
626         equals( values.join(""), "12", "Map the values from a hash to an array" );
627
628         var scripts = document.getElementsByTagName("script");
629         var mapped = jQuery.map( scripts, function( v, k ){
630                 return v;
631         }, {length:0} );
632
633         equals( mapped.length, scripts.length, "Map an array(-like) to a hash" );
634
635         var flat = jQuery.map( Array(4), function( v, k ){
636                 return k % 2 ? k : [k,k,k];//try mixing array and regular returns
637         });
638
639         equals( flat.join(""), "00012223", "try the new flatten technique(#2616)" );
640 });
641
642 test("jQuery.merge()", function() {
643         expect(8);
644
645         var parse = jQuery.merge;
646
647         same( parse([],[]), [], "Empty arrays" );
648
649         same( parse([1],[2]), [1,2], "Basic" );
650         same( parse([1,2],[3,4]), [1,2,3,4], "Basic" );
651
652         same( parse([1,2],[]), [1,2], "Second empty" );
653         same( parse([],[1,2]), [1,2], "First empty" );
654
655         // Fixed at [5998], #3641
656         same( parse([-2,-1], [0,1,2]), [-2,-1,0,1,2], "Second array including a zero (falsy)");
657         
658         // After fixing #5527
659         same( parse([], [null, undefined]), [null, undefined], "Second array including null and undefined values");
660         same( parse({length:0}, [1,2]), {length:2, 0:1, 1:2}, "First array like");
661 });
662
663 test("jQuery.extend(Object, Object)", function() {
664         expect(27);
665
666         var settings = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
667                 options = { xnumber2: 1, xstring2: "x", xxx: "newstring" },
668                 optionsCopy = { xnumber2: 1, xstring2: "x", xxx: "newstring" },
669                 merged = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "x", xxx: "newstring" },
670                 deep1 = { foo: { bar: true } },
671                 deep1copy = { foo: { bar: true } },
672                 deep2 = { foo: { baz: true }, foo2: document },
673                 deep2copy = { foo: { baz: true }, foo2: document },
674                 deepmerged = { foo: { bar: true, baz: true }, foo2: document },
675                 arr = [1, 2, 3],
676                 nestedarray = { arr: arr };
677
678         jQuery.extend(settings, options);
679         same( settings, merged, "Check if extended: settings must be extended" );
680         same( options, optionsCopy, "Check if not modified: options must not be modified" );
681
682         jQuery.extend(settings, null, options);
683         same( settings, merged, "Check if extended: settings must be extended" );
684         same( options, optionsCopy, "Check if not modified: options must not be modified" );
685
686         jQuery.extend(true, deep1, deep2);
687         same( deep1.foo, deepmerged.foo, "Check if foo: settings must be extended" );
688         same( deep2.foo, deep2copy.foo, "Check if not deep2: options must not be modified" );
689         equals( deep1.foo2, document, "Make sure that a deep clone was not attempted on the document" );
690
691         ok( jQuery.extend(true, [], arr) !== arr, "Deep extend of array must clone array" );
692         ok( jQuery.extend(true, {}, nestedarray).arr !== arr, "Deep extend of object must clone child array" );
693
694         var empty = {};
695         var optionsWithLength = { foo: { length: -1 } };
696         jQuery.extend(true, empty, optionsWithLength);
697         same( empty.foo, optionsWithLength.foo, "The length property must copy correctly" );
698
699         empty = {};
700         var optionsWithDate = { foo: { date: new Date } };
701         jQuery.extend(true, empty, optionsWithDate);
702         same( empty.foo, optionsWithDate.foo, "Dates copy correctly" );
703
704         var myKlass = function() {};
705         var customObject = new myKlass();
706         var optionsWithCustomObject = { foo: { date: customObject } };
707         empty = {};
708         jQuery.extend(true, empty, optionsWithCustomObject);
709         ok( empty.foo && empty.foo.date === customObject, "Custom objects copy correctly (no methods)" );
710         
711         // Makes the class a little more realistic
712         myKlass.prototype = { someMethod: function(){} };
713         empty = {};
714         jQuery.extend(true, empty, optionsWithCustomObject);
715         ok( empty.foo && empty.foo.date === customObject, "Custom objects copy correctly" );
716         
717         var ret = jQuery.extend(true, { foo: 4 }, { foo: new Number(5) } );
718         ok( ret.foo == 5, "Wrapped numbers copy correctly" );
719
720         var nullUndef;
721         nullUndef = jQuery.extend({}, options, { xnumber2: null });
722         ok( nullUndef.xnumber2 === null, "Check to make sure null values are copied");
723
724         nullUndef = jQuery.extend({}, options, { xnumber2: undefined });
725         ok( nullUndef.xnumber2 === options.xnumber2, "Check to make sure undefined values are not copied");
726
727         nullUndef = jQuery.extend({}, options, { xnumber0: null });
728         ok( nullUndef.xnumber0 === null, "Check to make sure null values are inserted");
729
730         var target = {};
731         var recursive = { foo:target, bar:5 };
732         jQuery.extend(true, target, recursive);
733         same( target, { bar:5 }, "Check to make sure a recursive obj doesn't go never-ending loop by not copying it over" );
734
735         var ret = jQuery.extend(true, { foo: [] }, { foo: [0] } ); // 1907
736         equals( ret.foo.length, 1, "Check to make sure a value with coersion 'false' copies over when necessary to fix #1907" );
737
738         var ret = jQuery.extend(true, { foo: "1,2,3" }, { foo: [1, 2, 3] } );
739         ok( typeof ret.foo != "string", "Check to make sure values equal with coersion (but not actually equal) overwrite correctly" );
740
741         var ret = jQuery.extend(true, { foo:"bar" }, { foo:null } );
742         ok( typeof ret.foo !== 'undefined', "Make sure a null value doesn't crash with deep extend, for #1908" );
743
744         var obj = { foo:null };
745         jQuery.extend(true, obj, { foo:"notnull" } );
746         equals( obj.foo, "notnull", "Make sure a null value can be overwritten" );
747
748         function func() {}
749         jQuery.extend(func, { key: "value" } );
750         equals( func.key, "value", "Verify a function can be extended" );
751
752         var defaults = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
753                 defaultsCopy = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
754                 options1 = { xnumber2: 1, xstring2: "x" },
755                 options1Copy = { xnumber2: 1, xstring2: "x" },
756                 options2 = { xstring2: "xx", xxx: "newstringx" },
757                 options2Copy = { xstring2: "xx", xxx: "newstringx" },
758                 merged2 = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "xx", xxx: "newstringx" };
759
760         var settings = jQuery.extend({}, defaults, options1, options2);
761         same( settings, merged2, "Check if extended: settings must be extended" );
762         same( defaults, defaultsCopy, "Check if not modified: options1 must not be modified" );
763         same( options1, options1Copy, "Check if not modified: options1 must not be modified" );
764         same( options2, options2Copy, "Check if not modified: options2 must not be modified" );
765 });
766
767 test("jQuery.each(Object,Function)", function() {
768         expect(13);
769         jQuery.each( [0,1,2], function(i, n){
770                 equals( i, n, "Check array iteration" );
771         });
772
773         jQuery.each( [5,6,7], function(i, n){
774                 equals( i, n - 5, "Check array iteration" );
775         });
776
777         jQuery.each( { name: "name", lang: "lang" }, function(i, n){
778                 equals( i, n, "Check object iteration" );
779         });
780
781         var total = 0;
782         jQuery.each([1,2,3], function(i,v){ total += v; });
783         equals( total, 6, "Looping over an array" );
784         total = 0;
785         jQuery.each([1,2,3], function(i,v){ total += v; if ( i == 1 ) return false; });
786         equals( total, 3, "Looping over an array, with break" );
787         total = 0;
788         jQuery.each({"a":1,"b":2,"c":3}, function(i,v){ total += v; });
789         equals( total, 6, "Looping over an object" );
790         total = 0;
791         jQuery.each({"a":3,"b":3,"c":3}, function(i,v){ total += v; return false; });
792         equals( total, 3, "Looping over an object, with break" );
793
794         var f = function(){};
795         f.foo = 'bar';
796         jQuery.each(f, function(i){
797                 f[i] = 'baz';
798         });
799         equals( "baz", f.foo, "Loop over a function" );
800 });
801
802 test("jQuery.makeArray", function(){
803         expect(17);
804
805         equals( jQuery.makeArray(jQuery('html>*'))[0].nodeName.toUpperCase(), "HEAD", "Pass makeArray a jQuery object" );
806
807         equals( jQuery.makeArray(document.getElementsByName("PWD")).slice(0,1)[0].name, "PWD", "Pass makeArray a nodelist" );
808
809         equals( (function(){ return jQuery.makeArray(arguments); })(1,2).join(""), "12", "Pass makeArray an arguments array" );
810
811         equals( jQuery.makeArray([1,2,3]).join(""), "123", "Pass makeArray a real array" );
812
813         equals( jQuery.makeArray().length, 0, "Pass nothing to makeArray and expect an empty array" );
814
815         equals( jQuery.makeArray( 0 )[0], 0 , "Pass makeArray a number" );
816
817         equals( jQuery.makeArray( "foo" )[0], "foo", "Pass makeArray a string" );
818
819         equals( jQuery.makeArray( true )[0].constructor, Boolean, "Pass makeArray a boolean" );
820
821         equals( jQuery.makeArray( document.createElement("div") )[0].nodeName.toUpperCase(), "DIV", "Pass makeArray a single node" );
822
823         equals( jQuery.makeArray( {length:2, 0:"a", 1:"b"} ).join(""), "ab", "Pass makeArray an array like map (with length)" );
824
825         ok( !!jQuery.makeArray( document.documentElement.childNodes ).slice(0,1)[0].nodeName, "Pass makeArray a childNodes array" );
826
827         // function, is tricky as it has length
828         equals( jQuery.makeArray( function(){ return 1;} )[0](), 1, "Pass makeArray a function" );
829
830         //window, also has length
831         equals( jQuery.makeArray(window)[0], window, "Pass makeArray the window" );
832
833         equals( jQuery.makeArray(/a/)[0].constructor, RegExp, "Pass makeArray a regex" );
834
835         ok( jQuery.makeArray(document.getElementById('form')).length >= 13, "Pass makeArray a form (treat as elements)" );
836
837         // For #5610
838         same( jQuery.makeArray({'length': '0'}), [], "Make sure object is coerced properly.");
839         same( jQuery.makeArray({'length': '5'}), [], "Make sure object is coerced properly.");
840 });
841
842 test("jQuery.isEmptyObject", function(){
843         expect(2);
844         
845         equals(true, jQuery.isEmptyObject({}), "isEmptyObject on empty object literal" );
846         equals(false, jQuery.isEmptyObject({a:1}), "isEmptyObject on non-empty object literal" );
847         
848         // What about this ?
849         // equals(true, jQuery.isEmptyObject(null), "isEmptyObject on null" );
850 });
851
852 test("jQuery.proxy", function(){
853         expect(4);
854
855         var test = function(){ equals( this, thisObject, "Make sure that scope is set properly." ); };
856         var thisObject = { foo: "bar", method: test };
857
858         // Make sure normal works
859         test.call( thisObject );
860
861         // Basic scoping
862         jQuery.proxy( test, thisObject )();
863
864         // Make sure it doesn't freak out
865         equals( jQuery.proxy( null, thisObject ), undefined, "Make sure no function was returned." );
866
867         // Use the string shortcut
868         jQuery.proxy( thisObject, "method" )();
869 });
870
871 test("jQuery.parseJSON", function(){
872         expect(8);
873         
874         equals( jQuery.parseJSON(), null, "Nothing in, null out." );
875         equals( jQuery.parseJSON( null ), null, "Nothing in, null out." );
876         equals( jQuery.parseJSON( "" ), null, "Nothing in, null out." );
877         
878         same( jQuery.parseJSON("{}"), {}, "Plain object parsing." );
879         same( jQuery.parseJSON('{"test":1}'), {"test":1}, "Plain object parsing." );
880
881         same( jQuery.parseJSON('\n{"test":1}'), {"test":1}, "Make sure leading whitespaces are handled." );
882         
883         try {
884                 jQuery.parseJSON("{a:1}");
885                 ok( false, "Test malformed JSON string." );
886         } catch( e ) {
887                 ok( true, "Test malformed JSON string." );
888         }
889         
890         try {
891                 jQuery.parseJSON("{'a':1}");
892                 ok( false, "Test malformed JSON string." );
893         } catch( e ) {
894                 ok( true, "Test malformed JSON string." );
895         }
896 });