Repository navigation
Expand file tree
/
Copy pathmsgpack_test.go
More file actions
151 lines (125 loc) · 3.51 KB
/
Copy pathmsgpack_test.go
File metadata and controls
151 lines (125 loc) · 3.51 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package enc_test
import (
"testing"
"time"
"github.com/ohait/forego/ctx"
"github.com/ohait/forego/enc"
"github.com/ohait/forego/test"
"github.com/vmihailenco/msgpack/v5"
)
type pointerColumn struct {
Name string `json:"name"`
}
func (x *pointerColumn) MarshalNode(c ctx.C) (enc.Node, error) {
return enc.String("custom:" + x.Name), nil
}
type pointerChunk struct {
Columns []pointerColumn `json:"columns,omitempty"`
}
func TestMsgPack(t *testing.T) {
c := test.Context(t)
codec := &enc.MsgPack{}
check := func(t *testing.T, nodeIn enc.Node) {
t.Helper()
data := codec.Encode(c, nodeIn)
nodeOut, err := codec.Decode(c, data)
test.NoError(t, err)
test.EqualsGo(t, nodeIn, nodeOut)
}
checkLeft := func(t *testing.T, nodeIn enc.Node) {
t.Helper()
data := codec.Encode(c, nodeIn)
nodeOut, err := codec.Decode(c, data)
test.NoError(t, err)
test.EqualsJSON(t, nodeIn, nodeOut)
}
t.Run("scalars", func(t *testing.T) {
check(t, enc.Nil{})
check(t, enc.Integer(1))
check(t, enc.Integer(-1))
check(t, enc.Float(3.14))
check(t, enc.Bool(true))
check(t, enc.String("foo"))
check(t, enc.Bytes{1, 2, 3})
check(t, enc.String(`\"`))
check(t, enc.Time(time.Unix(123, 456).UTC()))
checkLeft(t, enc.Duration(3*time.Second+250*time.Millisecond))
})
t.Run("digits", func(t *testing.T) {
checkLeft(t, enc.Digits("3.14"))
checkLeft(t, enc.Digits("3"))
checkLeft(t, enc.Digits("18446744073709551615"))
})
t.Run("maps", func(t *testing.T) {
check(t, enc.Map{})
checkLeft(t, enc.Map{"one": enc.Float(3.14)})
checkLeft(t, enc.Map{"one": enc.Integer(1), "nil": enc.Nil{}, "foo": enc.String("bar")})
})
t.Run("pairs", func(t *testing.T) {
checkLeft(t, enc.Pairs{})
checkLeft(t, enc.Pairs{{"b", enc.Integer(1)}, {"a", enc.Integer(2)}, {"", enc.Nil{}}})
})
t.Run("lists", func(t *testing.T) {
check(t, enc.List{})
check(t, enc.List{enc.Nil{}})
check(t, enc.List{enc.Integer(1), enc.String("two"), enc.Bool(false)})
})
t.Run("deep", func(t *testing.T) {
check(t, enc.List{enc.Map{}})
check(t, enc.List{enc.List{}, enc.Nil{}})
check(t, enc.Map{"l": enc.List{}})
})
}
func TestMsgPackHelpers(t *testing.T) {
c := test.Context(t)
type X struct {
S string `json:"s"`
I int `json:"i"`
}
in := X{S: "str", I: 42}
data, err := enc.MarshalMsgPack(c, in)
test.NoError(t, err)
var out X
err = enc.UnmarshalMsgPack(c, data, &out)
test.NoError(t, err)
test.EqualsGo(t, in, out)
}
func TestMsgPackHelpersPointerMarshalerInSlice(t *testing.T) {
c := test.Context(t)
in := pointerChunk{
Columns: []pointerColumn{{Name: "a"}},
}
n, err := enc.Marshal(c, in)
test.NoError(t, err)
test.EqualsJSON(t, enc.Pairs{{Name: "columns", Value: enc.List{enc.String("custom:a")}}}, n)
data, err := enc.MarshalMsgPack(c, in)
test.NoError(t, err)
outNode, err := enc.MsgPack{}.Decode(c, data)
test.NoError(t, err)
test.EqualsJSON(t, enc.Pairs{{Name: "columns", Value: enc.List{enc.String("custom:a")}}}, outNode)
}
func TestMsgPackNoData(t *testing.T) {
c := test.Context(t)
{
n, err := enc.MsgPack{}.Decode(c, nil)
test.NoError(t, err)
test.Nil(t, n)
}
{
n, err := enc.MsgPack{}.Decode(c, []byte{})
test.NoError(t, err)
test.Nil(t, n)
}
}
func TestMsgPackBinary(t *testing.T) {
c := test.Context(t)
data, err := msgpack.Marshal([]byte{1, 2, 3})
test.NoError(t, err)
n, err := enc.MsgPack{}.Decode(c, data)
test.NoError(t, err)
test.EqualsGo(t, enc.Bytes{1, 2, 3}, n)
var out []byte
err = enc.Unmarshal(c, n, &out)
test.NoError(t, err)
test.EqualsGo(t, []byte{1, 2, 3}, out)
}