// 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 ( "math" "testing" "github.com/stretchr/testify/assert" "github.com/wcharczuk/go-chart/v2" "github.com/wcharczuk/go-chart/v2/drawing" ) func TestParentOption(t *testing.T) { assert := assert.New(t) p, err := NewDraw(DrawOption{ Width: 400, Height: 300, }) assert.Nil(err) d, err := NewDraw(DrawOption{ Parent: p, }) assert.Nil(err) assert.Equal(p, d.parent) } func TestWidthHeightOption(t *testing.T) { assert := assert.New(t) // no parent width := 300 height := 200 d, err := NewDraw(DrawOption{ Width: width, Height: height, }) assert.Nil(err) assert.Equal(chart.Box{ Top: 0, Left: 0, Right: width, Bottom: height, }, d.Box) width = 500 height = 600 // with parent p, err := NewDraw( DrawOption{ Width: width, Height: height, }, PaddingOption(chart.NewBox(5, 5, 5, 5)), ) assert.Nil(err) d, err = NewDraw( DrawOption{ Parent: p, }, PaddingOption(chart.NewBox(1, 2, 3, 4)), ) assert.Nil(err) assert.Equal(chart.Box{ Top: 6, Left: 7, Right: 492, Bottom: 591, }, d.Box) } func TestPaddingOption(t *testing.T) { assert := assert.New(t) d, err := NewDraw(DrawOption{ Width: 400, Height: 300, }) assert.Nil(err) // 默认的box assert.Equal(chart.Box{ Right: 400, Bottom: 300, }, d.Box) // 设置padding之后的box d, err = NewDraw(DrawOption{ Width: 400, Height: 300, }, PaddingOption(chart.Box{ Left: 1, Top: 2, Right: 3, Bottom: 4, })) assert.Nil(err) assert.Equal(chart.Box{ Top: 2, Left: 1, Right: 397, Bottom: 296, }, d.Box) p := d // 设置父元素之后的box d, err = NewDraw( DrawOption{ Parent: p, }, PaddingOption(chart.Box{ Left: 1, Top: 2, Right: 3, Bottom: 4, }), ) assert.Nil(err) assert.Equal(chart.Box{ Top: 4, Left: 2, Right: 394, Bottom: 292, }, d.Box) } func TestParentTop(t *testing.T) { assert := assert.New(t) d1, err := NewDraw(DrawOption{ Width: 400, Height: 300, }) assert.Nil(err) d2, err := NewDraw(DrawOption{ Parent: d1, }) assert.Nil(err) d3, err := NewDraw(DrawOption{ Parent: d2, }) assert.Nil(err) assert.Equal(d2, d3.Parent()) assert.Equal(d1, d2.Parent()) assert.Equal(d1, d3.Top()) assert.Equal(d1, d2.Top()) } func TestDraw(t *testing.T) { assert := assert.New(t) tests := []struct { fn func(d *Draw) result string }{ // moveTo, lineTo { fn: func(d *Draw) { d.moveTo(1, 1) d.lineTo(2, 2) d.Render.Stroke() }, result: "\\n", }, // circle { fn: func(d *Draw) { d.circle(5, 2, 3) }, result: "\\n", }, // text { fn: func(d *Draw) { d.text("hello world!", 3, 6) }, result: "\\nhello world!", }, // line stroke { fn: func(d *Draw) { d.lineStroke([]Point{ { X: 1, Y: 2, }, { X: 3, Y: 4, }, }, LineStyle{ StrokeColor: drawing.ColorBlack, StrokeWidth: 1, }) }, result: "\\n", }, // set background { fn: func(d *Draw) { d.setBackground(400, 300, chart.ColorWhite) }, result: "\\n", }, // arcTo { fn: func(d *Draw) { chart.Style{ StrokeWidth: 1, StrokeColor: drawing.ColorBlack, FillColor: drawing.ColorBlue, }.WriteToRenderer(d.Render) d.arcTo(100, 100, 100, 100, 0, math.Pi/2) d.Render.Close() d.Render.FillStroke() }, result: "\\n", }, // pin { fn: func(d *Draw) { chart.Style{ StrokeWidth: 1, StrokeColor: drawing.Color{ R: 84, G: 112, B: 198, A: 255, }, FillColor: drawing.Color{ R: 84, G: 112, B: 198, A: 255, }, }.WriteToRenderer(d.Render) d.pin(30, 30, 30) }, result: "\\n", }, // arrow left { fn: func(d *Draw) { chart.Style{ StrokeWidth: 1, StrokeColor: drawing.Color{ R: 84, G: 112, B: 198, A: 255, }, FillColor: drawing.Color{ R: 84, G: 112, B: 198, A: 255, }, }.WriteToRenderer(d.Render) d.arrowLeft(30, 30, 16, 10) }, result: "\\n", }, // arrow right { fn: func(d *Draw) { chart.Style{ StrokeWidth: 1, StrokeColor: drawing.Color{ R: 84, G: 112, B: 198, A: 255, }, FillColor: drawing.Color{ R: 84, G: 112, B: 198, A: 255, }, }.WriteToRenderer(d.Render) d.arrowRight(30, 30, 16, 10) }, result: "\\n", }, // arrow top { fn: func(d *Draw) { chart.Style{ StrokeWidth: 1, StrokeColor: drawing.Color{ R: 84, G: 112, B: 198, A: 255, }, FillColor: drawing.Color{ R: 84, G: 112, B: 198, A: 255, }, }.WriteToRenderer(d.Render) d.arrowTop(30, 30, 10, 16) }, result: "\\n", }, // arrow bottom { fn: func(d *Draw) { chart.Style{ StrokeWidth: 1, StrokeColor: drawing.Color{ R: 84, G: 112, B: 198, A: 255, }, FillColor: drawing.Color{ R: 84, G: 112, B: 198, A: 255, }, }.WriteToRenderer(d.Render) d.arrowBottom(30, 30, 10, 16) }, result: "\\n", }, // mark line { fn: func(d *Draw) { chart.Style{ StrokeWidth: 1, StrokeColor: drawing.Color{ R: 84, G: 112, B: 198, A: 255, }, FillColor: drawing.Color{ R: 84, G: 112, B: 198, A: 255, }, StrokeDashArray: []float64{ 4, 2, }, }.WriteToRenderer(d.Render) d.makeLine(0, 20, 300) }, result: "\\n", }, } for _, tt := range tests { d, err := NewDraw(DrawOption{ Width: 400, Height: 300, }, PaddingOption(chart.Box{ Left: 5, Top: 10, })) assert.Nil(err) tt.fn(d) data, err := d.Bytes() assert.Nil(err) assert.Equal(tt.result, string(data)) } }