aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/ast/test_util.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/test_util.go
parent58579664687b203ff34fad8aa02bf470ef0bc981 (diff)
pkg/compiler: more static error checking
Update #217
Diffstat (limited to 'pkg/ast/test_util.go')
-rw-r--r--pkg/ast/test_util.go99
1 files changed, 99 insertions, 0 deletions
diff --git a/pkg/ast/test_util.go b/pkg/ast/test_util.go
new file mode 100644
index 000000000..0aed0a2dc
--- /dev/null
+++ b/pkg/ast/test_util.go
@@ -0,0 +1,99 @@
+// Copyright 2017 syzkaller project authors. All rights reserved.
+// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
+
+package ast
+
+import (
+ "bufio"
+ "bytes"
+ "io/ioutil"
+ "strings"
+ "testing"
+)
+
+type ErrorMatcher struct {
+ Data []byte
+ expect []*errorDesc
+ got []*errorDesc
+}
+
+type errorDesc struct {
+ file string
+ line int
+ col int
+ text string
+ matched bool
+}
+
+func NewErrorMatcher(t *testing.T, file string) *ErrorMatcher {
+ data, err := ioutil.ReadFile(file)
+ if err != nil {
+ t.Fatalf("failed to open input file: %v", err)
+ }
+ var stripped []byte
+ var errors []*errorDesc
+ 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, &errorDesc{
+ file: file,
+ 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)
+ }
+ return &ErrorMatcher{
+ Data: stripped,
+ expect: errors,
+ }
+}
+
+func (em *ErrorMatcher) ErrorHandler(pos Pos, msg string) {
+ em.got = append(em.got, &errorDesc{
+ file: pos.File,
+ line: pos.Line,
+ col: pos.Col,
+ text: msg,
+ })
+}
+
+func (em *ErrorMatcher) Count() int {
+ return len(em.got)
+}
+
+func (em *ErrorMatcher) Check(t *testing.T) {
+nextErr:
+ for _, e := range em.got {
+ for _, want := range em.expect {
+ if want.matched || want.line != e.line || want.text != e.text {
+ continue
+ }
+ want.matched = true
+ continue nextErr
+ }
+ t.Errorf("unexpected error: %v:%v:%v: %v", e.file, e.line, e.col, e.text)
+ }
+ for _, want := range em.expect {
+ if want.matched {
+ continue
+ }
+ t.Errorf("unmatched error: %v:%v: %v", want.file, want.line, want.text)
+ }
+}
+
+func (em *ErrorMatcher) DumpErrors(t *testing.T) {
+ for _, e := range em.got {
+ t.Logf("%v:%v:%v: %v", e.file, e.line, e.col, e.text)
+ }
+}