1// 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	"regexp"
37	"testing"
38
39	"cuelang.org/go/cue/errors"
40	"cuelang.org/go/cue/scanner"
41	"cuelang.org/go/cue/token"
42	"cuelang.org/go/internal/source"
43)
44
45func getPos(f *token.File, offset int) token.Pos {
46	if f != nil {
47		return f.Pos(offset, 0)
48	}
49	return token.NoPos
50}
51
52// ERROR comments must be of the form /* ERROR "rx" */ and rx is
53// a regular expression that matches the expected error message.
54// The special form /* ERROR HERE "rx" */ must be used for error
55// messages that appear immediately after a token, rather than at
56// a token's position.
57//
58var errRx = regexp.MustCompile(`^/\* *ERROR *(HERE)? *"([^"]*)" *\*/$`)
59
60// expectedErrors collects the regular expressions of ERROR comments found
61// in files and returns them as a map of error positions to error messages.
62//
63func expectedErrors(t *testing.T, file *token.File, src []byte) map[token.Pos]string {
64	errors := make(map[token.Pos]string)
65
66	var s scanner.Scanner
67	// file was parsed already - do not add it again to the file
68	// set otherwise the position information returned here will
69	// not match the position information collected by the parser
70	// file := token.NewFile(filename, -1, len(src))
71	s.Init(file, src, nil, scanner.ScanComments)
72	var prev token.Pos // position of last non-comment, non-semicolon token
73	var here token.Pos // position immediately after the token at position prev
74
75	for {
76		pos, tok, lit := s.Scan()
77		pos = pos.WithRel(0)
78		switch tok {
79		case token.EOF:
80			return errors
81		case token.COMMENT:
82			s := errRx.FindStringSubmatch(lit)
83			if len(s) == 3 {
84				pos := prev
85				if s[1] == "HERE" {
86					pos = here
87				}
88				errors[pos] = string(s[2])
89			}
90		default:
91			prev = pos
92			var l int // token length
93			if tok.IsLiteral() {
94				l = len(lit)
95			} else {
96				l = len(tok.String())
97			}
98			here = prev.Add(l)
99		}
100	}
101}
102
103// compareErrors compares the map of expected error messages with the list
104// of found errors and reports discrepancies.
105//
106func compareErrors(t *testing.T, file *token.File, expected map[token.Pos]string, found []errors.Error) {
107	t.Helper()
108	for _, error := range found {
109		// error.Pos is a Position, but we want
110		// a Pos so we can do a map lookup
111		ePos := error.Position()
112		eMsg := error.Error()
113		pos := getPos(file, ePos.Offset()).WithRel(0)
114		if msg, found := expected[pos]; found {
115			// we expect a message at pos; check if it matches
116			rx, err := regexp.Compile(msg)
117			if err != nil {
118				t.Errorf("%s: %v", ePos, err)
119				continue
120			}
121			if match := rx.MatchString(eMsg); !match {
122				t.Errorf("%s: %q does not match %q", ePos, eMsg, msg)
123				continue
124			}
125			// we have a match - eliminate this error
126			delete(expected, pos)
127		} else {
128			// To keep in mind when analyzing failed test output:
129			// If the same error position occurs multiple times in errors,
130			// this message will be triggered (because the first error at
131			// the position removes this position from the expected errors).
132			t.Errorf("%s: unexpected error: -%q-", ePos, eMsg)
133		}
134	}
135
136	// there should be no expected errors left
137	if len(expected) > 0 {
138		t.Errorf("%d errors not reported:", len(expected))
139		for pos, msg := range expected {
140			t.Errorf("%s: -%q-\n", pos, msg)
141		}
142	}
143}
144
145func checkErrors(t *testing.T, filename string, input interface{}) {
146	t.Helper()
147	src, err := source.Read(filename, input)
148	if err != nil {
149		t.Error(err)
150		return
151	}
152
153	f, err := ParseFile(filename, src, DeclarationErrors, AllErrors)
154	file := f.Pos().File()
155	found := errors.Errors(err)
156
157	// we are expecting the following errors
158	// (collect these after parsing a file so that it is found in the file set)
159	if file == nil {
160		t.Fatal("")
161	}
162	expected := expectedErrors(t, file, src)
163
164	// verify errors returned by the parser
165	compareErrors(t, file, expected, found)
166}
167
168func TestFuzz(t *testing.T) {
169	testCases := []string{
170		"(({\"\\(0)\"(",
171		"{{\"\\(0\xbf\"(",
172		"a:y for x n{b:\"\"(\"\\(" +
173			"\"\"\\\"(",
174	}
175	for _, tc := range testCases {
176		t.Run("", func(t *testing.T) {
177			_, _ = ParseFile("go-fuzz", []byte(tc))
178		})
179	}
180}
181