-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserializer_test.go
59 lines (50 loc) · 1.31 KB
/
serializer_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
package flyrpc
import (
"testing"
"github.com/golang/protobuf/proto"
"github.com/stretchr/testify/assert"
)
type TestNoneProto struct {
Id int32
Name string
}
type TestUser struct {
Id int32 `protobuf:"varint,1,opt,name=id" json:"id,omitempty"`
Name string `protobuf:"bytes,2,opt,name=name" json:"name,omitempty"`
}
func (m *TestUser) Reset() { *m = TestUser{} }
func (m *TestUser) String() string { return proto.CompactTextString(m) }
func (*TestUser) ProtoMessage() {}
func testSerializer(t *testing.T, s Serializer) {
bytes, err := s.Marshal(&TestUser{Id: 123, Name: "abc"})
assert.Nil(t, err)
u := &TestUser{}
err = s.Unmarshal(bytes, u)
assert.Nil(t, err)
assert.Equal(t, int32(123), u.Id)
assert.Equal(t, "abc", u.Name)
}
func TestJSONSerializer(t *testing.T) {
testSerializer(t, JSON)
}
/*
func TestProtoSerializer(t *testing.T) {
uid := int32(123)
s := Protobuf
bytes, err := s.Marshal(&TestNoneProto{Id: uid, Name: "abc"})
assert.NotNil(t, err)
bytes, err = s.Marshal(&TestUser{Id: uid, Name: "abc"})
assert.Nil(t, err)
m := &TestNoneProto{}
err = s.Unmarshal(bytes, m)
assert.NotNil(t, err)
u := &TestUser{}
err = s.Unmarshal(bytes, u)
assert.Nil(t, err)
assert.Equal(t, uid, u.Id)
assert.Equal(t, "abc", u.Name)
}
func TestMsgpack(t *testing.T) {
testSerializer(t, Msgpack)
}
*/