1
0
Fork 0

Moved test-util-clone into suite as Util/CloneTest

perfodd
Mike Gerwitz 2014-01-19 12:39:44 -05:00
parent ba1c3044de
commit 7b591ba426
2 changed files with 163 additions and 143 deletions

View File

@ -0,0 +1,163 @@
/**
* Tests util.clone
*
* Copyright (C) 2010, 2011, 2013 Mike Gerwitz
*
* This file is part of GNU ease.js.
*
* ease.js is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
require( 'common' ).testCase(
{
caseSetUp: function()
{
this.Sut = this.require( 'util' );
},
/**
* Cloning is intended to duplicate objects to avoid shared references.
*/
'Cloned array is not the same object as the original': function()
{
var arr = [ 1, 2, 3 ];
this.assertNotStrictEqual( this.Sut.clone( arr ), arr );
},
/**
* Same concept as above test.
*/
'Cloned object is not the same object as the original': function()
{
var obj = { foo: 'bar' };
this.assertNotStrictEqual( this.Sut.clone( obj ), obj );
},
/**
* Array data should be cloned such that it strictly matches the
* original; this means a shallow clone is the default.
*/
'Cloned array data mirrors original (shallow clone)': function()
{
var arr = [ 1, '2', { three: 3 }, [ 4 ] ],
arrc = this.Sut.clone( arr );
for ( var i = 0, len = arr.length; i < len; i++ )
{
// note that this implies a shallow clone
this.assertStrictEqual( arr[ i ], arrc[ i ] );
}
},
/**
* Same concept as the above test.
*/
'Cloned object data mirrors original (shallow clone)': function()
{
var obj = { a: 1, b: [ 2 ], c: { three: 3 }, d: '4' },
objc = this.Sut.clone( obj );
for ( var f in obj )
{
// note that this implies a shallow clone
this.assertStrictEqual( obj[ f ], objc[ f ] );
}
},
/**
* Same concept as a shallow clone, but we must recursively check for
* data equality since all objects should have been recursively cloned.
*/
'Deeply cloned array data mirrors original': function()
{
// TODO: we could benefit from a deepClone method instead of a
// cryptic second argument
var arr = [ [ 1, 2 ], [ 3, 4 ], [ 5, [ 6, 7 ] ], { a: 1 } ],
arrc = this.Sut.clone( arr, true );
this.assertDeepEqual( arr, arrc );
// there should be no shared references (yes, we're only checking
// one level here...)
for ( var i = 0, len = arr.length; i < len; i++ )
{
this.assertNotStrictEqual( arr[ i ] , arrc[ i ] );
}
},
/**
* Same concept as above test.
*/
'Deeply cloned object data mirrors original': function()
{
var obj = { a: [ 1 ], b: [ 2 ], c: { d: 3 } },
objc = this.Sut.clone( obj, true );
this.assertDeepEqual( obj, objc );
// there should be no shared references
for ( var f in obj )
{
this.assertNotStrictEqual( obj[ f ], objc[ f ] );
}
},
/**
* "Cloning" functions doesn't necessarily make sense, but it can,
* depending on how you think about it. We can do a toSource() in many
* circumstances and create a new function from that; but what's the
* point? It still does the same thing. As such, functions will not be
* cloned---they'll be returned by reference. This has the obvious
* downside that any properties set on the function itself are not
* cloned, but this is not a current consideration for ease.js.
*/
'Functions are returned by reference, not cloned': function()
{
var func = function() {},
obj = { foo: func };
this.assertStrictEqual( func, this.Sut.clone( obj, true ).foo );
},
/**
* Primitives cannot be cloned, so we should expect that they are simply
* returned
*/
'Primitives are returned by clone': function()
{
// we don't try NaN here because NaN != NaN; we'll try it separately
var prim = [ null, 1, true, false, undefined ],
i = prim.length;
while ( i-- )
{
var val = prim[ i ];
this.assertEqual( val, this.Sut.clone( val ),
'Failed to clone primitive value: ' + val
);
}
// test NaN separately
this.assertOk( isNaN( this.Sut.clone( NaN ) ) );
},
} );

View File

@ -1,143 +0,0 @@
/**
* Tests util.clone
*
* Copyright (C) 2010, 2011, 2013 Mike Gerwitz
*
* This file is part of GNU ease.js.
*
* ease.js is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
var common = require( './common' ),
assert = require( 'assert' ),
util = common.require( 'util' );
var arr = [ 1, 2, 3 ],
obj = { a: 1, b: 2 };
var arr2 = util.clone( arr ),
obj2 = util.clone( obj );
assert.ok(
( arr !== arr2 ),
"Cloned array is not the same object as the original"
);
assert.ok(
( obj !== obj2 ),
"Cloned object is not the same object as the original"
);
// ensure array was properly cloned
for ( var i = 0, len = arr.length; i < len; i++ )
{
assert.equal(
arr2[ i ],
arr[ i ],
"Array data is properly cloned"
);
}
// ensure object was properly cloned
for ( var prop in obj )
{
assert.equal(
obj2[ prop ],
obj[ prop ],
"Object data is properly cloned"
);
}
//
// deep clone
var deep_arr = [ [ 1, 2 ], [ 3, 4 ], [ 5, [ 6, 7 ] ], { a: 1 } ],
deep_obj = { a: [ 1 ], b: [ 2 ], c: { d: 3 } },
deep_arr2 = util.clone( deep_arr, true ),
deep_obj2 = util.clone( deep_obj, true ),
deep_i = 0;
// ensure that the cloned values still match
assert.deepEqual(
deep_arr2,
deep_arr,
"Deep cloned values are equal"
);
assert.deepEqual(
deep_obj2,
deep_obj,
"Deep cloned values are equal"
);
deep_i = deep_arr.length;
while ( deep_i-- )
{
assert.ok(
( deep_arr2[ i ] !== deep_arr[ i ] ),
"Deep cloned array's values are cloned"
);
}
for ( var prop in deep_obj )
{
assert.ok(
( deep_obj2[ prop ] !== deep_obj[ prop ] ),
"Deep cloned object's values are cloned (" + prop + ")"
);
}
/**
* "Cloning" functions doesn't necessarily make sense. It can, depending on how
* you think about it. We can do a toSource() in many circumstances and create a
* new function from that. But what's the point? It still does the same thing.
* As such, functions will not be cloned. They'll be returned by reference.
*/
( function testCloneDoesNothingWithFunctions()
{
var func = function() {},
obj = { foo: func };
assert.ok( func === util.clone( obj, true ).foo,
"Functions should not be cloned"
);
} )();
/**
* Primitives cannot be cloned, so we should expect that they are simply
* returned
*/
( function testPrimitivesAreProperlyReturnedByClone()
{
// we don't try NaN because NaN != NaN; we'll try it separately
var prim = [ null, 1, true, false, undefined ],
i = prim.length;
while ( i-- )
{
var val = prim[ i ];
assert.equal( val, util.clone( val ),
'Failed to clone primitive value: ' + val
);
}
// test NaN separately
assert.ok( isNaN( util.clone( NaN ) ) );
} )();