Skip to content

Commit

Permalink
Add test for Walk
Browse files Browse the repository at this point in the history
  • Loading branch information
otiai10 committed Dec 10, 2020
1 parent f2bad15 commit c5e4539
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
21 changes: 21 additions & 0 deletions all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/otiai10/marmoset"
. "github.com/otiai10/mint"
"golang.org/x/net/html"
)

func TestNew(t *testing.T) {
Expand Down Expand Up @@ -115,6 +116,26 @@ func TestFetchWithContext(t *testing.T) {
cancel100ms()
}

func TestOpenGraph_Walk(t *testing.T) {
s := dummyServer(2)
res, err := http.Get(s.URL)
Expect(t, err).ToBe(nil)

node, err := html.Parse(res.Body)
Expect(t, err).ToBe(nil)

og := New(s.URL)
err = og.Walk(node)
Expect(t, err).ToBe(nil)
Expect(t, og.Title).ToBe("はいさいナイト")
res.Body.Close()

again := New(s.URL)
err = again.Walk(node)
Expect(t, err).ToBe(nil)
Expect(t, again.Title).ToBe("はいさいナイト")
}

func dummyServer(id int) *httptest.Server {
marmoset.LoadViews("./test/html")
r := marmoset.NewRouter()
Expand Down
11 changes: 8 additions & 3 deletions opengraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,20 @@ func (og *OpenGraph) Parse(body io.Reader) error {
if err != nil {
return err
}
og.Walk(node)
og.walk(node)
return nil
}

// Walk scans HTML nodes to pick up meaningful OGP data.
func (og *OpenGraph) Walk(n *html.Node) error {
return og.walk(n)
}

func (og *OpenGraph) satisfied() bool {
return false
}

func (og *OpenGraph) Walk(n *html.Node) error {
func (og *OpenGraph) walk(n *html.Node) error {
if og.satisfied() {
return nil
}
Expand All @@ -166,7 +171,7 @@ func (og *OpenGraph) Walk(n *html.Node) error {
}

for child := n.FirstChild; child != nil; child = child.NextSibling {
og.Walk(child)
og.walk(child)
}

return nil
Expand Down

0 comments on commit c5e4539

Please sign in to comment.