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