OSDN Git Service

3fa35d5dbe4c5eb3fd0cc5a59884cfb92c3be162
[pf3gnuchains/gcc-fork.git] / libgo / go / html / parse_test.go
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.
4
5 package html
6
7 import (
8         "bufio"
9         "bytes"
10         "fmt"
11         "io"
12         "io/ioutil"
13         "os"
14         "strings"
15         "testing"
16 )
17
18 func pipeErr(err os.Error) io.Reader {
19         pr, pw := io.Pipe()
20         pw.CloseWithError(err)
21         return pr
22 }
23
24 func readDat(filename string, c chan io.Reader) {
25         f, err := os.Open("testdata/webkit/" + filename)
26         if err != nil {
27                 c <- pipeErr(err)
28                 return
29         }
30         defer f.Close()
31
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)
35         var pw *io.PipeWriter
36         for {
37                 line, err := r.ReadSlice('\n')
38                 if err != nil {
39                         if pw != nil {
40                                 pw.CloseWithError(err)
41                                 pw = nil
42                         } else {
43                                 c <- pipeErr(err)
44                         }
45                         return
46                 }
47                 if len(line) == 0 {
48                         continue
49                 }
50                 if line[0] == '#' {
51                         if pw != nil {
52                                 pw.Close()
53                         }
54                         var pr *io.PipeReader
55                         pr, pw = io.Pipe()
56                         c <- pr
57                         continue
58                 }
59                 if line[0] != '|' {
60                         // Strip the trailing '\n'.
61                         line = line[:len(line)-1]
62                 }
63                 if pw != nil {
64                         if _, err := pw.Write(line); err != nil {
65                                 pw.CloseWithError(err)
66                                 pw = nil
67                         }
68                 }
69         }
70 }
71
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, "  ")
76         }
77         switch n.Type {
78         case ErrorNode:
79                 return os.NewError("unexpected ErrorNode")
80         case DocumentNode:
81                 return os.NewError("unexpected DocumentNode")
82         case ElementNode:
83                 fmt.Fprintf(w, "<%s>", EscapeString(n.Data))
84         case TextNode:
85                 fmt.Fprintf(w, "%q", EscapeString(n.Data))
86         case CommentNode:
87                 return os.NewError("COMMENT")
88         default:
89                 return os.NewError("unknown node type")
90         }
91         io.WriteString(w, "\n")
92         for _, c := range n.Child {
93                 if err := dumpLevel(w, c, level+1); err != nil {
94                         return err
95                 }
96         }
97         return nil
98 }
99
100 func dump(n *Node) (string, os.Error) {
101         if n == nil || len(n.Child) == 0 {
102                 return "", nil
103         }
104         b := bytes.NewBuffer(nil)
105         for _, child := range n.Child {
106                 if err := dumpLevel(b, child, 0); err != nil {
107                         return "", err
108                 }
109         }
110         return b.String(), nil
111 }
112
113 func TestParser(t *testing.T) {
114         // TODO(nigeltao): Process all the .dat files, not just the first one.
115         filenames := []string{
116                 "tests1.dat",
117         }
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)
125                         if err != nil {
126                                 t.Fatal(err)
127                         }
128                         text := string(b)
129                         doc, err := Parse(strings.NewReader(text))
130                         if err != nil {
131                                 t.Fatal(err)
132                         }
133                         actual, err := dump(doc)
134                         if err != nil {
135                                 t.Fatal(err)
136                         }
137                         // Skip the #error section.
138                         if _, err := io.Copy(ioutil.Discard, <-rc); err != nil {
139                                 t.Fatal(err)
140                         }
141                         // Compare the parsed tree to the #document section.
142                         b, err = ioutil.ReadAll(<-rc)
143                         if err != nil {
144                                 t.Fatal(err)
145                         }
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)
149                         }
150                 }
151         }
152 }