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