Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure that extendDeepNoArrays includes typed arrays by reference #866

Merged
merged 4 commits into from
Aug 19, 2016
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
55 changes: 53 additions & 2 deletions test/jasmine/tests/extend_test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/*global Float32Array, Float64Array, Int16Array, Int32Array */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add these to the test/jasmine/.eslintrc file similar to the src/.eslintrc


var extendModule = require('@src/lib/extend.js');
var extendFlat = extendModule.extendFlat;
var extendDeep = extendModule.extendDeep;
Expand Down Expand Up @@ -454,12 +456,44 @@ describe('extendDeepAll', function() {
});
});

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

it('does not copy arrays', function() {
it('extendDeep DOES deep-copy untyped source arrays', function() {
var src = {foo: {bar: [1, 2, 3], baz: [5, 4, 3]}};
var tar = {foo: {bar: [4, 5, 6], bop: [8, 2, 1]}};
var ext = extendDeep(tar, src);

expect(ext).not.toBe(src);
expect(ext).toBe(tar);

expect(ext.foo).not.toBe(src.foo);
expect(ext.foo).toBe(tar.foo);

expect(ext.foo.bar).not.toBe(src.foo.bar);
expect(ext.foo.baz).not.toBe(src.foo.baz);
expect(ext.foo.bop).toBe(tar.foo.bop); // what comes from the target isn't deep copied
});

it('extendDeepNoArrays includes by reference untyped arrays from source', function() {
var src = {foo: {bar: [1, 2, 3], baz: [5, 4, 3]}};
var tar = {foo: {bar: [4, 5, 6], bop: [8, 2, 1]}};
var ext = extendDeepNoArrays(tar, src);

expect(ext).not.toBe(src);
expect(ext).toBe(tar);

expect(ext.foo).not.toBe(src.foo);
expect(ext.foo).toBe(tar.foo);

expect(ext.foo.bar).toBe(src.foo.bar);
expect(ext.foo.baz).toBe(src.foo.baz);
expect(ext.foo.bop).toBe(tar.foo.bop);
});

it('extendDeepNoArrays includes by reference typed arrays from source', function() {
var src = {foo: {bar: new Int32Array([1, 2, 3]), baz: new Float32Array([5, 4, 3])}};
var tar = {foo: {bar: new Int16Array([4, 5, 6]), bop: new Float64Array([8, 2, 1])}};
var ext = extendDeepNoArrays(tar, src);

expect(ext).not.toBe(src);
Expand All @@ -472,4 +506,21 @@ describe('extendDeepNoArrays', function() {
expect(ext.foo.baz).toBe(src.foo.baz);
expect(ext.foo.bop).toBe(tar.foo.bop);
});

it('extendDeep ALSO includes by reference typed arrays from source', function() {
var src = {foo: {bar: new Int32Array([1, 2, 3]), baz: new Float32Array([5, 4, 3])}};
var tar = {foo: {bar: new Int16Array([4, 5, 6]), bop: new Float64Array([8, 2, 1])}};
var ext = extendDeep(tar, src);

expect(ext).not.toBe(src);
expect(ext).toBe(tar);

expect(ext.foo).not.toBe(src.foo);
expect(ext.foo).toBe(tar.foo);

expect(ext.foo.bar).toBe(src.foo.bar);
expect(ext.foo.baz).toBe(src.foo.baz);
expect(ext.foo.bop).toBe(tar.foo.bop);
});

});