jQuery.extend(true, Object, Object) copies custom objects correctly.
[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(11);
16
17         // Basic constructor's behavior
18
19         equals( jQuery().length, 1, "jQuery() === jQuery(document)" );
20         equals( jQuery(undefined).length, 0, "jQuery(undefined) === jQuery([])" );
21         equals( jQuery(null).length, 0, "jQuery(null) === jQuery([])" );
22         equals( jQuery("").length, 0, "jQuery('') === jQuery([])" );
23
24                 // can actually yield more than one, when iframes are included, the window is an array as well
25         equals( 1, jQuery(window).length, "Correct number of elements generated for jQuery(window)" );
26
27
28         var main = jQuery("#main");
29         isSet( jQuery("div p", main).get(), q("sndp", "en", "sap"), "Basic selector with jQuery object as context" );
30
31 /*
32         // disabled since this test was doing nothing. i tried to fix it but i'm not sure
33         // what the expected behavior should even be. FF returns "\n" for the text node
34         // make sure this is handled
35         var crlfContainer = jQuery('<p>\r\n</p>');
36         var x = crlfContainer.contents().get(0).nodeValue;
37         equals( x, what???, "Check for \\r and \\n in jQuery()" );
38 */
39
40         /* // Disabled until we add this functionality in
41         var pass = true;
42         try {
43                 jQuery("<div>Testing</div>").appendTo(document.getElementById("iframe").contentDocument.body);
44         } catch(e){
45                 pass = false;
46         }
47         ok( pass, "jQuery('&lt;tag&gt;') needs optional document parameter to ease cross-frame DOM wrangling, see #968" );*/
48
49         var code = jQuery("<code/>");
50         equals( code.length, 1, "Correct number of elements generated for code" );
51         var img = jQuery("<img/>");
52         equals( img.length, 1, "Correct number of elements generated for img" );
53         var div = jQuery("<div/><hr/><code/><b/>");
54         equals( div.length, 4, "Correct number of elements generated for div hr code b" );
55
56         equals( jQuery([1,2,3]).get(1), 2, "Test passing an array to the factory" );
57
58         equals( jQuery(document.body).get(0), jQuery('body').get(0), "Test passing an html node to the factory" );
59 });
60
61 test("selector state", function() {
62         expect(30);
63
64         var test;
65
66         test = jQuery(undefined);
67         equals( test.selector, "", "Empty jQuery Selector" );
68         equals( test.context, undefined, "Empty jQuery Context" );
69
70         test = jQuery(document);
71         equals( test.selector, "", "Document Selector" );
72         equals( test.context, document, "Document Context" );
73
74         test = jQuery(document.body);
75         equals( test.selector, "", "Body Selector" );
76         equals( test.context, document.body, "Body Context" );
77
78         test = jQuery("#main");
79         equals( test.selector, "#main", "#main Selector" );
80         equals( test.context, document, "#main Context" );
81
82         test = jQuery("#notfoundnono");
83         equals( test.selector, "#notfoundnono", "#notfoundnono Selector" );
84         equals( test.context, document, "#notfoundnono Context" );
85
86         test = jQuery("#main", document);
87         equals( test.selector, "#main", "#main Selector" );
88         equals( test.context, document, "#main Context" );
89
90         test = jQuery("#main", document.body);
91         equals( test.selector, "#main", "#main Selector" );
92         equals( test.context, document.body, "#main Context" );
93
94         // Test cloning
95         test = jQuery(test);
96         equals( test.selector, "#main", "#main Selector" );
97         equals( test.context, document.body, "#main Context" );
98
99         test = jQuery(document.body).find("#main");
100         equals( test.selector, "#main", "#main find Selector" );
101         equals( test.context, document.body, "#main find Context" );
102
103         test = jQuery("#main").filter("div");
104         equals( test.selector, "#main.filter(div)", "#main filter Selector" );
105         equals( test.context, document, "#main filter Context" );
106
107         test = jQuery("#main").not("div");
108         equals( test.selector, "#main.not(div)", "#main not Selector" );
109         equals( test.context, document, "#main not Context" );
110
111         test = jQuery("#main").filter("div").not("div");
112         equals( test.selector, "#main.filter(div).not(div)", "#main filter, not Selector" );
113         equals( test.context, document, "#main filter, not Context" );
114
115         test = jQuery("#main").filter("div").not("div").end();
116         equals( test.selector, "#main.filter(div)", "#main filter, not, end Selector" );
117         equals( test.context, document, "#main filter, not, end Context" );
118
119         test = jQuery("#main").parent("body");
120         equals( test.selector, "#main.parent(body)", "#main parent Selector" );
121         equals( test.context, document, "#main parent Context" );
122
123         test = jQuery("#main").eq(0);
124         equals( test.selector, "#main.slice(0,1)", "#main eq Selector" );
125         equals( test.context, document, "#main eq Context" );
126 });
127
128 test("browser", function() {
129         expect(13);
130         var browsers = {
131                 //Internet Explorer
132                 "Mozilla/5.0 (Windows; U; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)": "6.0",
133                 "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",
134                 /** Failing #1876
135                  * "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",
136                  */
137                 //Browsers with Gecko engine
138                 //Mozilla
139                 "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.12) Gecko/20050915" : "1.7.12",
140                 //Firefox
141                 "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",
142                 //Netscape
143                 "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20070321 Netscape/8.1.3" : "1.7.5",
144                 //Flock
145                 "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",
146                 //Opera browser
147                 "Opera/9.20 (X11; Linux x86_64; U; en)": "9.20",
148                 "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; en) Opera 9.20" : "9.20",
149                 "Mozilla/5.0 (Windows NT 5.1; U; pl; rv:1.8.0) Gecko/20060728 Firefox/1.5.0 Opera 9.20": "9.20",
150                 //WebKit engine
151                 "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; sv-se) AppleWebKit/418.9 (KHTML, like Gecko) Safari/419.3": "418.9",
152                 "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/418.8 (KHTML, like Gecko) Safari/419.3" : "418.8",
153                 "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; sv-se) AppleWebKit/312.8 (KHTML, like Gecko) Safari/312.5": "312.8",
154                 //Other user agent string
155                 "Other browser's user agent 1.0":null
156         };
157         for (var i in browsers) {
158                 var v = i.toLowerCase().match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ); // RegEx from Core jQuery.browser.version check
159                 var version = v ? v[1] : null;
160                 equals( version, browsers[i], "Checking UA string" );
161         }
162 });
163
164 test("noConflict", function() {
165         expect(6);
166
167         var $$ = jQuery;
168
169         equals( jQuery, jQuery.noConflict(), "noConflict returned the jQuery object" );
170         equals( jQuery, $$, "Make sure jQuery wasn't touched." );
171         equals( $, original$, "Make sure $ was reverted." );
172
173         jQuery = $ = $$;
174
175         equals( jQuery.noConflict(true), $$, "noConflict returned the jQuery object" );
176         equals( jQuery, originaljQuery, "Make sure jQuery was reverted." );
177         equals( $, original$, "Make sure $ was reverted." );
178
179         jQuery = $$;
180 });
181
182 test("isFunction", function() {
183         expect(19);
184
185         // Make sure that false values return false
186         ok( !jQuery.isFunction(), "No Value" );
187         ok( !jQuery.isFunction( null ), "null Value" );
188         ok( !jQuery.isFunction( undefined ), "undefined Value" );
189         ok( !jQuery.isFunction( "" ), "Empty String Value" );
190         ok( !jQuery.isFunction( 0 ), "0 Value" );
191
192         // Check built-ins
193         // Safari uses "(Internal Function)"
194         ok( jQuery.isFunction(String), "String Function("+String+")" );
195         ok( jQuery.isFunction(Array), "Array Function("+Array+")" );
196         ok( jQuery.isFunction(Object), "Object Function("+Object+")" );
197         ok( jQuery.isFunction(Function), "Function Function("+Function+")" );
198
199         // When stringified, this could be misinterpreted
200         var mystr = "function";
201         ok( !jQuery.isFunction(mystr), "Function String" );
202
203         // When stringified, this could be misinterpreted
204         var myarr = [ "function" ];
205         ok( !jQuery.isFunction(myarr), "Function Array" );
206
207         // When stringified, this could be misinterpreted
208         var myfunction = { "function": "test" };
209         ok( !jQuery.isFunction(myfunction), "Function Object" );
210
211         // Make sure normal functions still work
212         var fn = function(){};
213         ok( jQuery.isFunction(fn), "Normal Function" );
214
215         var obj = document.createElement("object");
216
217         // Firefox says this is a function
218         ok( !jQuery.isFunction(obj), "Object Element" );
219
220         // IE says this is an object
221         // Since 1.3, this isn't supported (#2968)
222         //ok( jQuery.isFunction(obj.getAttribute), "getAttribute Function" );
223
224         var nodes = document.body.childNodes;
225
226         // Safari says this is a function
227         ok( !jQuery.isFunction(nodes), "childNodes Property" );
228
229         var first = document.body.firstChild;
230
231         // Normal elements are reported ok everywhere
232         ok( !jQuery.isFunction(first), "A normal DOM Element" );
233
234         var input = document.createElement("input");
235         input.type = "text";
236         document.body.appendChild( input );
237
238         // IE says this is an object
239         // Since 1.3, this isn't supported (#2968)
240         //ok( jQuery.isFunction(input.focus), "A default function property" );
241
242         document.body.removeChild( input );
243
244         var a = document.createElement("a");
245         a.href = "some-function";
246         document.body.appendChild( a );
247
248         // This serializes with the word 'function' in it
249         ok( !jQuery.isFunction(a), "Anchor Element" );
250
251         document.body.removeChild( a );
252
253         // Recursive function calls have lengths and array-like properties
254         function callme(callback){
255                 function fn(response){
256                         callback(response);
257                 }
258
259                 ok( jQuery.isFunction(fn), "Recursive Function Call" );
260
261                 fn({ some: "data" });
262         };
263
264         callme(function(){
265                 callme(function(){});
266         });
267 });
268
269 test("jQuery('html')", function() {
270         expect(8);
271
272         reset();
273         jQuery.foo = false;
274         var s = jQuery("<script>jQuery.foo='test';</script>")[0];
275         ok( s, "Creating a script" );
276         ok( !jQuery.foo, "Make sure the script wasn't executed prematurely" );
277         jQuery("body").append("<script>jQuery.foo='test';</script>");
278         ok( jQuery.foo, "Executing a scripts contents in the right context" );
279
280         reset();
281         ok( jQuery("<link rel='stylesheet'/>")[0], "Creating a link" );
282
283         ok( !jQuery("<script/>")[0].parentNode, "Create a script" );
284
285         ok( jQuery("<input/>").attr("type", "hidden"), "Create an input and set the type." );
286
287         var j = jQuery("<span>hi</span> there <!-- mon ami -->");
288         ok( j.length >= 2, "Check node,textnode,comment creation (some browsers delete comments)" );
289
290         ok( !jQuery("<option>test</option>")[0].selected, "Make sure that options are auto-selected #2050" );
291 });
292
293 test("jQuery('html', context)", function() {
294         expect(1);
295
296         var $div = jQuery("<div/>");
297         var $span = jQuery("<span/>", $div);
298         equals($span.length, 1, "Verify a span created with a div context works, #1763");
299 });
300
301 if ( !isLocal ) {
302 test("jQuery(selector, xml).text(str) - Loaded via XML document", function() {
303         expect(2);
304         stop();
305         jQuery.get('data/dashboard.xml', function(xml) {
306                 // tests for #1419 where IE was a problem
307                 var tab = jQuery("tab", xml).eq(0);
308                 equals( tab.text(), "blabla", "Verify initial text correct" );
309                 tab.text("newtext");
310                 equals( tab.text(), "newtext", "Verify new text correct" );
311                 start();
312         });
313 });
314 }
315
316 test("length", function() {
317         expect(1);
318         equals( jQuery("p").length, 6, "Get Number of Elements Found" );
319 });
320
321 test("size()", function() {
322         expect(1);
323         equals( jQuery("p").size(), 6, "Get Number of Elements Found" );
324 });
325
326 test("get()", function() {
327         expect(1);
328         isSet( jQuery("p").get(), q("firstp","ap","sndp","en","sap","first"), "Get All Elements" );
329 });
330
331 test("toArray()", function() {
332         expect(1);
333         isSet ( jQuery("p").toArray(),
334                 q("firstp","ap","sndp","en","sap","first"),
335                 "Convert jQuery object to an Array" )
336 })
337
338 test("get(Number)", function() {
339         expect(1);
340         equals( jQuery("p").get(0), document.getElementById("firstp"), "Get A Single Element" );
341 });
342
343 test("get(-Number)",function() {
344         expect(1);
345         equals( jQuery("p").get(-1),
346                 document.getElementById("first"),
347                 "Get a single element with negative index" )
348 })
349
350 test("add(String|Element|Array|undefined)", function() {
351         expect(12);
352         isSet( jQuery("#sndp").add("#en").add("#sap").get(), q("sndp", "en", "sap"), "Check elements from document" );
353         isSet( jQuery("#sndp").add( jQuery("#en")[0] ).add( jQuery("#sap") ).get(), q("sndp", "en", "sap"), "Check elements from document" );
354         ok( jQuery([]).add(jQuery("#form")[0].elements).length >= 13, "Check elements from array" );
355
356         // For the time being, we're discontinuing support for jQuery(form.elements) since it's ambiguous in IE
357         // use jQuery([]).add(form.elements) instead.
358         //equals( jQuery([]).add(jQuery("#form")[0].elements).length, jQuery(jQuery("#form")[0].elements).length, "Array in constructor must equals array in add()" );
359
360         var x = jQuery([]).add(jQuery("<p id='x1'>xxx</p>")).add(jQuery("<p id='x2'>xxx</p>"));
361         equals( x[0].id, "x1", "Check on-the-fly element1" );
362         equals( x[1].id, "x2", "Check on-the-fly element2" );
363
364         var x = jQuery([]).add("<p id='x1'>xxx</p>").add("<p id='x2'>xxx</p>");
365         equals( x[0].id, "x1", "Check on-the-fly element1" );
366         equals( x[1].id, "x2", "Check on-the-fly element2" );
367
368         var notDefined;
369         equals( jQuery([]).add(notDefined).length, 0, "Check that undefined adds nothing" );
370
371         // Added after #2811
372         equals( jQuery([]).add([window,document,document.body,document]).length, 3, "Pass an array" );
373         equals( jQuery(document).add(document).length, 1, "Check duplicated elements" );
374         equals( jQuery(window).add(window).length, 1, "Check duplicated elements using the window" );
375         ok( jQuery([]).add( document.getElementById('form') ).length >= 13, "Add a form (adds the elements)" );
376 });
377
378 test("each(Function)", function() {
379         expect(1);
380         var div = jQuery("div");
381         div.each(function(){this.foo = 'zoo';});
382         var pass = true;
383         for ( var i = 0; i < div.size(); i++ ) {
384                 if ( div.get(i).foo != "zoo" ) pass = false;
385         }
386         ok( pass, "Execute a function, Relative" );
387 });
388
389 test("index(Object|String|undefined)", function() {
390         expect(16);
391
392         var elements = jQuery([window, document]),
393                 inputElements = jQuery('#radio1,#radio2,#check1,#check2');
394
395         // Passing a node
396         equals( elements.index(window), 0, "Check for index of elements" );
397         equals( elements.index(document), 1, "Check for index of elements" );
398         equals( inputElements.index(document.getElementById('radio1')), 0, "Check for index of elements" );
399         equals( inputElements.index(document.getElementById('radio2')), 1, "Check for index of elements" );
400         equals( inputElements.index(document.getElementById('check1')), 2, "Check for index of elements" );
401         equals( inputElements.index(document.getElementById('check2')), 3, "Check for index of elements" );
402         equals( inputElements.index(window), -1, "Check for not found index" );
403         equals( inputElements.index(document), -1, "Check for not found index" );
404
405         // Passing a jQuery object
406         // enabled since [5500]
407         equals( elements.index( elements ), 0, "Pass in a jQuery object" );
408         equals( elements.index( elements.eq(1) ), 1, "Pass in a jQuery object" );
409         equals( jQuery("#form :radio").index( jQuery("#radio2") ), 1, "Pass in a jQuery object" );
410
411         // Passing a selector or nothing
412         // enabled since [6330]
413         equals( jQuery('#text2').index(), 2, "Check for index amongst siblings" );
414         equals( jQuery('#form').children().eq(4).index(), 4, "Check for index amongst siblings" );
415         equals( jQuery('#radio2').index('#form :radio') , 1, "Check for index within a selector" );
416         equals( jQuery('#form :radio').index( jQuery('#radio2') ), 1, "Check for index within a selector" );
417         equals( jQuery('#radio2').index('#form :text') , -1, "Check for index not found within a selector" );
418 });
419
420 test("jQuery.merge()", function() {
421         expect(6);
422
423         var parse = jQuery.merge;
424
425         same( parse([],[]), [], "Empty arrays" );
426
427         same( parse([1],[2]), [1,2], "Basic" );
428         same( parse([1,2],[3,4]), [1,2,3,4], "Basic" );
429
430         same( parse([1,2],[]), [1,2], "Second empty" );
431         same( parse([],[1,2]), [1,2], "First empty" );
432
433         // Fixed at [5998], #3641
434         same( parse([-2,-1], [0,1,2]), [-2,-1,0,1,2], "Second array including a zero (falsy)");
435 });
436
437 test("jQuery.extend(Object, Object)", function() {
438         expect(23);
439
440         var settings = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
441                 options = { xnumber2: 1, xstring2: "x", xxx: "newstring" },
442                 optionsCopy = { xnumber2: 1, xstring2: "x", xxx: "newstring" },
443                 merged = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "x", xxx: "newstring" },
444                 deep1 = { foo: { bar: true } },
445                 deep1copy = { foo: { bar: true } },
446                 deep2 = { foo: { baz: true }, foo2: document },
447                 deep2copy = { foo: { baz: true }, foo2: document },
448                 deepmerged = { foo: { bar: true, baz: true }, foo2: document };
449
450         jQuery.extend(settings, options);
451         isObj( settings, merged, "Check if extended: settings must be extended" );
452         isObj( options, optionsCopy, "Check if not modified: options must not be modified" );
453
454         jQuery.extend(settings, null, options);
455         isObj( settings, merged, "Check if extended: settings must be extended" );
456         isObj( options, optionsCopy, "Check if not modified: options must not be modified" );
457
458         jQuery.extend(true, deep1, deep2);
459         isObj( deep1.foo, deepmerged.foo, "Check if foo: settings must be extended" );
460         isObj( deep2.foo, deep2copy.foo, "Check if not deep2: options must not be modified" );
461         equals( deep1.foo2, document, "Make sure that a deep clone was not attempted on the document" );
462
463         var empty = {};
464         var optionsWithLength = { foo: { length: -1 } };
465         jQuery.extend(true, empty, optionsWithLength);
466         isObj( empty.foo, optionsWithLength.foo, "The length property must copy correctly" );
467
468         empty = {};
469         var optionsWithDate = { foo: { date: new Date } };
470         jQuery.extend(true, empty, optionsWithDate);
471         isObj( empty.foo, optionsWithDate.foo, "Dates copy correctly" );
472
473         var myKlass = function() {};
474         empty = {};
475         var optionsWithCustomObject = { foo: { date: new myKlass } };
476         jQuery.extend(true, empty, optionsWithCustomObject);
477         isObj( empty.foo, optionsWithCustomObject.foo, "Custom objects copy correctly" );
478
479         var nullUndef;
480         nullUndef = jQuery.extend({}, options, { xnumber2: null });
481         ok( nullUndef.xnumber2 === null, "Check to make sure null values are copied");
482
483         nullUndef = jQuery.extend({}, options, { xnumber2: undefined });
484         ok( nullUndef.xnumber2 === options.xnumber2, "Check to make sure undefined values are not copied");
485
486         nullUndef = jQuery.extend({}, options, { xnumber0: null });
487         ok( nullUndef.xnumber0 === null, "Check to make sure null values are inserted");
488
489         var target = {};
490         var recursive = { foo:target, bar:5 };
491         jQuery.extend(true, target, recursive);
492         isObj( target, { bar:5 }, "Check to make sure a recursive obj doesn't go never-ending loop by not copying it over" );
493
494         var ret = jQuery.extend(true, { foo: [] }, { foo: [0] } ); // 1907
495         equals( ret.foo.length, 1, "Check to make sure a value with coersion 'false' copies over when necessary to fix #1907" );
496
497         var ret = jQuery.extend(true, { foo: "1,2,3" }, { foo: [1, 2, 3] } );
498         ok( typeof ret.foo != "string", "Check to make sure values equal with coersion (but not actually equal) overwrite correctly" );
499
500         var ret = jQuery.extend(true, { foo:"bar" }, { foo:null } );
501         ok( typeof ret.foo !== 'undefined', "Make sure a null value doesn't crash with deep extend, for #1908" );
502
503         var obj = { foo:null };
504         jQuery.extend(true, obj, { foo:"notnull" } );
505         equals( obj.foo, "notnull", "Make sure a null value can be overwritten" );
506
507         function func() {}
508         jQuery.extend(func, { key: "value" } );
509         equals( func.key, "value", "Verify a function can be extended" );
510
511         var defaults = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
512                 defaultsCopy = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
513                 options1 = { xnumber2: 1, xstring2: "x" },
514                 options1Copy = { xnumber2: 1, xstring2: "x" },
515                 options2 = { xstring2: "xx", xxx: "newstringx" },
516                 options2Copy = { xstring2: "xx", xxx: "newstringx" },
517                 merged2 = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "xx", xxx: "newstringx" };
518
519         var settings = jQuery.extend({}, defaults, options1, options2);
520         isObj( settings, merged2, "Check if extended: settings must be extended" );
521         isObj( defaults, defaultsCopy, "Check if not modified: options1 must not be modified" );
522         isObj( options1, options1Copy, "Check if not modified: options1 must not be modified" );
523         isObj( options2, options2Copy, "Check if not modified: options2 must not be modified" );
524 });
525
526 test("jQuery.each(Object,Function)", function() {
527         expect(13);
528         jQuery.each( [0,1,2], function(i, n){
529                 equals( i, n, "Check array iteration" );
530         });
531
532         jQuery.each( [5,6,7], function(i, n){
533                 equals( i, n - 5, "Check array iteration" );
534         });
535
536         jQuery.each( { name: "name", lang: "lang" }, function(i, n){
537                 equals( i, n, "Check object iteration" );
538         });
539
540         var total = 0;
541         jQuery.each([1,2,3], function(i,v){ total += v; });
542         equals( total, 6, "Looping over an array" );
543         total = 0;
544         jQuery.each([1,2,3], function(i,v){ total += v; if ( i == 1 ) return false; });
545         equals( total, 3, "Looping over an array, with break" );
546         total = 0;
547         jQuery.each({"a":1,"b":2,"c":3}, function(i,v){ total += v; });
548         equals( total, 6, "Looping over an object" );
549         total = 0;
550         jQuery.each({"a":3,"b":3,"c":3}, function(i,v){ total += v; return false; });
551         equals( total, 3, "Looping over an object, with break" );
552
553         var f = function(){};
554         f.foo = 'bar';
555         jQuery.each(f, function(i){
556                 f[i] = 'baz';
557         });
558         equals( "baz", f.foo, "Loop over a function" );
559 });
560
561 test("jQuery.makeArray", function(){
562         expect(15);
563
564         equals( jQuery.makeArray(jQuery('html>*'))[0].nodeName.toUpperCase(), "HEAD", "Pass makeArray a jQuery object" );
565
566         equals( jQuery.makeArray(document.getElementsByName("PWD")).slice(0,1)[0].name, "PWD", "Pass makeArray a nodelist" );
567
568         equals( (function(){ return jQuery.makeArray(arguments); })(1,2).join(""), "12", "Pass makeArray an arguments array" );
569
570         equals( jQuery.makeArray([1,2,3]).join(""), "123", "Pass makeArray a real array" );
571
572         equals( jQuery.makeArray().length, 0, "Pass nothing to makeArray and expect an empty array" );
573
574         equals( jQuery.makeArray( 0 )[0], 0 , "Pass makeArray a number" );
575
576         equals( jQuery.makeArray( "foo" )[0], "foo", "Pass makeArray a string" );
577
578         equals( jQuery.makeArray( true )[0].constructor, Boolean, "Pass makeArray a boolean" );
579
580         equals( jQuery.makeArray( document.createElement("div") )[0].nodeName.toUpperCase(), "DIV", "Pass makeArray a single node" );
581
582         equals( jQuery.makeArray( {length:2, 0:"a", 1:"b"} ).join(""), "ab", "Pass makeArray an array like map (with length)" );
583
584         ok( !!jQuery.makeArray( document.documentElement.childNodes ).slice(0,1)[0].nodeName, "Pass makeArray a childNodes array" );
585
586         // function, is tricky as it has length
587         equals( jQuery.makeArray( function(){ return 1;} )[0](), 1, "Pass makeArray a function" );
588
589         //window, also has length
590         equals( jQuery.makeArray(window)[0], window, "Pass makeArray the window" );
591
592         equals( jQuery.makeArray(/a/)[0].constructor, RegExp, "Pass makeArray a regex" );
593
594         ok( jQuery.makeArray(document.getElementById('form')).length >= 13, "Pass makeArray a form (treat as elements)" );
595 });