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