Merge "Fix error in ve.copyArray() and add tests"

This commit is contained in:
Krinkle 2012-09-04 22:26:24 +00:00 committed by Gerrit Code Review
commit 193b70f36d
2 changed files with 48 additions and 2 deletions

View file

@ -176,3 +176,49 @@ QUnit.test( 'getObjectValues', 6, function ( assert ) {
'Throw exception for non-object (null)'
);
} );
QUnit.test( 'copyArray', 6, function ( assert ) {
var simpleArray = [ 'foo', 3 ],
withObj = [ { 'bar': 'baz', 'quux': 3 }, 5 ],
nestedArray = [ [ 'a', 'b' ], [ 1, 3, 4 ] ],
sparseArray = [ 'a', undefined, undefined, 'b' ],
withSparseArray = [ [ 'a', undefined, undefined, 'b' ] ],
Cloneable = function ( p ) {
this.p = p;
};
Cloneable.prototype.clone = function () {
return new Cloneable( this.p + '-clone' );
};
assert.deepEqual(
ve.copyArray( simpleArray ),
simpleArray,
'Simple array'
);
assert.deepEqual(
ve.copyArray( withObj ),
withObj,
'Array containing object'
);
assert.deepEqual(
ve.copyArray( [ new Cloneable( 'bar' ) ] ),
[ new Cloneable( 'bar-clone' ) ],
'Use the .clone() method if available'
);
assert.deepEqual(
ve.copyArray( nestedArray ),
nestedArray,
'Nested array'
);
assert.deepEqual(
ve.copyArray( sparseArray ),
sparseArray,
'Sparse array'
);
assert.deepEqual(
ve.copyArray( withSparseArray ),
withSparseArray,
'Nested sparse array'
);
} );

View file

@ -320,13 +320,13 @@ ve.copyArray = function ( source ) {
for ( i = 0; i < source.length; i++ ) {
sourceValue = source[i];
sourceType = typeof sourceValue;
if ( sourceType === 'string' || sourceType === 'number' ) {
if ( sourceType === 'string' || sourceType === 'number' || sourceType === 'undefined' ) {
destination.push( sourceValue );
} else if ( ve.isPlainObject( sourceValue ) ) {
destination.push( ve.copyObject( sourceValue ) );
} else if ( ve.isArray( sourceValue ) ) {
destination.push( ve.copyArray( sourceValue ) );
} else if ( typeof sourceValue.clone === 'function' ) {
} else if ( sourceValue && typeof sourceValue.clone === 'function' ) {
destination.push( sourceValue.clone() );
}
}