Skip to content

Commit c8a9275

Browse files
committed
Merge branch 'master' into ohlc-trace-name
2 parents 1811ad6 + 17102d8 commit c8a9275

File tree

3 files changed

+64
-15
lines changed

3 files changed

+64
-15
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
"brfs": "^1.4.3",
9595
"browserify": "^13.0.0",
9696
"browserify-transform-tools": "^1.5.1",
97+
"deep-equal": "^1.0.1",
9798
"ecstatic": "^1.4.0",
9899
"eslint": "^3.5.0",
99100
"falafel": "^1.2.0",

test/jasmine/assets/custom_matchers.js

+45-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,53 @@
11
'use strict';
22

33
var isNumeric = require('fast-isnumeric');
4-
4+
var Lib = require('@src/lib');
5+
var deepEqual = require('deep-equal');
56

67
module.exports = {
8+
// toEqual except with sparse arrays populated. This arises because:
9+
//
10+
// var x = new Array(2)
11+
// expect(x).toEqual([undefined, undefined])
12+
//
13+
// will fail assertion even though x[0] === undefined and x[1] === undefined.
14+
// This is because the array elements don't exist until assigned. Of course it
15+
// only fails on *some* platforms (old firefox, looking at you), which is why
16+
// this is worth all the footwork.
17+
toLooseDeepEqual: function() {
18+
function populateUndefinedArrayEls(x) {
19+
var i;
20+
if(Array.isArray(x)) {
21+
for(i = 0; i < x.length; i++) {
22+
x[i] = x[i];
23+
}
24+
} else if(Lib.isPlainObject(x)) {
25+
var keys = Object.keys(x);
26+
for(i = 0; i < keys.length; i++) {
27+
populateUndefinedArrayEls(x[keys[i]]);
28+
}
29+
}
30+
return x;
31+
}
32+
33+
return {
34+
compare: function(actual, expected, msgExtra) {
35+
var actualExpanded = populateUndefinedArrayEls(Lib.extendDeep({}, actual));
36+
var expectedExpanded = populateUndefinedArrayEls(Lib.extendDeep({}, expected));
37+
38+
var passed = deepEqual(actualExpanded, expectedExpanded);
39+
40+
var message = [
41+
'Expected', JSON.stringify(actual), 'to be close to', JSON.stringify(expected), msgExtra
42+
].join(' ');
43+
44+
return {
45+
pass: passed,
46+
message: message
47+
};
48+
}
49+
};
50+
},
751

852
// toBeCloseTo... but for arrays
953
toBeCloseToArray: function() {

test/jasmine/tests/lib_test.js

+18-14
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ var PlotlyInternal = require('@src/plotly');
77
var createGraphDiv = require('../assets/create_graph_div');
88
var destroyGraphDiv = require('../assets/destroy_graph_div');
99
var Plots = PlotlyInternal.Plots;
10-
10+
var customMatchers = require('../assets/custom_matchers');
1111

1212
describe('Test lib.js:', function() {
1313
'use strict';
@@ -477,6 +477,10 @@ describe('Test lib.js:', function() {
477477
});
478478

479479
describe('expandObjectPaths', function() {
480+
beforeAll(function() {
481+
jasmine.addMatchers(customMatchers);
482+
});
483+
480484
it('returns the original object', function() {
481485
var x = {};
482486
expect(Lib.expandObjectPaths(x)).toBe(x);
@@ -485,37 +489,37 @@ describe('Test lib.js:', function() {
485489
it('unpacks top-level paths', function() {
486490
var input = {'marker.color': 'red', 'marker.size': [1, 2, 3]};
487491
var expected = {marker: {color: 'red', size: [1, 2, 3]}};
488-
expect(Lib.expandObjectPaths(input)).toEqual(expected);
492+
expect(Lib.expandObjectPaths(input)).toLooseDeepEqual(expected);
489493
});
490494

491495
it('unpacks recursively', function() {
492496
var input = {'marker.color': {'red.certainty': 'definitely'}};
493497
var expected = {marker: {color: {red: {certainty: 'definitely'}}}};
494-
expect(Lib.expandObjectPaths(input)).toEqual(expected);
498+
expect(Lib.expandObjectPaths(input)).toLooseDeepEqual(expected);
495499
});
496500

497501
it('unpacks deep paths', function() {
498502
var input = {'foo.bar.baz': 'red'};
499503
var expected = {foo: {bar: {baz: 'red'}}};
500-
expect(Lib.expandObjectPaths(input)).toEqual(expected);
504+
expect(Lib.expandObjectPaths(input)).toLooseDeepEqual(expected);
501505
});
502506

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

509513
it('merges dotted properties into objects', function() {
510514
var input = {marker: {color: 'red'}, 'marker.size': 8};
511515
var expected = {marker: {color: 'red', size: 8}};
512-
expect(Lib.expandObjectPaths(input)).toEqual(expected);
516+
expect(Lib.expandObjectPaths(input)).toLooseDeepEqual(expected);
513517
});
514518

515519
it('merges objects into dotted properties', function() {
516520
var input = {'marker.size': 8, marker: {color: 'red'}};
517521
var expected = {marker: {color: 'red', size: 8}};
518-
expect(Lib.expandObjectPaths(input)).toEqual(expected);
522+
expect(Lib.expandObjectPaths(input)).toLooseDeepEqual(expected);
519523
});
520524

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

547551
it('expands nested arrays', function() {
548552
var input = {'marker[1].range[1]': 5};
549553
var expected = {marker: [undefined, {range: [undefined, 5]}]};
550554
var computed = Lib.expandObjectPaths(input);
551-
expect(computed).toEqual(expected);
555+
expect(computed).toLooseDeepEqual(expected);
552556
});
553557

554558
it('expands bracketed array with more nested attributes', function() {
555559
var input = {'marker[1]': {'color.alpha': 2}};
556560
var expected = {marker: [undefined, {color: {alpha: 2}}]};
557561
var computed = Lib.expandObjectPaths(input);
558-
expect(computed).toEqual(expected);
562+
expect(computed).toLooseDeepEqual(expected);
559563
});
560564

561565
it('expands bracketed array notation without further nesting', function() {
562566
var input = {'marker[1]': 8};
563567
var expected = {marker: [undefined, 8]};
564568
var computed = Lib.expandObjectPaths(input);
565-
expect(computed).toEqual(expected);
569+
expect(computed).toLooseDeepEqual(expected);
566570
});
567571

568572
it('expands bracketed array notation with further nesting', function() {
569573
var input = {'marker[1].size': 8};
570574
var expected = {marker: [undefined, {size: 8}]};
571575
var computed = Lib.expandObjectPaths(input);
572-
expect(computed).toEqual(expected);
576+
expect(computed).toLooseDeepEqual(expected);
573577
});
574578

575579
it('expands bracketed array notation with further nesting', function() {
576580
var input = {'marker[1].size.magnitude': 8};
577581
var expected = {marker: [undefined, {size: {magnitude: 8}}]};
578582
var computed = Lib.expandObjectPaths(input);
579-
expect(computed).toEqual(expected);
583+
expect(computed).toLooseDeepEqual(expected);
580584
});
581585

582586
it('combines changes with single array nesting', function() {
583587
var input = {'marker[1].foo': 5, 'marker[0].foo': 4};
584588
var expected = {marker: [{foo: 4}, {foo: 5}]};
585589
var computed = Lib.expandObjectPaths(input);
586-
expect(computed).toEqual(expected);
590+
expect(computed).toLooseDeepEqual(expected);
587591
});
588592

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

0 commit comments

Comments
 (0)