aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/ast/test_util.go
blob: 921232c965d82fe93c6d485009e7b5be77fbc407 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// 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"
	"fmt"
	"os"
	"path/filepath"
	"regexp"
	"sort"
	"strings"
	"testing"
)

type ErrorMatcher struct {
	t      *testing.T
	Data   []byte
	expect []*errorDesc
	got    []*errorDesc
}

type errorDesc struct {
	pos     Pos
	text    string
	matched bool
}

func NewErrorMatcher(t *testing.T, file string) *ErrorMatcher {
	data, err := os.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{
				pos:  Pos{File: filepath.Base(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{
		t:      t,
		Data:   stripped,
		expect: errors,
	}
}

var errorLocationRe = regexp.MustCompile(`at [a-z][a-z0-9]+\.txt:[0-9]+:[0-9]+`)

func (em *ErrorMatcher) ErrorHandler(pos Pos, msg string) {
	if match := errorLocationRe.FindStringSubmatchIndex(msg); match != nil {
		msg = msg[0:match[0]] + "at LOCATION" + msg[match[1]:]
	}
	em.got = append(em.got, &errorDesc{
		pos:  pos,
		text: msg,
	})
}

func (em *ErrorMatcher) Count() int {
	return len(em.got)
}

func (em *ErrorMatcher) Check() {
	em.t.Helper()
	errors := make(map[Pos][]string)
nextErr:
	for _, e := range em.got {
		for _, want := range em.expect {
			if want.matched || want.pos.Line != e.pos.Line || want.text != e.text {
				continue
			}
			want.matched = true
			continue nextErr
		}
		pos := e.pos
		pos.Col = 0
		pos.Off = 0
		errors[pos] = append(errors[pos], fmt.Sprintf("unexpected: %v", e.text))
	}
	for _, want := range em.expect {
		if want.matched {
			continue
		}
		errors[want.pos] = append(errors[want.pos], fmt.Sprintf("unmatched : %v", want.text))
	}

	if len(errors) == 0 {
		return
	}
	type Sorted struct {
		pos  Pos
		msgs []string
	}
	sorted := []Sorted{}
	for pos, msgs := range errors {
		sorted = append(sorted, Sorted{pos, msgs})
	}
	sort.Slice(sorted, func(i, j int) bool {
		return sorted[i].pos.less(sorted[j].pos)
	})
	buf := new(bytes.Buffer)
	for _, err := range sorted {
		if len(err.msgs) == 1 {
			fmt.Fprintf(buf, "%v: %v\n", err.pos, err.msgs[0])
			continue
		}
		sort.Strings(err.msgs)
		fmt.Fprintf(buf, "%v:\n\t%v\n", err.pos, strings.Join(err.msgs, "\n\t"))
	}
	em.t.Errorf("\n%s", buf.Bytes())
}

func (em *ErrorMatcher) DumpErrors() {
	em.t.Helper()
	for _, e := range em.got {
		em.t.Logf("%v: %v", e.pos, e.text)
	}
}