aboutsummaryrefslogtreecommitdiff
path: root/hotline/field_test.go
diff options
context:
space:
mode:
authorJeff Halter <868228+jhalter@users.noreply.github.com>2024-06-18 15:34:50 -0700
committerJeff Halter <868228+jhalter@users.noreply.github.com>2024-06-18 15:34:50 -0700
commita55350daaf83498b7a237c027ad0dd2377f06fee (patch)
treeb9f8c43a7b0778a8c2be01e72ecba24484f97a46 /hotline/field_test.go
parent153e2eac3b51a6a426556752fa2c532cfe53f026 (diff)
Adopt more fixed size array types for struct fields
Diffstat (limited to 'hotline/field_test.go')
-rw-r--r--hotline/field_test.go87
1 files changed, 87 insertions, 0 deletions
diff --git a/hotline/field_test.go b/hotline/field_test.go
index 317c2d9..ecae9c7 100644
--- a/hotline/field_test.go
+++ b/hotline/field_test.go
@@ -99,3 +99,90 @@ func Test_fieldScanner(t *testing.T) {
})
}
}
+
+func TestField_Read(t *testing.T) {
+ type fields struct {
+ ID [2]byte
+ FieldSize [2]byte
+ Data []byte
+ readOffset int
+ }
+ type args struct {
+ p []byte
+ }
+ tests := []struct {
+ name string
+ fields fields
+ args args
+ want int
+ wantErr assert.ErrorAssertionFunc
+ wantBytes []byte
+ }{
+ {
+ name: "returns field bytes",
+ fields: fields{
+ ID: [2]byte{0x00, 0x62},
+ FieldSize: [2]byte{0x00, 0x03},
+ Data: []byte("hai!"),
+ },
+ args: args{
+ p: make([]byte, 512),
+ },
+ want: 8,
+ wantErr: assert.NoError,
+ wantBytes: []byte{
+ 0x00, 0x62,
+ 0x00, 0x03,
+ 0x68, 0x61, 0x69, 0x21,
+ },
+ },
+ {
+ name: "returns field bytes from readOffset",
+ fields: fields{
+ ID: [2]byte{0x00, 0x62},
+ FieldSize: [2]byte{0x00, 0x03},
+ Data: []byte("hai!"),
+ readOffset: 4,
+ },
+ args: args{
+ p: make([]byte, 512),
+ },
+ want: 4,
+ wantErr: assert.NoError,
+ wantBytes: []byte{
+ 0x68, 0x61, 0x69, 0x21,
+ },
+ },
+ {
+ name: "returns io.EOF when all bytes read",
+ fields: fields{
+ ID: [2]byte{0x00, 0x62},
+ FieldSize: [2]byte{0x00, 0x03},
+ Data: []byte("hai!"),
+ readOffset: 8,
+ },
+ args: args{
+ p: make([]byte, 512),
+ },
+ want: 0,
+ wantErr: assert.Error,
+ wantBytes: []byte{},
+ },
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ f := &Field{
+ ID: tt.fields.ID,
+ FieldSize: tt.fields.FieldSize,
+ Data: tt.fields.Data,
+ readOffset: tt.fields.readOffset,
+ }
+ got, err := f.Read(tt.args.p)
+ if !tt.wantErr(t, err, fmt.Sprintf("Read(%v)", tt.args.p)) {
+ return
+ }
+ assert.Equalf(t, tt.want, got, "Read(%v)", tt.args.p)
+ assert.Equalf(t, tt.wantBytes, tt.args.p[:got], "Read(%v)", tt.args.p)
+ })
+ }
+}