Repository navigation
Expand file tree
/
Copy pathexport_test.go
More file actions
156 lines (146 loc) · 4.7 KB
/
Copy pathexport_test.go
File metadata and controls
156 lines (146 loc) · 4.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package chartframe
import (
"bytes"
"encoding/xml"
"errors"
"image/png"
"io"
"math"
"os"
"path/filepath"
"strings"
"testing"
)
func TestOutputFormats(t *testing.T) {
c := New("Measured <signal>").Size(640, 400).Labels("time", "value").
Line("observed", []float64{0, 1, 2}, []float64{1, 3, 2})
for _, format := range []string{"svg", "png", "pdf", "html"} {
t.Run(format, func(t *testing.T) {
var b bytes.Buffer
if err := c.Write(&b, format); err != nil {
t.Fatal(err)
}
switch format {
case "svg":
d := xml.NewDecoder(bytes.NewReader(b.Bytes()))
for {
_, err := d.Token()
if err == io.EOF {
break
}
if err != nil {
t.Fatal(err)
}
}
if !bytes.Contains(b.Bytes(), []byte("<signal>")) {
t.Fatal("title not escaped")
}
case "png":
cfg, err := png.DecodeConfig(bytes.NewReader(b.Bytes()))
if err != nil || cfg.Width != 640 || cfg.Height != 400 {
t.Fatalf("PNG config: %+v, %v", cfg, err)
}
case "pdf":
if !bytes.HasPrefix(b.Bytes(), []byte("%PDF-")) {
t.Fatal("missing PDF signature")
}
case "html":
if strings.Contains(b.String(), "https://") || !strings.Contains(b.String(), "viewBox") {
t.Fatal("HTML must be self-contained and zoomable")
}
}
})
}
}
type failingWriter struct{ err error }
func (w failingWriter) Write([]byte) (int, error) { return 0, w.err }
type shortWriter struct{}
func (shortWriter) Write(p []byte) (int, error) { return len(p) / 2, nil }
func TestWriteAndSaveFailures(t *testing.T) {
c := New("test").Line("x", []float64{1, 2}, []float64{2, 4})
want := errors.New("write failed")
if err := c.Write(failingWriter{want}, "svg"); !errors.Is(err, want) {
t.Fatalf("lost writer error: %v", err)
}
if err := c.Write(shortWriter{}, "svg"); !errors.Is(err, io.ErrShortWrite) {
t.Fatalf("short write: %v", err)
}
if err := c.Write(nil, "svg"); err == nil {
t.Fatal("nil writer accepted")
}
if err := c.Write(io.Discard, "exe"); err == nil {
t.Fatal("unsupported format accepted")
}
path := filepath.Join(t.TempDir(), "existing.svg")
if err := os.WriteFile(path, []byte("keep me"), 0600); err != nil {
t.Fatal(err)
}
if err := New("invalid").Save(path); err == nil {
t.Fatal("empty chart saved")
}
got, _ := os.ReadFile(path)
if string(got) != "keep me" {
t.Fatal("failed render overwrote destination")
}
if err := c.Save(path); err != nil {
t.Fatal(err)
}
got, _ = os.ReadFile(path)
if !bytes.Contains(got, []byte("<svg")) {
t.Fatal("saved SVG missing")
}
}
func TestAllChartKindsAndGrid(t *testing.T) {
charts := []*Chart{
New("scatter + fit").Scatter("a", []float64{1, 2, 3}, []float64{2, 4, 6}).LinearFit("fit", []float64{1, 2, 3}, []float64{2, 4, 6}),
New("bars").Bar("a", []string{"A", "B"}, []float64{-1, 3}).Bar("b", []string{"A", "B"}, []float64{2, 4}),
New("distribution").Histogram("x", []float64{1, 2, 2, 3}, 3),
New("boxes").Box("A", []float64{1, 2, 3, 4, 20}).Box("B", []float64{2, 3, 4, 5}),
New("heatmap").Heatmap("matrix", []string{"a", "b"}, []string{"x", "y"}, [][]float64{{1, 2}, {3, 4}}),
New("ECDF").ECDF("x", []float64{1, 2, 2, 3}).Dark(),
New("log").Line("x", []float64{1, 10, 100}, []float64{1, 100, 10000}).LogX().LogY(),
New("constant").Scatter("x", []float64{1}, []float64{2}),
}
for _, c := range charts {
if _, err := c.SVG(); err != nil {
t.Fatalf("%s: %v", c.title, err)
}
}
if _, err := Grid("Research", 2, charts[:6]...).SVG(); err != nil {
t.Fatal(err)
}
if _, err := Grid("bad", 0, charts[0]).SVG(); err == nil {
t.Fatal("zero columns accepted")
}
if _, err := Grid("bad", 2, nil).SVG(); err == nil {
t.Fatal("nil chart accepted")
}
}
func TestExtremeRangeReturnsError(t *testing.T) {
c := New("overflow").Line("x", []float64{-math.MaxFloat64, math.MaxFloat64}, []float64{1, 2})
if _, err := c.SVG(); err == nil {
t.Fatal("unrepresentable axis span accepted")
}
lo, hi := math.SmallestNonzeroFloat64, 2*math.SmallestNonzeroFloat64
_, err := New("subnormal").Line("x", []float64{lo, hi}, []float64{1, 2}).LogX().SVG()
// Go's architecture-specific Log implementations can round these subnormal
// inputs to the same coordinate. Reject a collapsed span, but allow it when
// the platform preserves two distinct logarithms.
if math.Log(lo) >= math.Log(hi) {
if err == nil {
t.Fatal("indistinguishable log coordinates accepted")
}
} else if err != nil {
t.Fatalf("representable subnormal log range: %v", err)
}
}
func TestHTMLLabelEscaping(t *testing.T) {
attack := `</script><script>alert("x")</script>`
b, err := New(attack).Line("x", []float64{1, 2}, []float64{3, 4}).HTML()
if err != nil {
t.Fatal(err)
}
if strings.Contains(string(b), attack) {
t.Fatal("unescaped label in HTML")
}
}