Skip to content

Commit e409950

Browse files
aykevldeadprogram
authored andcommitted
main: refactor error messages to use FileCheck-like patterns
Match the error messages in testdata/errors/*.go like LLVM FileCheck, which includes regular expressions. I believe this is much more flexible, and LLVM uses it in nearly all of their tests so it must work quite well for compilers.
1 parent 208eb1a commit e409950

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

errors_test.go

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"fmt"
66
"os"
77
"path/filepath"
8-
"runtime"
8+
"regexp"
99
"strings"
1010
"testing"
1111
"time"
@@ -62,20 +62,40 @@ func testErrorMessages(t *testing.T, filename string) {
6262
actual := strings.TrimRight(buf.String(), "\n")
6363

6464
// Check whether the error is as expected.
65-
if canonicalizeErrors(actual) != canonicalizeErrors(expected) {
65+
if !matchErrors(t, expected, actual) {
6666
t.Errorf("expected error:\n%s\ngot:\n%s", indentText(expected, "> "), indentText(actual, "> "))
6767
}
6868
}
6969

70-
func canonicalizeErrors(text string) string {
71-
// Fix for Windows: replace all backslashes with forward slashes so that
72-
// paths will be the same as on POSIX systems.
73-
// (It may also change some other backslashes, but since this is only for
74-
// comparing text it should be fine).
75-
if runtime.GOOS == "windows" {
76-
text = strings.ReplaceAll(text, "\\", "/")
70+
func matchErrors(t *testing.T, pattern, actual string) bool {
71+
patternLines := strings.Split(pattern, "\n")
72+
actualLines := strings.Split(actual, "\n")
73+
if len(patternLines) != len(actualLines) {
74+
return false
7775
}
78-
return text
76+
for i, patternLine := range patternLines {
77+
indices := regexp.MustCompile(`\{\{.*?\}\}`).FindAllStringIndex(patternLine, -1)
78+
patternParts := []string{"^"}
79+
lastStop := 0
80+
for _, startstop := range indices {
81+
start := startstop[0]
82+
stop := startstop[1]
83+
patternParts = append(patternParts,
84+
regexp.QuoteMeta(patternLine[lastStop:start]),
85+
patternLine[start+2:stop-2])
86+
lastStop = stop
87+
}
88+
patternParts = append(patternParts, regexp.QuoteMeta(patternLine[lastStop:]), "$")
89+
pattern := strings.Join(patternParts, "")
90+
re, err := regexp.Compile(pattern)
91+
if err != nil {
92+
t.Fatalf("could not compile regexp for %#v: %v", patternLine, err)
93+
}
94+
if !re.MatchString(actualLines[i]) {
95+
return false
96+
}
97+
}
98+
return true
7999
}
80100

81101
// Indent the given text with a given indentation string.

testdata/errors/loader-invaliddep.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ import _ "github.com/tinygo-org/tinygo/testdata/errors/invaliddep"
55
func main() {
66
}
77

8-
// ERROR: invaliddep/invaliddep.go:1:1: expected 'package', found ppackage
8+
// ERROR: invaliddep{{[\\/]}}invaliddep.go:1:1: expected 'package', found ppackage

0 commit comments

Comments
 (0)