go-chart/style_test.go

215 lines
5.8 KiB
Go
Raw Permalink Normal View History

2016-07-11 02:06:14 -04:00
package chart
import (
"testing"
"github.com/blendlabs/go-assert"
"github.com/golang/freetype/truetype"
"github.com/wcharczuk/go-chart/drawing"
)
func TestStyleIsZero(t *testing.T) {
assert := assert.New(t)
zero := Style{}
assert.True(zero.IsZero())
strokeColor := Style{StrokeColor: drawing.ColorWhite}
assert.False(strokeColor.IsZero())
fillColor := Style{FillColor: drawing.ColorWhite}
assert.False(fillColor.IsZero())
strokeWidth := Style{StrokeWidth: 5.0}
assert.False(strokeWidth.IsZero())
fontSize := Style{FontSize: 12.0}
assert.False(fontSize.IsZero())
fontColor := Style{FontColor: drawing.ColorWhite}
assert.False(fontColor.IsZero())
font := Style{Font: &truetype.Font{}}
assert.False(font.IsZero())
}
func TestStyleGetStrokeColor(t *testing.T) {
assert := assert.New(t)
unset := Style{}
assert.Equal(drawing.ColorTransparent, unset.GetStrokeColor())
assert.Equal(drawing.ColorWhite, unset.GetStrokeColor(drawing.ColorWhite))
set := Style{StrokeColor: drawing.ColorWhite}
assert.Equal(drawing.ColorWhite, set.GetStrokeColor())
assert.Equal(drawing.ColorWhite, set.GetStrokeColor(drawing.ColorBlack))
}
func TestStyleGetFillColor(t *testing.T) {
assert := assert.New(t)
unset := Style{}
assert.Equal(drawing.ColorTransparent, unset.GetFillColor())
assert.Equal(drawing.ColorWhite, unset.GetFillColor(drawing.ColorWhite))
set := Style{FillColor: drawing.ColorWhite}
assert.Equal(drawing.ColorWhite, set.GetFillColor())
assert.Equal(drawing.ColorWhite, set.GetFillColor(drawing.ColorBlack))
}
func TestStyleGetStrokeWidth(t *testing.T) {
assert := assert.New(t)
unset := Style{}
assert.Equal(DefaultStrokeWidth, unset.GetStrokeWidth())
assert.Equal(DefaultStrokeWidth+1, unset.GetStrokeWidth(DefaultStrokeWidth+1))
set := Style{StrokeWidth: DefaultStrokeWidth + 2}
assert.Equal(DefaultStrokeWidth+2, set.GetStrokeWidth())
assert.Equal(DefaultStrokeWidth+2, set.GetStrokeWidth(DefaultStrokeWidth+1))
}
func TestStyleGetFontSize(t *testing.T) {
assert := assert.New(t)
unset := Style{}
assert.Equal(DefaultFontSize, unset.GetFontSize())
assert.Equal(DefaultFontSize+1, unset.GetFontSize(DefaultFontSize+1))
set := Style{FontSize: DefaultFontSize + 2}
assert.Equal(DefaultFontSize+2, set.GetFontSize())
assert.Equal(DefaultFontSize+2, set.GetFontSize(DefaultFontSize+1))
}
func TestStyleGetFontColor(t *testing.T) {
assert := assert.New(t)
unset := Style{}
assert.Equal(drawing.ColorTransparent, unset.GetFontColor())
assert.Equal(drawing.ColorWhite, unset.GetFontColor(drawing.ColorWhite))
set := Style{FontColor: drawing.ColorWhite}
assert.Equal(drawing.ColorWhite, set.GetFontColor())
assert.Equal(drawing.ColorWhite, set.GetFontColor(drawing.ColorBlack))
}
func TestStyleGetFont(t *testing.T) {
assert := assert.New(t)
f, err := GetDefaultFont()
assert.Nil(err)
unset := Style{}
assert.Nil(unset.GetFont())
assert.Equal(f, unset.GetFont(f))
set := Style{Font: f}
assert.NotNil(set.GetFont())
}
func TestStyleGetPadding(t *testing.T) {
assert := assert.New(t)
unset := Style{}
assert.True(unset.GetPadding().IsZero())
assert.False(unset.GetPadding(DefaultBackgroundPadding).IsZero())
assert.Equal(DefaultBackgroundPadding, unset.GetPadding(DefaultBackgroundPadding))
set := Style{Padding: DefaultBackgroundPadding}
assert.False(set.GetPadding().IsZero())
assert.Equal(DefaultBackgroundPadding, set.GetPadding())
assert.Equal(DefaultBackgroundPadding, set.GetPadding(Box{
Top: DefaultBackgroundPadding.Top + 1,
Left: DefaultBackgroundPadding.Left + 1,
Right: DefaultBackgroundPadding.Right + 1,
Bottom: DefaultBackgroundPadding.Bottom + 1,
}))
}
func TestStyleWithDefaultsFrom(t *testing.T) {
assert := assert.New(t)
f, err := GetDefaultFont()
assert.Nil(err)
unset := Style{}
set := Style{
StrokeColor: drawing.ColorWhite,
StrokeWidth: 5.0,
FillColor: drawing.ColorWhite,
FontColor: drawing.ColorWhite,
Font: f,
Padding: DefaultBackgroundPadding,
}
coalesced := unset.InheritFrom(set)
2016-07-11 02:06:14 -04:00
assert.Equal(set, coalesced)
}
2016-07-29 19:36:29 -04:00
func TestStyleGetStrokeOptions(t *testing.T) {
2016-07-11 02:06:14 -04:00
assert := assert.New(t)
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()
2016-07-11 02:06:14 -04:00
assert.False(svgStroke.StrokeColor.IsZero())
assert.NotZero(svgStroke.StrokeWidth)
assert.True(svgStroke.FillColor.IsZero())
assert.True(svgStroke.FontColor.IsZero())
}
2016-07-29 19:36:29 -04:00
func TestStyleGetFillOptions(t *testing.T) {
2016-07-11 02:06:14 -04:00
assert := assert.New(t)
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()
2016-07-11 02:06:14 -04:00
assert.False(svgFill.FillColor.IsZero())
assert.Zero(svgFill.StrokeWidth)
assert.True(svgFill.StrokeColor.IsZero())
assert.True(svgFill.FontColor.IsZero())
}
2016-07-29 19:36:29 -04:00
func TestStyleGetFillAndStrokeOptions(t *testing.T) {
2016-07-11 02:06:14 -04:00
assert := assert.New(t)
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()
2016-07-11 02:06:14 -04:00
assert.False(svgFillAndStroke.FillColor.IsZero())
assert.NotZero(svgFillAndStroke.StrokeWidth)
assert.False(svgFillAndStroke.StrokeColor.IsZero())
assert.True(svgFillAndStroke.FontColor.IsZero())
}
2016-07-29 19:36:29 -04:00
func TestStyleGetTextOptions(t *testing.T) {
2016-07-11 02:06:14 -04:00
assert := assert.New(t)
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()
2016-07-11 02:06:14 -04:00
assert.True(svgStroke.StrokeColor.IsZero())
assert.Zero(svgStroke.StrokeWidth)
assert.True(svgStroke.FillColor.IsZero())
assert.False(svgStroke.FontColor.IsZero())
}