// MIT License // Copyright (c) 2022 Tree Xie // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. package charts import ( "testing" "github.com/stretchr/testify/assert" "github.com/wcharczuk/go-chart/v2" "github.com/wcharczuk/go-chart/v2/drawing" ) func TestOptionFunc(t *testing.T) { assert := assert.New(t) fns := []OptionFunc{ TypeOptionFunc(ChartOutputPNG), FontFamilyOptionFunc("fontFamily"), ThemeOptionFunc("black"), TitleOptionFunc(TitleOption{ Text: "title", }), LegendOptionFunc(LegendOption{ Data: []string{ "a", "b", }, }), XAxisOptionFunc(NewXAxisOption([]string{ "Mon", "Tue", })), YAxisOptionFunc(YAxisOption{ Min: NewFloatPoint(0), Max: NewFloatPoint(100), }), WidthOptionFunc(400), HeightOptionFunc(300), PaddingOptionFunc(chart.Box{ Top: 10, }), BoxOptionFunc(chart.Box{ Left: 0, Right: 300, }), ChildOptionFunc(ChartOption{}), RadarIndicatorOptionFunc(RadarIndicator{ Min: 0, Max: 10, }), BackgroundColorOptionFunc(drawing.ColorBlack), } opt := ChartOption{} for _, fn := range fns { fn(&opt) } assert.Equal("png", opt.Type) assert.Equal("fontFamily", opt.FontFamily) assert.Equal("black", opt.Theme) assert.Equal(TitleOption{ Text: "title", }, opt.Title) assert.Equal(LegendOption{ Data: []string{ "a", "b", }, }, opt.Legend) assert.Equal(NewXAxisOption([]string{ "Mon", "Tue", }), opt.XAxis) assert.Equal([]YAxisOption{ { Min: NewFloatPoint(0), Max: NewFloatPoint(100), }, }, opt.YAxisList) assert.Equal(400, opt.Width) assert.Equal(300, opt.Height) assert.Equal(chart.Box{ Top: 10, }, opt.Padding) assert.Equal(chart.Box{ Left: 0, Right: 300, }, opt.Box) assert.Equal(1, len(opt.Children)) assert.Equal([]RadarIndicator{ { Min: 0, Max: 10, }, }, opt.RadarIndicators) assert.Equal(drawing.ColorBlack, opt.BackgroundColor) } func TestLineRender(t *testing.T) { assert := assert.New(t) d, err := LineRender([][]float64{ { 1, 2, 3, }, { 1, 5, 2, }, }, XAxisOptionFunc(NewXAxisOption([]string{ "01", "02", "03", })), ) assert.Nil(err) data, err := d.Bytes() assert.Nil(err) assert.Equal("\\n010203024681012", string(data)) } func TestBarRender(t *testing.T) { assert := assert.New(t) d, err := BarRender([][]float64{ { 1, 2, 3, }, { 1, 5, 2, }, }, XAxisOptionFunc(NewXAxisOption([]string{ "01", "02", "03", })), ) assert.Nil(err) data, err := d.Bytes() assert.Nil(err) assert.Equal("\\n010203024681012", string(data)) } func TestPieRender(t *testing.T) { assert := assert.New(t) d, err := PieRender([]float64{ 1, 3, 5, }) assert.Nil(err) data, err := d.Bytes() assert.Nil(err) assert.Equal("\\n", string(data)) } func TestRadarRender(t *testing.T) { assert := assert.New(t) d, err := RadarRender([][]float64{ { 1, 2, 3, }, { 1, 5, 2, }, }, RadarIndicatorOptionFunc([]RadarIndicator{ { Name: "A", Min: 0, Max: 10, }, { Name: "B", Min: 0, Max: 10, }, { Name: "C", Min: 0, Max: 10, }, }...), ) assert.Nil(err) data, err := d.Bytes() assert.Nil(err) assert.Equal("\\nABC", string(data)) } func TestFunnelRender(t *testing.T) { assert := assert.New(t) d, err := FunnelRender([]float64{ 5, 3, 1, }) assert.Nil(err) data, err := d.Bytes() assert.Nil(err) assert.Equal("\\n(100%)(60%)(20%)", string(data)) }