A follow-up to [6578] (which stopped adding expandos to elements that didn't have...
[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         isSet( 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("isFunction", function() {
193         expect(19);
194
195         // Make sure that false values return false
196         ok( !jQuery.isFunction(), "No Value" );
197         ok( !jQuery.isFunction( null ), "null Value" );
198         ok( !jQuery.isFunction( undefined ), "undefined Value" );
199         ok( !jQuery.isFunction( "" ), "Empty String Value" );
200         ok( !jQuery.isFunction( 0 ), "0 Value" );
201
202         // Check built-ins
203         // Safari uses "(Internal Function)"
204         ok( jQuery.isFunction(String), "String Function("+String+")" );
205         ok( jQuery.isFunction(Array), "Array Function("+Array+")" );
206         ok( jQuery.isFunction(Object), "Object Function("+Object+")" );
207         ok( jQuery.isFunction(Function), "Function Function("+Function+")" );
208
209         // When stringified, this could be misinterpreted
210         var mystr = "function";
211         ok( !jQuery.isFunction(mystr), "Function String" );
212
213         // When stringified, this could be misinterpreted
214         var myarr = [ "function" ];
215         ok( !jQuery.isFunction(myarr), "Function Array" );
216
217         // When stringified, this could be misinterpreted
218         var myfunction = { "function": "test" };
219         ok( !jQuery.isFunction(myfunction), "Function Object" );
220
221         // Make sure normal functions still work
222         var fn = function(){};
223         ok( jQuery.isFunction(fn), "Normal Function" );
224
225         var obj = document.createElement("object");
226
227         // Firefox says this is a function
228         ok( !jQuery.isFunction(obj), "Object Element" );
229
230         // IE says this is an object
231         // Since 1.3, this isn't supported (#2968)
232         //ok( jQuery.isFunction(obj.getAttribute), "getAttribute Function" );
233
234         var nodes = document.body.childNodes;
235
236         // Safari says this is a function
237         ok( !jQuery.isFunction(nodes), "childNodes Property" );
238
239         var first = document.body.firstChild;
240
241         // Normal elements are reported ok everywhere
242         ok( !jQuery.isFunction(first), "A normal DOM Element" );
243
244         var input = document.createElement("input");
245         input.type = "text";
246         document.body.appendChild( input );
247
248         // IE says this is an object
249         // Since 1.3, this isn't supported (#2968)
250         //ok( jQuery.isFunction(input.focus), "A default function property" );
251
252         document.body.removeChild( input );
253
254         var a = document.createElement("a");
255         a.href = "some-function";
256         document.body.appendChild( a );
257
258         // This serializes with the word 'function' in it
259         ok( !jQuery.isFunction(a), "Anchor Element" );
260
261         document.body.removeChild( a );
262
263         // Recursive function calls have lengths and array-like properties
264         function callme(callback){
265                 function fn(response){
266                         callback(response);
267                 }
268
269                 ok( jQuery.isFunction(fn), "Recursive Function Call" );
270
271                 fn({ some: "data" });
272         };
273
274         callme(function(){
275                 callme(function(){});
276         });
277 });
278
279 test("isXMLDoc - HTML", function() {
280         expect(4);
281
282         ok( !jQuery.isXMLDoc( document ), "HTML document" );
283         ok( !jQuery.isXMLDoc( document.documentElement ), "HTML documentElement" );
284         ok( !jQuery.isXMLDoc( document.body ), "HTML Body Element" );
285
286         var iframe = document.createElement("iframe");
287         document.body.appendChild( iframe );
288
289         try {
290                 var body = jQuery(iframe).contents()[0];
291                 ok( !jQuery.isXMLDoc( body ), "Iframe body element" );
292         } catch(e){
293                 ok( false, "Iframe body element exception" );
294         }
295
296         document.body.removeChild( iframe );
297 });
298
299 if ( !isLocal ) {
300 test("isXMLDoc - XML", function() {
301         expect(3);
302         stop();
303         jQuery.get('data/dashboard.xml', function(xml) {
304                 ok( jQuery.isXMLDoc( xml ), "XML document" );
305                 ok( jQuery.isXMLDoc( xml.documentElement ), "XML documentElement" );
306                 ok( jQuery.isXMLDoc( jQuery("tab", xml)[0] ), "XML Tab Element" );
307                 start();
308         });
309 });
310 }
311
312 test("jQuery('html')", function() {
313         expect(13);
314
315         reset();
316         jQuery.foo = false;
317         var s = jQuery("<script>jQuery.foo='test';</script>")[0];
318         ok( s, "Creating a script" );
319         ok( !jQuery.foo, "Make sure the script wasn't executed prematurely" );
320         jQuery("body").append("<script>jQuery.foo='test';</script>");
321         ok( jQuery.foo, "Executing a scripts contents in the right context" );
322
323         // Test multi-line HTML
324         var div = jQuery("<div>\r\nsome text\n<p>some p</p>\nmore text\r\n</div>")[0];
325         equals( div.nodeName.toUpperCase(), "DIV", "Make sure we're getting a div." );
326         equals( div.firstChild.nodeType, 3, "Text node." );
327         equals( div.lastChild.nodeType, 3, "Text node." );
328         equals( div.childNodes[1].nodeType, 1, "Paragraph." );
329         equals( div.childNodes[1].firstChild.nodeType, 3, "Paragraph text." );
330
331         reset();
332         ok( jQuery("<link rel='stylesheet'/>")[0], "Creating a link" );
333
334         ok( !jQuery("<script/>")[0].parentNode, "Create a script" );
335
336         ok( jQuery("<input/>").attr("type", "hidden"), "Create an input and set the type." );
337
338         var j = jQuery("<span>hi</span> there <!-- mon ami -->");
339         ok( j.length >= 2, "Check node,textnode,comment creation (some browsers delete comments)" );
340
341         ok( !jQuery("<option>test</option>")[0].selected, "Make sure that options are auto-selected #2050" );
342 });
343
344 test("jQuery('html', context)", function() {
345         expect(1);
346
347         var $div = jQuery("<div/>")[0];
348         var $span = jQuery("<span/>", $div);
349         equals($span.length, 1, "Verify a span created with a div context works, #1763");
350 });
351
352 if ( !isLocal ) {
353 test("jQuery(selector, xml).text(str) - Loaded via XML document", function() {
354         expect(2);
355         stop();
356         jQuery.get('data/dashboard.xml', function(xml) {
357                 // tests for #1419 where IE was a problem
358                 var tab = jQuery("tab", xml).eq(0);
359                 equals( tab.text(), "blabla", "Verify initial text correct" );
360                 tab.text("newtext");
361                 equals( tab.text(), "newtext", "Verify new text correct" );
362                 start();
363         });
364 });
365 }
366
367 test("length", function() {
368         expect(1);
369         equals( jQuery("p").length, 6, "Get Number of Elements Found" );
370 });
371
372 test("size()", function() {
373         expect(1);
374         equals( jQuery("p").size(), 6, "Get Number of Elements Found" );
375 });
376
377 test("get()", function() {
378         expect(1);
379         isSet( jQuery("p").get(), q("firstp","ap","sndp","en","sap","first"), "Get All Elements" );
380 });
381
382 test("toArray()", function() {
383         expect(1);
384         isSet ( jQuery("p").toArray(),
385                 q("firstp","ap","sndp","en","sap","first"),
386                 "Convert jQuery object to an Array" )
387 })
388
389 test("get(Number)", function() {
390         expect(1);
391         equals( jQuery("p").get(0), document.getElementById("firstp"), "Get A Single Element" );
392 });
393
394 test("get(-Number)",function() {
395         expect(1);
396         equals( jQuery("p").get(-1),
397                 document.getElementById("first"),
398                 "Get a single element with negative index" )
399 })
400
401 test("add(String|Element|Array|undefined)", function() {
402         expect(16);
403         isSet( jQuery("#sndp").add("#en").add("#sap").get(), q("sndp", "en", "sap"), "Check elements from document" );
404         isSet( jQuery("#sndp").add( jQuery("#en")[0] ).add( jQuery("#sap") ).get(), q("sndp", "en", "sap"), "Check elements from document" );
405         ok( jQuery([]).add(jQuery("#form")[0].elements).length >= 13, "Check elements from array" );
406
407         // For the time being, we're discontinuing support for jQuery(form.elements) since it's ambiguous in IE
408         // use jQuery([]).add(form.elements) instead.
409         //equals( jQuery([]).add(jQuery("#form")[0].elements).length, jQuery(jQuery("#form")[0].elements).length, "Array in constructor must equals array in add()" );
410
411         var tmp = jQuery("<div/>");
412
413         var x = jQuery([]).add(jQuery("<p id='x1'>xxx</p>").appendTo(tmp)).add(jQuery("<p id='x2'>xxx</p>").appendTo(tmp));
414         equals( x[0].id, "x1", "Check on-the-fly element1" );
415         equals( x[1].id, "x2", "Check on-the-fly element2" );
416
417         var x = jQuery([]).add(jQuery("<p id='x1'>xxx</p>").appendTo(tmp)[0]).add(jQuery("<p id='x2'>xxx</p>").appendTo(tmp)[0]);
418         equals( x[0].id, "x1", "Check on-the-fly element1" );
419         equals( x[1].id, "x2", "Check on-the-fly element2" );
420
421         var x = jQuery([]).add(jQuery("<p id='x1'>xxx</p>")).add(jQuery("<p id='x2'>xxx</p>"));
422         equals( x[0].id, "x1", "Check on-the-fly element1" );
423         equals( x[1].id, "x2", "Check on-the-fly element2" );
424
425         var x = jQuery([]).add("<p id='x1'>xxx</p>").add("<p id='x2'>xxx</p>");
426         equals( x[0].id, "x1", "Check on-the-fly element1" );
427         equals( x[1].id, "x2", "Check on-the-fly element2" );
428
429         var notDefined;
430         equals( jQuery([]).add(notDefined).length, 0, "Check that undefined adds nothing" );
431
432         // Added after #2811
433         equals( jQuery([]).add([window,document,document.body,document]).length, 3, "Pass an array" );
434         equals( jQuery(document).add(document).length, 1, "Check duplicated elements" );
435         equals( jQuery(window).add(window).length, 1, "Check duplicated elements using the window" );
436         ok( jQuery([]).add( document.getElementById('form') ).length >= 13, "Add a form (adds the elements)" );
437 });
438
439 test("each(Function)", function() {
440         expect(1);
441         var div = jQuery("div");
442         div.each(function(){this.foo = 'zoo';});
443         var pass = true;
444         for ( var i = 0; i < div.size(); i++ ) {
445                 if ( div.get(i).foo != "zoo" ) pass = false;
446         }
447         ok( pass, "Execute a function, Relative" );
448 });
449
450 test("index()", function() {
451         expect(1);
452
453         equals( jQuery("#text2").index(), 2, "Returns the index of a child amongst its siblings" )
454 });
455
456 test("index(Object|String|undefined)", function() {
457         expect(16);
458
459         var elements = jQuery([window, document]),
460                 inputElements = jQuery('#radio1,#radio2,#check1,#check2');
461
462         // Passing a node
463         equals( elements.index(window), 0, "Check for index of elements" );
464         equals( elements.index(document), 1, "Check for index of elements" );
465         equals( inputElements.index(document.getElementById('radio1')), 0, "Check for index of elements" );
466         equals( inputElements.index(document.getElementById('radio2')), 1, "Check for index of elements" );
467         equals( inputElements.index(document.getElementById('check1')), 2, "Check for index of elements" );
468         equals( inputElements.index(document.getElementById('check2')), 3, "Check for index of elements" );
469         equals( inputElements.index(window), -1, "Check for not found index" );
470         equals( inputElements.index(document), -1, "Check for not found index" );
471
472         // Passing a jQuery object
473         // enabled since [5500]
474         equals( elements.index( elements ), 0, "Pass in a jQuery object" );
475         equals( elements.index( elements.eq(1) ), 1, "Pass in a jQuery object" );
476         equals( jQuery("#form :radio").index( jQuery("#radio2") ), 1, "Pass in a jQuery object" );
477
478         // Passing a selector or nothing
479         // enabled since [6330]
480         equals( jQuery('#text2').index(), 2, "Check for index amongst siblings" );
481         equals( jQuery('#form').children().eq(4).index(), 4, "Check for index amongst siblings" );
482         equals( jQuery('#radio2').index('#form :radio') , 1, "Check for index within a selector" );
483         equals( jQuery('#form :radio').index( jQuery('#radio2') ), 1, "Check for index within a selector" );
484         equals( jQuery('#radio2').index('#form :text') , -1, "Check for index not found within a selector" );
485 });
486
487 test("jQuery.merge()", function() {
488         expect(6);
489
490         var parse = jQuery.merge;
491
492         same( parse([],[]), [], "Empty arrays" );
493
494         same( parse([1],[2]), [1,2], "Basic" );
495         same( parse([1,2],[3,4]), [1,2,3,4], "Basic" );
496
497         same( parse([1,2],[]), [1,2], "Second empty" );
498         same( parse([],[1,2]), [1,2], "First empty" );
499
500         // Fixed at [5998], #3641
501         same( parse([-2,-1], [0,1,2]), [-2,-1,0,1,2], "Second array including a zero (falsy)");
502 });
503
504 test("jQuery.extend(Object, Object)", function() {
505         expect(23);
506
507         var settings = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
508                 options = { xnumber2: 1, xstring2: "x", xxx: "newstring" },
509                 optionsCopy = { xnumber2: 1, xstring2: "x", xxx: "newstring" },
510                 merged = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "x", xxx: "newstring" },
511                 deep1 = { foo: { bar: true } },
512                 deep1copy = { foo: { bar: true } },
513                 deep2 = { foo: { baz: true }, foo2: document },
514                 deep2copy = { foo: { baz: true }, foo2: document },
515                 deepmerged = { foo: { bar: true, baz: true }, foo2: document };
516
517         jQuery.extend(settings, options);
518         isObj( settings, merged, "Check if extended: settings must be extended" );
519         isObj( options, optionsCopy, "Check if not modified: options must not be modified" );
520
521         jQuery.extend(settings, null, options);
522         isObj( settings, merged, "Check if extended: settings must be extended" );
523         isObj( options, optionsCopy, "Check if not modified: options must not be modified" );
524
525         jQuery.extend(true, deep1, deep2);
526         isObj( deep1.foo, deepmerged.foo, "Check if foo: settings must be extended" );
527         isObj( deep2.foo, deep2copy.foo, "Check if not deep2: options must not be modified" );
528         equals( deep1.foo2, document, "Make sure that a deep clone was not attempted on the document" );
529
530         var empty = {};
531         var optionsWithLength = { foo: { length: -1 } };
532         jQuery.extend(true, empty, optionsWithLength);
533         isObj( empty.foo, optionsWithLength.foo, "The length property must copy correctly" );
534
535         empty = {};
536         var optionsWithDate = { foo: { date: new Date } };
537         jQuery.extend(true, empty, optionsWithDate);
538         isObj( empty.foo, optionsWithDate.foo, "Dates copy correctly" );
539
540         var myKlass = function() {};
541         empty = {};
542         var optionsWithCustomObject = { foo: { date: new myKlass } };
543         jQuery.extend(true, empty, optionsWithCustomObject);
544         isObj( empty.foo, optionsWithCustomObject.foo, "Custom objects copy correctly" );
545
546         var nullUndef;
547         nullUndef = jQuery.extend({}, options, { xnumber2: null });
548         ok( nullUndef.xnumber2 === null, "Check to make sure null values are copied");
549
550         nullUndef = jQuery.extend({}, options, { xnumber2: undefined });
551         ok( nullUndef.xnumber2 === options.xnumber2, "Check to make sure undefined values are not copied");
552
553         nullUndef = jQuery.extend({}, options, { xnumber0: null });
554         ok( nullUndef.xnumber0 === null, "Check to make sure null values are inserted");
555
556         var target = {};
557         var recursive = { foo:target, bar:5 };
558         jQuery.extend(true, target, recursive);
559         isObj( target, { bar:5 }, "Check to make sure a recursive obj doesn't go never-ending loop by not copying it over" );
560
561         var ret = jQuery.extend(true, { foo: [] }, { foo: [0] } ); // 1907
562         equals( ret.foo.length, 1, "Check to make sure a value with coersion 'false' copies over when necessary to fix #1907" );
563
564         var ret = jQuery.extend(true, { foo: "1,2,3" }, { foo: [1, 2, 3] } );
565         ok( typeof ret.foo != "string", "Check to make sure values equal with coersion (but not actually equal) overwrite correctly" );
566
567         var ret = jQuery.extend(true, { foo:"bar" }, { foo:null } );
568         ok( typeof ret.foo !== 'undefined', "Make sure a null value doesn't crash with deep extend, for #1908" );
569
570         var obj = { foo:null };
571         jQuery.extend(true, obj, { foo:"notnull" } );
572         equals( obj.foo, "notnull", "Make sure a null value can be overwritten" );
573
574         function func() {}
575         jQuery.extend(func, { key: "value" } );
576         equals( func.key, "value", "Verify a function can be extended" );
577
578         var defaults = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
579                 defaultsCopy = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
580                 options1 = { xnumber2: 1, xstring2: "x" },
581                 options1Copy = { xnumber2: 1, xstring2: "x" },
582                 options2 = { xstring2: "xx", xxx: "newstringx" },
583                 options2Copy = { xstring2: "xx", xxx: "newstringx" },
584                 merged2 = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "xx", xxx: "newstringx" };
585
586         var settings = jQuery.extend({}, defaults, options1, options2);
587         isObj( settings, merged2, "Check if extended: settings must be extended" );
588         isObj( defaults, defaultsCopy, "Check if not modified: options1 must not be modified" );
589         isObj( options1, options1Copy, "Check if not modified: options1 must not be modified" );
590         isObj( options2, options2Copy, "Check if not modified: options2 must not be modified" );
591 });
592
593 test("jQuery.each(Object,Function)", function() {
594         expect(13);
595         jQuery.each( [0,1,2], function(i, n){
596                 equals( i, n, "Check array iteration" );
597         });
598
599         jQuery.each( [5,6,7], function(i, n){
600                 equals( i, n - 5, "Check array iteration" );
601         });
602
603         jQuery.each( { name: "name", lang: "lang" }, function(i, n){
604                 equals( i, n, "Check object iteration" );
605         });
606
607         var total = 0;
608         jQuery.each([1,2,3], function(i,v){ total += v; });
609         equals( total, 6, "Looping over an array" );
610         total = 0;
611         jQuery.each([1,2,3], function(i,v){ total += v; if ( i == 1 ) return false; });
612         equals( total, 3, "Looping over an array, with break" );
613         total = 0;
614         jQuery.each({"a":1,"b":2,"c":3}, function(i,v){ total += v; });
615         equals( total, 6, "Looping over an object" );
616         total = 0;
617         jQuery.each({"a":3,"b":3,"c":3}, function(i,v){ total += v; return false; });
618         equals( total, 3, "Looping over an object, with break" );
619
620         var f = function(){};
621         f.foo = 'bar';
622         jQuery.each(f, function(i){
623                 f[i] = 'baz';
624         });
625         equals( "baz", f.foo, "Loop over a function" );
626 });
627
628 test("jQuery.makeArray", function(){
629         expect(15);
630
631         equals( jQuery.makeArray(jQuery('html>*'))[0].nodeName.toUpperCase(), "HEAD", "Pass makeArray a jQuery object" );
632
633         equals( jQuery.makeArray(document.getElementsByName("PWD")).slice(0,1)[0].name, "PWD", "Pass makeArray a nodelist" );
634
635         equals( (function(){ return jQuery.makeArray(arguments); })(1,2).join(""), "12", "Pass makeArray an arguments array" );
636
637         equals( jQuery.makeArray([1,2,3]).join(""), "123", "Pass makeArray a real array" );
638
639         equals( jQuery.makeArray().length, 0, "Pass nothing to makeArray and expect an empty array" );
640
641         equals( jQuery.makeArray( 0 )[0], 0 , "Pass makeArray a number" );
642
643         equals( jQuery.makeArray( "foo" )[0], "foo", "Pass makeArray a string" );
644
645         equals( jQuery.makeArray( true )[0].constructor, Boolean, "Pass makeArray a boolean" );
646
647         equals( jQuery.makeArray( document.createElement("div") )[0].nodeName.toUpperCase(), "DIV", "Pass makeArray a single node" );
648
649         equals( jQuery.makeArray( {length:2, 0:"a", 1:"b"} ).join(""), "ab", "Pass makeArray an array like map (with length)" );
650
651         ok( !!jQuery.makeArray( document.documentElement.childNodes ).slice(0,1)[0].nodeName, "Pass makeArray a childNodes array" );
652
653         // function, is tricky as it has length
654         equals( jQuery.makeArray( function(){ return 1;} )[0](), 1, "Pass makeArray a function" );
655
656         //window, also has length
657         equals( jQuery.makeArray(window)[0], window, "Pass makeArray the window" );
658
659         equals( jQuery.makeArray(/a/)[0].constructor, RegExp, "Pass makeArray a regex" );
660
661         ok( jQuery.makeArray(document.getElementById('form')).length >= 13, "Pass makeArray a form (treat as elements)" );
662 });
663
664 test("jQuery.isEmptyObject", function(){
665         expect(2);
666         
667         equals(true, jQuery.isEmptyObject({}), "isEmptyObject on empty object literal" );
668         equals(false, jQuery.isEmptyObject({a:1}), "isEmptyObject on non-empty object literal" );
669         
670         // What about this ?
671         // equals(true, jQuery.isEmptyObject(null), "isEmptyObject on null" );
672 });