-
Notifications
You must be signed in to change notification settings - Fork 3
/
message_test.go
65 lines (55 loc) · 1.44 KB
/
message_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package connqc_test
import (
"bytes"
"testing"
"github.com/nitrado/connqc"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestEncoder_Encode(t *testing.T) {
tests := []struct {
name string
msg connqc.Message
wantBytes []byte
wantErr require.ErrorAssertionFunc
}{
{
name: "handles encoding probe",
msg: connqc.Probe{ID: 2, Data: "Hello 2"},
wantBytes: []byte{'P', 'R', 'B', 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x7, 'H', 'e', 'l', 'l', 'o', ' ', '2'},
wantErr: require.NoError,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
buf := bytes.Buffer{}
enc := connqc.NewEncoder(&buf)
err := enc.Encode(test.msg)
test.wantErr(t, err)
assert.Equal(t, test.wantBytes, buf.Bytes())
})
}
}
func TestDecoder_Decode(t *testing.T) {
tests := []struct {
name string
data []byte
wantMsg connqc.Message
wantErr require.ErrorAssertionFunc
}{
{
name: "handles encoding probe",
data: []byte{'P', 'R', 'B', 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x7, 'H', 'e', 'l', 'l', 'o', ' ', '2'},
wantMsg: connqc.Probe{ID: 2, Data: "Hello 2"},
wantErr: require.NoError,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
dec := connqc.NewDecoder(bytes.NewReader(test.data))
got, err := dec.Decode()
test.wantErr(t, err)
assert.Equal(t, test.wantMsg, got)
})
}
}