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