Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1466f35

Browse files
committed
Reify that extendDeepNoArrays doesn't deep-copy typed arrays
(cherry picked from commit db1b6f0)
1 parent 7e2448e commit 1466f35

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

test/jasmine/tests/extend_test.js

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -454,10 +454,26 @@ describe('extendDeepAll', function() {
454454
});
455455
});
456456

457-
describe('extendDeepNoArrays', function() {
457+
describe('array by reference vs deep-copy', function() {
458458
'use strict';
459459

460-
it('does not copy arrays', function() {
460+
it('extendDeep DOES deep-copy untyped source arrays', function() {
461+
var src = {foo: {bar: [1, 2, 3], baz: [5, 4, 3]}};
462+
var tar = {foo: {bar: [4, 5, 6], bop: [8, 2, 1]}};
463+
var ext = extendDeep(tar, src);
464+
465+
expect(ext).not.toBe(src);
466+
expect(ext).toBe(tar);
467+
468+
expect(ext.foo).not.toBe(src.foo);
469+
expect(ext.foo).toBe(tar.foo);
470+
471+
expect(ext.foo.bar).not.toBe(src.foo.bar);
472+
expect(ext.foo.baz).not.toBe(src.foo.baz);
473+
expect(ext.foo.bop).toBe(tar.foo.bop); // what comes from the target isn't deep copied
474+
});
475+
476+
it('extendDeepNoArrays includes by reference untyped arrays from source', function() {
461477
var src = {foo: {bar: [1, 2, 3], baz: [5, 4, 3]}};
462478
var tar = {foo: {bar: [4, 5, 6], bop: [8, 2, 1]}};
463479
var ext = extendDeepNoArrays(tar, src);
@@ -472,4 +488,21 @@ describe('extendDeepNoArrays', function() {
472488
expect(ext.foo.baz).toBe(src.foo.baz);
473489
expect(ext.foo.bop).toBe(tar.foo.bop);
474490
});
491+
492+
it('extendDeepNoArrays includes by reference typed arrays from source', function() {
493+
var src = {foo: {bar: new Int32Array([1, 2, 3]), baz: new Float32Array([5, 4, 3])}};
494+
var tar = {foo: {bar: new Int16Array([4, 5, 6]), bop: new Float64Array([8, 2, 1])}};
495+
var ext = extendDeepNoArrays(tar, src);
496+
497+
expect(ext).not.toBe(src);
498+
expect(ext).toBe(tar);
499+
500+
expect(ext.foo).not.toBe(src.foo);
501+
expect(ext.foo).toBe(tar.foo);
502+
503+
expect(ext.foo.bar).toBe(src.foo.bar);
504+
expect(ext.foo.baz).toBe(src.foo.baz);
505+
expect(ext.foo.bop).toBe(tar.foo.bop);
506+
});
507+
475508
});

0 commit comments

Comments
 (0)