This repository has been archived by the owner on May 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
byte_encoding_test.go
105 lines (82 loc) · 1.85 KB
/
byte_encoding_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
97
98
99
100
101
102
103
104
105
package encoding
import (
"bytes"
"errors"
"io"
"github.com/stretchr/testify/suite"
)
func NewByteEncodingSuite(e ByteEncoding) *ByteEncodingSuite {
return &ByteEncodingSuite{
byteArrayEncoding: e,
byteStreamEncoding: e,
}
}
type ByteEncodingSuite struct {
suite.Suite
byteArrayEncoding ByteArrayEncoding
byteStreamEncoding ByteStreamEncoding
}
func (suite ByteEncodingSuite) TestByteArray() {
if suite.byteArrayEncoding == nil {
suite.T().Skip()
}
tests := [][]byte{
{},
{0},
{0, 0},
[]byte("123"),
[]byte(`"123"`),
}
for _, input := range tests {
suite.Run(string(input), func() {
enc, err := suite.byteArrayEncoding.Encode(input)
suite.Require().NoError(err)
dec, err := suite.byteArrayEncoding.Decode(enc)
suite.Require().NoError(err)
suite.Require().Equal(input, dec)
})
}
}
var _ io.ReadWriteCloser = (*closeableBuffer)(nil)
type closeableBuffer struct {
bytes.Buffer
closed bool
}
func (c *closeableBuffer) Close() error {
if c.closed {
return errors.New("already closed")
}
c.closed = true
return nil
}
func (suite ByteEncodingSuite) TestByteStream() {
if suite.byteStreamEncoding == nil {
suite.T().Skip()
}
tests := [][]byte{
{},
{0},
{0, 0},
[]byte("123"),
[]byte(`"123"`),
}
for _, input := range tests {
suite.Run(string(input), func() {
var buf closeableBuffer
w, err := suite.byteStreamEncoding.StreamEncode(&buf)
suite.Require().NoError(err)
_, err = w.Write(input)
suite.Require().NoError(err)
err = w.Close()
suite.Require().NoError(err)
r, err := suite.byteStreamEncoding.StreamDecode(&buf)
suite.Require().NoError(err)
dec, err := readAllClose(r)
suite.Require().NoError(err)
suite.Require().Equal(input, dec)
suite.Require().False(buf.closed)
suite.Require().NoError(buf.Close())
suite.Require().True(buf.closed)
})
}
}