cat / 012
clone

a spreadsheet for the terminal: lotus 1-2-3 looks, google sheets keys and formulas, one go binary

main
012 / internal / chart / svg_test.go
svg_test.go 2.2K
1package chart23import (4 "encoding/xml"5 "io"6 "strings"7 "testing"89 "src.rest/cat/012/internal/sheet"10)1112// TestSVGEveryType draws each type as SVG, checking it's well-formed13// XML with the chart's labels as text and shapes of its own kind.14func TestSVGEveryType(t *testing.T) {15 cases := []struct {16 typ sheet.ChartType17 data sheet.ChartData18 opts sheet.ChartOptions19 shape string20 }{21 {sheet.ChartColumn, budget, sheet.ChartOptions{}, "<rect"},22 {sheet.ChartBar, single, sheet.ChartOptions{}, "<rect"},23 {sheet.ChartLine, budget, sheet.ChartOptions{}, "<polyline"},24 {sheet.ChartArea, budget, sheet.ChartOptions{}, "<polygon"},25 {sheet.ChartArea, budget, sheet.ChartOptions{Stack: sheet.StackNormal}, "<polygon"},26 {sheet.ChartPie, single, sheet.ChartOptions{}, "<path"},27 {sheet.ChartScatter, xy, sheet.ChartOptions{Trend: true}, "<circle"},28 {sheet.ChartColumn, signs, sheet.ChartOptions{Stack: sheet.StackNormal}, "<rect"},29 }30 for _, c := range cases {31 t.Run(c.typ.String(), func(t *testing.T) {32 svg := SVG(c.typ, c.data, 60, 18, Options{Chart: c.opts}, "Budget & more")33 d := xml.NewDecoder(strings.NewReader(svg))34 for {35 if _, err := d.Token(); err == io.EOF {36 break37 } else if err != nil {38 t.Fatalf("not XML: %v\n%s", err, svg)39 }40 }41 if !strings.Contains(svg, c.shape) {42 t.Errorf("no %s in\n%s", c.shape, svg)43 }44 if !strings.Contains(svg, "<title>Budget &amp; more</title>") {45 t.Errorf("no title")46 }47 if c.typ != sheet.ChartPie && !strings.Contains(svg, `class="ct-lb"`) {48 t.Errorf("no labels")49 }50 })51 }52}5354// TestSVGAreaIsFewShapes joins an area's pixel columns into polygons.55func TestSVGAreaIsFewShapes(t *testing.T) {56 svg := SVG(sheet.ChartArea, single, 80, 20, Options{}, "")57 if n := strings.Count(svg, "<polygon"); n == 0 || n > 3 {58 t.Errorf("%d polygons", n)59 }60 if strings.Contains(svg, "<rect") {61 t.Errorf("area columns drawn as rects")62 }63}6465// TestSVGNothingToPlot shows the text chart's message alone.66func TestSVGNothingToPlot(t *testing.T) {67 svg := SVG(sheet.ChartColumn, sheet.ChartData{}, 40, 10, Options{}, "")68 if !strings.Contains(svg, "No numbers to chart") || strings.Contains(svg, "<g ") {69 t.Errorf("got %s", svg)70 }71}