Source file src/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  	"flag"
    27  	"go/scanner"
    28  	"go/token"
    29  	"os"
    30  	"path/filepath"
    31  	"regexp"
    32  	"strings"
    33  	"testing"
    34  )
    35  
    36  var traceErrs = flag.Bool("trace_errs", false, "whether to enable tracing for error tests")
    37  
    38  const testdata = "testdata"
    39  
    40  // getFile assumes that each filename occurs at most once
    41  func getFile(fset *token.FileSet, filename string) (file *token.File) {
    42  	fset.Iterate(func(f *token.File) bool {
    43  		if f.Name() == filename {
    44  			if file != nil {
    45  				panic(filename + " used multiple times")
    46  			}
    47  			file = f
    48  		}
    49  		return true
    50  	})
    51  	return file
    52  }
    53  
    54  func getPos(fset *token.FileSet, filename string, offset int) token.Pos {
    55  	if f := getFile(fset, filename); f != nil {
    56  		return f.Pos(offset)
    57  	}
    58  	return token.NoPos
    59  }
    60  
    61  // ERROR comments must be of the form /* ERROR "rx" */ and rx is
    62  // a regular expression that matches the expected error message.
    63  // The special form /* ERROR HERE "rx" */ must be used for error
    64  // messages that appear immediately after a token, rather than at
    65  // a token's position, and ERROR AFTER means after the comment
    66  // (e.g. at end of line).
    67  var errRx = regexp.MustCompile(`^/\* *ERROR *(HERE|AFTER)? *"([^"]*)" *\*/$`)
    68  
    69  // expectedErrors collects the regular expressions of ERROR comments found
    70  // in files and returns them as a map of error positions to error messages.
    71  func expectedErrors(fset *token.FileSet, filename string, src []byte) map[token.Pos]string {
    72  	errors := make(map[token.Pos]string)
    73  
    74  	var s scanner.Scanner
    75  	// file was parsed already - do not add it again to the file
    76  	// set otherwise the position information returned here will
    77  	// not match the position information collected by the parser
    78  	s.Init(getFile(fset, filename), src, nil, scanner.ScanComments)
    79  	var prev token.Pos // position of last non-comment, non-semicolon token
    80  	var here token.Pos // position immediately after the token at position prev
    81  
    82  	for {
    83  		pos, tok, lit := s.Scan()
    84  		switch tok {
    85  		case token.EOF:
    86  			return errors
    87  		case token.COMMENT:
    88  			s := errRx.FindStringSubmatch(lit)
    89  			if len(s) == 3 {
    90  				if s[1] == "HERE" {
    91  					pos = here // start of comment
    92  				} else if s[1] == "AFTER" {
    93  					pos += token.Pos(len(lit)) // end of comment
    94  				} else {
    95  					pos = prev // token prior to comment
    96  				}
    97  				errors[pos] = s[2]
    98  			}
    99  		case token.SEMICOLON:
   100  			// don't use the position of auto-inserted (invisible) semicolons
   101  			if lit != ";" {
   102  				break
   103  			}
   104  			fallthrough
   105  		default:
   106  			prev = pos
   107  			var l int // token length
   108  			if tok.IsLiteral() {
   109  				l = len(lit)
   110  			} else {
   111  				l = len(tok.String())
   112  			}
   113  			here = prev + token.Pos(l)
   114  		}
   115  	}
   116  }
   117  
   118  // compareErrors compares the map of expected error messages with the list
   119  // of found errors and reports discrepancies.
   120  func compareErrors(t *testing.T, fset *token.FileSet, expected map[token.Pos]string, found scanner.ErrorList) {
   121  	t.Helper()
   122  	for _, error := range found {
   123  		// error.Pos is a token.Position, but we want
   124  		// a token.Pos so we can do a map lookup
   125  		pos := getPos(fset, error.Pos.Filename, error.Pos.Offset)
   126  		if msg, found := expected[pos]; found {
   127  			// we expect a message at pos; check if it matches
   128  			rx, err := regexp.Compile(msg)
   129  			if err != nil {
   130  				t.Errorf("%s: %v", error.Pos, err)
   131  				continue
   132  			}
   133  			if match := rx.MatchString(error.Msg); !match {
   134  				t.Errorf("%s: %q does not match %q", error.Pos, error.Msg, msg)
   135  				continue
   136  			}
   137  			// we have a match - eliminate this error
   138  			delete(expected, pos)
   139  		} else {
   140  			// To keep in mind when analyzing failed test output:
   141  			// If the same error position occurs multiple times in errors,
   142  			// this message will be triggered (because the first error at
   143  			// the position removes this position from the expected errors).
   144  			t.Errorf("%s: unexpected error: %s", error.Pos, error.Msg)
   145  		}
   146  	}
   147  
   148  	// there should be no expected errors left
   149  	if len(expected) > 0 {
   150  		t.Errorf("%d errors not reported:", len(expected))
   151  		for pos, msg := range expected {
   152  			t.Errorf("%s: %s\n", fset.Position(pos), msg)
   153  		}
   154  	}
   155  }
   156  
   157  func checkErrors(t *testing.T, filename string, input any, mode Mode, expectErrors bool) {
   158  	t.Helper()
   159  	src, err := readSource(filename, input)
   160  	if err != nil {
   161  		t.Error(err)
   162  		return
   163  	}
   164  
   165  	fset := token.NewFileSet()
   166  	_, err = ParseFile(fset, filename, src, mode)
   167  	found, ok := err.(scanner.ErrorList)
   168  	if err != nil && !ok {
   169  		t.Error(err)
   170  		return
   171  	}
   172  	found.RemoveMultiples()
   173  
   174  	expected := map[token.Pos]string{}
   175  	if expectErrors {
   176  		// we are expecting the following errors
   177  		// (collect these after parsing a file so that it is found in the file set)
   178  		expected = expectedErrors(fset, filename, src)
   179  	}
   180  
   181  	// verify errors returned by the parser
   182  	compareErrors(t, fset, expected, found)
   183  }
   184  
   185  func TestErrors(t *testing.T) {
   186  	list, err := os.ReadDir(testdata)
   187  	if err != nil {
   188  		t.Fatal(err)
   189  	}
   190  	for _, d := range list {
   191  		name := d.Name()
   192  		t.Run(name, func(t *testing.T) {
   193  			if !d.IsDir() && !strings.HasPrefix(name, ".") && (strings.HasSuffix(name, ".src") || strings.HasSuffix(name, ".go2")) {
   194  				mode := DeclarationErrors | AllErrors
   195  				if *traceErrs {
   196  					mode |= Trace
   197  				}
   198  				checkErrors(t, filepath.Join(testdata, name), nil, mode, true)
   199  			}
   200  		})
   201  	}
   202  }
   203  

View as plain text