forked from ray-g/gopl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutline_test.go
67 lines (54 loc) · 1.1 KB
/
outline_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
package main
import (
"bytes"
"fmt"
"net/http"
"testing"
"golang.org/x/net/html"
)
func outlineOri(url string) error {
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
doc, err := html.Parse(resp.Body)
if err != nil {
return err
}
//!+call
forEachNode(doc, startElement, endElement)
//!-call
return nil
}
var gDepth int
func startElement(n *html.Node) {
if n.Type == html.ElementNode {
fmt.Fprintf(stdout, "%*s<%s>\n", gDepth*2, "", n.Data)
gDepth++
}
}
func endElement(n *html.Node) {
if n.Type == html.ElementNode {
gDepth--
fmt.Fprintf(stdout, "%*s</%s>\n", gDepth*2, "", n.Data)
}
}
func TestOutline(t *testing.T) {
var tests = []struct {
url string
}{
{"https://github.com/"},
}
for _, test := range tests {
stdout = new(bytes.Buffer)
outlineOri(test.url) //original implementation
expects := stdout.(*bytes.Buffer).String()
stdout = new(bytes.Buffer)
err := outline(test.url)
actual := stdout.(*bytes.Buffer).String()
if err != nil || actual != expects {
t.Errorf("Expects:\n%v\nActual:\n%v", expects, actual)
}
}
}