2019-01-07 00:16:48 -05:00
|
|
|
// Copyright 2019 Google Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2019-01-07 00:15:31 -05:00
|
|
|
package axes
|
|
|
|
|
|
|
|
import (
|
2019-01-08 00:24:48 -05:00
|
|
|
"image"
|
2019-01-07 00:15:31 -05:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/kylelemons/godebug/pretty"
|
|
|
|
)
|
|
|
|
|
|
|
|
type updateY struct {
|
|
|
|
minVal float64
|
|
|
|
maxVal float64
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestY(t *testing.T) {
|
|
|
|
tests := []struct {
|
2019-01-08 00:24:48 -05:00
|
|
|
desc string
|
|
|
|
minVal float64
|
|
|
|
maxVal float64
|
|
|
|
update *updateY
|
|
|
|
cvsHeight int
|
|
|
|
maxWidth int
|
|
|
|
wantWidth int
|
|
|
|
want *YDetails
|
|
|
|
wantErr bool
|
2019-01-07 00:15:31 -05:00
|
|
|
}{
|
|
|
|
{
|
2019-01-08 00:24:48 -05:00
|
|
|
desc: "fails on canvas too small",
|
2019-01-07 00:15:31 -05:00
|
|
|
minVal: 0,
|
|
|
|
maxVal: 3,
|
2019-01-08 00:24:48 -05:00
|
|
|
cvsHeight: 1,
|
|
|
|
maxWidth: 2,
|
2019-01-07 00:15:31 -05:00
|
|
|
wantWidth: 2,
|
2019-01-08 00:24:48 -05:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "fails on maxWidth less than required width",
|
|
|
|
minVal: 0,
|
|
|
|
maxVal: 3,
|
|
|
|
cvsHeight: 2,
|
|
|
|
maxWidth: 1,
|
|
|
|
wantWidth: 2,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "maxWidth equals required width",
|
|
|
|
minVal: 0,
|
|
|
|
maxVal: 3,
|
|
|
|
cvsHeight: 2,
|
|
|
|
wantWidth: 2,
|
|
|
|
maxWidth: 2,
|
2019-01-07 00:15:31 -05:00
|
|
|
want: &YDetails{
|
|
|
|
Width: 2,
|
2019-01-08 23:56:05 -05:00
|
|
|
Scale: mustNewYScale(0, 3, 2, nonZeroDecimals),
|
2019-01-08 00:24:48 -05:00
|
|
|
Labels: []*Label{
|
|
|
|
{NewValue(0, nonZeroDecimals), image.Point{0, 1}},
|
|
|
|
{NewValue(1.72, nonZeroDecimals), image.Point{0, 0}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "maxWidth just accommodates the longest label",
|
|
|
|
minVal: 0,
|
|
|
|
maxVal: 3,
|
|
|
|
cvsHeight: 2,
|
|
|
|
wantWidth: 2,
|
|
|
|
maxWidth: 5,
|
|
|
|
want: &YDetails{
|
|
|
|
Width: 5,
|
2019-01-08 23:56:05 -05:00
|
|
|
Scale: mustNewYScale(0, 3, 2, nonZeroDecimals),
|
2019-01-08 00:24:48 -05:00
|
|
|
Labels: []*Label{
|
|
|
|
{NewValue(0, nonZeroDecimals), image.Point{3, 1}},
|
|
|
|
{NewValue(1.72, nonZeroDecimals), image.Point{0, 0}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "maxWidth is more than we need",
|
|
|
|
minVal: 0,
|
|
|
|
maxVal: 3,
|
|
|
|
cvsHeight: 2,
|
|
|
|
wantWidth: 2,
|
|
|
|
maxWidth: 6,
|
|
|
|
want: &YDetails{
|
|
|
|
Width: 5,
|
2019-01-08 23:56:05 -05:00
|
|
|
Scale: mustNewYScale(0, 3, 2, nonZeroDecimals),
|
2019-01-08 00:24:48 -05:00
|
|
|
Labels: []*Label{
|
|
|
|
{NewValue(0, nonZeroDecimals), image.Point{3, 1}},
|
|
|
|
{NewValue(1.72, nonZeroDecimals), image.Point{0, 0}},
|
|
|
|
},
|
2019-01-07 00:15:31 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
t.Run(tc.desc, func(t *testing.T) {
|
2019-01-08 00:24:48 -05:00
|
|
|
y := NewY(tc.minVal, tc.maxVal)
|
2019-01-07 00:15:31 -05:00
|
|
|
if tc.update != nil {
|
2019-01-08 00:24:48 -05:00
|
|
|
y.Update(tc.update.minVal, tc.update.maxVal)
|
2019-01-07 00:15:31 -05:00
|
|
|
}
|
|
|
|
|
2019-01-08 00:24:48 -05:00
|
|
|
gotWidth := y.RequiredWidth()
|
2019-01-07 00:15:31 -05:00
|
|
|
if gotWidth != tc.wantWidth {
|
|
|
|
t.Errorf("RequiredWidth => got %v, want %v", gotWidth, tc.wantWidth)
|
|
|
|
}
|
|
|
|
|
2019-01-08 00:24:48 -05:00
|
|
|
got, err := y.Details(tc.cvsHeight, tc.maxWidth)
|
|
|
|
if (err != nil) != tc.wantErr {
|
|
|
|
t.Errorf("Details => unexpected error: %v, wantErr: %v", err, tc.wantErr)
|
2019-01-07 00:15:31 -05:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if diff := pretty.Compare(tc.want, got); diff != "" {
|
|
|
|
t.Errorf("Details => unexpected diff (-want, +got):\n%s", diff)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|