add NewDocumentFromReader, closes #20

This commit is contained in:
Martin Angers
2013-09-07 10:57:54 -04:00
parent e8c0a63721
commit f065786d41
3 changed files with 68 additions and 5 deletions

View File

@@ -1,6 +1,7 @@
package goquery
import (
"bytes"
"code.google.com/p/go.net/html"
"fmt"
"os"
@@ -99,3 +100,51 @@ func TestNewDocument(t *testing.T) {
}
}
}
func TestNewDocumentFromReader(t *testing.T) {
cases := []struct {
src string
err bool
sel string
cnt int
}{
0: {
src: `
<html>
<head>
<title>Test</title>
<body>
<h1>Hi</h1>
</body>
</html>`,
sel: "h1",
cnt: 1,
},
1: {
// Actually pretty hard to make html.Parse return an error
// based on content...
src: `<html><body><aef<eqf>>>qq></body></ht>`,
},
}
buf := bytes.NewBuffer(nil)
for i, c := range cases {
buf.Reset()
buf.WriteString(c.src)
d, e := NewDocumentFromReader(buf)
if (e != nil) != c.err {
if c.err {
t.Errorf("[%d] - expected error, got none", i)
} else {
t.Errorf("[%d] - expected no error, got %s", i, e)
}
}
if c.sel != "" {
s := d.Find(c.sel)
if s.Length() != c.cnt {
t.Errorf("[%d] - expected %d nodes, found %d", i, c.cnt, s.Length())
}
}
}
}