diff options
author | Michael Stapelberg <stapelberg@debian.org> | 2014-06-19 09:22:53 +0200 |
---|---|---|
committer | Michael Stapelberg <stapelberg@debian.org> | 2014-06-19 09:22:53 +0200 |
commit | 8a39ee361feb9bf46d728ff1ba4f07ca1d9610b1 (patch) | |
tree | 4449f2036cccf162e8417cc5841a35815b3e7ac5 /src/pkg/text/tabwriter/tabwriter_test.go | |
parent | c8bf49ef8a92e2337b69c14b9b88396efe498600 (diff) | |
download | golang-8a39ee361feb9bf46d728ff1ba4f07ca1d9610b1.tar.gz |
Imported Upstream version 1.3upstream/1.3
Diffstat (limited to 'src/pkg/text/tabwriter/tabwriter_test.go')
-rw-r--r-- | src/pkg/text/tabwriter/tabwriter_test.go | 39 |
1 files changed, 38 insertions, 1 deletions
diff --git a/src/pkg/text/tabwriter/tabwriter_test.go b/src/pkg/text/tabwriter/tabwriter_test.go index ace535647..9d3111e2c 100644 --- a/src/pkg/text/tabwriter/tabwriter_test.go +++ b/src/pkg/text/tabwriter/tabwriter_test.go @@ -14,7 +14,7 @@ type buffer struct { a []byte } -func (b *buffer) init(n int) { b.a = make([]byte, n)[0:0] } +func (b *buffer) init(n int) { b.a = make([]byte, 0, n) } func (b *buffer) clear() { b.a = b.a[0:0] } @@ -613,3 +613,40 @@ func Test(t *testing.T) { check(t, e.testname, e.minwidth, e.tabwidth, e.padding, e.padchar, e.flags, e.src, e.expected) } } + +type panicWriter struct{} + +func (panicWriter) Write([]byte) (int, error) { + panic("cannot write") +} + +func wantPanicString(t *testing.T, want string) { + if e := recover(); e != nil { + got, ok := e.(string) + switch { + case !ok: + t.Errorf("got %v (%T), want panic string", e, e) + case got != want: + t.Errorf("wrong panic message: got %q, want %q", got, want) + } + } +} + +func TestPanicDuringFlush(t *testing.T) { + defer wantPanicString(t, "tabwriter: panic during Flush") + var p panicWriter + w := new(Writer) + w.Init(p, 0, 0, 5, ' ', 0) + io.WriteString(w, "a") + w.Flush() + t.Errorf("failed to panic during Flush") +} + +func TestPanicDuringWrite(t *testing.T) { + defer wantPanicString(t, "tabwriter: panic during Write") + var p panicWriter + w := new(Writer) + w.Init(p, 0, 0, 5, ' ', 0) + io.WriteString(w, "a\n\n") // the second \n triggers a call to w.Write and thus a panic + t.Errorf("failed to panic during Write") +} |