github.com/hongwozai/go-src-1.4.3@v0.0.0-20191127132709-dc3fce3dbccb/src/go/parser/error_test.go (about)

     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  	"go/scanner"
    27  	"go/token"
    28  	"io/ioutil"
    29  	"path/filepath"
    30  	"regexp"
    31  	"strings"
    32  	"testing"
    33  )
    34  
    35  const testdata = "testdata"
    36  
    37  var fsetErrs = token.NewFileSet()
    38  
    39  // getFile assumes that each filename occurs at most once
    40  func getFile(filename string) (file *token.File) {
    41  	fsetErrs.Iterate(func(f *token.File) bool {
    42  		if f.Name() == filename {
    43  			if file != nil {
    44  				panic(filename + " used multiple times")
    45  			}
    46  			file = f
    47  		}
    48  		return true
    49  	})
    50  	return file
    51  }
    52  
    53  func getPos(filename string, offset int) token.Pos {
    54  	if f := getFile(filename); f != nil {
    55  		return f.Pos(offset)
    56  	}
    57  	return token.NoPos
    58  }
    59  
    60  // ERROR comments must be of the form /* ERROR "rx" */ and rx is
    61  // a regular expression that matches the expected error message.
    62  // The special form /* ERROR HERE "rx" */ must be used for error
    63  // messages that appear immediately after a token, rather than at
    64  // a token's position.
    65  //
    66  var errRx = regexp.MustCompile(`^/\* *ERROR *(HERE)? *"([^"]*)" *\*/$`)
    67  
    68  // expectedErrors collects the regular expressions of ERROR comments found
    69  // in files and returns them as a map of error positions to error messages.
    70  //
    71  func expectedErrors(t *testing.T, 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(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  				pos := prev
    91  				if s[1] == "HERE" {
    92  					pos = here
    93  				}
    94  				errors[pos] = string(s[2])
    95  			}
    96  		default:
    97  			prev = pos
    98  			var l int // token length
    99  			if tok.IsLiteral() {
   100  				l = len(lit)
   101  			} else {
   102  				l = len(tok.String())
   103  			}
   104  			here = prev + token.Pos(l)
   105  		}
   106  	}
   107  }
   108  
   109  // compareErrors compares the map of expected error messages with the list
   110  // of found errors and reports discrepancies.
   111  //
   112  func compareErrors(t *testing.T, expected map[token.Pos]string, found scanner.ErrorList) {
   113  	for _, error := range found {
   114  		// error.Pos is a token.Position, but we want
   115  		// a token.Pos so we can do a map lookup
   116  		pos := getPos(error.Pos.Filename, error.Pos.Offset)
   117  		if msg, found := expected[pos]; found {
   118  			// we expect a message at pos; check if it matches
   119  			rx, err := regexp.Compile(msg)
   120  			if err != nil {
   121  				t.Errorf("%s: %v", error.Pos, err)
   122  				continue
   123  			}
   124  			if match := rx.MatchString(error.Msg); !match {
   125  				t.Errorf("%s: %q does not match %q", error.Pos, error.Msg, msg)
   126  				continue
   127  			}
   128  			// we have a match - eliminate this error
   129  			delete(expected, pos)
   130  		} else {
   131  			// To keep in mind when analyzing failed test output:
   132  			// If the same error position occurs multiple times in errors,
   133  			// this message will be triggered (because the first error at
   134  			// the position removes this position from the expected errors).
   135  			t.Errorf("%s: unexpected error: %s", error.Pos, error.Msg)
   136  		}
   137  	}
   138  
   139  	// there should be no expected errors left
   140  	if len(expected) > 0 {
   141  		t.Errorf("%d errors not reported:", len(expected))
   142  		for pos, msg := range expected {
   143  			t.Errorf("%s: %s\n", fsetErrs.Position(pos), msg)
   144  		}
   145  	}
   146  }
   147  
   148  func checkErrors(t *testing.T, filename string, input interface{}) {
   149  	src, err := readSource(filename, input)
   150  	if err != nil {
   151  		t.Error(err)
   152  		return
   153  	}
   154  
   155  	_, err = ParseFile(fsetErrs, filename, src, DeclarationErrors|AllErrors)
   156  	found, ok := err.(scanner.ErrorList)
   157  	if err != nil && !ok {
   158  		t.Error(err)
   159  		return
   160  	}
   161  	found.RemoveMultiples()
   162  
   163  	// we are expecting the following errors
   164  	// (collect these after parsing a file so that it is found in the file set)
   165  	expected := expectedErrors(t, filename, src)
   166  
   167  	// verify errors returned by the parser
   168  	compareErrors(t, expected, found)
   169  }
   170  
   171  func TestErrors(t *testing.T) {
   172  	list, err := ioutil.ReadDir(testdata)
   173  	if err != nil {
   174  		t.Fatal(err)
   175  	}
   176  	for _, fi := range list {
   177  		name := fi.Name()
   178  		if !fi.IsDir() && !strings.HasPrefix(name, ".") && strings.HasSuffix(name, ".src") {
   179  			checkErrors(t, filepath.Join(testdata, name), nil)
   180  		}
   181  	}
   182  }