-
Notifications
You must be signed in to change notification settings - Fork 18
/
find_test.go
73 lines (60 loc) · 1.91 KB
/
find_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
package svgparser_test
import (
"testing"
"github.com/JoshVarga/svgparser"
)
func testElement() *svgparser.Element {
svg := `
<svg width="1000" height="600">
<g id="first">
<rect width="5" height="3" id="inFirst"/>
<rect width="5" height="2" id="inFirst"/>
</g>
<g id="second">
<path d="M50 50 Q50 100 100 100"/>
<rect width="5" height="1"/>
</g>
</svg>
`
element, _ := parse(svg, false)
return element
}
func equals(t *testing.T, name string, expected, actual *svgparser.Element) {
if !(expected == actual || expected.Compare(actual)) {
t.Errorf("%s: expected %v, actual %v\n", name, expected, actual)
}
}
func equalSlices(t *testing.T, name string, expected, actual []*svgparser.Element) {
if len(expected) != len(actual) {
t.Errorf("%s: expected %v, actual %v\n", name, expected, actual)
return
}
for i, r := range actual {
equals(t, name, expected[i], r)
}
}
func TestFindAll(t *testing.T) {
svgElement := testElement()
equalSlices(t, "Find", []*svgparser.Element{
element("rect", map[string]string{"width": "5", "height": "3", "id": "inFirst"}),
element("rect", map[string]string{"width": "5", "height": "2", "id": "inFirst"}),
element("rect", map[string]string{"width": "5", "height": "1"}),
}, svgElement.FindAll("rect"))
equalSlices(t, "Find", []*svgparser.Element{}, svgElement.FindAll("circle"))
}
func TestFindID(t *testing.T) {
svgElement := testElement()
equals(t, "Find", &svgparser.Element{
Name: "g",
Attributes: map[string]string{"id": "second"},
Children: []*svgparser.Element{
element("path", map[string]string{"d": "M50 50 Q50 100 100 100"}),
element("rect", map[string]string{"width": "5", "height": "1"}),
},
}, svgElement.FindID("second"))
equals(t, "Find",
element("rect", map[string]string{"width": "5", "height": "3", "id": "inFirst"}),
svgElement.FindID("inFirst"),
)
equals(t, "Find", nil, svgElement.FindID("missing"))
}