-
Notifications
You must be signed in to change notification settings - Fork 18
/
parser_test.go
92 lines (85 loc) · 2.34 KB
/
parser_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
package svgparser_test
import (
"testing"
"github.com/JoshVarga/svgparser"
)
func TestParser(t *testing.T) {
var testCases = []struct {
svg string
element svgparser.Element
}{
{
`
<svg width="100" height="100">
<circle cx="50" cy="50" r="40" fill="red" />
</svg>
`,
svgparser.Element{
Name: "svg",
Attributes: map[string]string{
"width": "100",
"height": "100",
},
Children: []*svgparser.Element{
element("circle", map[string]string{"cx": "50", "cy": "50", "r": "40", "fill": "red"}),
},
},
},
{
`
<svg height="400" width="450">
<g stroke="black" stroke-width="3" fill="black">
<path id="AB" d="M 100 350 L 150 -300" stroke="red" />
<path id="BC" d="M 250 50 L 150 300" stroke="red" />
<path d="M 175 200 L 150 0" stroke="green" />
</g>
</svg>
`,
svgparser.Element{
Name: "svg",
Attributes: map[string]string{
"width": "450",
"height": "400",
},
Children: []*svgparser.Element{
&svgparser.Element{
Name: "g",
Attributes: map[string]string{
"stroke": "black",
"stroke-width": "3",
"fill": "black",
},
Children: []*svgparser.Element{
element("path", map[string]string{"id": "AB", "d": "M 100 350 L 150 -300", "stroke": "red"}),
element("path", map[string]string{"id": "BC", "d": "M 250 50 L 150 300", "stroke": "red"}),
element("path", map[string]string{"d": "M 175 200 L 150 0", "stroke": "green"}),
},
},
},
},
},
{
"",
svgparser.Element{},
},
}
for _, test := range testCases {
actual, err := parse(test.svg, false)
if !(test.element.Compare(actual) && err == nil) {
t.Errorf("Parse: expected %v, actual %v\n", test.element, actual)
}
}
}
func TestValidDocument(t *testing.T) {
svg := `
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" id="svg-root" width="100%" height="100%" viewBox="0 0 480 360">
<title id="test-title">color-prop-01-b</title>
<desc id="test-desc">Test that viewer has the basic capability to process the color property</desc>
<rect id="test-frame" x="1" y="1" width="478" height="358" fill="none" stroke="#000000"/>
</svg>
`
element, err := parse(svg, true)
if !(element != nil && err == nil) {
t.Errorf("Validation: expected %v, actual %v\n", nil, err)
}
}