1 test( "$.widget.extend()", function() {
4 var ret, empty, optionsWithLength, optionsWithDate, myKlass, customObject, optionsWithCustomObject, nullUndef,
5 target, recursive, obj, input, output,
6 settings = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
7 options = { xnumber2: 1, xstring2: "x", xxx: "newstring" },
8 optionsCopy = { xnumber2: 1, xstring2: "x", xxx: "newstring" },
9 merged = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "x", xxx: "newstring" },
10 deep1 = { foo: { bar: true } },
11 deep2 = { foo: { baz: true }, foo2: document },
12 deep2copy = { foo: { baz: true }, foo2: document },
13 deepmerged = { foo: { bar: true, baz: true }, foo2: document },
15 nestedarray = { arr: arr },
16 defaults = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
17 defaultsCopy = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
18 options1 = { xnumber2: 1, xstring2: "x" },
19 options1Copy = { xnumber2: 1, xstring2: "x" },
20 options2 = { xstring2: "xx", xxx: "newstringx" },
21 options2Copy = { xstring2: "xx", xxx: "newstringx" },
22 merged2 = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "xx", xxx: "newstringx" };
24 $.widget.extend( settings, options );
25 deepEqual( settings, merged, "Check if extended: settings must be extended" );
26 deepEqual( options, optionsCopy, "Check if not modified: options must not be modified" );
28 $.widget.extend( deep1, deep2 );
29 deepEqual( deep1.foo, deepmerged.foo, "Check if foo: settings must be extended" );
30 deepEqual( deep2.foo, deep2copy.foo, "Check if not deep2: options must not be modified" );
31 equal( deep1.foo2, document, "Make sure that a deep clone was not attempted on the document" );
33 strictEqual( $.widget.extend({}, nestedarray).arr, arr, "Don't clone arrays" );
34 ok( $.isPlainObject( $.widget.extend({ arr: arr }, { arr: {} }).arr ), "Cloned object heve to be an plain object" );
37 optionsWithLength = { foo: { length: -1 } };
38 $.widget.extend( empty, optionsWithLength );
39 deepEqual( empty.foo, optionsWithLength.foo, "The length property must copy correctly" );
42 optionsWithDate = { foo: { date: new Date() } };
43 $.widget.extend( empty, optionsWithDate );
44 deepEqual( empty.foo, optionsWithDate.foo, "Dates copy correctly" );
46 myKlass = function() {};
47 customObject = new myKlass();
48 optionsWithCustomObject = { foo: { date: customObject } };
50 $.widget.extend( empty, optionsWithCustomObject );
51 strictEqual( empty.foo.date, customObject, "Custom objects copy correctly (no methods)" );
53 // Makes the class a little more realistic
54 myKlass.prototype = { someMethod: function(){} };
56 $.widget.extend( empty, optionsWithCustomObject );
57 strictEqual( empty.foo.date, customObject, "Custom objects copy correctly" );
59 ret = $.widget.extend({ foo: 4 }, { foo: Number(5) } );
60 equal( ret.foo, 5, "Wrapped numbers copy correctly" );
62 nullUndef = $.widget.extend( {}, options, { xnumber2: null } );
63 strictEqual( nullUndef.xnumber2, null, "Check to make sure null values are copied");
65 nullUndef = $.widget.extend( {}, options, { xnumber2: undefined } );
66 strictEqual( nullUndef.xnumber2, options.xnumber2, "Check to make sure undefined values are not copied");
68 nullUndef = $.widget.extend( {}, options, { xnumber0: null } );
69 strictEqual( nullUndef.xnumber0, null, "Check to make sure null values are inserted");
72 recursive = { foo:target, bar:5 };
73 $.widget.extend( target, recursive );
74 deepEqual( target, { foo: {}, bar: 5 }, "Check to make sure a recursive obj doesn't go never-ending loop by not copying it over" );
76 ret = $.widget.extend( { foo: [] }, { foo: [0] } ); // 1907
77 equal( ret.foo.length, 1, "Check to make sure a value with coersion 'false' copies over when necessary to fix #1907" );
79 ret = $.widget.extend( { foo: "1,2,3" }, { foo: [ 1, 2, 3 ] } );
80 deepEqual( ret.foo, [ 1, 2, 3 ], "Properly extend a string to array." );
82 ret = $.widget.extend( { foo: "1,2,3" }, { foo: { to: "object" } } );
83 deepEqual( ret.foo, { to: "object" }, "Properly extend a string to object." );
85 ret = $.widget.extend( { foo: "bar" }, { foo: null } );
86 strictEqual( ret.foo, null, "Make sure a null value doesn't crash with deep extend, for #1908" );
89 $.widget.extend( obj, { foo:"notnull" } );
90 equal( obj.foo, "notnull", "Make sure a null value can be overwritten" );
92 settings = $.widget.extend( {}, defaults, options1, options2 );
93 deepEqual( settings, merged2, "Check if extended: settings must be extended" );
94 deepEqual( defaults, defaultsCopy, "Check if not modified: options1 must not be modified" );
95 deepEqual( options1, options1Copy, "Check if not modified: options1 must not be modified" );
96 deepEqual( options2, options2Copy, "Check if not modified: options2 must not be modified" );
101 output = $.widget.extend( {}, input );
102 deepEqual( input, output, "don't clone arrays" );
104 deepEqual( input, output, "don't clone arrays" );