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-02-18 18:48:19 -05:00
|
|
|
"fmt"
|
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-02-15 21:19:04 -05:00
|
|
|
desc string
|
|
|
|
yp *YProperties
|
|
|
|
cvsAr image.Rectangle
|
|
|
|
wantWidth int
|
|
|
|
want *YDetails
|
|
|
|
wantErr bool
|
2019-01-07 00:15:31 -05:00
|
|
|
}{
|
|
|
|
{
|
2019-02-15 21:19:04 -05:00
|
|
|
desc: "fails on canvas too small",
|
|
|
|
yp: &YProperties{
|
|
|
|
Min: 0,
|
|
|
|
Max: 3,
|
|
|
|
ReqXHeight: 2,
|
|
|
|
},
|
|
|
|
cvsAr: image.Rect(0, 0, 3, 2),
|
|
|
|
wantWidth: 2,
|
|
|
|
wantErr: true,
|
2019-01-08 00:24:48 -05:00
|
|
|
},
|
|
|
|
{
|
2019-02-15 21:19:04 -05:00
|
|
|
desc: "fails on cvsWidth less than required width",
|
|
|
|
yp: &YProperties{
|
|
|
|
Min: 0,
|
|
|
|
Max: 3,
|
|
|
|
ReqXHeight: 2,
|
|
|
|
},
|
|
|
|
cvsAr: image.Rect(0, 0, 2, 4),
|
|
|
|
wantWidth: 2,
|
|
|
|
wantErr: true,
|
2019-01-08 00:24:48 -05:00
|
|
|
},
|
2019-01-12 15:55:49 -05:00
|
|
|
{
|
2019-02-15 21:19:04 -05:00
|
|
|
desc: "fails when max is less than min",
|
|
|
|
yp: &YProperties{
|
|
|
|
Min: 0,
|
|
|
|
Max: -1,
|
|
|
|
ReqXHeight: 2,
|
|
|
|
},
|
|
|
|
cvsAr: image.Rect(0, 0, 4, 4),
|
|
|
|
wantWidth: 3,
|
|
|
|
wantErr: true,
|
2019-01-12 15:55:49 -05:00
|
|
|
},
|
2019-01-08 00:24:48 -05:00
|
|
|
{
|
2019-02-15 21:19:04 -05:00
|
|
|
desc: "cvsWidth equals required width",
|
|
|
|
yp: &YProperties{
|
|
|
|
Min: 0,
|
|
|
|
Max: 3,
|
|
|
|
ReqXHeight: 2,
|
|
|
|
},
|
|
|
|
cvsAr: image.Rect(0, 0, 3, 4),
|
|
|
|
wantWidth: 2,
|
2019-01-07 00:15:31 -05:00
|
|
|
want: &YDetails{
|
|
|
|
Width: 2,
|
2019-01-13 00:03:19 -05:00
|
|
|
Start: image.Point{1, 0},
|
|
|
|
End: image.Point{1, 2},
|
2019-01-26 22:23:55 -05:00
|
|
|
Scale: mustNewYScale(0, 3, 2, nonZeroDecimals, YScaleModeAnchored),
|
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}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-01-26 22:23:55 -05:00
|
|
|
{
|
2019-02-15 21:19:04 -05:00
|
|
|
desc: "success for anchored scale",
|
|
|
|
yp: &YProperties{
|
|
|
|
Min: 1,
|
|
|
|
Max: 3,
|
|
|
|
ReqXHeight: 2,
|
|
|
|
ScaleMode: YScaleModeAnchored,
|
|
|
|
},
|
|
|
|
cvsAr: image.Rect(0, 0, 3, 4),
|
|
|
|
wantWidth: 2,
|
2019-01-26 22:23:55 -05:00
|
|
|
want: &YDetails{
|
|
|
|
Width: 2,
|
|
|
|
Start: image.Point{1, 0},
|
|
|
|
End: image.Point{1, 2},
|
|
|
|
Scale: mustNewYScale(0, 3, 2, nonZeroDecimals, YScaleModeAnchored),
|
|
|
|
Labels: []*Label{
|
|
|
|
{NewValue(0, nonZeroDecimals), image.Point{0, 1}},
|
|
|
|
{NewValue(1.72, nonZeroDecimals), image.Point{0, 0}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-02-15 21:19:04 -05:00
|
|
|
desc: "accommodates X scale that needs more height",
|
|
|
|
yp: &YProperties{
|
|
|
|
Min: 1,
|
|
|
|
Max: 3,
|
|
|
|
ReqXHeight: 4,
|
|
|
|
ScaleMode: YScaleModeAnchored,
|
|
|
|
},
|
|
|
|
cvsAr: image.Rect(0, 0, 3, 6),
|
|
|
|
wantWidth: 2,
|
2019-02-14 00:37:35 -05:00
|
|
|
want: &YDetails{
|
|
|
|
Width: 2,
|
|
|
|
Start: image.Point{1, 0},
|
|
|
|
End: image.Point{1, 2},
|
|
|
|
Scale: mustNewYScale(0, 3, 2, nonZeroDecimals, YScaleModeAnchored),
|
|
|
|
Labels: []*Label{
|
|
|
|
{NewValue(0, nonZeroDecimals), image.Point{0, 1}},
|
|
|
|
{NewValue(1.72, nonZeroDecimals), image.Point{0, 0}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-02-15 21:19:04 -05:00
|
|
|
desc: "success for adaptive scale",
|
|
|
|
yp: &YProperties{
|
|
|
|
Min: 1,
|
|
|
|
Max: 6,
|
|
|
|
ReqXHeight: 2,
|
|
|
|
ScaleMode: YScaleModeAdaptive,
|
|
|
|
},
|
|
|
|
cvsAr: image.Rect(0, 0, 3, 4),
|
|
|
|
wantWidth: 2,
|
2019-01-26 22:23:55 -05:00
|
|
|
want: &YDetails{
|
|
|
|
Width: 2,
|
|
|
|
Start: image.Point{1, 0},
|
|
|
|
End: image.Point{1, 2},
|
|
|
|
Scale: mustNewYScale(1, 6, 2, nonZeroDecimals, YScaleModeAdaptive),
|
|
|
|
Labels: []*Label{
|
|
|
|
{NewValue(1, nonZeroDecimals), image.Point{0, 1}},
|
|
|
|
{NewValue(3.88, nonZeroDecimals), image.Point{0, 0}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-01-08 00:24:48 -05:00
|
|
|
{
|
2019-02-15 21:19:04 -05:00
|
|
|
desc: "cvsWidth just accommodates the longest label",
|
|
|
|
yp: &YProperties{
|
|
|
|
Min: 0,
|
|
|
|
Max: 3,
|
|
|
|
ReqXHeight: 2,
|
|
|
|
},
|
|
|
|
cvsAr: image.Rect(0, 0, 6, 4),
|
|
|
|
wantWidth: 2,
|
2019-01-08 00:24:48 -05:00
|
|
|
want: &YDetails{
|
|
|
|
Width: 5,
|
2019-01-13 00:03:19 -05:00
|
|
|
Start: image.Point{4, 0},
|
|
|
|
End: image.Point{4, 2},
|
2019-01-26 22:23:55 -05:00
|
|
|
Scale: mustNewYScale(0, 3, 2, nonZeroDecimals, YScaleModeAnchored),
|
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-02-15 21:19:04 -05:00
|
|
|
desc: "cvsWidth is more than we need",
|
|
|
|
yp: &YProperties{
|
|
|
|
Min: 0,
|
|
|
|
Max: 3,
|
|
|
|
ReqXHeight: 2,
|
|
|
|
},
|
|
|
|
cvsAr: image.Rect(0, 0, 7, 4),
|
|
|
|
wantWidth: 2,
|
2019-01-08 00:24:48 -05:00
|
|
|
want: &YDetails{
|
|
|
|
Width: 5,
|
2019-01-13 00:03:19 -05:00
|
|
|
Start: image.Point{4, 0},
|
|
|
|
End: image.Point{4, 2},
|
2019-01-26 22:23:55 -05:00
|
|
|
Scale: mustNewYScale(0, 3, 2, nonZeroDecimals, YScaleModeAnchored),
|
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-02-15 21:19:04 -05:00
|
|
|
gotWidth := RequiredWidth(tc.yp.Min, tc.yp.Max)
|
2019-01-07 00:15:31 -05:00
|
|
|
if gotWidth != tc.wantWidth {
|
|
|
|
t.Errorf("RequiredWidth => got %v, want %v", gotWidth, tc.wantWidth)
|
|
|
|
}
|
|
|
|
|
2019-02-15 21:19:04 -05:00
|
|
|
got, err := NewYDetails(tc.cvsAr, tc.yp)
|
2019-01-08 00:24:48 -05:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-01-12 15:55:49 -05:00
|
|
|
|
|
|
|
func TestNewXDetails(t *testing.T) {
|
|
|
|
tests := []struct {
|
2019-02-15 22:44:40 -05:00
|
|
|
desc string
|
|
|
|
xp *XProperties
|
|
|
|
cvsAr image.Rectangle
|
|
|
|
want *XDetails
|
|
|
|
wantErr bool
|
2019-01-12 15:55:49 -05:00
|
|
|
}{
|
|
|
|
{
|
2019-02-15 22:44:40 -05:00
|
|
|
desc: "fails when min is negative",
|
|
|
|
xp: &XProperties{
|
|
|
|
Min: -1,
|
|
|
|
Max: 0,
|
|
|
|
ReqYWidth: 0,
|
|
|
|
},
|
|
|
|
cvsAr: image.Rect(0, 0, 2, 3),
|
|
|
|
wantErr: true,
|
2019-01-12 15:55:49 -05:00
|
|
|
},
|
|
|
|
{
|
2019-02-15 22:44:40 -05:00
|
|
|
desc: "fails when cvsAr isn't wide enough",
|
|
|
|
xp: &XProperties{
|
|
|
|
Min: 0,
|
|
|
|
Max: 0,
|
|
|
|
ReqYWidth: 0,
|
|
|
|
},
|
|
|
|
cvsAr: image.Rect(0, 0, 1, 3),
|
|
|
|
wantErr: true,
|
2019-01-13 00:03:19 -05:00
|
|
|
},
|
|
|
|
{
|
2019-02-15 22:44:40 -05:00
|
|
|
desc: "fails when cvsAr isn't tall enough",
|
|
|
|
xp: &XProperties{
|
|
|
|
Min: 0,
|
|
|
|
Max: 0,
|
|
|
|
ReqYWidth: 0,
|
|
|
|
},
|
|
|
|
cvsAr: image.Rect(0, 0, 3, 2),
|
|
|
|
wantErr: true,
|
2019-01-12 15:55:49 -05:00
|
|
|
},
|
|
|
|
{
|
2019-02-15 22:44:40 -05:00
|
|
|
desc: "works with no data points",
|
|
|
|
xp: &XProperties{
|
|
|
|
Min: 0,
|
|
|
|
Max: 0,
|
|
|
|
ReqYWidth: 0,
|
|
|
|
},
|
|
|
|
cvsAr: image.Rect(0, 0, 2, 3),
|
2019-01-12 15:55:49 -05:00
|
|
|
want: &XDetails{
|
2019-01-13 00:03:19 -05:00
|
|
|
Start: image.Point{0, 1},
|
|
|
|
End: image.Point{1, 1},
|
2019-02-15 22:44:40 -05:00
|
|
|
Scale: mustNewXScale(0, 0, 1, nonZeroDecimals),
|
2019-01-12 15:55:49 -05:00
|
|
|
Labels: []*Label{
|
|
|
|
{
|
|
|
|
Value: NewValue(0, nonZeroDecimals),
|
2019-01-13 00:03:19 -05:00
|
|
|
Pos: image.Point{1, 2},
|
2019-01-12 15:55:49 -05:00
|
|
|
},
|
|
|
|
},
|
2019-02-17 21:24:23 -05:00
|
|
|
Properties: &XProperties{
|
|
|
|
Min: 0,
|
|
|
|
Max: 0,
|
|
|
|
ReqYWidth: 0,
|
|
|
|
},
|
2019-01-12 15:55:49 -05:00
|
|
|
},
|
|
|
|
},
|
2019-02-14 00:37:35 -05:00
|
|
|
{
|
2019-02-15 22:44:40 -05:00
|
|
|
desc: "works with no data points, vertical",
|
|
|
|
xp: &XProperties{
|
|
|
|
Min: 0,
|
|
|
|
Max: 0,
|
|
|
|
ReqYWidth: 0,
|
|
|
|
LO: LabelOrientationVertical,
|
|
|
|
},
|
|
|
|
cvsAr: image.Rect(0, 0, 2, 3),
|
2019-02-14 00:37:35 -05:00
|
|
|
want: &XDetails{
|
|
|
|
Start: image.Point{0, 1},
|
|
|
|
End: image.Point{1, 1},
|
2019-02-15 22:44:40 -05:00
|
|
|
Scale: mustNewXScale(0, 0, 1, nonZeroDecimals),
|
2019-02-14 00:37:35 -05:00
|
|
|
Labels: []*Label{
|
|
|
|
{
|
|
|
|
Value: NewValue(0, nonZeroDecimals),
|
|
|
|
Pos: image.Point{1, 2},
|
|
|
|
},
|
|
|
|
},
|
2019-02-17 21:24:23 -05:00
|
|
|
Properties: &XProperties{
|
|
|
|
Min: 0,
|
|
|
|
Max: 0,
|
|
|
|
ReqYWidth: 0,
|
|
|
|
LO: LabelOrientationVertical,
|
|
|
|
},
|
2019-02-14 00:37:35 -05:00
|
|
|
},
|
|
|
|
},
|
2019-01-12 15:55:49 -05:00
|
|
|
{
|
2019-02-15 22:44:40 -05:00
|
|
|
desc: "accounts for non-zero yStart",
|
|
|
|
xp: &XProperties{
|
|
|
|
Min: 0,
|
|
|
|
Max: 0,
|
|
|
|
ReqYWidth: 2,
|
|
|
|
},
|
|
|
|
cvsAr: image.Rect(0, 0, 4, 5),
|
2019-01-12 15:55:49 -05:00
|
|
|
want: &XDetails{
|
2019-01-13 00:03:19 -05:00
|
|
|
Start: image.Point{2, 3},
|
|
|
|
End: image.Point{3, 3},
|
2019-02-15 22:44:40 -05:00
|
|
|
Scale: mustNewXScale(0, 0, 1, nonZeroDecimals),
|
2019-01-12 15:55:49 -05:00
|
|
|
Labels: []*Label{
|
|
|
|
{
|
|
|
|
Value: NewValue(0, nonZeroDecimals),
|
2019-01-13 00:03:19 -05:00
|
|
|
Pos: image.Point{3, 4},
|
2019-01-12 15:55:49 -05:00
|
|
|
},
|
|
|
|
},
|
2019-02-17 21:24:23 -05:00
|
|
|
Properties: &XProperties{
|
|
|
|
Min: 0,
|
|
|
|
Max: 0,
|
|
|
|
ReqYWidth: 2,
|
|
|
|
},
|
2019-01-12 15:55:49 -05:00
|
|
|
},
|
|
|
|
},
|
2019-02-14 00:37:35 -05:00
|
|
|
{
|
2019-02-15 22:44:40 -05:00
|
|
|
desc: "accounts for longer vertical labels, the tallest didn't fit",
|
|
|
|
xp: &XProperties{
|
|
|
|
Min: 0,
|
|
|
|
Max: 1000,
|
|
|
|
ReqYWidth: 2,
|
|
|
|
LO: LabelOrientationVertical,
|
|
|
|
},
|
|
|
|
cvsAr: image.Rect(0, 0, 10, 10),
|
2019-02-14 00:37:35 -05:00
|
|
|
want: &XDetails{
|
|
|
|
Start: image.Point{2, 5},
|
|
|
|
End: image.Point{9, 5},
|
2019-02-15 22:44:40 -05:00
|
|
|
Scale: mustNewXScale(0, 1000, 7, nonZeroDecimals),
|
2019-02-14 00:37:35 -05:00
|
|
|
Labels: []*Label{
|
|
|
|
{
|
|
|
|
Value: NewValue(0, nonZeroDecimals),
|
|
|
|
Pos: image.Point{3, 6},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Value: NewValue(615, nonZeroDecimals),
|
|
|
|
Pos: image.Point{7, 6},
|
|
|
|
},
|
|
|
|
},
|
2019-02-17 21:24:23 -05:00
|
|
|
Properties: &XProperties{
|
|
|
|
Min: 0,
|
|
|
|
Max: 1000,
|
|
|
|
ReqYWidth: 2,
|
|
|
|
LO: LabelOrientationVertical,
|
|
|
|
},
|
2019-02-14 00:37:35 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-02-15 22:44:40 -05:00
|
|
|
desc: "accounts for longer vertical labels, the tallest label fits",
|
|
|
|
xp: &XProperties{
|
|
|
|
Min: 0,
|
|
|
|
Max: 999,
|
|
|
|
ReqYWidth: 2,
|
|
|
|
LO: LabelOrientationVertical,
|
|
|
|
},
|
|
|
|
cvsAr: image.Rect(0, 0, 10, 10),
|
2019-02-14 00:37:35 -05:00
|
|
|
want: &XDetails{
|
|
|
|
Start: image.Point{2, 6},
|
|
|
|
End: image.Point{9, 6},
|
2019-02-15 22:44:40 -05:00
|
|
|
Scale: mustNewXScale(0, 999, 7, nonZeroDecimals),
|
2019-02-14 00:37:35 -05:00
|
|
|
Labels: []*Label{
|
|
|
|
{
|
|
|
|
Value: NewValue(0, nonZeroDecimals),
|
|
|
|
Pos: image.Point{3, 7},
|
|
|
|
},
|
|
|
|
{
|
2019-02-15 22:44:40 -05:00
|
|
|
Value: NewValue(615, nonZeroDecimals),
|
2019-02-14 00:37:35 -05:00
|
|
|
Pos: image.Point{7, 7},
|
|
|
|
},
|
|
|
|
},
|
2019-02-17 21:24:23 -05:00
|
|
|
Properties: &XProperties{
|
|
|
|
Min: 0,
|
|
|
|
Max: 999,
|
|
|
|
ReqYWidth: 2,
|
|
|
|
LO: LabelOrientationVertical,
|
|
|
|
},
|
2019-02-14 00:37:35 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-02-15 22:44:40 -05:00
|
|
|
desc: "accounts for longer custom labels, vertical",
|
|
|
|
xp: &XProperties{
|
|
|
|
Min: 0,
|
|
|
|
Max: 1,
|
|
|
|
ReqYWidth: 5,
|
|
|
|
CustomLabels: map[int]string{
|
|
|
|
0: "start",
|
|
|
|
1: "end",
|
|
|
|
},
|
|
|
|
LO: LabelOrientationVertical,
|
2019-02-14 00:37:35 -05:00
|
|
|
},
|
2019-02-15 22:44:40 -05:00
|
|
|
cvsAr: image.Rect(0, 0, 20, 10),
|
2019-02-14 00:37:35 -05:00
|
|
|
want: &XDetails{
|
|
|
|
Start: image.Point{5, 4},
|
|
|
|
End: image.Point{19, 4},
|
2019-02-15 22:44:40 -05:00
|
|
|
Scale: mustNewXScale(0, 1, 14, nonZeroDecimals),
|
2019-02-14 00:37:35 -05:00
|
|
|
Labels: []*Label{
|
|
|
|
{
|
|
|
|
Value: NewTextValue("start"),
|
|
|
|
Pos: image.Point{6, 5},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Value: NewTextValue("end"),
|
|
|
|
Pos: image.Point{19, 5},
|
|
|
|
},
|
|
|
|
},
|
2019-02-17 21:24:23 -05:00
|
|
|
Properties: &XProperties{
|
|
|
|
Min: 0,
|
|
|
|
Max: 1,
|
|
|
|
ReqYWidth: 5,
|
|
|
|
CustomLabels: map[int]string{
|
|
|
|
0: "start",
|
|
|
|
1: "end",
|
|
|
|
},
|
|
|
|
LO: LabelOrientationVertical,
|
|
|
|
},
|
2019-02-14 00:37:35 -05:00
|
|
|
},
|
|
|
|
},
|
2019-01-12 15:55:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
t.Run(tc.desc, func(t *testing.T) {
|
2019-02-15 22:44:40 -05:00
|
|
|
got, err := NewXDetails(tc.cvsAr, tc.xp)
|
2019-01-12 15:55:49 -05:00
|
|
|
if (err != nil) != tc.wantErr {
|
|
|
|
t.Errorf("NewXDetails => unexpected error: %v, wantErr: %v", err, tc.wantErr)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2019-02-18 18:48:19 -05:00
|
|
|
t.Log(fmt.Sprintf("got: %v", got))
|
2019-01-12 15:55:49 -05:00
|
|
|
|
|
|
|
if diff := pretty.Compare(tc.want, got); diff != "" {
|
|
|
|
t.Errorf("NewXDetails => unexpected diff (-want, +got):\n%s", diff)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-02-13 22:53:19 -05:00
|
|
|
|
|
|
|
func TestRequiredHeight(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
desc string
|
2019-02-15 22:44:40 -05:00
|
|
|
max int
|
2019-02-13 22:53:19 -05:00
|
|
|
customLabels map[int]string
|
|
|
|
labelOrientation LabelOrientation
|
|
|
|
want int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "horizontal orientation",
|
|
|
|
want: 2,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "vertical orientation, no custom labels, need single row for max label",
|
2019-02-15 22:44:40 -05:00
|
|
|
max: 8,
|
2019-02-13 22:53:19 -05:00
|
|
|
labelOrientation: LabelOrientationVertical,
|
|
|
|
want: 2,
|
|
|
|
},
|
|
|
|
{
|
2019-02-15 22:44:40 -05:00
|
|
|
desc: "vertical orientation, no custom labels, need multiple rows for max label",
|
|
|
|
max: 100,
|
2019-02-13 22:53:19 -05:00
|
|
|
labelOrientation: LabelOrientationVertical,
|
|
|
|
want: 4,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "vertical orientation, custom labels but all shorter than max label",
|
2019-02-15 22:44:40 -05:00
|
|
|
max: 100,
|
2019-02-13 22:53:19 -05:00
|
|
|
customLabels: map[int]string{1: "a", 2: "b"},
|
|
|
|
labelOrientation: LabelOrientationVertical,
|
|
|
|
want: 4,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "vertical orientation, custom labels and some longer than max label",
|
2019-02-15 22:44:40 -05:00
|
|
|
max: 99,
|
2019-02-13 22:53:19 -05:00
|
|
|
customLabels: map[int]string{1: "a", 2: "bbbbb"},
|
|
|
|
labelOrientation: LabelOrientationVertical,
|
|
|
|
want: 6,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
t.Run(tc.desc, func(t *testing.T) {
|
2019-02-15 22:44:40 -05:00
|
|
|
got := RequiredHeight(tc.max, tc.customLabels, tc.labelOrientation)
|
2019-02-13 22:53:19 -05:00
|
|
|
if got != tc.want {
|
|
|
|
t.Errorf("RequiredHeight => %d, want %d", got, tc.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|