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