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