aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/ast/parser_test.go
diff options
context:
space:
mode:
authorDmitry Vyukov <dvyukov@google.com>2017-08-27 19:55:14 +0200
committerDmitry Vyukov <dvyukov@google.com>2017-08-27 20:19:41 +0200
commit4074aed7c0c28afc7d4a3522045196c3f39b5208 (patch)
tree8d2c2ce5f6767f8f4355e37e262f85223ee362e3 /pkg/ast/parser_test.go
parent58579664687b203ff34fad8aa02bf470ef0bc981 (diff)
pkg/compiler: more static error checking
Update #217
Diffstat (limited to 'pkg/ast/parser_test.go')
-rw-r--r--pkg/ast/parser_test.go83
1 files changed, 11 insertions, 72 deletions
diff --git a/pkg/ast/parser_test.go b/pkg/ast/parser_test.go
index 84a5fedf0..46ad1e5d3 100644
--- a/pkg/ast/parser_test.go
+++ b/pkg/ast/parser_test.go
@@ -4,7 +4,6 @@
package ast
import (
- "bufio"
"bytes"
"io/ioutil"
"path/filepath"
@@ -29,7 +28,7 @@ func TestParseAll(t *testing.T) {
}
t.Run(file.Name(), func(t *testing.T) {
eh := func(pos Pos, msg string) {
- t.Fatalf("%v:%v:%v: %v", pos.File, pos.Line, pos.Col, msg)
+ t.Fatalf("%v: %v", pos, msg)
}
desc := Parse(data, file.Name(), eh)
if desc == nil {
@@ -65,7 +64,7 @@ func TestParse(t *testing.T) {
for _, test := range parseTests {
t.Run(test.name, func(t *testing.T) {
errorHandler := func(pos Pos, msg string) {
- t.Logf("%v:%v:%v: %v", pos.File, pos.Line, pos.Col, msg)
+ t.Logf("%v: %v", pos, msg)
}
Parse([]byte(test.input), "foo", errorHandler)
})
@@ -96,13 +95,6 @@ var parseTests = []struct {
},
}
-type Error struct {
- Line int
- Col int
- Text string
- Matched bool
-}
-
func TestErrors(t *testing.T) {
files, err := ioutil.ReadDir("testdata")
if err != nil {
@@ -115,71 +107,18 @@ func TestErrors(t *testing.T) {
if !strings.HasSuffix(f.Name(), ".txt") {
continue
}
- t.Run(f.Name(), func(t *testing.T) {
- data, err := ioutil.ReadFile(filepath.Join("testdata", f.Name()))
- if err != nil {
- t.Fatalf("failed to open input file: %v", err)
- }
- var stripped []byte
- var errors []*Error
- s := bufio.NewScanner(bytes.NewReader(data))
- for i := 1; s.Scan(); i++ {
- ln := s.Bytes()
- for {
- pos := bytes.LastIndex(ln, []byte("###"))
- if pos == -1 {
- break
- }
- errors = append(errors, &Error{
- Line: i,
- Text: strings.TrimSpace(string(ln[pos+3:])),
- })
- ln = ln[:pos]
- }
- stripped = append(stripped, ln...)
- stripped = append(stripped, '\n')
- }
- if err := s.Err(); err != nil {
- t.Fatalf("failed to scan input file: %v", err)
+ name := f.Name()
+ t.Run(name, func(t *testing.T) {
+ em := NewErrorMatcher(t, filepath.Join("testdata", name))
+ desc := Parse(em.Data, name, em.ErrorHandler)
+ if desc != nil && em.Count() != 0 {
+ em.DumpErrors(t)
+ t.Fatalf("parsing succeed, but got errors")
}
- var got []*Error
- desc := Parse(stripped, "test", func(pos Pos, msg string) {
- got = append(got, &Error{
- Line: pos.Line,
- Col: pos.Col,
- Text: msg,
- })
- })
- if desc != nil && len(got) != 0 {
- t.Fatalf("parsing succeed, but got errors: %v", got)
- }
- if desc == nil && len(got) == 0 {
+ if desc == nil && em.Count() == 0 {
t.Fatalf("parsing failed, but got no errors")
}
- nextErr:
- for _, gotErr := range got {
- for _, wantErr := range errors {
- if wantErr.Matched {
- continue
- }
- if wantErr.Line != gotErr.Line {
- continue
- }
- if wantErr.Text != gotErr.Text {
- continue
- }
- wantErr.Matched = true
- continue nextErr
- }
- t.Errorf("unexpected error: %v:%v: %v",
- gotErr.Line, gotErr.Col, gotErr.Text)
- }
- for _, wantErr := range errors {
- if wantErr.Matched {
- continue
- }
- t.Errorf("not matched error: %v: %v", wantErr.Line, wantErr.Text)
- }
+ em.Check(t)
})
}
}