1 // Copyright 2010 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
18 func pipeErr(err os.Error) io.Reader {
20 pw.CloseWithError(err)
24 func readDat(filename string, c chan io.Reader) {
25 f, err := os.Open("testdata/webkit/" + filename)
32 // Loop through the lines of the file. Each line beginning with "#" denotes
33 // a new section, which is returned as a separate io.Reader.
34 r := bufio.NewReader(f)
37 line, err := r.ReadSlice('\n')
40 pw.CloseWithError(err)
60 // Strip the trailing '\n'.
61 line = line[:len(line)-1]
64 if _, err := pw.Write(line); err != nil {
65 pw.CloseWithError(err)
72 func dumpLevel(w io.Writer, n *Node, level int) os.Error {
73 io.WriteString(w, "| ")
74 for i := 0; i < level; i++ {
75 io.WriteString(w, " ")
79 return os.NewError("unexpected ErrorNode")
81 return os.NewError("unexpected DocumentNode")
83 fmt.Fprintf(w, "<%s>", EscapeString(n.Data))
85 fmt.Fprintf(w, "%q", EscapeString(n.Data))
87 return os.NewError("COMMENT")
89 return os.NewError("unknown node type")
91 io.WriteString(w, "\n")
92 for _, c := range n.Child {
93 if err := dumpLevel(w, c, level+1); err != nil {
100 func dump(n *Node) (string, os.Error) {
101 if n == nil || len(n.Child) == 0 {
104 b := bytes.NewBuffer(nil)
105 for _, child := range n.Child {
106 if err := dumpLevel(b, child, 0); err != nil {
110 return b.String(), nil
113 func TestParser(t *testing.T) {
114 // TODO(nigeltao): Process all the .dat files, not just the first one.
115 filenames := []string{
118 for _, filename := range filenames {
119 rc := make(chan io.Reader)
120 go readDat(filename, rc)
121 // TODO(nigeltao): Process all test cases, not just a subset.
122 for i := 0; i < 22; i++ {
123 // Parse the #data section.
124 b, err := ioutil.ReadAll(<-rc)
129 doc, err := Parse(strings.NewReader(text))
133 actual, err := dump(doc)
137 // Skip the #error section.
138 if _, err := io.Copy(ioutil.Discard, <-rc); err != nil {
141 // Compare the parsed tree to the #document section.
142 b, err = ioutil.ReadAll(<-rc)
146 expected := string(b)
147 if actual != expected {
148 t.Errorf("%s test #%d %q, actual vs expected:\n----\n%s----\n%s----", filename, i, text, actual, expected)