...
Run Format

Source file src/go/types/check_test.go

Documentation: go/types

     1  // Copyright 2011 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 typechecker test harness. The packages specified
     6  // in tests are typechecked. Error messages reported by the typechecker are
     7  // compared against the error messages expected in the test files.
     8  //
     9  // Expected errors are indicated in the test files by putting a comment
    10  // of the form /* ERROR "rx" */ immediately following an offending token.
    11  // The harness will verify that an error matching the regular expression
    12  // rx is reported at that source position. Consecutive comments may be
    13  // used to indicate multiple errors for the same token 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  // TODO(gri) Also collect strict mode errors of the form /* STRICT ... */
    24  //           and test against strict mode.
    25  
    26  package types_test
    27  
    28  import (
    29  	"flag"
    30  	"go/ast"
    31  	"go/importer"
    32  	"go/parser"
    33  	"go/scanner"
    34  	"go/token"
    35  	"internal/testenv"
    36  	"io/ioutil"
    37  	"regexp"
    38  	"strings"
    39  	"testing"
    40  
    41  	. "go/types"
    42  )
    43  
    44  var (
    45  	listErrors = flag.Bool("errlist", false, "list errors")
    46  	testFiles  = flag.String("files", "", "space-separated list of test files")
    47  )
    48  
    49  // The test filenames do not end in .go so that they are invisible
    50  // to gofmt since they contain comments that must not change their
    51  // positions relative to surrounding tokens.
    52  
    53  // Each tests entry is list of files belonging to the same package.
    54  var tests = [][]string{
    55  	{"testdata/errors.src"},
    56  	{"testdata/importdecl0a.src", "testdata/importdecl0b.src"},
    57  	{"testdata/importdecl1a.src", "testdata/importdecl1b.src"},
    58  	{"testdata/importC.src"}, // special handling in checkFiles
    59  	{"testdata/cycles.src"},
    60  	{"testdata/cycles1.src"},
    61  	{"testdata/cycles2.src"},
    62  	{"testdata/cycles3.src"},
    63  	{"testdata/cycles4.src"},
    64  	{"testdata/cycles5.src"},
    65  	{"testdata/init0.src"},
    66  	{"testdata/init1.src"},
    67  	{"testdata/init2.src"},
    68  	{"testdata/decls0.src"},
    69  	{"testdata/decls1.src"},
    70  	{"testdata/decls2a.src", "testdata/decls2b.src"},
    71  	{"testdata/decls3.src"},
    72  	{"testdata/decls4.src"},
    73  	{"testdata/decls5.src"},
    74  	{"testdata/const0.src"},
    75  	{"testdata/const1.src"},
    76  	{"testdata/constdecl.src"},
    77  	{"testdata/vardecl.src"},
    78  	{"testdata/expr0.src"},
    79  	{"testdata/expr1.src"},
    80  	{"testdata/expr2.src"},
    81  	{"testdata/expr3.src"},
    82  	{"testdata/methodsets.src"},
    83  	{"testdata/shifts.src"},
    84  	{"testdata/builtins.src"},
    85  	{"testdata/conversions.src"},
    86  	{"testdata/conversions2.src"},
    87  	{"testdata/stmt0.src"},
    88  	{"testdata/stmt1.src"},
    89  	{"testdata/gotos.src"},
    90  	{"testdata/labels.src"},
    91  	{"testdata/issues.src"},
    92  	{"testdata/blank.src"},
    93  	{"testdata/issue25008b.src", "testdata/issue25008a.src"}, // order (b before a) is crucial!
    94  	{"testdata/issue26390.src"},                              // stand-alone test to ensure case is triggered
    95  }
    96  
    97  var fset = token.NewFileSet()
    98  
    99  // Positioned errors are of the form filename:line:column: message .
   100  var posMsgRx = regexp.MustCompile(`^(.*:[0-9]+:[0-9]+): *(.*)`)
   101  
   102  // splitError splits an error's error message into a position string
   103  // and the actual error message. If there's no position information,
   104  // pos is the empty string, and msg is the entire error message.
   105  //
   106  func splitError(err error) (pos, msg string) {
   107  	msg = err.Error()
   108  	if m := posMsgRx.FindStringSubmatch(msg); len(m) == 3 {
   109  		pos = m[1]
   110  		msg = m[2]
   111  	}
   112  	return
   113  }
   114  
   115  func parseFiles(t *testing.T, filenames []string) ([]*ast.File, []error) {
   116  	var files []*ast.File
   117  	var errlist []error
   118  	for _, filename := range filenames {
   119  		file, err := parser.ParseFile(fset, filename, nil, parser.AllErrors)
   120  		if file == nil {
   121  			t.Fatalf("%s: %s", filename, err)
   122  		}
   123  		files = append(files, file)
   124  		if err != nil {
   125  			if list, _ := err.(scanner.ErrorList); len(list) > 0 {
   126  				for _, err := range list {
   127  					errlist = append(errlist, err)
   128  				}
   129  			} else {
   130  				errlist = append(errlist, err)
   131  			}
   132  		}
   133  	}
   134  	return files, errlist
   135  }
   136  
   137  // ERROR comments must start with text `ERROR "rx"` or `ERROR rx` where
   138  // rx is a regular expression that matches the expected error message.
   139  // Space around "rx" or rx is ignored. Use the form `ERROR HERE "rx"`
   140  // for error messages that are located immediately after rather than
   141  // at a token's position.
   142  //
   143  var errRx = regexp.MustCompile(`^ *ERROR *(HERE)? *"?([^"]*)"?`)
   144  
   145  // errMap collects the regular expressions of ERROR comments found
   146  // in files and returns them as a map of error positions to error messages.
   147  //
   148  func errMap(t *testing.T, testname string, files []*ast.File) map[string][]string {
   149  	// map of position strings to lists of error message patterns
   150  	errmap := make(map[string][]string)
   151  
   152  	for _, file := range files {
   153  		filename := fset.Position(file.Package).Filename
   154  		src, err := ioutil.ReadFile(filename)
   155  		if err != nil {
   156  			t.Fatalf("%s: could not read %s", testname, filename)
   157  		}
   158  
   159  		var s scanner.Scanner
   160  		s.Init(fset.AddFile(filename, -1, len(src)), src, nil, scanner.ScanComments)
   161  		var prev token.Pos // position of last non-comment, non-semicolon token
   162  		var here token.Pos // position immediately after the token at position prev
   163  
   164  	scanFile:
   165  		for {
   166  			pos, tok, lit := s.Scan()
   167  			switch tok {
   168  			case token.EOF:
   169  				break scanFile
   170  			case token.COMMENT:
   171  				if lit[1] == '*' {
   172  					lit = lit[:len(lit)-2] // strip trailing */
   173  				}
   174  				if s := errRx.FindStringSubmatch(lit[2:]); len(s) == 3 {
   175  					pos := prev
   176  					if s[1] == "HERE" {
   177  						pos = here
   178  					}
   179  					p := fset.Position(pos).String()
   180  					errmap[p] = append(errmap[p], strings.TrimSpace(s[2]))
   181  				}
   182  			case token.SEMICOLON:
   183  				// ignore automatically inserted semicolon
   184  				if lit == "\n" {
   185  					continue scanFile
   186  				}
   187  				fallthrough
   188  			default:
   189  				prev = pos
   190  				var l int // token length
   191  				if tok.IsLiteral() {
   192  					l = len(lit)
   193  				} else {
   194  					l = len(tok.String())
   195  				}
   196  				here = prev + token.Pos(l)
   197  			}
   198  		}
   199  	}
   200  
   201  	return errmap
   202  }
   203  
   204  func eliminate(t *testing.T, errmap map[string][]string, errlist []error) {
   205  	for _, err := range errlist {
   206  		pos, gotMsg := splitError(err)
   207  		list := errmap[pos]
   208  		index := -1 // list index of matching message, if any
   209  		// we expect one of the messages in list to match the error at pos
   210  		for i, wantRx := range list {
   211  			rx, err := regexp.Compile(wantRx)
   212  			if err != nil {
   213  				t.Errorf("%s: %v", pos, err)
   214  				continue
   215  			}
   216  			if rx.MatchString(gotMsg) {
   217  				index = i
   218  				break
   219  			}
   220  		}
   221  		if index >= 0 {
   222  			// eliminate from list
   223  			if n := len(list) - 1; n > 0 {
   224  				// not the last entry - swap in last element and shorten list by 1
   225  				list[index] = list[n]
   226  				errmap[pos] = list[:n]
   227  			} else {
   228  				// last entry - remove list from map
   229  				delete(errmap, pos)
   230  			}
   231  		} else {
   232  			t.Errorf("%s: no error expected: %q", pos, gotMsg)
   233  		}
   234  	}
   235  }
   236  
   237  func checkFiles(t *testing.T, testfiles []string) {
   238  	// parse files and collect parser errors
   239  	files, errlist := parseFiles(t, testfiles)
   240  
   241  	pkgName := "<no package>"
   242  	if len(files) > 0 {
   243  		pkgName = files[0].Name.Name
   244  	}
   245  
   246  	if *listErrors && len(errlist) > 0 {
   247  		t.Errorf("--- %s:", pkgName)
   248  		for _, err := range errlist {
   249  			t.Error(err)
   250  		}
   251  	}
   252  
   253  	// typecheck and collect typechecker errors
   254  	var conf Config
   255  	// special case for importC.src
   256  	if len(testfiles) == 1 && testfiles[0] == "testdata/importC.src" {
   257  		conf.FakeImportC = true
   258  	}
   259  	conf.Importer = importer.Default()
   260  	conf.Error = func(err error) {
   261  		if *listErrors {
   262  			t.Error(err)
   263  			return
   264  		}
   265  		// Ignore secondary error messages starting with "\t";
   266  		// they are clarifying messages for a primary error.
   267  		if !strings.Contains(err.Error(), ": \t") {
   268  			errlist = append(errlist, err)
   269  		}
   270  	}
   271  	conf.Check(pkgName, fset, files, nil)
   272  
   273  	if *listErrors {
   274  		return
   275  	}
   276  
   277  	// match and eliminate errors;
   278  	// we are expecting the following errors
   279  	errmap := errMap(t, pkgName, files)
   280  	eliminate(t, errmap, errlist)
   281  
   282  	// there should be no expected errors left
   283  	if len(errmap) > 0 {
   284  		t.Errorf("--- %s: %d source positions with expected (but not reported) errors:", pkgName, len(errmap))
   285  		for pos, list := range errmap {
   286  			for _, rx := range list {
   287  				t.Errorf("%s: %q", pos, rx)
   288  			}
   289  		}
   290  	}
   291  }
   292  
   293  func TestCheck(t *testing.T) {
   294  	testenv.MustHaveGoBuild(t)
   295  
   296  	// Declare builtins for testing.
   297  	DefPredeclaredTestFuncs()
   298  
   299  	// If explicit test files are specified, only check those.
   300  	if files := *testFiles; files != "" {
   301  		checkFiles(t, strings.Split(files, " "))
   302  		return
   303  	}
   304  
   305  	// Otherwise, run all the tests.
   306  	for _, files := range tests {
   307  		checkFiles(t, files)
   308  	}
   309  }
   310  

View as plain text