Debian Codegraph @

chart_test.go

raw symbols SCIP
1 package chart
2
3 import (
4 "bytes"
5 "image"
6 "image/png"
7 "math"
8 "testing"
9 "time"
10
11 "github.com/wcharczuk/go-chart/v2/drawing"
12 "github.com/wcharczuk/go-chart/v2/testutil"
13 )
14
15 func TestChartGetDPI(t *testing.T) {
16 // replaced new assertions helper
17
18 unset := Chart{}
19 testutil.AssertEqual(t, DefaultDPI, unset.GetDPI())
20 testutil.AssertEqual(t, 192, unset.GetDPI(192))
21
22 set := Chart{DPI: 128}
23 testutil.AssertEqual(t, 128, set.GetDPI())
24 testutil.AssertEqual(t, 128, set.GetDPI(192))
25 }
26
27 func TestChartGetFont(t *testing.T) {
28 // replaced new assertions helper
29
30 f, err := GetDefaultFont()
31 testutil.AssertNil(t, err)
32
33 unset := Chart{}
34 testutil.AssertNil(t, unset.GetFont())
35
36 set := Chart{Font: f}
37 testutil.AssertNotNil(t, set.GetFont())
38 }
39
40 func TestChartGetWidth(t *testing.T) {
41 // replaced new assertions helper
42
43 unset := Chart{}
44 testutil.AssertEqual(t, DefaultChartWidth, unset.GetWidth())
45
46 set := Chart{Width: DefaultChartWidth + 10}
47 testutil.AssertEqual(t, DefaultChartWidth+10, set.GetWidth())
48 }
49
50 func TestChartGetHeight(t *testing.T) {
51 // replaced new assertions helper
52
53 unset := Chart{}
54 testutil.AssertEqual(t, DefaultChartHeight, unset.GetHeight())
55
56 set := Chart{Height: DefaultChartHeight + 10}
57 testutil.AssertEqual(t, DefaultChartHeight+10, set.GetHeight())
58 }
59
60 func TestChartGetRanges(t *testing.T) {
61 // replaced new assertions helper
62
63 c := Chart{
64 Series: []Series{
65 ContinuousSeries{
66 XValues: []float64{-2.0, -1.0, 0, 1.0, 2.0},
67 YValues: []float64{1.0, 2.0, 3.0, 4.0, 4.5},
68 },
69 ContinuousSeries{
70 XValues: []float64{1.0, 2.0, 3.0, 4.0, 5.0},
71 YValues: []float64{-2.1, -1.0, 0, 1.0, 2.0},
72 },
73 ContinuousSeries{
74 YAxis: YAxisSecondary,
75 XValues: []float64{1.0, 2.0, 3.0, 4.0, 5.0},
76 YValues: []float64{10.0, 11.0, 12.0, 13.0, 14.0},
77 },
78 },
79 }
80
81 xrange, yrange, yrangeAlt := c.getRanges()
82 testutil.AssertEqual(t, -2.0, xrange.GetMin())
83 testutil.AssertEqual(t, 5.0, xrange.GetMax())
84
85 testutil.AssertEqual(t, -2.1, yrange.GetMin())
86 testutil.AssertEqual(t, 4.5, yrange.GetMax())
87
88 testutil.AssertEqual(t, 10.0, yrangeAlt.GetMin())
89 testutil.AssertEqual(t, 14.0, yrangeAlt.GetMax())
90
91 cSet := Chart{
92 XAxis: XAxis{
93 Range: &ContinuousRange{Min: 9.8, Max: 19.8},
94 },
95 YAxis: YAxis{
96 Range: &ContinuousRange{Min: 9.9, Max: 19.9},
97 },
98 YAxisSecondary: YAxis{
99 Range: &ContinuousRange{Min: 9.7, Max: 19.7},
100 },
101 Series: []Series{
102 ContinuousSeries{
103 XValues: []float64{-2.0, -1.0, 0, 1.0, 2.0},
104 YValues: []float64{1.0, 2.0, 3.0, 4.0, 4.5},
105 },
106 ContinuousSeries{
107 XValues: []float64{1.0, 2.0, 3.0, 4.0, 5.0},
108 YValues: []float64{-2.1, -1.0, 0, 1.0, 2.0},
109 },
110 ContinuousSeries{
111 YAxis: YAxisSecondary,
112 XValues: []float64{1.0, 2.0, 3.0, 4.0, 5.0},
113 YValues: []float64{10.0, 11.0, 12.0, 13.0, 14.0},
114 },
115 },
116 }
117
118 xr2, yr2, yra2 := cSet.getRanges()
119 testutil.AssertEqual(t, 9.8, xr2.GetMin())
120 testutil.AssertEqual(t, 19.8, xr2.GetMax())
121
122 testutil.AssertEqual(t, 9.9, yr2.GetMin())
123 testutil.AssertEqual(t, 19.9, yr2.GetMax())
124
125 testutil.AssertEqual(t, 9.7, yra2.GetMin())
126 testutil.AssertEqual(t, 19.7, yra2.GetMax())
127 }
128
129 func TestChartGetRangesUseTicks(t *testing.T) {
130 // replaced new assertions helper
131
132 // this test asserts that ticks should supercede manual ranges when generating the overall ranges.
133
134 c := Chart{
135 YAxis: YAxis{
136 Ticks: []Tick{
137 {0.0, "Zero"},
138 {1.0, "1.0"},
139 {2.0, "2.0"},
140 {3.0, "3.0"},
141 {4.0, "4.0"},
142 {5.0, "Five"},
143 },
144 Range: &ContinuousRange{
145 Min: -5.0,
146 Max: 5.0,
147 },
148 },
149 Series: []Series{
150 ContinuousSeries{
151 XValues: []float64{-2.0, -1.0, 0, 1.0, 2.0},
152 YValues: []float64{1.0, 2.0, 3.0, 4.0, 4.5},
153 },
154 },
155 }
156
157 xr, yr, yar := c.getRanges()
158 testutil.AssertEqual(t, -2.0, xr.GetMin())
159 testutil.AssertEqual(t, 2.0, xr.GetMax())
160 testutil.AssertEqual(t, 0.0, yr.GetMin())
161 testutil.AssertEqual(t, 5.0, yr.GetMax())
162 testutil.AssertTrue(t, yar.IsZero(), yar.String())
163 }
164
165 func TestChartGetRangesUseUserRanges(t *testing.T) {
166 // replaced new assertions helper
167
168 c := Chart{
169 YAxis: YAxis{
170 Range: &ContinuousRange{
171 Min: -5.0,
172 Max: 5.0,
173 },
174 },
175 Series: []Series{
176 ContinuousSeries{
177 XValues: []float64{-2.0, -1.0, 0, 1.0, 2.0},
178 YValues: []float64{1.0, 2.0, 3.0, 4.0, 4.5},
179 },
180 },
181 }
182
183 xr, yr, yar := c.getRanges()
184 testutil.AssertEqual(t, -2.0, xr.GetMin())
185 testutil.AssertEqual(t, 2.0, xr.GetMax())
186 testutil.AssertEqual(t, -5.0, yr.GetMin())
187 testutil.AssertEqual(t, 5.0, yr.GetMax())
188 testutil.AssertTrue(t, yar.IsZero(), yar.String())
189 }
190
191 func TestChartGetBackgroundStyle(t *testing.T) {
192 // replaced new assertions helper
193
194 c := Chart{
195 Background: Style{
196 FillColor: drawing.ColorBlack,
197 },
198 }
199
200 bs := c.getBackgroundStyle()
201 testutil.AssertEqual(t, bs.FillColor.String(), drawing.ColorBlack.String())
202 }
203
204 func TestChartGetCanvasStyle(t *testing.T) {
205 // replaced new assertions helper
206
207 c := Chart{
208 Canvas: Style{
209 FillColor: drawing.ColorBlack,
210 },
211 }
212
213 bs := c.getCanvasStyle()
214 testutil.AssertEqual(t, bs.FillColor.String(), drawing.ColorBlack.String())
215 }
216
217 func TestChartGetDefaultCanvasBox(t *testing.T) {
218 // replaced new assertions helper
219
220 c := Chart{}
221 canvasBoxDefault := c.getDefaultCanvasBox()
222 testutil.AssertFalse(t, canvasBoxDefault.IsZero())
223 testutil.AssertEqual(t, DefaultBackgroundPadding.Top, canvasBoxDefault.Top)
224 testutil.AssertEqual(t, DefaultBackgroundPadding.Left, canvasBoxDefault.Left)
225 testutil.AssertEqual(t, c.GetWidth()-DefaultBackgroundPadding.Right, canvasBoxDefault.Right)
226 testutil.AssertEqual(t, c.GetHeight()-DefaultBackgroundPadding.Bottom, canvasBoxDefault.Bottom)
227
228 custom := Chart{
229 Background: Style{
230 Padding: Box{
231 Top: DefaultBackgroundPadding.Top + 1,
232 Left: DefaultBackgroundPadding.Left + 1,
233 Right: DefaultBackgroundPadding.Right + 1,
234 Bottom: DefaultBackgroundPadding.Bottom + 1,
235 },
236 },
237 }
238 canvasBoxCustom := custom.getDefaultCanvasBox()
239 testutil.AssertFalse(t, canvasBoxCustom.IsZero())
240 testutil.AssertEqual(t, DefaultBackgroundPadding.Top+1, canvasBoxCustom.Top)
241 testutil.AssertEqual(t, DefaultBackgroundPadding.Left+1, canvasBoxCustom.Left)
242 testutil.AssertEqual(t, c.GetWidth()-(DefaultBackgroundPadding.Right+1), canvasBoxCustom.Right)
243 testutil.AssertEqual(t, c.GetHeight()-(DefaultBackgroundPadding.Bottom+1), canvasBoxCustom.Bottom)
244 }
245
246 func TestChartGetValueFormatters(t *testing.T) {
247 // replaced new assertions helper
248
249 c := Chart{
250 Series: []Series{
251 ContinuousSeries{
252 XValues: []float64{-2.0, -1.0, 0, 1.0, 2.0},
253 YValues: []float64{1.0, 2.0, 3.0, 4.0, 4.5},
254 },
255 ContinuousSeries{
256 XValues: []float64{1.0, 2.0, 3.0, 4.0, 5.0},
257 YValues: []float64{-2.1, -1.0, 0, 1.0, 2.0},
258 },
259 ContinuousSeries{
260 YAxis: YAxisSecondary,
261 XValues: []float64{1.0, 2.0, 3.0, 4.0, 5.0},
262 YValues: []float64{10.0, 11.0, 12.0, 13.0, 14.0},
263 },
264 },
265 }
266
267 dxf, dyf, dyaf := c.getValueFormatters()
268 testutil.AssertNotNil(t, dxf)
269 testutil.AssertNotNil(t, dyf)
270 testutil.AssertNotNil(t, dyaf)
271 }
272
273 func TestChartHasAxes(t *testing.T) {
274 // replaced new assertions helper
275
276 testutil.AssertTrue(t, Chart{}.hasAxes())
277 testutil.AssertFalse(t, Chart{XAxis: XAxis{Style: Hidden()}, YAxis: YAxis{Style: Hidden()}, YAxisSecondary: YAxis{Style: Hidden()}}.hasAxes())
278
279 x := Chart{
280 XAxis: XAxis{
281 Style: Hidden(),
282 },
283 YAxis: YAxis{
284 Style: Shown(),
285 },
286 YAxisSecondary: YAxis{
287 Style: Hidden(),
288 },
289 }
290 testutil.AssertTrue(t, x.hasAxes())
291
292 y := Chart{
293 XAxis: XAxis{
294 Style: Shown(),
295 },
296 YAxis: YAxis{
297 Style: Hidden(),
298 },
299 YAxisSecondary: YAxis{
300 Style: Hidden(),
301 },
302 }
303 testutil.AssertTrue(t, y.hasAxes())
304
305 ya := Chart{
306 XAxis: XAxis{
307 Style: Hidden(),
308 },
309 YAxis: YAxis{
310 Style: Hidden(),
311 },
312 YAxisSecondary: YAxis{
313 Style: Shown(),
314 },
315 }
316 testutil.AssertTrue(t, ya.hasAxes())
317 }
318
319 func TestChartGetAxesTicks(t *testing.T) {
320 // replaced new assertions helper
321
322 r, err := PNG(1024, 1024)
323 testutil.AssertNil(t, err)
324
325 c := Chart{
326 XAxis: XAxis{
327 Range: &ContinuousRange{Min: 9.8, Max: 19.8},
328 },
329 YAxis: YAxis{
330 Range: &ContinuousRange{Min: 9.9, Max: 19.9},
331 },
332 YAxisSecondary: YAxis{
333 Range: &ContinuousRange{Min: 9.7, Max: 19.7},
334 },
335 }
336 xr, yr, yar := c.getRanges()
337
338 xt, yt, yat := c.getAxesTicks(r, xr, yr, yar, FloatValueFormatter, FloatValueFormatter, FloatValueFormatter)
339 testutil.AssertNotEmpty(t, xt)
340 testutil.AssertNotEmpty(t, yt)
341 testutil.AssertNotEmpty(t, yat)
342 }
343
344 func TestChartSingleSeries(t *testing.T) {
345 // replaced new assertions helper
346 now := time.Now()
347 c := Chart{
348 Title: "Hello!",
349 Width: 1024,
350 Height: 400,
351 YAxis: YAxis{
352 Range: &ContinuousRange{
353 Min: 0.0,
354 Max: 4.0,
355 },
356 },
357 Series: []Series{
358 TimeSeries{
359 Name: "goog",
360 XValues: []time.Time{now.AddDate(0, 0, -3), now.AddDate(0, 0, -2), now.AddDate(0, 0, -1)},
361 YValues: []float64{1.0, 2.0, 3.0},
362 },
363 },
364 }
365
366 buffer := bytes.NewBuffer([]byte{})
367 c.Render(PNG, buffer)
368 testutil.AssertNotEmpty(t, buffer.Bytes())
369 }
370
371 func TestChartRegressionBadRanges(t *testing.T) {
372 // replaced new assertions helper
373
374 c := Chart{
375 Series: []Series{
376 ContinuousSeries{
377 XValues: []float64{math.Inf(1), math.Inf(1), math.Inf(1), math.Inf(1), math.Inf(1)},
378 YValues: []float64{1.0, 2.0, 3.0, 4.0, 4.5},
379 },
380 },
381 }
382 buffer := bytes.NewBuffer([]byte{})
383 c.Render(PNG, buffer)
384 testutil.AssertTrue(t, true, "Render needs to finish.")
385 }
386
387 func TestChartRegressionBadRangesByUser(t *testing.T) {
388 // replaced new assertions helper
389
390 c := Chart{
391 YAxis: YAxis{
392 Range: &ContinuousRange{
393 Min: math.Inf(-1),
394 Max: math.Inf(1), // this could really happen? eh.
395 },
396 },
397 Series: []Series{
398 ContinuousSeries{
399 XValues: LinearRange(1.0, 10.0),
400 YValues: LinearRange(1.0, 10.0),
401 },
402 },
403 }
404 buffer := bytes.NewBuffer([]byte{})
405 c.Render(PNG, buffer)
406 testutil.AssertTrue(t, true, "Render needs to finish.")
407 }
408
409 func TestChartValidatesSeries(t *testing.T) {
410 // replaced new assertions helper
411
412 c := Chart{
413 Series: []Series{
414 ContinuousSeries{
415 XValues: LinearRange(1.0, 10.0),
416 YValues: LinearRange(1.0, 10.0),
417 },
418 },
419 }
420
421 testutil.AssertNil(t, c.validateSeries())
422
423 c = Chart{
424 Series: []Series{
425 ContinuousSeries{
426 XValues: LinearRange(1.0, 10.0),
427 },
428 },
429 }
430
431 testutil.AssertNotNil(t, c.validateSeries())
432 }
433
434 func TestChartCheckRanges(t *testing.T) {
435 // replaced new assertions helper
436
437 c := Chart{
438 Series: []Series{
439 ContinuousSeries{
440 XValues: []float64{1.0, 2.0},
441 YValues: []float64{3.10, 3.14},
442 },
443 },
444 }
445
446 xr, yr, yra := c.getRanges()
447 testutil.AssertNil(t, c.checkRanges(xr, yr, yra))
448 }
449
450 func TestChartCheckRangesWithRanges(t *testing.T) {
451 // replaced new assertions helper
452
453 c := Chart{
454 XAxis: XAxis{
455 Range: &ContinuousRange{
456 Min: 0,
457 Max: 10,
458 },
459 },
460 YAxis: YAxis{
461 Range: &ContinuousRange{
462 Min: 0,
463 Max: 5,
464 },
465 },
466 Series: []Series{
467 ContinuousSeries{
468 XValues: []float64{1.0, 2.0},
469 YValues: []float64{3.14, 3.14},
470 },
471 },
472 }
473
474 xr, yr, yra := c.getRanges()
475 testutil.AssertNil(t, c.checkRanges(xr, yr, yra))
476 }
477
478 func at(i image.Image, x, y int) drawing.Color {
479 return drawing.ColorFromAlphaMixedRGBA(i.At(x, y).RGBA())
480 }
481
482 func TestChartE2ELine(t *testing.T) {
483 // replaced new assertions helper
484
485 c := Chart{
486 Height: 50,
487 Width: 50,
488 TitleStyle: Hidden(),
489 XAxis: HideXAxis(),
490 YAxis: HideYAxis(),
491 YAxisSecondary: HideYAxis(),
492 Canvas: Style{
493 Padding: BoxZero,
494 },
495 Background: Style{
496 Padding: BoxZero,
497 },
498 Series: []Series{
499 ContinuousSeries{
500 XValues: LinearRangeWithStep(0, 4, 1),
501 YValues: LinearRangeWithStep(0, 4, 1),
502 },
503 },
504 }
505
506 var buffer = &bytes.Buffer{}
507 err := c.Render(PNG, buffer)
508 testutil.AssertNil(t, err)
509
510 // do color tests ...
511
512 i, err := png.Decode(buffer)
513 testutil.AssertNil(t, err)
514
515 // test the bottom and top of the line
516 testutil.AssertEqual(t, drawing.ColorWhite, at(i, 0, 0))
517 testutil.AssertEqual(t, drawing.ColorWhite, at(i, 49, 49))
518
519 // test a line mid point
520 defaultSeriesColor := GetDefaultColor(0)
521 testutil.AssertEqual(t, defaultSeriesColor, at(i, 0, 49))
522 testutil.AssertEqual(t, defaultSeriesColor, at(i, 49, 0))
523 testutil.AssertEqual(t, drawing.ColorFromHex("bddbf6"), at(i, 24, 24))
524 }
525
526 func TestChartE2ELineWithFill(t *testing.T) {
527 // replaced new assertions helper
528
529 logBuffer := new(bytes.Buffer)
530
531 c := Chart{
532 Height: 50,
533 Width: 50,
534 Canvas: Style{
535 Padding: BoxZero,
536 },
537 Background: Style{
538 Padding: BoxZero,
539 },
540 TitleStyle: Hidden(),
541 XAxis: HideXAxis(),
542 YAxis: HideYAxis(),
543 YAxisSecondary: HideYAxis(),
544 Series: []Series{
545 ContinuousSeries{
546 Style: Style{
547 StrokeColor: drawing.ColorBlue,
548 FillColor: drawing.ColorRed,
549 },
550 XValues: LinearRangeWithStep(0, 4, 1),
551 YValues: LinearRangeWithStep(0, 4, 1),
552 },
553 },
554 Log: NewLogger(OptLoggerStdout(logBuffer), OptLoggerStderr(logBuffer)),
555 }
556
557 testutil.AssertEqual(t, 5, len(c.Series[0].(ContinuousSeries).XValues))
558 testutil.AssertEqual(t, 5, len(c.Series[0].(ContinuousSeries).YValues))
559
560 var buffer = &bytes.Buffer{}
561 err := c.Render(PNG, buffer)
562 testutil.AssertNil(t, err)
563
564 i, err := png.Decode(buffer)
565 testutil.AssertNil(t, err)
566
567 // test the bottom and top of the line
568 testutil.AssertEqual(t, drawing.ColorWhite, at(i, 0, 0))
569 testutil.AssertEqual(t, drawing.ColorRed, at(i, 49, 49))
570
571 // test a line mid point
572 defaultSeriesColor := drawing.ColorBlue
573 testutil.AssertEqual(t, defaultSeriesColor, at(i, 0, 49))
574 testutil.AssertEqual(t, defaultSeriesColor, at(i, 49, 0))
575 }
576
577 func Test_Chart_cve(t *testing.T) {
578 poc := StackedBarChart{
579 Title: "poc",
580 Bars: []StackedBar{
581 {
582 Name: "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111",
583 Values: []Value{
584 {Value: 1, Label: "infinite"},
585 {Value: 1, Label: "loop"},
586 },
587 },
588 },
589 }
590
591 var imgContent bytes.Buffer
592 err := poc.Render(PNG, &imgContent)
593 testutil.AssertNotNil(t, err)
594 }