forked from viney-shih/go-cache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
marshaler_test.go
121 lines (96 loc) · 2.3 KB
/
marshaler_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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package cache
import (
"testing"
"time"
"github.com/stretchr/testify/suite"
)
var (
mockTimeNow = time.Date(2022, 11, 23, 0, 0, 0, 0, time.Local)
)
type marshalerSuite struct {
suite.Suite
}
func (s *marshalerSuite) SetupSuite() {}
func (s *marshalerSuite) TearDownSuite() {}
func (s *marshalerSuite) SetupTest() {}
func (s *marshalerSuite) TearDownTest() {}
func TestMarshalerSuite(t *testing.T) {
suite.Run(t, new(marshalerSuite))
}
type mockStruct struct {
ID int64
Key string
CreatedAt time.Time
child *mockStruct
}
func (s *marshalerSuite) TestMarshaler() {
var bs []byte
var err error
marshal := Marshal
unmarshal := Unmarshal
// nil
var null error
bs, err = marshal(null)
s.Require().NoError(err)
var retNull error
s.Require().NoError(unmarshal(bs, &retNull))
s.Require().Equal(null, retNull)
// bytes
bytes := []byte("strings to bytes")
bs, err = marshal(bytes)
s.Require().NoError(err)
var retBytes []byte
s.Require().NoError(unmarshal(bs, &retBytes))
s.Require().Equal(bytes, retBytes)
// string
str := "this is a string"
bs, err = marshal(str)
s.Require().NoError(err)
var retStr string
s.Require().NoError(unmarshal(bs, &retStr))
s.Require().Equal(str, retStr)
// pointer
num := 100
intPtr := &num
bs, err = marshal(intPtr)
s.Require().NoError(err)
var retIntPtr *int
s.Require().NoError(unmarshal(bs, &retIntPtr))
s.Require().Equal(intPtr, retIntPtr)
// struct
st := mockStruct{
ID: 28825252,
Key: "I am rich",
CreatedAt: mockTimeNow,
}
bs, err = marshal(st)
s.Require().NoError(err)
retSt := mockStruct{}
s.Require().NoError(unmarshal(bs, &retSt))
s.Require().Equal(st, retSt)
// struct without nil pointer
st2 := mockStruct{
ID: 28825252,
Key: "I am rich",
CreatedAt: mockTimeNow,
child: &mockStruct{
ID: 2266,
},
}
bs, err = marshal(st2)
s.Require().NoError(err)
var retSt2 mockStruct
s.Require().NoError(unmarshal(bs, &retSt2))
s.Require().Equal(st, retSt2)
// compress
st3 := mockStruct{
ID: 1234567890,
Key: `1234567890123456789012345678901234567890123456789012345678901234567890`, // 70 chars
CreatedAt: mockTimeNow,
}
bs, err = marshal(st3)
s.Require().NoError(err)
var retSt3 mockStruct
s.Require().NoError(unmarshal(bs, &retSt3))
s.Require().Equal(st3, retSt3)
}