From b8622b91e3941e585846def15e1aaf9e976916fe Mon Sep 17 00:00:00 2001 From: Gordon Woodhull Date: Tue, 22 Dec 2015 15:46:55 -0500 Subject: [PATCH] matcher toEqualIntList for dot-dash specs fixes #1064 --- spec/helpers/custom_matchers.js | 29 +++++++++++++++++++++++++++++ spec/legend-spec.js | 4 ++-- spec/line-chart-spec.js | 6 +++--- spec/series-chart-spec.js | 4 ++-- 4 files changed, 36 insertions(+), 7 deletions(-) diff --git a/spec/helpers/custom_matchers.js b/spec/helpers/custom_matchers.js index 6d7168242..557d45710 100644 --- a/spec/helpers/custom_matchers.js +++ b/spec/helpers/custom_matchers.js @@ -95,6 +95,7 @@ function compareSubPath (got, wanted, i, j, delta) { } return {pass: true}; } + function comparePaths (actual, expected, delta) { delta = delta || 1; // default delta of 1px var got = parsePath(actual), @@ -108,6 +109,7 @@ function comparePaths (actual, expected, delta) { } return compareSubPath(got, wanted, 0, 0, delta); } + function findSubPath (actual, expected, delta) { delta = delta || 1; // default delta of 1px var got = parsePath(actual), @@ -125,6 +127,28 @@ function findSubPath (actual, expected, delta) { }; } +// actually number list, but presumably you'd want closeness if you need that +function compareIntList (actual, expected) { + var aparts = actual.split(/, */), + eparts = expected.split(/, */); + if (aparts.length !== eparts.length) { + return { + pass: false, + message: 'actual number of list items ' + aparts.length + + ' did not match expected number ' + eparts.length + }; + } + for (var i = 0; i < eparts.length; ++i) { + if (+aparts[i] !== +eparts[i]) { + return { + pass: false, + message: 'list item[' + i + '] value ' + aparts[i] + ' did not equal expected value ' + eparts[i] + }; + } + } + return {pass: true}; +} + beforeEach(function () { jasmine.addMatchers({ toBeWithinDelta: function (_) { @@ -179,6 +203,11 @@ beforeEach(function () { return { compare: findSubPath }; + }, + toEqualIntList: function () { + return { + compare: compareIntList + }; } }); }); diff --git a/spec/legend-spec.js b/spec/legend-spec.js index 79ddc5149..1c8eabef9 100644 --- a/spec/legend-spec.js +++ b/spec/legend-spec.js @@ -206,8 +206,8 @@ describe('dc.legend', function () { }); it('should style legend line correctly', function () { - expect(legendLine(0).attr('stroke-dasharray')).toEqual('10,1'); - expect(legendLine(1).attr('stroke-dasharray')).toEqual('2,1'); + expect(legendLine(0).attr('stroke-dasharray')).toEqualIntList('10,1'); + expect(legendLine(1).attr('stroke-dasharray')).toEqualIntList('2,1'); }); }); diff --git a/spec/line-chart-spec.js b/spec/line-chart-spec.js index 745df366a..aabd66d5c 100644 --- a/spec/line-chart-spec.js +++ b/spec/line-chart-spec.js @@ -29,7 +29,7 @@ describe('dc.lineChart', function () { }); it('should be dash-dot-dot-dot to match the specified style', function () { - expect(chart.selectAll('path.line').attr('stroke-dasharray')).toBe('3,1,1,1'); + expect(chart.selectAll('path.line').attr('stroke-dasharray')).toEqualIntList('3,1,1,1'); }); }); @@ -195,8 +195,8 @@ describe('dc.lineChart', function () { }); it('should have a stroke dash', function () { - expect(chart.selectAll('path.xRef').attr('stroke-dasharray')).toBe('5,5'); - expect(chart.selectAll('path.yRef').attr('stroke-dasharray')).toBe('5,5'); + expect(chart.selectAll('path.xRef').attr('stroke-dasharray')).toEqualIntList('5,5'); + expect(chart.selectAll('path.yRef').attr('stroke-dasharray')).toEqualIntList('5,5'); }); describe('when dot is hovered over', function () { diff --git a/spec/series-chart-spec.js b/spec/series-chart-spec.js index 97642f341..cf6305d83 100644 --- a/spec/series-chart-spec.js +++ b/spec/series-chart-spec.js @@ -90,8 +90,8 @@ describe('dc.seriesChart', function () { var lines = chart.selectAll('path.line'); var areas = chart.selectAll('path.area'); - expect(d3.select(lines[0][0]).attr('stroke-dasharray')).toBe('3,1,1'); - expect(d3.select(lines[0][1]).attr('stroke-dasharray')).toBe('3,1,1'); + expect(d3.select(lines[0][0]).attr('stroke-dasharray')).toEqualIntList('3,1,1'); + expect(d3.select(lines[0][1]).attr('stroke-dasharray')).toEqualIntList('3,1,1'); expect(d3.select(areas[0][0]).attr('fill')).toBe('#000001'); expect(d3.select(areas[0][1]).attr('fill')).toBe('#000002');