// expectedErrors collects the regular expressions of ERROR comments // found in the package files of pkg and returns them in sorted order // (by filename and position). func expectedErrors(t *testing.T, pkg *ast.Package) (list scanner.ErrorList) { // scan all package files for filename := range pkg.Files { src, err := ioutil.ReadFile(filename) if err != nil { t.Fatalf("expectedErrors(%s): %v", pkg.Name, err) } var s scanner.Scanner file := fset.AddFile(filename, fset.Base(), len(src)) s.Init(file, src, nil, scanner.ScanComments) var prev token.Pos // position of last non-comment token loop: for { pos, tok, lit := s.Scan() switch tok { case token.EOF: break loop case token.COMMENT: s := errRx.FindStringSubmatch(lit) if len(s) == 2 { list = append(list, &scanner.Error{fset.Position(prev), string(s[1])}) } default: prev = pos } } } sort.Sort(list) // multiple files may not be sorted return }
// expectedErrors collects the regular expressions of ERROR comments found // in files and returns them as a map of error positions to error messages. // func expectedErrors(t *testing.T, testname string, files map[string]*ast.File) map[token.Pos]string { errors := make(map[token.Pos]string) for filename := range files { src, err := ioutil.ReadFile(filename) if err != nil { t.Fatalf("%s: could not read %s", testname, filename) } var s scanner.Scanner // file was parsed already - do not add it again to the file // set otherwise the position information returned here will // not match the position information collected by the parser s.Init(getFile(filename), src, nil, scanner.ScanComments) var prev token.Pos // position of last non-comment token scanFile: for { pos, tok, lit := s.Scan() switch tok { case token.EOF: break scanFile case token.COMMENT: s := errRx.FindStringSubmatch(lit) if len(s) == 2 { errors[prev] = string(s[1]) } default: prev = pos } } } return errors }