go-chart/style_test.go

215 lines
6.6 KiB
Go
Raw Permalink Normal View History

2016-07-11 02:06:14 -04:00
package chart
import (
"testing"
"github.com/golang/freetype/truetype"
"github.com/wcharczuk/go-chart/v2/drawing"
"github.com/wcharczuk/go-chart/v2/testutil"
2016-07-11 02:06:14 -04:00
)
func TestStyleIsZero(t *testing.T) {
// replaced new assertions helper
2016-07-11 02:06:14 -04:00
zero := Style{}
testutil.AssertTrue(t, zero.IsZero())
2016-07-11 02:06:14 -04:00
strokeColor := Style{StrokeColor: drawing.ColorWhite}
testutil.AssertFalse(t, strokeColor.IsZero())
2016-07-11 02:06:14 -04:00
fillColor := Style{FillColor: drawing.ColorWhite}
testutil.AssertFalse(t, fillColor.IsZero())
2016-07-11 02:06:14 -04:00
strokeWidth := Style{StrokeWidth: 5.0}
testutil.AssertFalse(t, strokeWidth.IsZero())
2016-07-11 02:06:14 -04:00
fontSize := Style{FontSize: 12.0}
testutil.AssertFalse(t, fontSize.IsZero())
2016-07-11 02:06:14 -04:00
fontColor := Style{FontColor: drawing.ColorWhite}
testutil.AssertFalse(t, fontColor.IsZero())
2016-07-11 02:06:14 -04:00
font := Style{Font: &truetype.Font{}}
testutil.AssertFalse(t, font.IsZero())
2016-07-11 02:06:14 -04:00
}
func TestStyleGetStrokeColor(t *testing.T) {
// replaced new assertions helper
2016-07-11 02:06:14 -04:00
unset := Style{}
testutil.AssertEqual(t, drawing.ColorTransparent, unset.GetStrokeColor())
testutil.AssertEqual(t, drawing.ColorWhite, unset.GetStrokeColor(drawing.ColorWhite))
2016-07-11 02:06:14 -04:00
set := Style{StrokeColor: drawing.ColorWhite}
testutil.AssertEqual(t, drawing.ColorWhite, set.GetStrokeColor())
testutil.AssertEqual(t, drawing.ColorWhite, set.GetStrokeColor(drawing.ColorBlack))
2016-07-11 02:06:14 -04:00
}
func TestStyleGetFillColor(t *testing.T) {
// replaced new assertions helper
2016-07-11 02:06:14 -04:00
unset := Style{}
testutil.AssertEqual(t, drawing.ColorTransparent, unset.GetFillColor())
testutil.AssertEqual(t, drawing.ColorWhite, unset.GetFillColor(drawing.ColorWhite))
2016-07-11 02:06:14 -04:00
set := Style{FillColor: drawing.ColorWhite}
testutil.AssertEqual(t, drawing.ColorWhite, set.GetFillColor())
testutil.AssertEqual(t, drawing.ColorWhite, set.GetFillColor(drawing.ColorBlack))
2016-07-11 02:06:14 -04:00
}
func TestStyleGetStrokeWidth(t *testing.T) {
// replaced new assertions helper
2016-07-11 02:06:14 -04:00
unset := Style{}
testutil.AssertEqual(t, DefaultStrokeWidth, unset.GetStrokeWidth())
testutil.AssertEqual(t, DefaultStrokeWidth+1, unset.GetStrokeWidth(DefaultStrokeWidth+1))
2016-07-11 02:06:14 -04:00
set := Style{StrokeWidth: DefaultStrokeWidth + 2}
testutil.AssertEqual(t, DefaultStrokeWidth+2, set.GetStrokeWidth())
testutil.AssertEqual(t, DefaultStrokeWidth+2, set.GetStrokeWidth(DefaultStrokeWidth+1))
2016-07-11 02:06:14 -04:00
}
func TestStyleGetFontSize(t *testing.T) {
// replaced new assertions helper
2016-07-11 02:06:14 -04:00
unset := Style{}
testutil.AssertEqual(t, DefaultFontSize, unset.GetFontSize())
testutil.AssertEqual(t, DefaultFontSize+1, unset.GetFontSize(DefaultFontSize+1))
2016-07-11 02:06:14 -04:00
set := Style{FontSize: DefaultFontSize + 2}
testutil.AssertEqual(t, DefaultFontSize+2, set.GetFontSize())
testutil.AssertEqual(t, DefaultFontSize+2, set.GetFontSize(DefaultFontSize+1))
2016-07-11 02:06:14 -04:00
}
func TestStyleGetFontColor(t *testing.T) {
// replaced new assertions helper
2016-07-11 02:06:14 -04:00
unset := Style{}
testutil.AssertEqual(t, drawing.ColorTransparent, unset.GetFontColor())
testutil.AssertEqual(t, drawing.ColorWhite, unset.GetFontColor(drawing.ColorWhite))
2016-07-11 02:06:14 -04:00
set := Style{FontColor: drawing.ColorWhite}
testutil.AssertEqual(t, drawing.ColorWhite, set.GetFontColor())
testutil.AssertEqual(t, drawing.ColorWhite, set.GetFontColor(drawing.ColorBlack))
2016-07-11 02:06:14 -04:00
}
func TestStyleGetFont(t *testing.T) {
// replaced new assertions helper
2016-07-11 02:06:14 -04:00
f, err := GetDefaultFont()
testutil.AssertNil(t, err)
2016-07-11 02:06:14 -04:00
unset := Style{}
testutil.AssertNil(t, unset.GetFont())
testutil.AssertEqual(t, f, unset.GetFont(f))
2016-07-11 02:06:14 -04:00
set := Style{Font: f}
testutil.AssertNotNil(t, set.GetFont())
2016-07-11 02:06:14 -04:00
}
func TestStyleGetPadding(t *testing.T) {
// replaced new assertions helper
2016-07-11 02:06:14 -04:00
unset := Style{}
testutil.AssertTrue(t, unset.GetPadding().IsZero())
testutil.AssertFalse(t, unset.GetPadding(DefaultBackgroundPadding).IsZero())
testutil.AssertEqual(t, DefaultBackgroundPadding, unset.GetPadding(DefaultBackgroundPadding))
2016-07-11 02:06:14 -04:00
set := Style{Padding: DefaultBackgroundPadding}
testutil.AssertFalse(t, set.GetPadding().IsZero())
testutil.AssertEqual(t, DefaultBackgroundPadding, set.GetPadding())
testutil.AssertEqual(t, DefaultBackgroundPadding, set.GetPadding(Box{
2016-07-11 02:06:14 -04:00
Top: DefaultBackgroundPadding.Top + 1,
Left: DefaultBackgroundPadding.Left + 1,
Right: DefaultBackgroundPadding.Right + 1,
Bottom: DefaultBackgroundPadding.Bottom + 1,
}))
}
func TestStyleWithDefaultsFrom(t *testing.T) {
// replaced new assertions helper
2016-07-11 02:06:14 -04:00
f, err := GetDefaultFont()
testutil.AssertNil(t, err)
2016-07-11 02:06:14 -04:00
unset := Style{}
set := Style{
StrokeColor: drawing.ColorWhite,
StrokeWidth: 5.0,
FillColor: drawing.ColorWhite,
FontColor: drawing.ColorWhite,
Font: f,
Padding: DefaultBackgroundPadding,
}
coalesced := unset.InheritFrom(set)
testutil.AssertEqual(t, set, coalesced)
2016-07-11 02:06:14 -04:00
}
2016-07-29 19:36:29 -04:00
func TestStyleGetStrokeOptions(t *testing.T) {
// replaced new assertions helper
2016-07-11 02:06:14 -04:00
set := Style{
StrokeColor: drawing.ColorWhite,
StrokeWidth: 5.0,
FillColor: drawing.ColorWhite,
FontColor: drawing.ColorWhite,
Padding: DefaultBackgroundPadding,
}
2016-07-29 19:36:29 -04:00
svgStroke := set.GetStrokeOptions()
testutil.AssertFalse(t, svgStroke.StrokeColor.IsZero())
testutil.AssertNotZero(t, svgStroke.StrokeWidth)
testutil.AssertTrue(t, svgStroke.FillColor.IsZero())
testutil.AssertTrue(t, svgStroke.FontColor.IsZero())
2016-07-11 02:06:14 -04:00
}
2016-07-29 19:36:29 -04:00
func TestStyleGetFillOptions(t *testing.T) {
// replaced new assertions helper
2016-07-11 02:06:14 -04:00
set := Style{
StrokeColor: drawing.ColorWhite,
StrokeWidth: 5.0,
FillColor: drawing.ColorWhite,
FontColor: drawing.ColorWhite,
Padding: DefaultBackgroundPadding,
}
2016-07-29 19:36:29 -04:00
svgFill := set.GetFillOptions()
testutil.AssertFalse(t, svgFill.FillColor.IsZero())
testutil.AssertZero(t, svgFill.StrokeWidth)
testutil.AssertTrue(t, svgFill.StrokeColor.IsZero())
testutil.AssertTrue(t, svgFill.FontColor.IsZero())
2016-07-11 02:06:14 -04:00
}
2016-07-29 19:36:29 -04:00
func TestStyleGetFillAndStrokeOptions(t *testing.T) {
// replaced new assertions helper
2016-07-11 02:06:14 -04:00
set := Style{
StrokeColor: drawing.ColorWhite,
StrokeWidth: 5.0,
FillColor: drawing.ColorWhite,
FontColor: drawing.ColorWhite,
Padding: DefaultBackgroundPadding,
}
2016-07-29 19:36:29 -04:00
svgFillAndStroke := set.GetFillAndStrokeOptions()
testutil.AssertFalse(t, svgFillAndStroke.FillColor.IsZero())
testutil.AssertNotZero(t, svgFillAndStroke.StrokeWidth)
testutil.AssertFalse(t, svgFillAndStroke.StrokeColor.IsZero())
testutil.AssertTrue(t, svgFillAndStroke.FontColor.IsZero())
2016-07-11 02:06:14 -04:00
}
2016-07-29 19:36:29 -04:00
func TestStyleGetTextOptions(t *testing.T) {
// replaced new assertions helper
2016-07-11 02:06:14 -04:00
set := Style{
StrokeColor: drawing.ColorWhite,
StrokeWidth: 5.0,
FillColor: drawing.ColorWhite,
FontColor: drawing.ColorWhite,
Padding: DefaultBackgroundPadding,
}
2016-07-29 19:36:29 -04:00
svgStroke := set.GetTextOptions()
testutil.AssertTrue(t, svgStroke.StrokeColor.IsZero())
testutil.AssertZero(t, svgStroke.StrokeWidth)
testutil.AssertTrue(t, svgStroke.FillColor.IsZero())
testutil.AssertFalse(t, svgStroke.FontColor.IsZero())
2016-07-11 02:06:14 -04:00
}