-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencode_test.go
More file actions
94 lines (85 loc) · 1.95 KB
/
encode_test.go
File metadata and controls
94 lines (85 loc) · 1.95 KB
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
package engineio
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type mockRefProvider map[string]any
func (m mockRefProvider) GetReference(ref ReferenceVal) (any, bool) {
val, ok := m[string(ref)]
return val, ok
}
func TestReferenceVal_String(t *testing.T) {
ref := ReferenceVal("foo")
assert.Equal(t, "ref<foo>", ref.String())
}
func TestEncodeValues(t *testing.T) {
tests := []struct {
name string
input any
refs ReferenceProvider
output []byte
err string
}{
{
name: "encode simple int",
input: 100,
output: []byte{0x03, 0x64},
err: "",
},
{
name: "encode simple string",
input: "hello world",
output: []byte{0x6, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64},
err: "",
},
{
name: "encode reference",
input: ReferenceVal("myref"),
refs: &mockRefProvider{"myref": 100},
output: []byte{0x03, 0x64},
err: "",
},
{
name: "encode reference without provider",
input: ReferenceVal("myref"),
output: nil,
err: "encountered reference value without a ref provider",
},
{
name: "encode object",
input: map[string]any{
"foo": 1,
"bar": 2,
},
output: []byte{0xd, 0x5f, 0x6, 0x35, 0x56, 0x85, 0x1, 0x62, 0x61, 0x72, 0x3, 0x2, 0x66, 0x6f, 0x6f, 0x3, 0x1},
err: "",
},
{
name: "encode slice",
input: []any{1, 2},
output: []byte{0xe, 0x2f, 0x3, 0x13, 0x1, 0x2},
err: "",
},
{
name: "encode map",
input: map[any]any{
"foo": 1,
"bar": 2,
},
output: []byte{0xe, 0x4f, 0x6, 0x33, 0x46, 0x73, 0x62, 0x61, 0x72, 0x2, 0x66, 0x6f, 0x6f, 0x1},
err: "",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
output, err := EncodeValues(test.input, test.refs)
if test.err != "" {
require.EqualError(t, err, test.err)
} else {
require.NoError(t, err)
assert.Equal(t, test.output, output)
}
})
}
}