-
Notifications
You must be signed in to change notification settings - Fork 0
/
encode.array_test.go
88 lines (78 loc) · 2.9 KB
/
encode.array_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
package msgpack
import (
"errors"
"fmt"
"testing"
"github.com/blugnu/test"
)
func TestEncodeArray(t *testing.T) {
// ARRANGE
enc, buf := NewTestEncoder()
encerr := errors.New("encoder error")
type expect struct {
header []byte
error
n int
}
testcases := []struct {
errorState bool
n int
expect
skip bool
}{
{n: 0, expect: expect{n: 0, header: []byte{atomEmptyArray}}},
{n: 1, expect: expect{n: 1, header: []byte{maskFixArray | byte(1)}}},
{n: 15, expect: expect{n: 15, header: []byte{maskFixArray | byte(15)}}},
{n: 16, expect: expect{n: 16, header: []byte{typeArray16, 0x00, 0x10}}},
{n: 65535, expect: expect{n: 65535, header: []byte{typeArray16, 0xff, 0xff}}},
{n: 65536, expect: expect{n: 65536, header: []byte{typeArray32, 0x00, 0x01, 0x00, 0x00}}},
{n: 1 << 24, expect: expect{n: 1 << 24, header: []byte{typeArray32, 0x01, 0x00, 0x00, 0x00}}, skip: !*allTests},
{n: (1 << 32) - 1, expect: expect{n: (1 << 32) - 1, header: []byte{typeArray32, 0x01, 0x00, 0x00, 0x00}}, skip: true}, // NOTE: this test cannot be run by passing -all; it must be explicitly set to skip: false
{errorState: true, n: 0, expect: expect{error: encerr}},
{errorState: true, n: 1, expect: expect{error: encerr}},
{errorState: true, n: 15, expect: expect{error: encerr}},
{errorState: true, n: 16, expect: expect{error: encerr}},
{errorState: true, n: 65535, expect: expect{error: encerr}},
{errorState: true, n: 65536, expect: expect{error: encerr}},
{errorState: true, n: 1 << 24, expect: expect{error: encerr}, skip: !*allTests},
{errorState: true, n: (1 << 32) - 1, expect: expect{error: encerr}, skip: true}, // NOTE: this test cannot be run by passing -all; it must be explicitly set to skip: false},
}
for _, tc := range testcases {
t.Run(fmt.Sprintf("%d elements, error: %v", tc.n, tc.errorState), func(t *testing.T) {
if tc.skip {
t.Skip("skipping slow test")
}
defer buf.Reset()
defer func() { _ = enc.ClearErr() }()
// ARRANGE
if tc.errorState {
enc.err = encerr
}
// we test using a slice of zero-value int's which will pack as single
// bytes (fixed positive integer 0-127) enabling the written values to
// be tested by simply comparing the overall buffer length that is written
s := make([]int, tc.n)
// ACT
err := EncodeArray(enc, s, nil)
// ASSERT
test.Error(t, err).Is(tc.expect.error)
test.Value(t, buf.Len()-len(tc.header), "# encoded items").Equals(tc.expect.n)
test.Slice(t, buf.Bytes()[:len(tc.header)], "encoded header").Equals(tc.header)
})
}
t.Run("when error occurs writing items", func(t *testing.T) {
// ARRANGE
enc.err = nil
buf.Reset()
// ACT
err := EncodeArray(enc, []int{1, 2, 3}, func(enc Encoder, v int) error {
if v > 1 {
return encerr
}
return enc.Encode(v)
})
// ASSERT
test.Error(t, err).Is(encerr)
test.Slice(t, buf.Bytes()).Equals([]byte{maskFixArray | byte(3), 0x01})
})
}