From 39b9840933957640fe26e9a9f9c30e1a5625c782 Mon Sep 17 00:00:00 2001 From: archmoj Date: Tue, 23 Jul 2019 15:54:48 -0400 Subject: [PATCH] add jasmine tests to guard against negative sizes during calc step --- test/jasmine/tests/bar_test.js | 14 ++++++++ test/jasmine/tests/funnel_test.js | 28 ++++++++++++++++ test/jasmine/tests/scatter_test.js | 52 ++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+) diff --git a/test/jasmine/tests/bar_test.js b/test/jasmine/tests/bar_test.js index 9a108e5644a..16fc23250f1 100644 --- a/test/jasmine/tests/bar_test.js +++ b/test/jasmine/tests/bar_test.js @@ -407,6 +407,20 @@ describe('Bar.calc', function() { assertPointField(cd, 'x', [[1, NaN, NaN, 15]]); assertPointField(cd, 'y', [[1, 2, 10, 30]]); }); + + it('should guard against negative marker.line.width values', function() { + var gd = mockBarPlot([{ + marker: { + line: { + width: [2, 1, 0, -1, false, true, null, [], -Infinity, Infinity, NaN, {}] + } + }, + y: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] + }], {}); + + var cd = gd.calcdata; + assertPointField(cd, 'mlw', [[2, 1, 0, 0, 0, 1, 0, 0, 0, Infinity, NaN, NaN]]); + }); }); describe('Bar.crossTraceCalc (formerly known as setPositions)', function() { diff --git a/test/jasmine/tests/funnel_test.js b/test/jasmine/tests/funnel_test.js index cb3861833ae..af4898a5979 100644 --- a/test/jasmine/tests/funnel_test.js +++ b/test/jasmine/tests/funnel_test.js @@ -351,6 +351,34 @@ describe('Funnel.calc', function() { assertPointField(cd, 'y', [[1, NaN, NaN, 15]]); assertPointField(cd, 'x', [[0.5, 1, 5, 15]]); }); + + it('should guard against negative marker.line.width values', function() { + var gd = mockFunnelPlot([{ + marker: { + line: { + width: [2, 1, 0, -1, false, true, null, [], -Infinity, Infinity, NaN, {}] + } + }, + y: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] + }], {}); + + var cd = gd.calcdata; + assertPointField(cd, 'mlw', [[2, 1, 0, 0, 0, 1, 0, 0, 0, Infinity, NaN, NaN]]); + }); + + it('should guard against negative marker.line.width values', function() { + var gd = mockFunnelPlot([{ + marker: { + line: { + width: [2, 1, 0, -1, false, true, null, [], -Infinity, Infinity, NaN, {}] + } + }, + y: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] + }], {}); + + var cd = gd.calcdata; + assertPointField(cd, 'mlw', [[2, 1, 0, 0, 0, 1, 0, 0, 0, Infinity, NaN, NaN]]); + }); }); describe('Funnel.crossTraceCalc', function() { diff --git a/test/jasmine/tests/scatter_test.js b/test/jasmine/tests/scatter_test.js index 1c24d177a3b..8aab391f5d5 100644 --- a/test/jasmine/tests/scatter_test.js +++ b/test/jasmine/tests/scatter_test.js @@ -3,6 +3,7 @@ var Scatter = require('@src/traces/scatter'); var makeBubbleSizeFn = require('@src/traces/scatter/make_bubble_size_func'); var linePoints = require('@src/traces/scatter/line_points'); var Lib = require('@src/lib'); +var Plots = require('@src/plots/plots'); var Plotly = require('@lib/index'); var createGraphDiv = require('../assets/create_graph_div'); @@ -18,6 +19,8 @@ var assertMultiNodeOrder = customAssertions.assertMultiNodeOrder; var checkEventData = require('../assets/check_event_data'); var constants = require('@src/traces/scatter/constants'); +var supplyAllDefaults = require('../assets/supply_defaults'); + var getOpacity = function(node) { return Number(node.style.opacity); }; var getFillOpacity = function(node) { return Number(node.style['fill-opacity']); }; var getColor = function(node) { return node.style.fill; }; @@ -273,6 +276,55 @@ describe('Test scatter', function() { }); }); + describe('calc', function() { + function assertPointField(calcData, prop, expectation) { + var values = []; + + calcData.forEach(function(calcTrace) { + var vals = calcTrace.map(function(pt) { + return Lib.nestedProperty(pt, prop).get(); + }); + + values.push(vals); + }); + + expect(values).toBeCloseTo2DArray(expectation, undefined, '(field ' + prop + ')'); + } + + it('should guard against negative size values', function() { + var gd = { + data: [{ + type: 'scatter', + mode: 'markers+text', + marker: { + line: { + width: [2, 1, 0, -1, false, true, null, [], -Infinity, Infinity, NaN, {}] + }, + opacity: [2, 1, 0, -1, false, true, null, [], -Infinity, Infinity, NaN, {}], + size: [2, 1, 0, -1, false, true, null, [], -Infinity, Infinity, NaN, {}] + }, + textfont: { + size: [2, 1, 0, -1, false, true, null, [], -Infinity, Infinity, NaN, {}] + }, + text: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'], + y: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] + }], + layout: {}, + calcdata: [], + _context: {locale: 'en', locales: {}} + }; + + supplyAllDefaults(gd); + Plots.doCalcdata(gd); + + var cd = gd.calcdata; + assertPointField(cd, 'mlw', [[2, 1, 0, 0, 0, 1, 0, 0, 0, Infinity, NaN, NaN]]); + assertPointField(cd, 'mo', [[2, 1, 0, 0, 0, 1, 0, 0, 0, Infinity, NaN, NaN]]); + assertPointField(cd, 'ms', [[2, 1, 0, 0, 0, 1, 0, 0, 0, Infinity, NaN, NaN]]); + assertPointField(cd, 'ts', [[2, 1, 0, 0, 0, 1, 0, 0, 0, Infinity, NaN, NaN]]); + }); + }); + describe('isBubble', function() { it('should return true when marker.size is an Array', function() { var trace = {