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