-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector_renderer_test.go
120 lines (90 loc) · 2.78 KB
/
vector_renderer_test.go
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
package chart
import (
"bytes"
"fmt"
"strings"
"testing"
"github.com/userstyles-world/go-chart/v2/drawing"
"github.com/userstyles-world/go-chart/v2/testutil"
"github.com/valyala/bytebufferpool"
)
func TestVectorRendererPath(t *testing.T) {
// replaced new assertions helper
vr, err := SVG(100, 100)
testutil.AssertNil(t, err)
typed, isTyped := vr.(*vectorRenderer)
testutil.AssertTrue(t, isTyped)
typed.MoveTo(0, 0)
typed.LineTo(100, 100)
typed.LineTo(0, 100)
typed.Close()
typed.FillStroke()
buffer := bytes.NewBuffer([]byte{})
err = typed.Save(buffer)
testutil.AssertNil(t, err)
raw := buffer.String()
testutil.AssertTrue(t, strings.HasPrefix(raw, "<svg"))
testutil.AssertTrue(t, strings.HasSuffix(raw, "</svg>"))
}
func TestVectorRendererMeasureText(t *testing.T) {
// replaced new assertions helper
f, err := GetDefaultFont()
testutil.AssertNil(t, err)
vr, err := SVG(100, 100)
testutil.AssertNil(t, err)
vr.SetDPI(DefaultDPI)
vr.SetFont(f)
vr.SetFontSize(12.0)
tb := vr.MeasureText("Ljp")
testutil.AssertEqual(t, 21, tb.Width())
testutil.AssertEqual(t, 15, tb.Height())
}
func TestCanvasStyleSVG(t *testing.T) {
// replaced new assertions helper
f, err := GetDefaultFont()
testutil.AssertNil(t, err)
set := Style{
StrokeColor: drawing.ColorWhite,
StrokeWidth: 5.0,
FillColor: drawing.ColorWhite,
FontColor: drawing.ColorWhite,
Font: f,
Padding: DefaultBackgroundPadding,
}
canvas := &canvas{dpi: DefaultDPI}
svgString := canvas.styleAsSVG(set)
testutil.AssertNotEmpty(t, svgString)
testutil.AssertTrue(t, strings.HasPrefix(svgString, "style=\""))
testutil.AssertTrue(t, strings.Contains(svgString, "stroke:rgba(255,255,255,1)"))
testutil.AssertTrue(t, strings.Contains(svgString, "stroke-width:5"))
testutil.AssertTrue(t, strings.Contains(svgString, "fill:rgba(255,255,255,1)"))
testutil.AssertTrue(t, strings.HasSuffix(svgString, "\""))
}
func TestCanvasClassSVG(t *testing.T) {
set := Style{
ClassName: "test-class",
}
canvas := &canvas{dpi: DefaultDPI}
testutil.AssertEqual(t, "class=\"test-class\"", canvas.styleAsSVG(set))
}
func TestCanvasCustomInlineStylesheet(t *testing.T) {
b := bytebufferpool.Get()
defer bytebufferpool.Put(b)
canvas := &canvas{
w: b,
css: ".background { fill: red }",
}
canvas.Start(200, 200)
testutil.AssertContains(t, b.String(), fmt.Sprintf(`<style type="text/css"><![CDATA[%s]]></style>`, canvas.css))
}
func TestCanvasCustomInlineStylesheetWithNonce(t *testing.T) {
b := bytebufferpool.Get()
defer bytebufferpool.Put(b)
canvas := &canvas{
w: b,
css: ".background { fill: red }",
nonce: "RAND0MSTRING",
}
canvas.Start(200, 200)
testutil.AssertContains(t, b.String(), fmt.Sprintf(`<style type="text/css" nonce="%s"><![CDATA[%s]]></style>`, canvas.nonce, canvas.css))
}