forked from googollee/go-socket.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuffer_test.go
96 lines (76 loc) · 1.77 KB
/
buffer_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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package parser
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var attachmentTests = []struct {
buffer Buffer
textEncoding string
binaryEncoding string
}{
{
Buffer{0, false, []byte{1, 255}},
`{"type":"Buffer","data":[1,255]}`,
`{"_placeholder":true,"num":0}`,
},
{
Buffer{1, false, []byte{}},
`{"type":"Buffer","data":[]}`,
`{"_placeholder":true,"num":1}`,
},
{
Buffer{2, false, nil},
`{"type":"Buffer","data":[]}`,
`{"_placeholder":true,"num":2}`,
},
}
func TestAttachmentEncodeText(t *testing.T) {
should := assert.New(t)
must := require.New(t)
for _, test := range attachmentTests {
a := test.buffer
a.isBinary = false
j, err := json.Marshal(a)
must.NoError(err)
should.Equal(test.textEncoding, string(j))
}
}
func TestAttachmentEncodeBinary(t *testing.T) {
should := assert.New(t)
must := require.New(t)
for _, test := range attachmentTests {
a := test.buffer
a.isBinary = true
j, err := json.Marshal(a)
must.NoError(err)
should.Equal(test.binaryEncoding, string(j))
}
}
func TestAttachmentDecodeText(t *testing.T) {
should := assert.New(t)
must := require.New(t)
for _, test := range attachmentTests {
var a Buffer
err := json.Unmarshal([]byte(test.textEncoding), &a)
must.NoError(err)
should.False(a.isBinary)
if len(test.buffer.Data) == 0 {
should.Equal([]byte{}, a.Data)
continue
}
should.Equal(test.buffer.Data, a.Data)
}
}
func TestAttachmentDecodeBinary(t *testing.T) {
should := assert.New(t)
must := require.New(t)
for _, test := range attachmentTests {
var a Buffer
err := json.Unmarshal([]byte(test.binaryEncoding), &a)
must.NoError(err)
should.True(a.isBinary)
should.Equal(test.buffer.num, a.num)
}
}