aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/compile/internal/syntax/parser_test.go
diff options
context:
space:
mode:
authorRobert Griesemer <gri@golang.org>2020-12-15 21:55:28 -0800
committerRobert Griesemer <gri@golang.org>2020-12-16 18:41:38 +0000
commit068dd0470bb796f9497d3d069c0f3208fd4dda36 (patch)
treea26755230d9ba3a69d9aaf3f73f807258485ef2a /src/cmd/compile/internal/syntax/parser_test.go
parent7909d6ec284da0e6a45bdf8fc2afdbb8bbcaeec2 (diff)
downloadgo-068dd0470bb796f9497d3d069c0f3208fd4dda36.tar.gz
go-068dd0470bb796f9497d3d069c0f3208fd4dda36.zip
[dev.typeparams] cmd/compile/internal/syntax: don't panic when providing -verify
The -verify flag is used to verify idempotent printing of syntax trees. While syntax tree printing is not actively used at the moment, the verification code still shouldn't panic. Fixed the cause for the panic (after reading from a bytes.Buffer that buffer is empty and so doesn't compare to the unread buffer), and replaced the panic with a test error. Added a test that makes sure the code invoked by -verify is run. Change-Id: I38634ed7cfa8668deb0ea2ee9fb74a8f86cfc195 Reviewed-on: https://go-review.googlesource.com/c/go/+/278477 Trust: Robert Griesemer <gri@golang.org> Run-TryBot: Robert Griesemer <gri@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Robert Findley <rfindley@google.com>
Diffstat (limited to 'src/cmd/compile/internal/syntax/parser_test.go')
-rw-r--r--src/cmd/compile/internal/syntax/parser_test.go23
1 files changed, 17 insertions, 6 deletions
diff --git a/src/cmd/compile/internal/syntax/parser_test.go b/src/cmd/compile/internal/syntax/parser_test.go
index 70651efeae..ea9e9acc83 100644
--- a/src/cmd/compile/internal/syntax/parser_test.go
+++ b/src/cmd/compile/internal/syntax/parser_test.go
@@ -29,6 +29,14 @@ func TestParse(t *testing.T) {
ParseFile(*src_, func(err error) { t.Error(err) }, nil, AllowGenerics)
}
+func TestVerify(t *testing.T) {
+ ast, err := ParseFile(*src_, func(err error) { t.Error(err) }, nil, AllowGenerics)
+ if err != nil {
+ return // error already reported
+ }
+ verifyPrint(t, *src_, ast)
+}
+
func TestParseGo2(t *testing.T) {
dir := filepath.Join(testdata, "go2")
list, err := ioutil.ReadDir(dir)
@@ -91,7 +99,7 @@ func testStdLib(t *testing.T, mode Mode) {
return
}
if *verify {
- verifyPrint(filename, ast)
+ verifyPrint(t, filename, ast)
}
results <- parseResult{filename, ast.EOF.Line()}
})
@@ -159,12 +167,13 @@ func walkDirs(t *testing.T, dir string, action func(string)) {
}
}
-func verifyPrint(filename string, ast1 *File) {
+func verifyPrint(t *testing.T, filename string, ast1 *File) {
var buf1 bytes.Buffer
_, err := Fprint(&buf1, ast1, true)
if err != nil {
panic(err)
}
+ bytes1 := buf1.Bytes()
ast2, err := Parse(NewFileBase(filename), &buf1, nil, nil, 0)
if err != nil {
@@ -176,16 +185,18 @@ func verifyPrint(filename string, ast1 *File) {
if err != nil {
panic(err)
}
+ bytes2 := buf2.Bytes()
- if bytes.Compare(buf1.Bytes(), buf2.Bytes()) != 0 {
+ if bytes.Compare(bytes1, bytes2) != 0 {
fmt.Printf("--- %s ---\n", filename)
- fmt.Printf("%s\n", buf1.Bytes())
+ fmt.Printf("%s\n", bytes1)
fmt.Println()
fmt.Printf("--- %s ---\n", filename)
- fmt.Printf("%s\n", buf2.Bytes())
+ fmt.Printf("%s\n", bytes2)
fmt.Println()
- panic("not equal")
+
+ t.Error("printed syntax trees do not match")
}
}