Fixes $.trim for   closes #4980
[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(12);
16
17         // Basic constructor's behavior
18
19         equals( jQuery().length, 1, "jQuery() === jQuery(document)" );
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         var img = jQuery("<img/>");
55         equals( img.length, 1, "Correct number of elements generated for img" );
56         var div = jQuery("<div/><hr/><code/><b/>");
57         equals( div.length, 4, "Correct number of elements generated for div hr code b" );
58
59         equals( jQuery([1,2,3]).get(1), 2, "Test passing an array to the factory" );
60
61         equals( jQuery(document.body).get(0), jQuery('body').get(0), "Test passing an html node to the factory" );
62 });
63
64 test("selector state", function() {
65         expect(31);
66
67         var test;
68
69         test = jQuery(undefined);
70         equals( test.selector, "", "Empty jQuery Selector" );
71         equals( test.context, undefined, "Empty jQuery Context" );
72
73         test = jQuery(document);
74         equals( test.selector, "", "Document Selector" );
75         equals( test.context, document, "Document Context" );
76
77         test = jQuery(document.body);
78         equals( test.selector, "", "Body Selector" );
79         equals( test.context, document.body, "Body Context" );
80
81         test = jQuery("#main");
82         equals( test.selector, "#main", "#main Selector" );
83         equals( test.context, document, "#main Context" );
84
85         test = jQuery("#notfoundnono");
86         equals( test.selector, "#notfoundnono", "#notfoundnono Selector" );
87         equals( test.context, document, "#notfoundnono Context" );
88
89         test = jQuery("#main", document);
90         equals( test.selector, "#main", "#main Selector" );
91         equals( test.context, document, "#main Context" );
92
93         test = jQuery("#main", document.body);
94         equals( test.selector, "#main", "#main Selector" );
95         equals( test.context, document.body, "#main Context" );
96
97         // Test cloning
98         test = jQuery(test);
99         equals( test.selector, "#main", "#main Selector" );
100         equals( test.context, document.body, "#main Context" );
101
102         test = jQuery(document.body).find("#main");
103         equals( test.selector, "#main", "#main find Selector" );
104         equals( test.context, document.body, "#main find Context" );
105
106         test = jQuery("#main").filter("div");
107         equals( test.selector, "#main.filter(div)", "#main filter Selector" );
108         equals( test.context, document, "#main filter Context" );
109
110         test = jQuery("#main").not("div");
111         equals( test.selector, "#main.not(div)", "#main not Selector" );
112         equals( test.context, document, "#main not Context" );
113
114         test = jQuery("#main").filter("div").not("div");
115         equals( test.selector, "#main.filter(div).not(div)", "#main filter, not Selector" );
116         equals( test.context, document, "#main filter, not Context" );
117
118         test = jQuery("#main").filter("div").not("div").end();
119         equals( test.selector, "#main.filter(div)", "#main filter, not, end Selector" );
120         equals( test.context, document, "#main filter, not, end Context" );
121
122         test = jQuery("#main").parent("body");
123         equals( test.selector, "#main.parent(body)", "#main parent Selector" );
124         equals( test.context, document, "#main parent Context" );
125
126         test = jQuery("#main").eq(0);
127         equals( test.selector, "#main.slice(0,1)", "#main eq Selector" );
128         equals( test.context, document, "#main eq Context" );
129         
130         var d = "<div />";
131         equals(
132                 jQuery(d).appendTo(jQuery(d)).selector,
133                 jQuery(d).appendTo(d).selector,
134                 "manipulation methods make same selector for jQuery objects"
135         );
136 });
137
138 test("browser", function() {
139         expect(13);
140         var browsers = {
141                 //Internet Explorer
142                 "Mozilla/5.0 (Windows; U; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)": "6.0",
143                 "Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 5.1; .NET CLR 1.1.4322; InfoPath.1; .NET CLR 2.0.50727)": "7.0",
144                 /** Failing #1876
145                  * "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 2.0.50727; .NET CLR 1.1.4322; .NET CLR 3.0.04506.30)": "7.0",
146                  */
147                 //Browsers with Gecko engine
148                 //Mozilla
149                 "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.12) Gecko/20050915" : "1.7.12",
150                 //Firefox
151                 "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3": "1.8.1.3",
152                 //Netscape
153                 "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20070321 Netscape/8.1.3" : "1.7.5",
154                 //Flock
155                 "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.11) Gecko/20070321 Firefox/1.5.0.11 Flock/0.7.12" : "1.8.0.11",
156                 //Opera browser
157                 "Opera/9.20 (X11; Linux x86_64; U; en)": "9.20",
158                 "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; en) Opera 9.20" : "9.20",
159                 "Mozilla/5.0 (Windows NT 5.1; U; pl; rv:1.8.0) Gecko/20060728 Firefox/1.5.0 Opera 9.20": "9.20",
160                 //WebKit engine
161                 "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; sv-se) AppleWebKit/418.9 (KHTML, like Gecko) Safari/419.3": "418.9",
162                 "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/418.8 (KHTML, like Gecko) Safari/419.3" : "418.8",
163                 "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; sv-se) AppleWebKit/312.8 (KHTML, like Gecko) Safari/312.5": "312.8",
164                 //Other user agent string
165                 "Other browser's user agent 1.0":null
166         };
167         for (var i in browsers) {
168                 var v = i.toLowerCase().match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ); // RegEx from Core jQuery.browser.version check
169                 var version = v ? v[1] : null;
170                 equals( version, browsers[i], "Checking UA string" );
171         }
172 });
173
174 test("noConflict", function() {
175         expect(6);
176
177         var $$ = jQuery;
178
179         equals( jQuery, jQuery.noConflict(), "noConflict returned the jQuery object" );
180         equals( jQuery, $$, "Make sure jQuery wasn't touched." );
181         equals( $, original$, "Make sure $ was reverted." );
182
183         jQuery = $ = $$;
184
185         equals( jQuery.noConflict(true), $$, "noConflict returned the jQuery object" );
186         equals( jQuery, originaljQuery, "Make sure jQuery was reverted." );
187         equals( $, original$, "Make sure $ was reverted." );
188
189         jQuery = $$;
190 });
191
192 test("trim", function() {
193   expect(4);
194
195   var nbsp = String.fromCharCode(160);
196
197   equals( jQuery.trim("hello  "), "hello", "trailing space" );
198   equals( jQuery.trim("  hello"), "hello", "leading space" );
199   equals( jQuery.trim("  hello   "), "hello", "space on both sides" );
200   equals( jQuery.trim("  " + nbsp + "hello  " + nbsp + " "), "hello", "&nbsp;" );
201 });
202
203 test("isFunction", function() {
204         expect(19);
205
206         // Make sure that false values return false
207         ok( !jQuery.isFunction(), "No Value" );
208         ok( !jQuery.isFunction( null ), "null Value" );
209         ok( !jQuery.isFunction( undefined ), "undefined Value" );
210         ok( !jQuery.isFunction( "" ), "Empty String Value" );
211         ok( !jQuery.isFunction( 0 ), "0 Value" );
212
213         // Check built-ins
214         // Safari uses "(Internal Function)"
215         ok( jQuery.isFunction(String), "String Function("+String+")" );
216         ok( jQuery.isFunction(Array), "Array Function("+Array+")" );
217         ok( jQuery.isFunction(Object), "Object Function("+Object+")" );
218         ok( jQuery.isFunction(Function), "Function Function("+Function+")" );
219
220         // When stringified, this could be misinterpreted
221         var mystr = "function";
222         ok( !jQuery.isFunction(mystr), "Function String" );
223
224         // When stringified, this could be misinterpreted
225         var myarr = [ "function" ];
226         ok( !jQuery.isFunction(myarr), "Function Array" );
227
228         // When stringified, this could be misinterpreted
229         var myfunction = { "function": "test" };
230         ok( !jQuery.isFunction(myfunction), "Function Object" );
231
232         // Make sure normal functions still work
233         var fn = function(){};
234         ok( jQuery.isFunction(fn), "Normal Function" );
235
236         var obj = document.createElement("object");
237
238         // Firefox says this is a function
239         ok( !jQuery.isFunction(obj), "Object Element" );
240
241         // IE says this is an object
242         // Since 1.3, this isn't supported (#2968)
243         //ok( jQuery.isFunction(obj.getAttribute), "getAttribute Function" );
244
245         var nodes = document.body.childNodes;
246
247         // Safari says this is a function
248         ok( !jQuery.isFunction(nodes), "childNodes Property" );
249
250         var first = document.body.firstChild;
251
252         // Normal elements are reported ok everywhere
253         ok( !jQuery.isFunction(first), "A normal DOM Element" );
254
255         var input = document.createElement("input");
256         input.type = "text";
257         document.body.appendChild( input );
258
259         // IE says this is an object
260         // Since 1.3, this isn't supported (#2968)
261         //ok( jQuery.isFunction(input.focus), "A default function property" );
262
263         document.body.removeChild( input );
264
265         var a = document.createElement("a");
266         a.href = "some-function";
267         document.body.appendChild( a );
268
269         // This serializes with the word 'function' in it
270         ok( !jQuery.isFunction(a), "Anchor Element" );
271
272         document.body.removeChild( a );
273
274         // Recursive function calls have lengths and array-like properties
275         function callme(callback){
276                 function fn(response){
277                         callback(response);
278                 }
279
280                 ok( jQuery.isFunction(fn), "Recursive Function Call" );
281
282                 fn({ some: "data" });
283         };
284
285         callme(function(){
286                 callme(function(){});
287         });
288 });
289
290 test("isXMLDoc - HTML", function() {
291         expect(4);
292
293         ok( !jQuery.isXMLDoc( document ), "HTML document" );
294         ok( !jQuery.isXMLDoc( document.documentElement ), "HTML documentElement" );
295         ok( !jQuery.isXMLDoc( document.body ), "HTML Body Element" );
296
297         var iframe = document.createElement("iframe");
298         document.body.appendChild( iframe );
299
300         try {
301                 var body = jQuery(iframe).contents()[0];
302                 ok( !jQuery.isXMLDoc( body ), "Iframe body element" );
303         } catch(e){
304                 ok( false, "Iframe body element exception" );
305         }
306
307         document.body.removeChild( iframe );
308 });
309
310 if ( !isLocal ) {
311 test("isXMLDoc - XML", function() {
312         expect(3);
313         stop();
314         jQuery.get('data/dashboard.xml', function(xml) {
315                 ok( jQuery.isXMLDoc( xml ), "XML document" );
316                 ok( jQuery.isXMLDoc( xml.documentElement ), "XML documentElement" );
317                 ok( jQuery.isXMLDoc( jQuery("tab", xml)[0] ), "XML Tab Element" );
318                 start();
319         });
320 });
321 }
322
323 test("jQuery('html')", function() {
324         expect(15);
325
326         reset();
327         jQuery.foo = false;
328         var s = jQuery("<script>jQuery.foo='test';</script>")[0];
329         ok( s, "Creating a script" );
330         ok( !jQuery.foo, "Make sure the script wasn't executed prematurely" );
331         jQuery("body").append("<script>jQuery.foo='test';</script>");
332         ok( jQuery.foo, "Executing a scripts contents in the right context" );
333
334         // Test multi-line HTML
335         var div = jQuery("<div>\r\nsome text\n<p>some p</p>\nmore text\r\n</div>")[0];
336         equals( div.nodeName.toUpperCase(), "DIV", "Make sure we're getting a div." );
337         equals( div.firstChild.nodeType, 3, "Text node." );
338         equals( div.lastChild.nodeType, 3, "Text node." );
339         equals( div.childNodes[1].nodeType, 1, "Paragraph." );
340         equals( div.childNodes[1].firstChild.nodeType, 3, "Paragraph text." );
341
342         reset();
343         ok( jQuery("<link rel='stylesheet'/>")[0], "Creating a link" );
344
345         ok( !jQuery("<script/>")[0].parentNode, "Create a script" );
346
347         ok( jQuery("<input/>").attr("type", "hidden"), "Create an input and set the type." );
348
349         var j = jQuery("<span>hi</span> there <!-- mon ami -->");
350         ok( j.length >= 2, "Check node,textnode,comment creation (some browsers delete comments)" );
351
352         ok( !jQuery("<option>test</option>")[0].selected, "Make sure that options are auto-selected #2050" );
353
354         ok( jQuery("<div></div>")[0], "Create a div with closing tag." );
355         ok( jQuery("<table></table>")[0], "Create a table with closing tag." );
356 });
357
358 test("jQuery('html', context)", function() {
359         expect(1);
360
361         var $div = jQuery("<div/>")[0];
362         var $span = jQuery("<span/>", $div);
363         equals($span.length, 1, "Verify a span created with a div context works, #1763");
364 });
365
366 if ( !isLocal ) {
367 test("jQuery(selector, xml).text(str) - Loaded via XML document", function() {
368         expect(2);
369         stop();
370         jQuery.get('data/dashboard.xml', function(xml) {
371                 // tests for #1419 where IE was a problem
372                 var tab = jQuery("tab", xml).eq(0);
373                 equals( tab.text(), "blabla", "Verify initial text correct" );
374                 tab.text("newtext");
375                 equals( tab.text(), "newtext", "Verify new text correct" );
376                 start();
377         });
378 });
379 }
380
381 test("length", function() {
382         expect(1);
383         equals( jQuery("p").length, 6, "Get Number of Elements Found" );
384 });
385
386 test("size()", function() {
387         expect(1);
388         equals( jQuery("p").size(), 6, "Get Number of Elements Found" );
389 });
390
391 test("get()", function() {
392         expect(1);
393         same( jQuery("p").get(), q("firstp","ap","sndp","en","sap","first"), "Get All Elements" );
394 });
395
396 test("toArray()", function() {
397         expect(1);
398         same( jQuery("p").toArray(),
399                 q("firstp","ap","sndp","en","sap","first"),
400                 "Convert jQuery object to an Array" )
401 })
402
403 test("get(Number)", function() {
404         expect(1);
405         equals( jQuery("p").get(0), document.getElementById("firstp"), "Get A Single Element" );
406 });
407
408 test("get(-Number)",function() {
409         expect(1);
410         equals( jQuery("p").get(-1),
411                 document.getElementById("first"),
412                 "Get a single element with negative index" )
413 })
414
415 test("add(String|Element|Array|undefined)", function() {
416         expect(16);
417         same( jQuery("#sndp").add("#en").add("#sap").get(), q("sndp", "en", "sap"), "Check elements from document" );
418         same( jQuery("#sndp").add( jQuery("#en")[0] ).add( jQuery("#sap") ).get(), q("sndp", "en", "sap"), "Check elements from document" );
419         ok( jQuery([]).add(jQuery("#form")[0].elements).length >= 13, "Check elements from array" );
420
421         // For the time being, we're discontinuing support for jQuery(form.elements) since it's ambiguous in IE
422         // use jQuery([]).add(form.elements) instead.
423         //equals( jQuery([]).add(jQuery("#form")[0].elements).length, jQuery(jQuery("#form")[0].elements).length, "Array in constructor must equals array in add()" );
424
425         var tmp = jQuery("<div/>");
426
427         var x = jQuery([]).add(jQuery("<p id='x1'>xxx</p>").appendTo(tmp)).add(jQuery("<p id='x2'>xxx</p>").appendTo(tmp));
428         equals( x[0].id, "x1", "Check on-the-fly element1" );
429         equals( x[1].id, "x2", "Check on-the-fly element2" );
430
431         var x = jQuery([]).add(jQuery("<p id='x1'>xxx</p>").appendTo(tmp)[0]).add(jQuery("<p id='x2'>xxx</p>").appendTo(tmp)[0]);
432         equals( x[0].id, "x1", "Check on-the-fly element1" );
433         equals( x[1].id, "x2", "Check on-the-fly element2" );
434
435         var x = jQuery([]).add(jQuery("<p id='x1'>xxx</p>")).add(jQuery("<p id='x2'>xxx</p>"));
436         equals( x[0].id, "x1", "Check on-the-fly element1" );
437         equals( x[1].id, "x2", "Check on-the-fly element2" );
438
439         var x = jQuery([]).add("<p id='x1'>xxx</p>").add("<p id='x2'>xxx</p>");
440         equals( x[0].id, "x1", "Check on-the-fly element1" );
441         equals( x[1].id, "x2", "Check on-the-fly element2" );
442
443         var notDefined;
444         equals( jQuery([]).add(notDefined).length, 0, "Check that undefined adds nothing" );
445
446         // Added after #2811
447         equals( jQuery([]).add([window,document,document.body,document]).length, 3, "Pass an array" );
448         equals( jQuery(document).add(document).length, 1, "Check duplicated elements" );
449         equals( jQuery(window).add(window).length, 1, "Check duplicated elements using the window" );
450         ok( jQuery([]).add( document.getElementById('form') ).length >= 13, "Add a form (adds the elements)" );
451 });
452
453 test("add(String, Context)", function() {
454         expect(6);
455
456         equals( jQuery(document).add("#form").length, 2, "Make sure that using regular context document still works." );
457         equals( jQuery(document.body).add("#form").length, 2, "Using a body context." );
458         equals( jQuery(document.body).add("#html").length, 1, "Using a body context." );
459
460         equals( jQuery(document).add("#form", document).length, 2, "Use a passed in document context." );
461         equals( jQuery(document).add("#form", document.body).length, 2, "Use a passed in body context." );
462         equals( jQuery(document).add("#html", document.body).length, 1, "Use a passed in body context." );
463 });
464
465 test("each(Function)", function() {
466         expect(1);
467         var div = jQuery("div");
468         div.each(function(){this.foo = 'zoo';});
469         var pass = true;
470         for ( var i = 0; i < div.size(); i++ ) {
471                 if ( div.get(i).foo != "zoo" ) pass = false;
472         }
473         ok( pass, "Execute a function, Relative" );
474 });
475
476 test("index()", function() {
477         expect(1);
478
479         equals( jQuery("#text2").index(), 2, "Returns the index of a child amongst its siblings" )
480 });
481
482 test("index(Object|String|undefined)", function() {
483         expect(16);
484
485         var elements = jQuery([window, document]),
486                 inputElements = jQuery('#radio1,#radio2,#check1,#check2');
487
488         // Passing a node
489         equals( elements.index(window), 0, "Check for index of elements" );
490         equals( elements.index(document), 1, "Check for index of elements" );
491         equals( inputElements.index(document.getElementById('radio1')), 0, "Check for index of elements" );
492         equals( inputElements.index(document.getElementById('radio2')), 1, "Check for index of elements" );
493         equals( inputElements.index(document.getElementById('check1')), 2, "Check for index of elements" );
494         equals( inputElements.index(document.getElementById('check2')), 3, "Check for index of elements" );
495         equals( inputElements.index(window), -1, "Check for not found index" );
496         equals( inputElements.index(document), -1, "Check for not found index" );
497
498         // Passing a jQuery object
499         // enabled since [5500]
500         equals( elements.index( elements ), 0, "Pass in a jQuery object" );
501         equals( elements.index( elements.eq(1) ), 1, "Pass in a jQuery object" );
502         equals( jQuery("#form :radio").index( jQuery("#radio2") ), 1, "Pass in a jQuery object" );
503
504         // Passing a selector or nothing
505         // enabled since [6330]
506         equals( jQuery('#text2').index(), 2, "Check for index amongst siblings" );
507         equals( jQuery('#form').children().eq(4).index(), 4, "Check for index amongst siblings" );
508         equals( jQuery('#radio2').index('#form :radio') , 1, "Check for index within a selector" );
509         equals( jQuery('#form :radio').index( jQuery('#radio2') ), 1, "Check for index within a selector" );
510         equals( jQuery('#radio2').index('#form :text') , -1, "Check for index not found within a selector" );
511 });
512
513 test("jQuery.merge()", function() {
514         expect(8);
515
516         var parse = jQuery.merge;
517
518         same( parse([],[]), [], "Empty arrays" );
519
520         same( parse([1],[2]), [1,2], "Basic" );
521         same( parse([1,2],[3,4]), [1,2,3,4], "Basic" );
522
523         same( parse([1,2],[]), [1,2], "Second empty" );
524         same( parse([],[1,2]), [1,2], "First empty" );
525
526         // Fixed at [5998], #3641
527         same( parse([-2,-1], [0,1,2]), [-2,-1,0,1,2], "Second array including a zero (falsy)");
528         
529         // After fixing #5527
530         same( parse([], [null, undefined]), [null, undefined], "Second array including null and undefined values");
531         same( parse({length:0}, [1,2]), {length:2, 0:1, 1:2}, "First array like");
532 });
533
534 test("jQuery.extend(Object, Object)", function() {
535         expect(25);
536
537         var settings = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
538                 options = { xnumber2: 1, xstring2: "x", xxx: "newstring" },
539                 optionsCopy = { xnumber2: 1, xstring2: "x", xxx: "newstring" },
540                 merged = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "x", xxx: "newstring" },
541                 deep1 = { foo: { bar: true } },
542                 deep1copy = { foo: { bar: true } },
543                 deep2 = { foo: { baz: true }, foo2: document },
544                 deep2copy = { foo: { baz: true }, foo2: document },
545                 deepmerged = { foo: { bar: true, baz: true }, foo2: document };
546
547         jQuery.extend(settings, options);
548         same( settings, merged, "Check if extended: settings must be extended" );
549         same( options, optionsCopy, "Check if not modified: options must not be modified" );
550
551         jQuery.extend(settings, null, options);
552         same( settings, merged, "Check if extended: settings must be extended" );
553         same( options, optionsCopy, "Check if not modified: options must not be modified" );
554
555         jQuery.extend(true, deep1, deep2);
556         same( deep1.foo, deepmerged.foo, "Check if foo: settings must be extended" );
557         same( deep2.foo, deep2copy.foo, "Check if not deep2: options must not be modified" );
558         equals( deep1.foo2, document, "Make sure that a deep clone was not attempted on the document" );
559
560         var empty = {};
561         var optionsWithLength = { foo: { length: -1 } };
562         jQuery.extend(true, empty, optionsWithLength);
563         same( empty.foo, optionsWithLength.foo, "The length property must copy correctly" );
564
565         empty = {};
566         var optionsWithDate = { foo: { date: new Date } };
567         jQuery.extend(true, empty, optionsWithDate);
568         same( empty.foo, optionsWithDate.foo, "Dates copy correctly" );
569
570         var myKlass = function() {};
571         var customObject = new myKlass();
572         var optionsWithCustomObject = { foo: { date: customObject } };
573         empty = {};
574         jQuery.extend(true, empty, optionsWithCustomObject);
575         ok( empty.foo && empty.foo.date === customObject, "Custom objects copy correctly (no methods)" );
576         
577         // Makes the class a little more realistic
578         myKlass.prototype = { someMethod: function(){} };
579         empty = {};
580         jQuery.extend(true, empty, optionsWithCustomObject);
581         ok( empty.foo && empty.foo.date === customObject, "Custom objects copy correctly" );
582         
583         var ret = jQuery.extend(true, { foo: 4 }, { foo: new Number(5) } );
584         ok( ret.foo == 5, "Wrapped numbers copy correctly" );
585
586         var nullUndef;
587         nullUndef = jQuery.extend({}, options, { xnumber2: null });
588         ok( nullUndef.xnumber2 === null, "Check to make sure null values are copied");
589
590         nullUndef = jQuery.extend({}, options, { xnumber2: undefined });
591         ok( nullUndef.xnumber2 === options.xnumber2, "Check to make sure undefined values are not copied");
592
593         nullUndef = jQuery.extend({}, options, { xnumber0: null });
594         ok( nullUndef.xnumber0 === null, "Check to make sure null values are inserted");
595
596         var target = {};
597         var recursive = { foo:target, bar:5 };
598         jQuery.extend(true, target, recursive);
599         same( target, { bar:5 }, "Check to make sure a recursive obj doesn't go never-ending loop by not copying it over" );
600
601         var ret = jQuery.extend(true, { foo: [] }, { foo: [0] } ); // 1907
602         equals( ret.foo.length, 1, "Check to make sure a value with coersion 'false' copies over when necessary to fix #1907" );
603
604         var ret = jQuery.extend(true, { foo: "1,2,3" }, { foo: [1, 2, 3] } );
605         ok( typeof ret.foo != "string", "Check to make sure values equal with coersion (but not actually equal) overwrite correctly" );
606
607         var ret = jQuery.extend(true, { foo:"bar" }, { foo:null } );
608         ok( typeof ret.foo !== 'undefined', "Make sure a null value doesn't crash with deep extend, for #1908" );
609
610         var obj = { foo:null };
611         jQuery.extend(true, obj, { foo:"notnull" } );
612         equals( obj.foo, "notnull", "Make sure a null value can be overwritten" );
613
614         function func() {}
615         jQuery.extend(func, { key: "value" } );
616         equals( func.key, "value", "Verify a function can be extended" );
617
618         var defaults = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
619                 defaultsCopy = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
620                 options1 = { xnumber2: 1, xstring2: "x" },
621                 options1Copy = { xnumber2: 1, xstring2: "x" },
622                 options2 = { xstring2: "xx", xxx: "newstringx" },
623                 options2Copy = { xstring2: "xx", xxx: "newstringx" },
624                 merged2 = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "xx", xxx: "newstringx" };
625
626         var settings = jQuery.extend({}, defaults, options1, options2);
627         same( settings, merged2, "Check if extended: settings must be extended" );
628         same( defaults, defaultsCopy, "Check if not modified: options1 must not be modified" );
629         same( options1, options1Copy, "Check if not modified: options1 must not be modified" );
630         same( options2, options2Copy, "Check if not modified: options2 must not be modified" );
631 });
632
633 test("jQuery.each(Object,Function)", function() {
634         expect(13);
635         jQuery.each( [0,1,2], function(i, n){
636                 equals( i, n, "Check array iteration" );
637         });
638
639         jQuery.each( [5,6,7], function(i, n){
640                 equals( i, n - 5, "Check array iteration" );
641         });
642
643         jQuery.each( { name: "name", lang: "lang" }, function(i, n){
644                 equals( i, n, "Check object iteration" );
645         });
646
647         var total = 0;
648         jQuery.each([1,2,3], function(i,v){ total += v; });
649         equals( total, 6, "Looping over an array" );
650         total = 0;
651         jQuery.each([1,2,3], function(i,v){ total += v; if ( i == 1 ) return false; });
652         equals( total, 3, "Looping over an array, with break" );
653         total = 0;
654         jQuery.each({"a":1,"b":2,"c":3}, function(i,v){ total += v; });
655         equals( total, 6, "Looping over an object" );
656         total = 0;
657         jQuery.each({"a":3,"b":3,"c":3}, function(i,v){ total += v; return false; });
658         equals( total, 3, "Looping over an object, with break" );
659
660         var f = function(){};
661         f.foo = 'bar';
662         jQuery.each(f, function(i){
663                 f[i] = 'baz';
664         });
665         equals( "baz", f.foo, "Loop over a function" );
666 });
667
668 test("jQuery.makeArray", function(){
669         expect(15);
670
671         equals( jQuery.makeArray(jQuery('html>*'))[0].nodeName.toUpperCase(), "HEAD", "Pass makeArray a jQuery object" );
672
673         equals( jQuery.makeArray(document.getElementsByName("PWD")).slice(0,1)[0].name, "PWD", "Pass makeArray a nodelist" );
674
675         equals( (function(){ return jQuery.makeArray(arguments); })(1,2).join(""), "12", "Pass makeArray an arguments array" );
676
677         equals( jQuery.makeArray([1,2,3]).join(""), "123", "Pass makeArray a real array" );
678
679         equals( jQuery.makeArray().length, 0, "Pass nothing to makeArray and expect an empty array" );
680
681         equals( jQuery.makeArray( 0 )[0], 0 , "Pass makeArray a number" );
682
683         equals( jQuery.makeArray( "foo" )[0], "foo", "Pass makeArray a string" );
684
685         equals( jQuery.makeArray( true )[0].constructor, Boolean, "Pass makeArray a boolean" );
686
687         equals( jQuery.makeArray( document.createElement("div") )[0].nodeName.toUpperCase(), "DIV", "Pass makeArray a single node" );
688
689         equals( jQuery.makeArray( {length:2, 0:"a", 1:"b"} ).join(""), "ab", "Pass makeArray an array like map (with length)" );
690
691         ok( !!jQuery.makeArray( document.documentElement.childNodes ).slice(0,1)[0].nodeName, "Pass makeArray a childNodes array" );
692
693         // function, is tricky as it has length
694         equals( jQuery.makeArray( function(){ return 1;} )[0](), 1, "Pass makeArray a function" );
695
696         //window, also has length
697         equals( jQuery.makeArray(window)[0], window, "Pass makeArray the window" );
698
699         equals( jQuery.makeArray(/a/)[0].constructor, RegExp, "Pass makeArray a regex" );
700
701         ok( jQuery.makeArray(document.getElementById('form')).length >= 13, "Pass makeArray a form (treat as elements)" );
702 });
703
704 test("jQuery.isEmptyObject", function(){
705         expect(2);
706         
707         equals(true, jQuery.isEmptyObject({}), "isEmptyObject on empty object literal" );
708         equals(false, jQuery.isEmptyObject({a:1}), "isEmptyObject on non-empty object literal" );
709         
710         // What about this ?
711         // equals(true, jQuery.isEmptyObject(null), "isEmptyObject on null" );
712 });