From 8b0670733640c20680bdebdf2a356e8f7f4351ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn=20Zaefferer?= Date: Wed, 27 Apr 2011 23:51:56 +0200 Subject: [PATCH 1/4] See if we can replace deep cloning with something more sane. Keeps semantics mostly the same, but without cloning anything but plain objects, e.g. not cloning of arrays --- tests/unit/widget/extend2.js | 113 ++++++++++++++++++++++++++++++++++ tests/unit/widget/widget.html | 1 + ui/jquery.ui.widget.js | 30 +++++++-- 3 files changed, 138 insertions(+), 6 deletions(-) create mode 100644 tests/unit/widget/extend2.js diff --git a/tests/unit/widget/extend2.js b/tests/unit/widget/extend2.js new file mode 100644 index 00000000000..b673b7228b2 --- /dev/null +++ b/tests/unit/widget/extend2.js @@ -0,0 +1,113 @@ +test("jQuery.extend(Object, Object)", function() { + expect(28); + + var settings = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" }, + options = { xnumber2: 1, xstring2: "x", xxx: "newstring" }, + optionsCopy = { xnumber2: 1, xstring2: "x", xxx: "newstring" }, + merged = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "x", xxx: "newstring" }, + deep1 = { foo: { bar: true } }, + deep1copy = { foo: { bar: true } }, + deep2 = { foo: { baz: true }, foo2: document }, + deep2copy = { foo: { baz: true }, foo2: document }, + deepmerged = { foo: { bar: true, baz: true }, foo2: document }, + arr = [1, 2, 3], + nestedarray = { arr: arr }; + + jQuery.extend2(settings, options); + same( settings, merged, "Check if extended: settings must be extended" ); + same( options, optionsCopy, "Check if not modified: options must not be modified" ); + jQuery.extend2(settings, null, options); + same( settings, merged, "Check if extended: settings must be extended" ); + same( options, optionsCopy, "Check if not modified: options must not be modified" ); + + jQuery.extend2(deep1, deep2); + same( deep1.foo, deepmerged.foo, "Check if foo: settings must be extended" ); + same( deep2.foo, deep2copy.foo, "Check if not deep2: options must not be modified" ); + equals( deep1.foo2, document, "Make sure that a deep clone was not attempted on the document" ); + + ok( jQuery.extend2({}, nestedarray).arr === arr, "Don't clone arrays" ); + ok( jQuery.isPlainObject( jQuery.extend2({ arr: arr }, { arr: {} }).arr ), "Cloned object heve to be an plain object" ); + + var empty = {}; + var optionsWithLength = { foo: { length: -1 } }; + jQuery.extend2(empty, optionsWithLength); + same( empty.foo, optionsWithLength.foo, "The length property must copy correctly" ); + + empty = {}; + var optionsWithDate = { foo: { date: new Date } }; + jQuery.extend2(empty, optionsWithDate); + same( empty.foo, optionsWithDate.foo, "Dates copy correctly" ); + + var myKlass = function() {}; + var customObject = new myKlass(); + var optionsWithCustomObject = { foo: { date: customObject } }; + empty = {}; + jQuery.extend2(empty, optionsWithCustomObject); + ok( empty.foo && empty.foo.date === customObject, "Custom objects copy correctly (no methods)" ); + + // Makes the class a little more realistic + myKlass.prototype = { someMethod: function(){} }; + empty = {}; + jQuery.extend2(empty, optionsWithCustomObject); + ok( empty.foo && empty.foo.date === customObject, "Custom objects copy correctly" ); + + var ret = jQuery.extend2({ foo: 4 }, { foo: new Number(5) } ); + ok( ret.foo == 5, "Wrapped numbers copy correctly" ); + + var nullUndef; + nullUndef = jQuery.extend2({}, options, { xnumber2: null }); + ok( nullUndef.xnumber2 === null, "Check to make sure null values are copied"); + + nullUndef = jQuery.extend2({}, options, { xnumber2: undefined }); + ok( nullUndef.xnumber2 === options.xnumber2, "Check to make sure undefined values are not copied"); + + nullUndef = jQuery.extend2({}, options, { xnumber0: null }); + ok( nullUndef.xnumber0 === null, "Check to make sure null values are inserted"); + + // TODO weird test + /* + var target = {}; + var recursive = { foo:target, bar:5 }; + jQuery.extend2(target, recursive); + same( target, { bar:5 }, "Check to make sure a recursive obj doesn't go never-ending loop by not copying it over" ); + */ + + var ret = jQuery.extend(true, { foo: [] }, { foo: [0] } ); // 1907 + equals( ret.foo.length, 1, "Check to make sure a value with coersion 'false' copies over when necessary to fix #1907" ); + + var ret = jQuery.extend(true, { foo: "1,2,3" }, { foo: [1, 2, 3] } ); + ok( typeof ret.foo != "string", "Check to make sure values equal with coersion (but not actually equal) overwrite correctly" ); + + var ret = jQuery.extend(true, { foo:"bar" }, { foo:null } ); + ok( typeof ret.foo !== "undefined", "Make sure a null value doesn't crash with deep extend, for #1908" ); + + var obj = { foo:null }; + jQuery.extend(true, obj, { foo:"notnull" } ); + equals( obj.foo, "notnull", "Make sure a null value can be overwritten" ); + + function func() {} + jQuery.extend(func, { key: "value" } ); + equals( func.key, "value", "Verify a function can be extended" ); + + var defaults = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" }, + defaultsCopy = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" }, + options1 = { xnumber2: 1, xstring2: "x" }, + options1Copy = { xnumber2: 1, xstring2: "x" }, + options2 = { xstring2: "xx", xxx: "newstringx" }, + options2Copy = { xstring2: "xx", xxx: "newstringx" }, + merged2 = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "xx", xxx: "newstringx" }; + + var settings = jQuery.extend({}, defaults, options1, options2); + same( settings, merged2, "Check if extended: settings must be extended" ); + same( defaults, defaultsCopy, "Check if not modified: options1 must not be modified" ); + same( options1, options1Copy, "Check if not modified: options1 must not be modified" ); + same( options2, options2Copy, "Check if not modified: options2 must not be modified" ); + + var input = { + key: [ 1, 2, 3 ] + } + var output = jQuery.extend2( {}, input ); + deepEqual( input, output, "don't clone arrays" ); + input.key[0] = 10; + deepEqual( input, output, "don't clone arrays" ); +}); diff --git a/tests/unit/widget/widget.html b/tests/unit/widget/widget.html index e74abb31703..773761b41c7 100644 --- a/tests/unit/widget/widget.html +++ b/tests/unit/widget/widget.html @@ -14,6 +14,7 @@ + diff --git a/ui/jquery.ui.widget.js b/ui/jquery.ui.widget.js index 55b9f7984e5..809bc9d9033 100644 --- a/ui/jquery.ui.widget.js +++ b/ui/jquery.ui.widget.js @@ -11,6 +11,24 @@ var slice = Array.prototype.slice; +function extend( target ) { + var input = slice.call( arguments, 1 ), + inputIndex = 0, + inputLength = input.length, + key, + value; + for ( ; inputIndex < inputLength; inputIndex++ ) { + for ( key in input[ inputIndex ] ) { + value = input[ inputIndex ][ key ]; + if (input[ inputIndex ].hasOwnProperty( key ) && value !== undefined ) { + target[ key ] = $.isPlainObject( value ) ? extend( {}, target[ key ], value ) : value; + } + } + } + return target; +} +$.extend2 = extend; + var _cleanData = $.cleanData; $.cleanData = function( elems ) { for ( var i = 0, elem; (elem = elems[i]) != null; i++ ) { @@ -55,7 +73,7 @@ $.widget = function( name, base, prototype ) { // we need to make the options hash a property directly on the new instance // otherwise we'll modify the options hash on the prototype that we're // inheriting from - basePrototype.options = $.extend( true, {}, basePrototype.options ); + basePrototype.options = extend( {}, basePrototype.options ); $.each( prototype, function( prop, value ) { if ( $.isFunction( value ) ) { prototype[ prop ] = (function() { @@ -83,7 +101,7 @@ $.widget = function( name, base, prototype ) { }()); } }); - $[ namespace ][ name ].prototype = $.extend( true, basePrototype, { + $[ namespace ][ name ].prototype = extend( basePrototype, { namespace: namespace, widgetName: name, widgetEventPrefix: name, @@ -101,7 +119,7 @@ $.widget.bridge = function( name, object ) { // allow multiple hashes to be passed on init options = !isMethodCall && args.length ? - $.extend.apply( null, [ true, options ].concat(args) ) : + extend.apply( null, [ options ].concat(args) ) : options; if ( isMethodCall ) { @@ -163,7 +181,7 @@ $.Widget.prototype = { _createWidget: function( options, element ) { element = $( element || this.defaultElement || this )[ 0 ]; this.element = $( element ); - this.options = $.extend( true, {}, + this.options = extend( {}, this.options, this._getCreateOptions(), options ); @@ -218,7 +236,7 @@ $.Widget.prototype = { if ( arguments.length === 0 ) { // don't return a reference to the internal hash - return $.extend( {}, this.options ); + return extend( {}, this.options ); } if ( typeof key === "string" ) { @@ -230,7 +248,7 @@ $.Widget.prototype = { parts = key.split( "." ); key = parts.shift(); if ( parts.length ) { - curOption = options[ key ] = $.extend( true, {}, this.options[ key ] ); + curOption = options[ key ] = extend( {}, this.options[ key ] ); for ( i = 0; i < parts.length - 1; i++ ) { curOption[ parts[ i ] ] = curOption[ parts[ i ] ] || {}; curOption = curOption[ parts[ i ] ]; From 6411cdf9667e4165bdd206db6b31619dac4dbfa4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn=20Zaefferer?= Date: Mon, 2 May 2011 13:30:57 +0200 Subject: [PATCH 2/4] Rename $.extend2 to $.widget.extend --- tests/unit/widget/widget.html | 2 +- .../widget/{extend2.js => widget_extend.js} | 32 ++++++------ ui/jquery.ui.widget.js | 49 +++++++++---------- 3 files changed, 41 insertions(+), 42 deletions(-) rename tests/unit/widget/{extend2.js => widget_extend.js} (82%) diff --git a/tests/unit/widget/widget.html b/tests/unit/widget/widget.html index 773761b41c7..1835376122f 100644 --- a/tests/unit/widget/widget.html +++ b/tests/unit/widget/widget.html @@ -14,7 +14,7 @@ - + diff --git a/tests/unit/widget/extend2.js b/tests/unit/widget/widget_extend.js similarity index 82% rename from tests/unit/widget/extend2.js rename to tests/unit/widget/widget_extend.js index b673b7228b2..a99a336d3f8 100644 --- a/tests/unit/widget/extend2.js +++ b/tests/unit/widget/widget_extend.js @@ -1,4 +1,4 @@ -test("jQuery.extend(Object, Object)", function() { +test("$.widget.extend(Object, Object)", function() { expect(28); var settings = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" }, @@ -13,62 +13,62 @@ test("jQuery.extend(Object, Object)", function() { arr = [1, 2, 3], nestedarray = { arr: arr }; - jQuery.extend2(settings, options); + $.widget.extend(settings, options); same( settings, merged, "Check if extended: settings must be extended" ); same( options, optionsCopy, "Check if not modified: options must not be modified" ); - jQuery.extend2(settings, null, options); + $.widget.extend(settings, null, options); same( settings, merged, "Check if extended: settings must be extended" ); same( options, optionsCopy, "Check if not modified: options must not be modified" ); - jQuery.extend2(deep1, deep2); + $.widget.extend(deep1, deep2); same( deep1.foo, deepmerged.foo, "Check if foo: settings must be extended" ); same( deep2.foo, deep2copy.foo, "Check if not deep2: options must not be modified" ); equals( deep1.foo2, document, "Make sure that a deep clone was not attempted on the document" ); - ok( jQuery.extend2({}, nestedarray).arr === arr, "Don't clone arrays" ); - ok( jQuery.isPlainObject( jQuery.extend2({ arr: arr }, { arr: {} }).arr ), "Cloned object heve to be an plain object" ); + ok( $.widget.extend({}, nestedarray).arr === arr, "Don't clone arrays" ); + ok( jQuery.isPlainObject( $.widget.extend({ arr: arr }, { arr: {} }).arr ), "Cloned object heve to be an plain object" ); var empty = {}; var optionsWithLength = { foo: { length: -1 } }; - jQuery.extend2(empty, optionsWithLength); + $.widget.extend(empty, optionsWithLength); same( empty.foo, optionsWithLength.foo, "The length property must copy correctly" ); empty = {}; var optionsWithDate = { foo: { date: new Date } }; - jQuery.extend2(empty, optionsWithDate); + $.widget.extend(empty, optionsWithDate); same( empty.foo, optionsWithDate.foo, "Dates copy correctly" ); var myKlass = function() {}; var customObject = new myKlass(); var optionsWithCustomObject = { foo: { date: customObject } }; empty = {}; - jQuery.extend2(empty, optionsWithCustomObject); + $.widget.extend(empty, optionsWithCustomObject); ok( empty.foo && empty.foo.date === customObject, "Custom objects copy correctly (no methods)" ); // Makes the class a little more realistic myKlass.prototype = { someMethod: function(){} }; empty = {}; - jQuery.extend2(empty, optionsWithCustomObject); + $.widget.extend(empty, optionsWithCustomObject); ok( empty.foo && empty.foo.date === customObject, "Custom objects copy correctly" ); - var ret = jQuery.extend2({ foo: 4 }, { foo: new Number(5) } ); + var ret = $.widget.extend({ foo: 4 }, { foo: new Number(5) } ); ok( ret.foo == 5, "Wrapped numbers copy correctly" ); var nullUndef; - nullUndef = jQuery.extend2({}, options, { xnumber2: null }); + nullUndef = $.widget.extend({}, options, { xnumber2: null }); ok( nullUndef.xnumber2 === null, "Check to make sure null values are copied"); - nullUndef = jQuery.extend2({}, options, { xnumber2: undefined }); + nullUndef = $.widget.extend({}, options, { xnumber2: undefined }); ok( nullUndef.xnumber2 === options.xnumber2, "Check to make sure undefined values are not copied"); - nullUndef = jQuery.extend2({}, options, { xnumber0: null }); + nullUndef = $.widget.extend({}, options, { xnumber0: null }); ok( nullUndef.xnumber0 === null, "Check to make sure null values are inserted"); // TODO weird test /* var target = {}; var recursive = { foo:target, bar:5 }; - jQuery.extend2(target, recursive); + $.widget.extend(target, recursive); same( target, { bar:5 }, "Check to make sure a recursive obj doesn't go never-ending loop by not copying it over" ); */ @@ -106,7 +106,7 @@ test("jQuery.extend(Object, Object)", function() { var input = { key: [ 1, 2, 3 ] } - var output = jQuery.extend2( {}, input ); + var output = $.widget.extend( {}, input ); deepEqual( input, output, "don't clone arrays" ); input.key[0] = 10; deepEqual( input, output, "don't clone arrays" ); diff --git a/ui/jquery.ui.widget.js b/ui/jquery.ui.widget.js index 809bc9d9033..abceec3a20e 100644 --- a/ui/jquery.ui.widget.js +++ b/ui/jquery.ui.widget.js @@ -11,24 +11,6 @@ var slice = Array.prototype.slice; -function extend( target ) { - var input = slice.call( arguments, 1 ), - inputIndex = 0, - inputLength = input.length, - key, - value; - for ( ; inputIndex < inputLength; inputIndex++ ) { - for ( key in input[ inputIndex ] ) { - value = input[ inputIndex ][ key ]; - if (input[ inputIndex ].hasOwnProperty( key ) && value !== undefined ) { - target[ key ] = $.isPlainObject( value ) ? extend( {}, target[ key ], value ) : value; - } - } - } - return target; -} -$.extend2 = extend; - var _cleanData = $.cleanData; $.cleanData = function( elems ) { for ( var i = 0, elem; (elem = elems[i]) != null; i++ ) { @@ -56,7 +38,7 @@ $.widget = function( name, base, prototype ) { $[ namespace ] = $[ namespace ] || {}; // create the constructor using $.extend() so we can carry over any // static properties stored on the existing constructor (if there is one) - $[ namespace ][ name ] = $.extend( function( options, element ) { + $[ namespace ][ name ] = $.widget.extend( function( options, element ) { // allow instantiation without "new" keyword if ( !this._createWidget ) { return new $[ namespace ][ name ]( options, element ); @@ -73,7 +55,7 @@ $.widget = function( name, base, prototype ) { // we need to make the options hash a property directly on the new instance // otherwise we'll modify the options hash on the prototype that we're // inheriting from - basePrototype.options = extend( {}, basePrototype.options ); + basePrototype.options = $.widget.extend( {}, basePrototype.options ); $.each( prototype, function( prop, value ) { if ( $.isFunction( value ) ) { prototype[ prop ] = (function() { @@ -101,7 +83,7 @@ $.widget = function( name, base, prototype ) { }()); } }); - $[ namespace ][ name ].prototype = extend( basePrototype, { + $[ namespace ][ name ].prototype = $.widget.extend( basePrototype, { namespace: namespace, widgetName: name, widgetEventPrefix: name, @@ -111,6 +93,23 @@ $.widget = function( name, base, prototype ) { $.widget.bridge( name, $[ namespace ][ name ] ); }; +$.widget.extend = function( target ) { + var input = slice.call( arguments, 1 ), + inputIndex = 0, + inputLength = input.length, + key, + value; + for ( ; inputIndex < inputLength; inputIndex++ ) { + for ( key in input[ inputIndex ] ) { + value = input[ inputIndex ][ key ]; + if (input[ inputIndex ].hasOwnProperty( key ) && value !== undefined ) { + target[ key ] = $.isPlainObject( value ) ? $.widget.extend( {}, target[ key ], value ) : value; + } + } + } + return target; +}; + $.widget.bridge = function( name, object ) { $.fn[ name ] = function( options ) { var isMethodCall = typeof options === "string", @@ -119,7 +118,7 @@ $.widget.bridge = function( name, object ) { // allow multiple hashes to be passed on init options = !isMethodCall && args.length ? - extend.apply( null, [ options ].concat(args) ) : + $.widget.extend.apply( null, [ options ].concat(args) ) : options; if ( isMethodCall ) { @@ -181,7 +180,7 @@ $.Widget.prototype = { _createWidget: function( options, element ) { element = $( element || this.defaultElement || this )[ 0 ]; this.element = $( element ); - this.options = extend( {}, + this.options = $.widget.extend( {}, this.options, this._getCreateOptions(), options ); @@ -236,7 +235,7 @@ $.Widget.prototype = { if ( arguments.length === 0 ) { // don't return a reference to the internal hash - return extend( {}, this.options ); + return $.widget.extend( {}, this.options ); } if ( typeof key === "string" ) { @@ -248,7 +247,7 @@ $.Widget.prototype = { parts = key.split( "." ); key = parts.shift(); if ( parts.length ) { - curOption = options[ key ] = extend( {}, this.options[ key ] ); + curOption = options[ key ] = $.widget.extend( {}, this.options[ key ] ); for ( i = 0; i < parts.length - 1; i++ ) { curOption[ parts[ i ] ] = curOption[ parts[ i ] ] || {}; curOption = curOption[ parts[ i ] ]; From fc5fab227554412186310c5b5919bae4140f1162 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn=20Zaefferer?= Date: Sat, 7 May 2011 14:58:11 +0200 Subject: [PATCH 3/4] Widget: Clean up extend tests --- tests/unit/widget/widget_extend.js | 64 +++++++++++++++--------------- 1 file changed, 31 insertions(+), 33 deletions(-) diff --git a/tests/unit/widget/widget_extend.js b/tests/unit/widget/widget_extend.js index a99a336d3f8..4122111ae9c 100644 --- a/tests/unit/widget/widget_extend.js +++ b/tests/unit/widget/widget_extend.js @@ -1,5 +1,5 @@ test("$.widget.extend(Object, Object)", function() { - expect(28); + expect(26); var settings = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" }, options = { xnumber2: 1, xstring2: "x", xxx: "newstring" }, @@ -11,83 +11,81 @@ test("$.widget.extend(Object, Object)", function() { deep2copy = { foo: { baz: true }, foo2: document }, deepmerged = { foo: { bar: true, baz: true }, foo2: document }, arr = [1, 2, 3], - nestedarray = { arr: arr }; + nestedarray = { arr: arr }, + ret; $.widget.extend(settings, options); - same( settings, merged, "Check if extended: settings must be extended" ); - same( options, optionsCopy, "Check if not modified: options must not be modified" ); - $.widget.extend(settings, null, options); - same( settings, merged, "Check if extended: settings must be extended" ); - same( options, optionsCopy, "Check if not modified: options must not be modified" ); + deepEqual( settings, merged, "Check if extended: settings must be extended" ); + deepEqual( options, optionsCopy, "Check if not modified: options must not be modified" ); $.widget.extend(deep1, deep2); - same( deep1.foo, deepmerged.foo, "Check if foo: settings must be extended" ); - same( deep2.foo, deep2copy.foo, "Check if not deep2: options must not be modified" ); - equals( deep1.foo2, document, "Make sure that a deep clone was not attempted on the document" ); + deepEqual( deep1.foo, deepmerged.foo, "Check if foo: settings must be extended" ); + deepEqual( deep2.foo, deep2copy.foo, "Check if not deep2: options must not be modified" ); + equal( deep1.foo2, document, "Make sure that a deep clone was not attempted on the document" ); - ok( $.widget.extend({}, nestedarray).arr === arr, "Don't clone arrays" ); + strictEqual( $.widget.extend({}, nestedarray).arr, arr, "Don't clone arrays" ); ok( jQuery.isPlainObject( $.widget.extend({ arr: arr }, { arr: {} }).arr ), "Cloned object heve to be an plain object" ); var empty = {}; var optionsWithLength = { foo: { length: -1 } }; $.widget.extend(empty, optionsWithLength); - same( empty.foo, optionsWithLength.foo, "The length property must copy correctly" ); + deepEqual( empty.foo, optionsWithLength.foo, "The length property must copy correctly" ); empty = {}; var optionsWithDate = { foo: { date: new Date } }; $.widget.extend(empty, optionsWithDate); - same( empty.foo, optionsWithDate.foo, "Dates copy correctly" ); + deepEqual( empty.foo, optionsWithDate.foo, "Dates copy correctly" ); var myKlass = function() {}; var customObject = new myKlass(); var optionsWithCustomObject = { foo: { date: customObject } }; empty = {}; $.widget.extend(empty, optionsWithCustomObject); - ok( empty.foo && empty.foo.date === customObject, "Custom objects copy correctly (no methods)" ); + strictEqual( empty.foo.date, customObject, "Custom objects copy correctly (no methods)" ); // Makes the class a little more realistic myKlass.prototype = { someMethod: function(){} }; empty = {}; $.widget.extend(empty, optionsWithCustomObject); - ok( empty.foo && empty.foo.date === customObject, "Custom objects copy correctly" ); + strictEqual( empty.foo.date, customObject, "Custom objects copy correctly" ); - var ret = $.widget.extend({ foo: 4 }, { foo: new Number(5) } ); - ok( ret.foo == 5, "Wrapped numbers copy correctly" ); + ret = $.widget.extend({ foo: 4 }, { foo: new Number(5) } ); + equal( ret.foo, 5, "Wrapped numbers copy correctly" ); var nullUndef; nullUndef = $.widget.extend({}, options, { xnumber2: null }); - ok( nullUndef.xnumber2 === null, "Check to make sure null values are copied"); + strictEqual( nullUndef.xnumber2, null, "Check to make sure null values are copied"); nullUndef = $.widget.extend({}, options, { xnumber2: undefined }); - ok( nullUndef.xnumber2 === options.xnumber2, "Check to make sure undefined values are not copied"); + strictEqual( nullUndef.xnumber2, options.xnumber2, "Check to make sure undefined values are not copied"); nullUndef = $.widget.extend({}, options, { xnumber0: null }); - ok( nullUndef.xnumber0 === null, "Check to make sure null values are inserted"); + strictEqual( nullUndef.xnumber0, null, "Check to make sure null values are inserted"); // TODO weird test /* var target = {}; var recursive = { foo:target, bar:5 }; $.widget.extend(target, recursive); - same( target, { bar:5 }, "Check to make sure a recursive obj doesn't go never-ending loop by not copying it over" ); + deepEqual( target, { bar:5 }, "Check to make sure a recursive obj doesn't go never-ending loop by not copying it over" ); */ - var ret = jQuery.extend(true, { foo: [] }, { foo: [0] } ); // 1907 - equals( ret.foo.length, 1, "Check to make sure a value with coersion 'false' copies over when necessary to fix #1907" ); + ret = jQuery.extend(true, { foo: [] }, { foo: [0] } ); // 1907 + equal( ret.foo.length, 1, "Check to make sure a value with coersion 'false' copies over when necessary to fix #1907" ); - var ret = jQuery.extend(true, { foo: "1,2,3" }, { foo: [1, 2, 3] } ); - ok( typeof ret.foo != "string", "Check to make sure values equal with coersion (but not actually equal) overwrite correctly" ); + ret = jQuery.extend(true, { foo: "1,2,3" }, { foo: [1, 2, 3] } ); + notStrictEqual( typeof ret.foo, "string", "Check to make sure values equal with coersion (but not actually equal) overwrite correctly" ); - var ret = jQuery.extend(true, { foo:"bar" }, { foo:null } ); - ok( typeof ret.foo !== "undefined", "Make sure a null value doesn't crash with deep extend, for #1908" ); + ret = jQuery.extend(true, { foo:"bar" }, { foo:null } ); + notStrictEqual( typeof ret.foo, "undefined", "Make sure a null value doesn't crash with deep extend, for #1908" ); var obj = { foo:null }; jQuery.extend(true, obj, { foo:"notnull" } ); - equals( obj.foo, "notnull", "Make sure a null value can be overwritten" ); + equal( obj.foo, "notnull", "Make sure a null value can be overwritten" ); function func() {} jQuery.extend(func, { key: "value" } ); - equals( func.key, "value", "Verify a function can be extended" ); + equal( func.key, "value", "Verify a function can be extended" ); var defaults = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" }, defaultsCopy = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" }, @@ -98,10 +96,10 @@ test("$.widget.extend(Object, Object)", function() { merged2 = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "xx", xxx: "newstringx" }; var settings = jQuery.extend({}, defaults, options1, options2); - same( settings, merged2, "Check if extended: settings must be extended" ); - same( defaults, defaultsCopy, "Check if not modified: options1 must not be modified" ); - same( options1, options1Copy, "Check if not modified: options1 must not be modified" ); - same( options2, options2Copy, "Check if not modified: options2 must not be modified" ); + deepEqual( settings, merged2, "Check if extended: settings must be extended" ); + deepEqual( defaults, defaultsCopy, "Check if not modified: options1 must not be modified" ); + deepEqual( options1, options1Copy, "Check if not modified: options1 must not be modified" ); + deepEqual( options2, options2Copy, "Check if not modified: options2 must not be modified" ); var input = { key: [ 1, 2, 3 ] From 684fae7dd192ebd9ef01626b03c6606464e92744 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn=20Zaefferer?= Date: Tue, 10 May 2011 17:58:49 +0200 Subject: [PATCH 4/4] Wdiget: More cleanup on extend tests --- tests/unit/widget/widget_extend.js | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/tests/unit/widget/widget_extend.js b/tests/unit/widget/widget_extend.js index 4122111ae9c..e231fddbf62 100644 --- a/tests/unit/widget/widget_extend.js +++ b/tests/unit/widget/widget_extend.js @@ -1,5 +1,5 @@ test("$.widget.extend(Object, Object)", function() { - expect(26); + expect(27); var settings = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" }, options = { xnumber2: 1, xstring2: "x", xxx: "newstring" }, @@ -24,7 +24,7 @@ test("$.widget.extend(Object, Object)", function() { equal( deep1.foo2, document, "Make sure that a deep clone was not attempted on the document" ); strictEqual( $.widget.extend({}, nestedarray).arr, arr, "Don't clone arrays" ); - ok( jQuery.isPlainObject( $.widget.extend({ arr: arr }, { arr: {} }).arr ), "Cloned object heve to be an plain object" ); + ok( $.isPlainObject( $.widget.extend({ arr: arr }, { arr: {} }).arr ), "Cloned object heve to be an plain object" ); var empty = {}; var optionsWithLength = { foo: { length: -1 } }; @@ -62,29 +62,26 @@ test("$.widget.extend(Object, Object)", function() { nullUndef = $.widget.extend({}, options, { xnumber0: null }); strictEqual( nullUndef.xnumber0, null, "Check to make sure null values are inserted"); - // TODO weird test - /* var target = {}; var recursive = { foo:target, bar:5 }; $.widget.extend(target, recursive); - deepEqual( target, { bar:5 }, "Check to make sure a recursive obj doesn't go never-ending loop by not copying it over" ); - */ + deepEqual( target, { foo: {}, bar: 5 }, "Check to make sure a recursive obj doesn't go never-ending loop by not copying it over" ); - ret = jQuery.extend(true, { foo: [] }, { foo: [0] } ); // 1907 + ret = $.widget.extend( { foo: [] }, { foo: [0] } ); // 1907 equal( ret.foo.length, 1, "Check to make sure a value with coersion 'false' copies over when necessary to fix #1907" ); - ret = jQuery.extend(true, { foo: "1,2,3" }, { foo: [1, 2, 3] } ); - notStrictEqual( typeof ret.foo, "string", "Check to make sure values equal with coersion (but not actually equal) overwrite correctly" ); + ret = $.widget.extend( { foo: "1,2,3" }, { foo: [1, 2, 3] } ); + strictEqual( typeof ret.foo, "object", "Check to make sure values equal with coersion (but not actually equal) overwrite correctly" ); - ret = jQuery.extend(true, { foo:"bar" }, { foo:null } ); - notStrictEqual( typeof ret.foo, "undefined", "Make sure a null value doesn't crash with deep extend, for #1908" ); + ret = $.widget.extend( { foo:"bar" }, { foo:null } ); + strictEqual( typeof ret.foo, "object", "Make sure a null value doesn't crash with deep extend, for #1908" ); var obj = { foo:null }; - jQuery.extend(true, obj, { foo:"notnull" } ); + $.widget.extend( obj, { foo:"notnull" } ); equal( obj.foo, "notnull", "Make sure a null value can be overwritten" ); function func() {} - jQuery.extend(func, { key: "value" } ); + $.widget.extend(func, { key: "value" } ); equal( func.key, "value", "Verify a function can be extended" ); var defaults = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" }, @@ -95,7 +92,7 @@ test("$.widget.extend(Object, Object)", function() { options2Copy = { xstring2: "xx", xxx: "newstringx" }, merged2 = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "xx", xxx: "newstringx" }; - var settings = jQuery.extend({}, defaults, options1, options2); + var settings = $.widget.extend({}, defaults, options1, options2); deepEqual( settings, merged2, "Check if extended: settings must be extended" ); deepEqual( defaults, defaultsCopy, "Check if not modified: options1 must not be modified" ); deepEqual( options1, options1Copy, "Check if not modified: options1 must not be modified" );