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