Skip to content

Commit

Permalink
#62: eliminate marshaledBytesConsumer in favor of testBinary
Browse files Browse the repository at this point in the history
  • Loading branch information
dekarrin committed Dec 1, 2023
1 parent 403ec4c commit 7edb97d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 60 deletions.
69 changes: 25 additions & 44 deletions primitives_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -955,56 +955,34 @@ func Test_encBinary(t *testing.T) {
}

func Test_decBinary(t *testing.T) {
var received []byte

sendToReceived := func(b []byte) error {
received = make([]byte, len(b))
copy(received, b)
return nil
}

testCases := []struct {
name string
input []byte
expectReceive []byte
expectRead int
expectError bool
consumerFunc func([]byte) error
name string
input []byte
expect testBinary
expectRead int
expectError bool
}{
{
name: "empty",
input: []byte{0x00},
expectReceive: []byte{},
expectRead: 1,
consumerFunc: sendToReceived,
name: "empty",
input: []byte{0x01, 0x02, 0x00, 0x00},
expect: testBinary{},
expectRead: 4,
},
{
name: "nil",
input: []byte{0x00},
expectReceive: []byte{},
expectRead: 1,
consumerFunc: sendToReceived,
name: "filled",
input: []byte{0x01, 0x0b, 0x41, 0x82, 0x06, 0x56, 0x52, 0x49, 0x53, 0x4b, 0x41, 0x01, 0x08},
expect: testBinary{number: 8, data: "VRISKA"},
expectRead: 13,
},
{
name: "1 byte",
input: []byte{0x01, 0x01, 0xff},
expectReceive: []byte{0xff},
expectRead: 3,
consumerFunc: sendToReceived,
name: "filled, followed by unrelated",
input: []byte{0x01, 0x0b, 0x41, 0x82, 0x06, 0x56, 0x52, 0x49, 0x53, 0x4b, 0x41, 0x01, 0x08, 0x01, 0x02, 0x03},
expect: testBinary{number: 8, data: "VRISKA"},
expectRead: 13,
},
{
name: "several bytes, followed by unrelated",
input: []byte{0x01, 0x05, 0xff, 0x0a, 0x0b, 0x0c, 0x0e, 0xff},
expectReceive: []byte{0xff, 0x0a, 0x0b, 0x0c, 0x0e},
expectRead: 7,
consumerFunc: sendToReceived,
},
{
name: "several bytes, but it will error",
input: []byte{0x01, 0x05, 0xff, 0x0a, 0x0b, 0x0c, 0x0e},
consumerFunc: func(b []byte) error {
return fmt.Errorf("error")
},
name: "several bytes, but it will error",
input: []byte{0x01, 0x05, 0xff, 0x0a, 0x0b, 0x0c, 0x0e},
expectError: true,
},
}
Expand All @@ -1013,17 +991,20 @@ func Test_decBinary(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
assert := assert.New(t)

unmarshalTo := valueThatUnmarshalsWith(tc.consumerFunc)
var actual testBinary
if tc.expectError {
actual.decErr = "error"
}

actualRead, err := decBinary(tc.input, unmarshalTo)
actualRead, err := decBinary(tc.input, &actual)
if tc.expectError {
assert.Error(err)
return
} else if !assert.NoError(err) {
return
}

assert.Equal(tc.expectReceive, received)
assert.Equal(tc.expect, actual)
assert.Equal(tc.expectRead, actualRead, "num read bytes does not match expected")
})
}
Expand Down
29 changes: 13 additions & 16 deletions rezi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,10 +296,6 @@ func (m testMarshaler) MarshalBinary() ([]byte, error) {
return m()
}

func valueThatUnmarshalsWith(byteConsumer func([]byte) error) encoding.BinaryUnmarshaler {
return marshaledBytesConsumer{fn: byteConsumer}
}

func valueThatMarshalsWith(byteProducer func() []byte) encoding.BinaryMarshaler {
return marshaledBytesProducer{fn: byteProducer}
}
Expand Down Expand Up @@ -352,21 +348,32 @@ func (ttv *testText) UnmarshalText(data []byte) error {
}

// testBinary is a small struct that implements BinaryMarshaler and
// BinaryUnmarshaler. It has two fields, that it lays out as such in encoding:
// "data", a string, followed by "number", an int32.
// BinaryUnmarshaler. It has two fields that it lays out as such in encoding:
// "data", a string, followed by "number", an int32. decErr is an error string
// to return from UnmarshalBinary via errors.New(decErr) instead of decoding, if
// left blank no error is returned; same with encErr but for encoding. Neither
// err field is encoded.
type testBinary struct {
number int32
data string
decErr string
encErr string
}

func (tbv testBinary) MarshalBinary() ([]byte, error) {
if tbv.encErr != "" {
return nil, errors.New(tbv.encErr)
}
var b []byte
b = append(b, MustEnc(tbv.data)...)
b = append(b, MustEnc(tbv.number)...)
return b, nil
}

func (tbv *testBinary) UnmarshalBinary(data []byte) error {
if tbv.decErr != "" {
return errors.New(tbv.decErr)
}
var n int
var err error

Expand All @@ -385,16 +392,6 @@ func (tbv *testBinary) UnmarshalBinary(data []byte) error {
return nil
}

// TODO: see if we can't eliminate marshaledBytesConsumer and marsaledBytesProducer in tests
// at some point; feels like there's a lot of duplication in these fixtures.
type marshaledBytesConsumer struct {
fn func([]byte) error
}

func (mv marshaledBytesConsumer) UnmarshalBinary(b []byte) error {
return mv.fn(b)
}

type marshaledBytesProducer struct {
fn func() []byte
}
Expand Down

0 comments on commit 7edb97d

Please sign in to comment.