forked from ericchiang/pup
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpup_test.go
155 lines (133 loc) · 3.92 KB
/
pup_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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package main
import (
"bytes"
"embed"
"flag"
"fmt"
"os"
"io"
"io/ioutil"
"testing"
"github.com/google/go-cmp/cmp"
)
var update = flag.Bool("update", false, "update .golden files")
//go:embed testdata
var testdataFS embed.FS
func testFile(p string) io.Reader {
r, err := testdataFS.Open(p)
if err != nil {
panic(err)
}
return r
}
func testString(p string) string {
r := testFile(p)
b, err := io.ReadAll(r)
if err != nil {
panic(err)
}
return string(b)
}
func TestMain(t *testing.T) {
type Test struct {
args []string
}
tests := []Test{
0: {[]string{"#footer"}},
1: {[]string{"#footer li"}},
2: {[]string{"#footer ul + div"}},
3: {[]string{"#footer ul + div attr{style}"}},
4: {[]string{"#toc li > a"}},
5: {[]string{"table li"}},
6: {[]string{"table li:first-child"}},
7: {[]string{"table li:first-of-type"}},
8: {[]string{"table li:last-child"}},
9: {[]string{"table li:last-of-type"}},
10: {[]string{`table a[title="The Practice of Programming"]`}},
11: {[]string{`table a[title="The Practice of Programming"] text{}`}},
12: {[]string{"json{}"}},
13: {[]string{"text{}"}},
14: {[]string{".after-portlet"}},
15: {[]string{".organiser"}},
16: {[]string{":empty"}},
17: {[]string{"td:empty"}},
18: {[]string{".navbox-list li:nth-child(1)"}},
19: {[]string{".navbox-list li:nth-child(2)"}},
20: {[]string{".navbox-list li:nth-child(3)"}},
21: {[]string{".navbox-list li:nth-last-child(1)"}},
22: {[]string{".navbox-list li:nth-last-child(2)"}},
23: {[]string{".navbox-list li:nth-last-child(3)"}},
24: {[]string{".navbox-list li:nth-child(n+1)"}},
25: {[]string{".navbox-list li:nth-child(3n+1)"}},
26: {[]string{".navbox-list li:nth-last-child(n+1)"}},
27: {[]string{".navbox-list li:nth-last-child(3n+1)"}},
28: {[]string{":only-child"}},
29: {[]string{".navbox-list li:only-child"}},
30: {[]string{".summary"}},
31: {[]string{"[class=summary]"}},
32: {[]string{`[class="summary"]`}},
33: {[]string{"#toc"}},
34: {[]string{"#toc div + ul"}},
35: {[]string{"#toc div + ul li:nth-of-type(1) a span:nth-of-type(1) text{}"}},
36: {[]string{"#toc div + ul li:nth-of-type(1) a span:nth-of-type(1) json{}"}},
37: {[]string{"#toc div + ul span + span"}},
38: {[]string{"span + a"}},
39: {[]string{"#footer a > img"}},
40: {[]string{"li a:not([rel])"}},
41: {[]string{"link, a"}},
42: {[]string{"link ,a"}},
43: {[]string{"link , a"}},
44: {[]string{"link , a sup"}},
45: {[]string{"link , a:parent-of(sup)"}},
46: {[]string{"link , a:parent-of(sup) sup"}},
47: {[]string{"li", "--number"}},
48: {[]string{"li", "-n"}},
49: {[]string{"table.infobox tr > td:first-child json{}"}},
50: {[]string{"table.infobox tr > th:first-child json{}"}},
51: {[]string{"table.infobox tr > *:first-child json{}"}},
52: {[]string{"frew text{}"}},
53: {[]string{"li", "-v"}},
}
for i, tt := range tests {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
// Reset globals
pupCharset = ""
pupMaxPrintLevel = -1
pupPreformatted = false
pupPrintColor = false
pupEscapeHTML = true
pupIndentString = " "
pupDisplayer = TreeDisplayer{}
t.Logf("testing args: %v", tt.args)
a := []string{"pup"}
a = append(a, tt.args...)
os.Args = a
cmds, err := ParseArgs()
if err != nil {
panic(err)
}
f := testFile("testdata/index.html")
root, err := ParseHTML(f, pupCharset)
if err != nil {
panic(err)
}
buf := &bytes.Buffer{}
if err := runSelectors(buf, cmds, root); err != nil {
panic(err)
}
gp := fmt.Sprintf("testdata/%03d.golden", i)
if *update {
if err := ioutil.WriteFile(gp, buf.Bytes(), 0644); err != nil {
t.Fatalf("failed to update golden file: %s", err)
}
return
}
if len(buf.Bytes()) == 0 {
t.Error("test has no output")
}
if diff := cmp.Diff(testString(gp), buf.String()); diff != "" {
t.Errorf("test does not match golden data (-got, +expect): %s", diff)
}
})
}
}