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