From 17b28beae8c242c0ba38d9590bf1de2da08481eb Mon Sep 17 00:00:00 2001 From: Will Charczuk Date: Thu, 2 Mar 2017 14:39:32 -0800 Subject: [PATCH] fixing range validation and tests. --- chart.go | 3 +++ chart_test.go | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/chart.go b/chart.go index 49c64f1..5c8ab4d 100644 --- a/chart.go +++ b/chart.go @@ -295,6 +295,9 @@ func (c Chart) checkRanges(xr, yr, yra Range) error { if math.IsNaN(xDelta) { return errors.New("nan x-range delta") } + if xDelta == 0 { + return errors.New("zero x-range delta; there needs to be at least (2) values") + } yDelta := yr.GetDelta() if math.IsInf(yDelta, 0) { diff --git a/chart_test.go b/chart_test.go index 7afd072..87d00ed 100644 --- a/chart_test.go +++ b/chart_test.go @@ -422,5 +422,64 @@ func TestChartValidatesSeries(t *testing.T) { } assert.NotNil(c.validateSeries()) - +} + +func TestChartCheckRanges(t *testing.T) { + assert := assert.New(t) + + c := Chart{ + Series: []Series{ + ContinuousSeries{ + XValues: []float64{1.0, 2.0}, + YValues: []float64{3.10, 3.14}, + }, + }, + } + + xr, yr, yra := c.getRanges() + assert.Nil(c.checkRanges(xr, yr, yra)) +} + +func TestChartCheckRangesFailure(t *testing.T) { + assert := assert.New(t) + + c := Chart{ + Series: []Series{ + ContinuousSeries{ + XValues: []float64{1.0, 2.0}, + YValues: []float64{3.14, 3.14}, + }, + }, + } + + xr, yr, yra := c.getRanges() + assert.NotNil(c.checkRanges(xr, yr, yra)) +} + +func TestChartCheckRangesWithRanges(t *testing.T) { + assert := assert.New(t) + + c := Chart{ + XAxis: XAxis{ + Range: &ContinuousRange{ + Min: 0, + Max: 10, + }, + }, + YAxis: YAxis{ + Range: &ContinuousRange{ + Min: 0, + Max: 5, + }, + }, + Series: []Series{ + ContinuousSeries{ + XValues: []float64{1.0, 2.0}, + YValues: []float64{3.14, 3.14}, + }, + }, + } + + xr, yr, yra := c.getRanges() + assert.Nil(c.checkRanges(xr, yr, yra)) }