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.
20 func (devNull) Write(p []byte) (int, os.Error) {
24 func pipeErr(err os.Error) io.Reader {
26 pw.CloseWithError(err)
30 func readDat(filename string, c chan io.Reader) {
31 f, err := os.Open("testdata/webkit/"+filename, os.O_RDONLY, 0600)
38 // Loop through the lines of the file. Each line beginning with "#" denotes
39 // a new section, which is returned as a separate io.Reader.
40 r := bufio.NewReader(f)
43 line, err := r.ReadSlice('\n')
46 pw.CloseWithError(err)
66 // Strip the trailing '\n'.
67 line = line[:len(line)-1]
70 if _, err := pw.Write(line); err != nil {
71 pw.CloseWithError(err)
78 func dumpLevel(w io.Writer, n *Node, level int) os.Error {
79 io.WriteString(w, "| ")
80 for i := 0; i < level; i++ {
81 io.WriteString(w, " ")
85 return os.NewError("unexpected ErrorNode")
87 return os.NewError("unexpected DocumentNode")
89 fmt.Fprintf(w, "<%s>", EscapeString(n.Data))
91 fmt.Fprintf(w, "%q", EscapeString(n.Data))
93 return os.NewError("COMMENT")
95 return os.NewError("unknown node type")
97 io.WriteString(w, "\n")
98 for _, c := range n.Child {
99 if err := dumpLevel(w, c, level+1); err != nil {
106 func dump(n *Node) (string, os.Error) {
107 if n == nil || len(n.Child) == 0 {
110 b := bytes.NewBuffer(nil)
111 for _, child := range n.Child {
112 if err := dumpLevel(b, child, 0); err != nil {
116 return b.String(), nil
119 func TestParser(t *testing.T) {
120 // TODO(nigeltao): Process all the .dat files, not just the first one.
121 filenames := []string{
124 for _, filename := range filenames {
125 rc := make(chan io.Reader)
126 go readDat(filename, rc)
127 // TODO(nigeltao): Process all test cases, not just a subset.
128 for i := 0; i < 22; i++ {
129 // Parse the #data section.
130 b, err := ioutil.ReadAll(<-rc)
135 doc, err := Parse(strings.NewReader(text))
139 actual, err := dump(doc)
143 // Skip the #error section.
144 if _, err := io.Copy(devNull{}, <-rc); err != nil {
147 // Compare the parsed tree to the #document section.
148 b, err = ioutil.ReadAll(<-rc)
152 expected := string(b)
153 if actual != expected {
154 t.Errorf("%s test #%d %q, actual vs expected:\n----\n%s----\n%s----", filename, i, text, actual, expected)