Skip to content

Fix expect(new Array(1)).toEqual([undefined]) failures on *some* platforms #1048

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

Merged
merged 4 commits into from
Oct 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
"brfs": "^1.4.3",
"browserify": "^13.0.0",
"browserify-transform-tools": "^1.5.1",
"deep-equal": "^1.0.1",
"ecstatic": "^1.4.0",
"eslint": "^3.5.0",
"falafel": "^1.2.0",
Expand Down
46 changes: 45 additions & 1 deletion test/jasmine/assets/custom_matchers.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,53 @@
'use strict';

var isNumeric = require('fast-isnumeric');

var Lib = require('@src/lib');
var deepEqual = require('deep-equal');

module.exports = {
// toEqual except with sparse arrays populated. This arises because:
//
// var x = new Array(2)
// expect(x).toEqual([undefined, undefined])
//
// will fail assertion even though x[0] === undefined and x[1] === undefined.
// This is because the array elements don't exist until assigned. Of course it
// only fails on *some* platforms (old firefox, looking at you), which is why
// this is worth all the footwork.
toLooseDeepEqual: function() {
function populateUndefinedArrayEls(x) {
var i;
if(Array.isArray(x)) {
for(i = 0; i < x.length; i++) {
x[i] = x[i];
}
} else if(Lib.isPlainObject(x)) {
var keys = Object.keys(x);
for(i = 0; i < keys.length; i++) {
populateUndefinedArrayEls(x[keys[i]]);
}
}
return x;
}

return {
compare: function(actual, expected, msgExtra) {
var actualExpanded = populateUndefinedArrayEls(Lib.extendDeep({}, actual));
var expectedExpanded = populateUndefinedArrayEls(Lib.extendDeep({}, expected));

var passed = deepEqual(actualExpanded, expectedExpanded);

var message = [
'Expected', JSON.stringify(actual), 'to be close to', JSON.stringify(expected), msgExtra
].join(' ');

return {
pass: passed,
message: message
};
}
};
},

// toBeCloseTo... but for arrays
toBeCloseToArray: function() {
Expand Down
32 changes: 18 additions & 14 deletions test/jasmine/tests/lib_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var PlotlyInternal = require('@src/plotly');
var createGraphDiv = require('../assets/create_graph_div');
var destroyGraphDiv = require('../assets/destroy_graph_div');
var Plots = PlotlyInternal.Plots;

var customMatchers = require('../assets/custom_matchers');

describe('Test lib.js:', function() {
'use strict';
Expand Down Expand Up @@ -477,6 +477,10 @@ describe('Test lib.js:', function() {
});

describe('expandObjectPaths', function() {
beforeAll(function() {
jasmine.addMatchers(customMatchers);
});

it('returns the original object', function() {
var x = {};
expect(Lib.expandObjectPaths(x)).toBe(x);
Expand All @@ -485,37 +489,37 @@ describe('Test lib.js:', function() {
it('unpacks top-level paths', function() {
var input = {'marker.color': 'red', 'marker.size': [1, 2, 3]};
var expected = {marker: {color: 'red', size: [1, 2, 3]}};
expect(Lib.expandObjectPaths(input)).toEqual(expected);
expect(Lib.expandObjectPaths(input)).toLooseDeepEqual(expected);
});

it('unpacks recursively', function() {
var input = {'marker.color': {'red.certainty': 'definitely'}};
var expected = {marker: {color: {red: {certainty: 'definitely'}}}};
expect(Lib.expandObjectPaths(input)).toEqual(expected);
expect(Lib.expandObjectPaths(input)).toLooseDeepEqual(expected);
});

it('unpacks deep paths', function() {
var input = {'foo.bar.baz': 'red'};
var expected = {foo: {bar: {baz: 'red'}}};
expect(Lib.expandObjectPaths(input)).toEqual(expected);
expect(Lib.expandObjectPaths(input)).toLooseDeepEqual(expected);
});

it('unpacks non-top-level deep paths', function() {
var input = {color: {'foo.bar.baz': 'red'}};
var expected = {color: {foo: {bar: {baz: 'red'}}}};
expect(Lib.expandObjectPaths(input)).toEqual(expected);
expect(Lib.expandObjectPaths(input)).toLooseDeepEqual(expected);
});

it('merges dotted properties into objects', function() {
var input = {marker: {color: 'red'}, 'marker.size': 8};
var expected = {marker: {color: 'red', size: 8}};
expect(Lib.expandObjectPaths(input)).toEqual(expected);
expect(Lib.expandObjectPaths(input)).toLooseDeepEqual(expected);
});

it('merges objects into dotted properties', function() {
var input = {'marker.size': 8, marker: {color: 'red'}};
var expected = {marker: {color: 'red', size: 8}};
expect(Lib.expandObjectPaths(input)).toEqual(expected);
expect(Lib.expandObjectPaths(input)).toLooseDeepEqual(expected);
});

it('retains the identity of nested objects', function() {
Expand All @@ -541,49 +545,49 @@ describe('Test lib.js:', function() {
it('expands bracketed array notation', function() {
var input = {'marker[1]': {color: 'red'}};
var expected = {marker: [undefined, {color: 'red'}]};
expect(Lib.expandObjectPaths(input)).toEqual(expected);
expect(Lib.expandObjectPaths(input)).toLooseDeepEqual(expected);
});

it('expands nested arrays', function() {
var input = {'marker[1].range[1]': 5};
var expected = {marker: [undefined, {range: [undefined, 5]}]};
var computed = Lib.expandObjectPaths(input);
expect(computed).toEqual(expected);
expect(computed).toLooseDeepEqual(expected);
});

it('expands bracketed array with more nested attributes', function() {
var input = {'marker[1]': {'color.alpha': 2}};
var expected = {marker: [undefined, {color: {alpha: 2}}]};
var computed = Lib.expandObjectPaths(input);
expect(computed).toEqual(expected);
expect(computed).toLooseDeepEqual(expected);
});

it('expands bracketed array notation without further nesting', function() {
var input = {'marker[1]': 8};
var expected = {marker: [undefined, 8]};
var computed = Lib.expandObjectPaths(input);
expect(computed).toEqual(expected);
expect(computed).toLooseDeepEqual(expected);
});

it('expands bracketed array notation with further nesting', function() {
var input = {'marker[1].size': 8};
var expected = {marker: [undefined, {size: 8}]};
var computed = Lib.expandObjectPaths(input);
expect(computed).toEqual(expected);
expect(computed).toLooseDeepEqual(expected);
});

it('expands bracketed array notation with further nesting', function() {
var input = {'marker[1].size.magnitude': 8};
var expected = {marker: [undefined, {size: {magnitude: 8}}]};
var computed = Lib.expandObjectPaths(input);
expect(computed).toEqual(expected);
expect(computed).toLooseDeepEqual(expected);
});

it('combines changes with single array nesting', function() {
var input = {'marker[1].foo': 5, 'marker[0].foo': 4};
var expected = {marker: [{foo: 4}, {foo: 5}]};
var computed = Lib.expandObjectPaths(input);
expect(computed).toEqual(expected);
expect(computed).toLooseDeepEqual(expected);
});

// TODO: This test is unimplemented since it's a currently-unused corner case.
Expand Down