Imported Upstream version 4.8.1
[platform/upstream/gcc48.git] / libgo / go / go / parser / error_test.go
1 // Copyright 2012 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 // This file implements a parser test harness. The files in the testdata
6 // directory are parsed and the errors reported are compared against the
7 // error messages expected in the test files. The test files must end in
8 // .src rather than .go so that they are not disturbed by gofmt runs.
9 //
10 // Expected errors are indicated in the test files by putting a comment
11 // of the form /* ERROR "rx" */ immediately following an offending token.
12 // The harness will verify that an error matching the regular expression
13 // rx is reported at that source position.
14 //
15 // For instance, the following test file indicates that a "not declared"
16 // error should be reported for the undeclared variable x:
17 //
18 //      package p
19 //      func f() {
20 //              _ = x /* ERROR "not declared" */ + 1
21 //      }
22
23 package parser
24
25 import (
26         "go/scanner"
27         "go/token"
28         "io/ioutil"
29         "path/filepath"
30         "regexp"
31         "strings"
32         "testing"
33 )
34
35 const testdata = "testdata"
36
37 var fsetErrs *token.FileSet
38
39 // getFile assumes that each filename occurs at most once
40 func getFile(filename string) (file *token.File) {
41         fsetErrs.Iterate(func(f *token.File) bool {
42                 if f.Name() == filename {
43                         if file != nil {
44                                 panic(filename + " used multiple times")
45                         }
46                         file = f
47                 }
48                 return true
49         })
50         return file
51 }
52
53 func getPos(filename string, offset int) token.Pos {
54         if f := getFile(filename); f != nil {
55                 return f.Pos(offset)
56         }
57         return token.NoPos
58 }
59
60 // ERROR comments must be of the form /* ERROR "rx" */ and rx is
61 // a regular expression that matches the expected error message.
62 //
63 var errRx = regexp.MustCompile(`^/\* *ERROR *"([^"]*)" *\*/$`)
64
65 // expectedErrors collects the regular expressions of ERROR comments found
66 // in files and returns them as a map of error positions to error messages.
67 //
68 func expectedErrors(t *testing.T, filename string, src []byte) map[token.Pos]string {
69         errors := make(map[token.Pos]string)
70
71         var s scanner.Scanner
72         // file was parsed already - do not add it again to the file
73         // set otherwise the position information returned here will
74         // not match the position information collected by the parser
75         s.Init(getFile(filename), src, nil, scanner.ScanComments)
76         var prev token.Pos // position of last non-comment, non-semicolon token
77
78         for {
79                 pos, tok, lit := s.Scan()
80                 switch tok {
81                 case token.EOF:
82                         return errors
83                 case token.COMMENT:
84                         s := errRx.FindStringSubmatch(lit)
85                         if len(s) == 2 {
86                                 errors[prev] = string(s[1])
87                         }
88                 default:
89                         prev = pos
90                 }
91         }
92
93         panic("unreachable")
94 }
95
96 // compareErrors compares the map of expected error messages with the list
97 // of found errors and reports discrepancies.
98 //
99 func compareErrors(t *testing.T, expected map[token.Pos]string, found scanner.ErrorList) {
100         for _, error := range found {
101                 // error.Pos is a token.Position, but we want
102                 // a token.Pos so we can do a map lookup
103                 pos := getPos(error.Pos.Filename, error.Pos.Offset)
104                 if msg, found := expected[pos]; found {
105                         // we expect a message at pos; check if it matches
106                         rx, err := regexp.Compile(msg)
107                         if err != nil {
108                                 t.Errorf("%s: %v", error.Pos, err)
109                                 continue
110                         }
111                         if match := rx.MatchString(error.Msg); !match {
112                                 t.Errorf("%s: %q does not match %q", error.Pos, error.Msg, msg)
113                                 continue
114                         }
115                         // we have a match - eliminate this error
116                         delete(expected, pos)
117                 } else {
118                         // To keep in mind when analyzing failed test output:
119                         // If the same error position occurs multiple times in errors,
120                         // this message will be triggered (because the first error at
121                         // the position removes this position from the expected errors).
122                         t.Errorf("%s: unexpected error: %s", error.Pos, error.Msg)
123                 }
124         }
125
126         // there should be no expected errors left
127         if len(expected) > 0 {
128                 t.Errorf("%d errors not reported:", len(expected))
129                 for pos, msg := range expected {
130                         t.Errorf("%s: %s\n", fsetErrs.Position(pos), msg)
131                 }
132         }
133 }
134
135 func checkErrors(t *testing.T, filename string, input interface{}) {
136         src, err := readSource(filename, input)
137         if err != nil {
138                 t.Error(err)
139                 return
140         }
141
142         _, err = ParseFile(fsetErrs, filename, src, DeclarationErrors)
143         found, ok := err.(scanner.ErrorList)
144         if err != nil && !ok {
145                 t.Error(err)
146                 return
147         }
148
149         // we are expecting the following errors
150         // (collect these after parsing a file so that it is found in the file set)
151         expected := expectedErrors(t, filename, src)
152
153         // verify errors returned by the parser
154         compareErrors(t, expected, found)
155 }
156
157 func TestErrors(t *testing.T) {
158         fsetErrs = token.NewFileSet()
159         list, err := ioutil.ReadDir(testdata)
160         if err != nil {
161                 t.Fatal(err)
162         }
163         for _, fi := range list {
164                 name := fi.Name()
165                 if !fi.IsDir() && !strings.HasPrefix(name, ".") && strings.HasSuffix(name, ".src") {
166                         checkErrors(t, filepath.Join(testdata, name), nil)
167                 }
168         }
169 }