diff options
author | Robert Griesemer <gri@golang.org> | 2021-01-07 12:57:15 -0800 |
---|---|---|
committer | Robert Griesemer <gri@golang.org> | 2021-01-08 17:32:14 +0000 |
commit | 934f9dc0efbae667c445684915676323b98b34d0 (patch) | |
tree | 1c4af8dff3303dc337466dedad8a45f41a647191 /src/cmd/compile/internal/syntax/printer_test.go | |
parent | 5b9152de579a75962a48d5380abcc2d4deedbf28 (diff) | |
download | go-934f9dc0efbae667c445684915676323b98b34d0.tar.gz go-934f9dc0efbae667c445684915676323b98b34d0.zip |
[dev.typeparams] cmd/compile/internal/syntax: clean up node printing API
Preparation for using the syntax printer as expression printer in types2.
- Introduced Form to control printing format
- Cleaned up/added String and ShortString convenience functions
- Implemented ShortForm format which prints … for non-empty
function and composite literal bodies
- Added test to check write error handling
Change-Id: Ie86e46d766fb60fcf07ef643c7788b2ef440ffa8
Reviewed-on: https://go-review.googlesource.com/c/go/+/282552
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Diffstat (limited to 'src/cmd/compile/internal/syntax/printer_test.go')
-rw-r--r-- | src/cmd/compile/internal/syntax/printer_test.go | 29 |
1 files changed, 28 insertions, 1 deletions
diff --git a/src/cmd/compile/internal/syntax/printer_test.go b/src/cmd/compile/internal/syntax/printer_test.go index 9f1f7e18cb..6c07fe0a26 100644 --- a/src/cmd/compile/internal/syntax/printer_test.go +++ b/src/cmd/compile/internal/syntax/printer_test.go @@ -25,11 +25,38 @@ func TestPrint(t *testing.T) { } if ast != nil { - Fprint(testOut(), ast, true) + Fprint(testOut(), ast, LineForm) fmt.Println() } } +type shortBuffer struct { + buf []byte +} + +func (w *shortBuffer) Write(data []byte) (n int, err error) { + w.buf = append(w.buf, data...) + n = len(data) + if len(w.buf) > 10 { + err = io.ErrShortBuffer + } + return +} + +func TestPrintError(t *testing.T) { + const src = "package p; var x int" + ast, err := Parse(nil, strings.NewReader(src), nil, nil, 0) + if err != nil { + t.Fatal(err) + } + + var buf shortBuffer + _, err = Fprint(&buf, ast, 0) + if err == nil || err != io.ErrShortBuffer { + t.Errorf("got err = %s, want %s", err, io.ErrShortBuffer) + } +} + var stringTests = []string{ "package p", "package p; type _ int; type T1 = struct{}; type ( _ *struct{}; T2 = float32 )", |