| 1 | package recordio |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "io" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | type writerFunc func([]byte) (int, error) |
| 10 | |
| 11 | func (f writerFunc) Write(b []byte) (int, error) { return f(b) } |
| 12 | |
| 13 | func TestWriter(t *testing.T) { |
| 14 | for i, tc := range []struct { |
| 15 | frame []byte |
| 16 | output []byte |
| 17 | err error |
| 18 | }{ |
| 19 | {nil, ([]byte)("0\n"), nil}, |
| 20 | {([]byte)("a"), ([]byte)("1\na"), nil}, |
| 21 | {([]byte)("abc\n"), ([]byte)("4\nabc\n"), nil}, |
| 22 | } { |
| 23 | var buf bytes.Buffer |
| 24 | w := NewWriter(&buf) |
| 25 | err := w.WriteFrame(tc.frame) |
| 26 | |
| 27 | if err != tc.err { |
| 28 | t.Fatalf("test case %d failed: expected error %v instead of %v", i, tc.err, err) |
| 29 | } |
| 30 | if b := buf.Bytes(); bytes.Compare(b, tc.output) != 0 { |
| 31 | t.Fatalf("test case %d failed: expected bytes %q instead of %q", i, string(b), string(tc.output)) |
| 32 | } |
| 33 | } |
| 34 | for i := 0; i < 5; i++ { |
| 35 | w := NewWriter(writerFunc(func(_ []byte) (int, error) { return i, nil })) |
| 36 | err := w.WriteFrame(([]byte)("james")) |
| 37 | if err != io.ErrShortWrite { |
| 38 | t.Fatalf("expected short write error with i=%d instead of %v", i, err) |
| 39 | } |
| 40 | } |
| 41 | } |