go-chart/vector_renderer_test.go

118 lines
2.7 KiB
Go
Raw Normal View History

2016-07-11 02:21:19 -04:00
package chart
import (
"bytes"
"fmt"
2016-07-11 02:21:19 -04:00
"strings"
"testing"
"github.com/wcharczuk/go-chart/v2/drawing"
"github.com/wcharczuk/go-chart/v2/testutil"
2016-07-11 02:21:19 -04:00
)
func TestVectorRendererPath(t *testing.T) {
// replaced new assertions helper
2016-07-11 02:21:19 -04:00
vr, err := SVG(100, 100)
testutil.AssertNil(t, err)
2016-07-11 02:21:19 -04:00
typed, isTyped := vr.(*vectorRenderer)
testutil.AssertTrue(t, isTyped)
2016-07-11 02:21:19 -04:00
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)
2016-07-11 02:21:19 -04:00
raw := string(buffer.Bytes())
testutil.AssertTrue(t, strings.HasPrefix(raw, "<svg"))
testutil.AssertTrue(t, strings.HasSuffix(raw, "</svg>"))
2016-07-11 02:21:19 -04:00
}
func TestVectorRendererMeasureText(t *testing.T) {
// replaced new assertions helper
2016-07-11 02:21:19 -04:00
f, err := GetDefaultFont()
testutil.AssertNil(t, err)
2016-07-11 02:21:19 -04:00
vr, err := SVG(100, 100)
testutil.AssertNil(t, err)
2016-07-11 02:21:19 -04:00
vr.SetDPI(DefaultDPI)
vr.SetFont(f)
vr.SetFontSize(12.0)
2016-07-12 02:32:31 -04:00
tb := vr.MeasureText("Ljp")
testutil.AssertEqual(t, 21, tb.Width())
testutil.AssertEqual(t, 15, tb.Height())
2016-07-11 02:21:19 -04:00
}
2016-07-29 19:36:29 -04:00
func TestCanvasStyleSVG(t *testing.T) {
// replaced new assertions helper
2016-07-29 19:36:29 -04:00
f, err := GetDefaultFont()
testutil.AssertNil(t, err)
2016-07-29 19:36:29 -04:00
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.0)"))
testutil.AssertTrue(t, strings.Contains(svgString, "stroke-width:5"))
testutil.AssertTrue(t, strings.Contains(svgString, "fill:rgba(255,255,255,1.0)"))
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))
2016-07-29 19:36:29 -04:00
}
func TestCanvasCustomInlineStylesheet(t *testing.T) {
b := strings.Builder{}
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 := strings.Builder{}
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))
}