mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-20 04:19:04 +00:00
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.
This commit is contained in:
committed by
Ron Evans
parent
208eb1a817
commit
e409950492
+30
-10
@@ -5,7 +5,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@@ -62,20 +62,40 @@ func testErrorMessages(t *testing.T, filename string) {
|
|||||||
actual := strings.TrimRight(buf.String(), "\n")
|
actual := strings.TrimRight(buf.String(), "\n")
|
||||||
|
|
||||||
// Check whether the error is as expected.
|
// Check whether the error is as expected.
|
||||||
if canonicalizeErrors(actual) != canonicalizeErrors(expected) {
|
if !matchErrors(t, expected, actual) {
|
||||||
t.Errorf("expected error:\n%s\ngot:\n%s", indentText(expected, "> "), indentText(actual, "> "))
|
t.Errorf("expected error:\n%s\ngot:\n%s", indentText(expected, "> "), indentText(actual, "> "))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func canonicalizeErrors(text string) string {
|
func matchErrors(t *testing.T, pattern, actual string) bool {
|
||||||
// Fix for Windows: replace all backslashes with forward slashes so that
|
patternLines := strings.Split(pattern, "\n")
|
||||||
// paths will be the same as on POSIX systems.
|
actualLines := strings.Split(actual, "\n")
|
||||||
// (It may also change some other backslashes, but since this is only for
|
if len(patternLines) != len(actualLines) {
|
||||||
// comparing text it should be fine).
|
return false
|
||||||
if runtime.GOOS == "windows" {
|
|
||||||
text = strings.ReplaceAll(text, "\\", "/")
|
|
||||||
}
|
}
|
||||||
return text
|
for i, patternLine := range patternLines {
|
||||||
|
indices := regexp.MustCompile(`\{\{.*?\}\}`).FindAllStringIndex(patternLine, -1)
|
||||||
|
patternParts := []string{"^"}
|
||||||
|
lastStop := 0
|
||||||
|
for _, startstop := range indices {
|
||||||
|
start := startstop[0]
|
||||||
|
stop := startstop[1]
|
||||||
|
patternParts = append(patternParts,
|
||||||
|
regexp.QuoteMeta(patternLine[lastStop:start]),
|
||||||
|
patternLine[start+2:stop-2])
|
||||||
|
lastStop = stop
|
||||||
|
}
|
||||||
|
patternParts = append(patternParts, regexp.QuoteMeta(patternLine[lastStop:]), "$")
|
||||||
|
pattern := strings.Join(patternParts, "")
|
||||||
|
re, err := regexp.Compile(pattern)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("could not compile regexp for %#v: %v", patternLine, err)
|
||||||
|
}
|
||||||
|
if !re.MatchString(actualLines[i]) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// Indent the given text with a given indentation string.
|
// Indent the given text with a given indentation string.
|
||||||
|
|||||||
Vendored
+1
-1
@@ -5,4 +5,4 @@ import _ "github.com/tinygo-org/tinygo/testdata/errors/invaliddep"
|
|||||||
func main() {
|
func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ERROR: invaliddep/invaliddep.go:1:1: expected 'package', found ppackage
|
// ERROR: invaliddep{{[\\/]}}invaliddep.go:1:1: expected 'package', found ppackage
|
||||||
|
|||||||
Reference in New Issue
Block a user