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