Debian Codegraph @

bar_chart_test.go

raw symbols SCIP
1 package chart
2
3 import (
4 "bytes"
5 "math"
6 "testing"
7
8 "github.com/wcharczuk/go-chart/v2/testutil"
9 )
10
11 func TestBarChartRender(t *testing.T) {
12 // replaced new assertions helper
13
14 bc := BarChart{
15 Width: 1024,
16 Title: "Test Title",
17 Bars: []Value{
18 {Value: 1.0, Label: "One"},
19 {Value: 2.0, Label: "Two"},
20 {Value: 3.0, Label: "Three"},
21 {Value: 4.0, Label: "Four"},
22 {Value: 5.0, Label: "Five"},
23 },
24 }
25
26 buf := bytes.NewBuffer([]byte{})
27 err := bc.Render(PNG, buf)
28 testutil.AssertNil(t, err)
29 testutil.AssertNotZero(t, buf.Len())
30 }
31
32 func TestBarChartRenderZero(t *testing.T) {
33 // replaced new assertions helper
34
35 bc := BarChart{
36 Width: 1024,
37 Title: "Test Title",
38 Bars: []Value{
39 {Value: 0.0, Label: "One"},
40 {Value: 0.0, Label: "Two"},
41 },
42 }
43
44 buf := bytes.NewBuffer([]byte{})
45 err := bc.Render(PNG, buf)
46 testutil.AssertNotNil(t, err)
47 }
48
49 func TestBarChartProps(t *testing.T) {
50 // replaced new assertions helper
51
52 bc := BarChart{}
53
54 testutil.AssertEqual(t, DefaultDPI, bc.GetDPI())
55 bc.DPI = 100
56 testutil.AssertEqual(t, 100, bc.GetDPI())
57
58 testutil.AssertNil(t, bc.GetFont())
59 f, err := GetDefaultFont()
60 testutil.AssertNil(t, err)
61 bc.Font = f
62 testutil.AssertNotNil(t, bc.GetFont())
63
64 testutil.AssertEqual(t, DefaultChartWidth, bc.GetWidth())
65 bc.Width = DefaultChartWidth - 1
66 testutil.AssertEqual(t, DefaultChartWidth-1, bc.GetWidth())
67
68 testutil.AssertEqual(t, DefaultChartHeight, bc.GetHeight())
69 bc.Height = DefaultChartHeight - 1
70 testutil.AssertEqual(t, DefaultChartHeight-1, bc.GetHeight())
71
72 testutil.AssertEqual(t, DefaultBarSpacing, bc.GetBarSpacing())
73 bc.BarSpacing = 150
74 testutil.AssertEqual(t, 150, bc.GetBarSpacing())
75
76 testutil.AssertEqual(t, DefaultBarWidth, bc.GetBarWidth())
77 bc.BarWidth = 75
78 testutil.AssertEqual(t, 75, bc.GetBarWidth())
79 }
80
81 func TestBarChartRenderNoBars(t *testing.T) {
82 // replaced new assertions helper
83
84 bc := BarChart{}
85 err := bc.Render(PNG, bytes.NewBuffer([]byte{}))
86 testutil.AssertNotNil(t, err)
87 }
88
89 func TestBarChartGetRanges(t *testing.T) {
90 // replaced new assertions helper
91
92 bc := BarChart{}
93
94 yr := bc.getRanges()
95 testutil.AssertNotNil(t, yr)
96 testutil.AssertFalse(t, yr.IsZero())
97
98 testutil.AssertEqual(t, -math.MaxFloat64, yr.GetMax())
99 testutil.AssertEqual(t, math.MaxFloat64, yr.GetMin())
100 }
101
102 func TestBarChartGetRangesBarsMinMax(t *testing.T) {
103 // replaced new assertions helper
104
105 bc := BarChart{
106 Bars: []Value{
107 {Value: 1.0},
108 {Value: 10.0},
109 },
110 }
111
112 yr := bc.getRanges()
113 testutil.AssertNotNil(t, yr)
114 testutil.AssertFalse(t, yr.IsZero())
115
116 testutil.AssertEqual(t, 10, yr.GetMax())
117 testutil.AssertEqual(t, 1, yr.GetMin())
118 }
119
120 func TestBarChartGetRangesMinMax(t *testing.T) {
121 // replaced new assertions helper
122
123 bc := BarChart{
124 YAxis: YAxis{
125 Range: &ContinuousRange{
126 Min: 5.0,
127 Max: 15.0,
128 },
129 Ticks: []Tick{
130 {Value: 7.0, Label: "Foo"},
131 {Value: 11.0, Label: "Foo2"},
132 },
133 },
134 Bars: []Value{
135 {Value: 1.0},
136 {Value: 10.0},
137 },
138 }
139
140 yr := bc.getRanges()
141 testutil.AssertNotNil(t, yr)
142 testutil.AssertFalse(t, yr.IsZero())
143
144 testutil.AssertEqual(t, 15, yr.GetMax())
145 testutil.AssertEqual(t, 5, yr.GetMin())
146 }
147
148 func TestBarChartGetRangesTicksMinMax(t *testing.T) {
149 // replaced new assertions helper
150
151 bc := BarChart{
152 YAxis: YAxis{
153 Ticks: []Tick{
154 {Value: 7.0, Label: "Foo"},
155 {Value: 11.0, Label: "Foo2"},
156 },
157 },
158 Bars: []Value{
159 {Value: 1.0},
160 {Value: 10.0},
161 },
162 }
163
164 yr := bc.getRanges()
165 testutil.AssertNotNil(t, yr)
166 testutil.AssertFalse(t, yr.IsZero())
167
168 testutil.AssertEqual(t, 11, yr.GetMax())
169 testutil.AssertEqual(t, 7, yr.GetMin())
170 }
171
172 func TestBarChartHasAxes(t *testing.T) {
173 // replaced new assertions helper
174
175 bc := BarChart{}
176 testutil.AssertTrue(t, bc.hasAxes())
177 bc.YAxis = YAxis{
178 Style: Hidden(),
179 }
180 testutil.AssertFalse(t, bc.hasAxes())
181 }
182
183 func TestBarChartGetDefaultCanvasBox(t *testing.T) {
184 // replaced new assertions helper
185
186 bc := BarChart{}
187 b := bc.getDefaultCanvasBox()
188 testutil.AssertFalse(t, b.IsZero())
189 }
190
191 func TestBarChartSetRangeDomains(t *testing.T) {
192 // replaced new assertions helper
193
194 bc := BarChart{}
195 cb := bc.box()
196 yr := bc.getRanges()
197 yr2 := bc.setRangeDomains(cb, yr)
198 testutil.AssertNotZero(t, yr2.GetDomain())
199 }
200
201 func TestBarChartGetValueFormatters(t *testing.T) {
202 // replaced new assertions helper
203
204 bc := BarChart{}
205 vf := bc.getValueFormatters()
206 testutil.AssertNotNil(t, vf)
207 testutil.AssertEqual(t, "1234.00", vf(1234.0))
208
209 bc.YAxis.ValueFormatter = func(_ interface{}) string { return "test" }
210 testutil.AssertEqual(t, "test", bc.getValueFormatters()(1234))
211 }
212
213 func TestBarChartGetAxesTicks(t *testing.T) {
214 // replaced new assertions helper
215
216 bc := BarChart{
217 Bars: []Value{
218 {Value: 1.0},
219 {Value: 2.0},
220 {Value: 3.0},
221 },
222 }
223
224 r, err := PNG(128, 128)
225 testutil.AssertNil(t, err)
226 yr := bc.getRanges()
227 yf := bc.getValueFormatters()
228
229 bc.YAxis.Style.Hidden = true
230 ticks := bc.getAxesTicks(r, yr, yf)
231 testutil.AssertEmpty(t, ticks)
232
233 bc.YAxis.Style.Hidden = false
234 ticks = bc.getAxesTicks(r, yr, yf)
235 testutil.AssertLen(t, ticks, 2)
236 }
237
238 func TestBarChartCalculateEffectiveBarSpacing(t *testing.T) {
239 // replaced new assertions helper
240
241 bc := BarChart{
242 Width: 1024,
243 BarWidth: 10,
244 Bars: []Value{
245 {Value: 1.0, Label: "One"},
246 {Value: 2.0, Label: "Two"},
247 {Value: 3.0, Label: "Three"},
248 {Value: 4.0, Label: "Four"},
249 {Value: 5.0, Label: "Five"},
250 },
251 }
252
253 spacing := bc.calculateEffectiveBarSpacing(bc.box())
254 testutil.AssertNotZero(t, spacing)
255
256 bc.BarWidth = 250
257 spacing = bc.calculateEffectiveBarSpacing(bc.box())
258 testutil.AssertZero(t, spacing)
259 }
260
261 func TestBarChartCalculateEffectiveBarWidth(t *testing.T) {
262 // replaced new assertions helper
263
264 bc := BarChart{
265 Width: 1024,
266 BarWidth: 10,
267 Bars: []Value{
268 {Value: 1.0, Label: "One"},
269 {Value: 2.0, Label: "Two"},
270 {Value: 3.0, Label: "Three"},
271 {Value: 4.0, Label: "Four"},
272 {Value: 5.0, Label: "Five"},
273 },
274 }
275
276 cb := bc.box()
277
278 spacing := bc.calculateEffectiveBarSpacing(bc.box())
279 testutil.AssertNotZero(t, spacing)
280
281 barWidth := bc.calculateEffectiveBarWidth(bc.box(), spacing)
282 testutil.AssertEqual(t, 10, barWidth)
283
284 bc.BarWidth = 250
285 spacing = bc.calculateEffectiveBarSpacing(bc.box())
286 testutil.AssertZero(t, spacing)
287 barWidth = bc.calculateEffectiveBarWidth(bc.box(), spacing)
288 testutil.AssertEqual(t, 199, barWidth)
289
290 testutil.AssertEqual(t, cb.Width()+1, bc.calculateTotalBarWidth(barWidth, spacing))
291
292 bw, bs, total := bc.calculateScaledTotalWidth(cb)
293 testutil.AssertEqual(t, spacing, bs)
294 testutil.AssertEqual(t, barWidth, bw)
295 testutil.AssertEqual(t, cb.Width()+1, total)
296 }
297
298 func TestBarChatGetTitleFontSize(t *testing.T) {
299 // replaced new assertions helper
300 size := BarChart{Width: 2049, Height: 2049}.getTitleFontSize()
301 testutil.AssertEqual(t, 48, size)
302 size = BarChart{Width: 1025, Height: 1025}.getTitleFontSize()
303 testutil.AssertEqual(t, 24, size)
304 size = BarChart{Width: 513, Height: 513}.getTitleFontSize()
305 testutil.AssertEqual(t, 18, size)
306 size = BarChart{Width: 257, Height: 257}.getTitleFontSize()
307 testutil.AssertEqual(t, 12, size)
308 size = BarChart{Width: 128, Height: 128}.getTitleFontSize()
309 testutil.AssertEqual(t, 10, size)
310 }