Skip to content

Commit

Permalink
Add support for expected errors in TestReadPaddedString
Browse files Browse the repository at this point in the history
  • Loading branch information
peternewman committed Mar 22, 2021
1 parent 7075a58 commit 952f77b
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions osc/osc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package osc
import (
"bufio"
"bytes"
"io"
"net"
"reflect"
"sync"
Expand Down Expand Up @@ -314,18 +315,19 @@ func TestReadPaddedString(t *testing.T) {
buf []byte // buffer
n int // bytes needed
s string // resulting string
e error // expected error
}{
{[]byte{'t', 'e', 's', 't', 's', 't', 'r', 'i', 'n', 'g', 0, 0}, 12, "teststring"},
{[]byte{'t', 'e', 's', 't', 'e', 'r', 's', 0}, 8, "testers"},
{[]byte{'t', 'e', 's', 't', 's', 0, 0, 0}, 8, "tests"},
{[]byte{'t', 'e', 's', 't', 0, 0, 0, 0}, 8, "test"},
{[]byte{'t', 'e', 's', 0}, 4, "tes", nil}, // OSC uses null terminated strings
{[]byte{'t', 'e', 's', 't', 's', 't', 'r', 'i', 'n', 'g', 0, 0}, 12, "teststring", nil},
{[]byte{'t', 'e', 's', 't', 'e', 'r', 's', 0}, 8, "testers", nil},
{[]byte{'t', 'e', 's', 't', 's', 0, 0, 0}, 8, "tests", nil},
{[]byte{'t', 'e', 's', 't', 0, 0, 0, 0}, 8, "test", nil},
{[]byte{'t', 'e', 's', 0}, 4, "tes", nil}, // OSC uses null terminated strings
{[]byte{'t', 'e', 's', 't'}, 0, "", io.EOF}, // if there is no null byte at the end, it doesn't work.
} {
buf := bytes.NewBuffer(tt.buf)
s, n, err := readPaddedString(bufio.NewReader(buf))
if err != nil {
t.Errorf("%s: Error reading padded string: %s", s, err)
if got, want := err, tt.e; got != want {
t.Errorf("%s: Unexpected error reading padded string; got = %s, want = %s", tt.s, got, want)
}
if got, want := n, tt.n; got != want {
t.Errorf("%s: Bytes needed don't match; got = %d, want = %d", tt.s, got, want)
Expand Down

0 comments on commit 952f77b

Please sign in to comment.