forked from torbiak/gopl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
66 lines (61 loc) · 1.64 KB
/
main_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
package main
import (
"bytes"
"strings"
"testing"
)
func TestParseSelectors(t *testing.T) {
sels, err := parseSelectors(`a p [href="hi"][id=big] p[class="blue"] [class]`)
if err != nil {
t.Error(err)
}
expected := []string{
"a", "p", `[href="hi"][id="big"]`, `p[class="blue"]`, "[class]",
}
if len(expected) != len(sels) {
t.Errorf("%s != %s", sels, expected)
return
}
for i, ex := range expected {
actual := sels[i].String()
if actual != ex {
t.Errorf("%s != %s and %s != %s", actual, ex, sels, expected)
return
}
}
}
func TestSelectorParseFailure_badAttr(t *testing.T) {
sels, err := parseSelectors("a]")
if !strings.Contains(err.Error(), "want ident") {
t.Error(sels, err)
}
}
func TestSelectorParseFailure_badTag(t *testing.T) {
sels, err := parseSelectors(`a "p"`)
if !strings.Contains(err.Error(), "want ident") {
t.Error(sels, err)
}
}
func TestXMLSelect(t *testing.T) {
tests := []struct {
selectors, xml, want string
}{
{`a[id="3"] [id="4"]`, `<a id="3"><p id="4">good</p></a>`, "good\n"},
{`a[id="3"] [id="4"]`, `<a><p id="4">bad</p></a>`, ""},
{`[id="3"] [id]`, `<a id="3"><p id="4">good</p></a><a><p id="4">bad</p></a>`, "good\n"},
{`[id="3"][class=big]`, `<a id="3" class="big">good</a><a id="3">bad</a>`, "good\n"},
{`p a`, `<p><a>1</a><p><a>2</a></p></p><a>bad</a><p><a>3</a></p>`, "1\n2\n3\n"},
}
for _, test := range tests {
sels, err := parseSelectors(test.selectors)
if err != nil {
t.Error(test, err)
continue
}
w := &bytes.Buffer{}
xmlselect(w, strings.NewReader(test.xml), sels)
if w.String() != test.want {
t.Errorf("%s: got %q, want %q", test, w.String(), test.want)
}
}
}