blob: 5bfff878df54578b90429b170ff4065c648a9571 [file] [log] [blame]
Marcel van Lohuizend96ad3d2018-12-10 15:30:20 +01001// Copyright 2018 The CUE Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15// This file implements a parser test harness. The files in the testdata
16// directory are parsed and the errors reported are compared against the
17// error messages expected in the test files. The test files must end in
18// .src rather than .go so that they are not disturbed by gofmt runs.
19//
20// Expected errors are indicated in the test files by putting a comment
21// of the form /* ERROR "rx" */ immediately following an offending
22// The harness will verify that an error matching the regular expression
23// rx is reported at that source position.
24//
25// For instance, the following test file indicates that a "not declared"
26// error should be reported for the undeclared variable x:
27//
28// package p
29// {
30// a = x /* ERROR "not declared" */ + 1
31// }
32
33package parser
34
35import (
36 "io/ioutil"
37 "path/filepath"
38 "regexp"
39 "strings"
40 "testing"
41
42 "cuelang.org/go/cue/errors"
43 "cuelang.org/go/cue/scanner"
44 "cuelang.org/go/cue/token"
45)
46
47const testdata = "testdata"
48
49// getFile assumes that each filename occurs at most once
50func getFile(fset *token.FileSet, filename string) (info *token.File) {
51 fset.Iterate(func(f *token.File) bool {
52 if f.Name() == filename {
53 if info != nil {
54 panic(filename + " used multiple times")
55 }
56 info = f
57 }
58 return true
59 })
60 return info
61}
62
63func getPos(fset *token.FileSet, filename string, offset int) token.Pos {
64 if f := getFile(fset, filename); f != nil {
65 return f.Pos(offset, 0)
66 }
67 return token.NoPos
68}
69
70// ERROR comments must be of the form /* ERROR "rx" */ and rx is
71// a regular expression that matches the expected error message.
72// The special form /* ERROR HERE "rx" */ must be used for error
73// messages that appear immediately after a token, rather than at
74// a token's position.
75//
76var errRx = regexp.MustCompile(`^/\* *ERROR *(HERE)? *"([^"]*)" *\*/$`)
77
78// expectedErrors collects the regular expressions of ERROR comments found
79// in files and returns them as a map of error positions to error messages.
80//
81func expectedErrors(t *testing.T, fset *token.FileSet, filename string, src []byte) map[token.Pos]string {
82 errors := make(map[token.Pos]string)
83
84 var s scanner.Scanner
85 // file was parsed already - do not add it again to the file
86 // set otherwise the position information returned here will
87 // not match the position information collected by the parser
88 s.Init(getFile(fset, filename), src, nil, scanner.ScanComments)
89 var prev token.Pos // position of last non-comment, non-semicolon token
90 var here token.Pos // position immediately after the token at position prev
91
92 for {
93 pos, tok, lit := s.Scan()
94 pos = pos.WithRel(0)
95 switch tok {
96 case token.EOF:
97 return errors
98 case token.COMMENT:
99 s := errRx.FindStringSubmatch(lit)
100 if len(s) == 3 {
101 pos := prev
102 if s[1] == "HERE" {
103 pos = here
104 }
105 errors[pos] = string(s[2])
106 }
107 default:
108 prev = pos
109 var l int // token length
110 if tok.IsLiteral() {
111 l = len(lit)
112 } else {
113 l = len(tok.String())
114 }
115 here = prev + token.Pos(l)
116 }
117 }
118}
119
120// compareErrors compares the map of expected error messages with the list
121// of found errors and reports discrepancies.
122//
123func compareErrors(t *testing.T, fset *token.FileSet, expected map[token.Pos]string, found errors.List) {
124 t.Helper()
125 for _, error := range found {
126 // error.Pos is a Position, but we want
127 // a Pos so we can do a map lookup
128 ePos := error.Position()
129 eMsg := error.Error()
130 pos := getPos(fset, ePos.Filename, ePos.Offset).WithRel(0)
131 if msg, found := expected[pos]; found {
132 // we expect a message at pos; check if it matches
133 rx, err := regexp.Compile(msg)
134 if err != nil {
135 t.Errorf("%s: %v", ePos, err)
136 continue
137 }
138 if match := rx.MatchString(eMsg); !match {
139 t.Errorf("%s: %q does not match %q", ePos, eMsg, msg)
140 continue
141 }
142 // we have a match - eliminate this error
143 delete(expected, pos)
144 } else {
145 // To keep in mind when analyzing failed test output:
146 // If the same error position occurs multiple times in errors,
147 // this message will be triggered (because the first error at
148 // the position removes this position from the expected errors).
149 t.Errorf("%s: unexpected error: -%q-", ePos, eMsg)
150 }
151 }
152
153 // there should be no expected errors left
154 if len(expected) > 0 {
155 t.Errorf("%d errors not reported:", len(expected))
156 for pos, msg := range expected {
157 t.Errorf("%s: -%q-\n", fset.Position(pos), msg)
158 }
159 }
160}
161
162func checkErrors(t *testing.T, filename string, input interface{}) {
163 t.Helper()
164 src, err := readSource(filename, input)
165 if err != nil {
166 t.Error(err)
167 return
168 }
169
170 fset := token.NewFileSet()
Marcel van Lohuizen2bf066f2018-12-16 11:43:49 +0100171 _, err = ParseFile(fset, filename, src, DeclarationErrors, AllErrors)
Marcel van Lohuizend96ad3d2018-12-10 15:30:20 +0100172 found, ok := err.(errors.List)
173 if err != nil && !ok {
174 t.Error(err)
175 return
176 }
177 found.RemoveMultiples()
178
179 // we are expecting the following errors
180 // (collect these after parsing a file so that it is found in the file set)
181 expected := expectedErrors(t, fset, filename, src)
182
183 // verify errors returned by the parser
184 compareErrors(t, fset, expected, found)
185}
186
187func TestErrors(t *testing.T) {
188 list, err := ioutil.ReadDir(testdata)
189 if err != nil {
190 t.Fatal(err)
191 }
192 for _, fi := range list {
193 name := fi.Name()
194 if !fi.IsDir() && !strings.HasPrefix(name, ".") && strings.HasSuffix(name, ".src") {
195 checkErrors(t, filepath.Join(testdata, name), nil)
196 }
197 }
198}