-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,33 @@ var createGraphDiv = require('../assets/create_graph_div'); | |
var destroyGraphDiv = require('../assets/destroy_graph_div'); | ||
var Plots = PlotlyInternal.Plots; | ||
|
||
/* This is a one-off function to fully populate sparse arrays. 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. | ||
*/ | ||
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]]); | ||
} | ||
} | ||
} | ||
|
||
function expectLooseDeepEqual(a, b) { | ||
expect(populateUndefinedArrayEls(a)).toEqual(populateUndefinedArrayEls(b)); | ||
} | ||
|
||
|
||
describe('Test lib.js:', function() { | ||
'use strict'; | ||
|
@@ -485,37 +512,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); | ||
expectLooseDeepEqual(Lib.expandObjectPaths(input), 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); | ||
expectLooseDeepEqual(Lib.expandObjectPaths(input), expected); | ||
}); | ||
|
||
it('unpacks deep paths', function() { | ||
var input = {'foo.bar.baz': 'red'}; | ||
var expected = {foo: {bar: {baz: 'red'}}}; | ||
expect(Lib.expandObjectPaths(input)).toEqual(expected); | ||
expectLooseDeepEqual(Lib.expandObjectPaths(input), 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); | ||
expectLooseDeepEqual(Lib.expandObjectPaths(input), 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); | ||
expectLooseDeepEqual(Lib.expandObjectPaths(input), 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); | ||
expectLooseDeepEqual(Lib.expandObjectPaths(input), expected); | ||
}); | ||
|
||
it('retains the identity of nested objects', function() { | ||
|
@@ -541,49 +568,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); | ||
expectLooseDeepEqual(Lib.expandObjectPaths(input), 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); | ||
expectLooseDeepEqual(computed, 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); | ||
expectLooseDeepEqual(computed, 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); | ||
expectLooseDeepEqual(computed, 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); | ||
expectLooseDeepEqual(computed, 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); | ||
expectLooseDeepEqual(computed, expected); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of that custom assert routine, why not just have to expect(computed.marker[0]).toBeUndefined();
expect(computed.marker[1]).toEqual({ size: { magnitude: 8 } }); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can move the function inside this block, but ideally wanted to avoid manually digging through the objects. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah that make sense. What about moving your new |
||
}); | ||
|
||
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); | ||
expectLooseDeepEqual(computed, expected); | ||
}); | ||
|
||
// TODO: This test is unimplemented since it's a currently-unused corner case. | ||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😐