forked from mission-liao/dingo
/
marshaller_test.go
180 lines (151 loc) · 3.01 KB
/
marshaller_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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package dingo
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)
//
// Marshaller
//
// TODO: make this private
type MarshallerTestSuite struct {
suite.Suite
m Marshaller
}
func (ts *MarshallerTestSuite) TestTask() {
task, err := composeTask(
"test", DefaultOption().IgnoreReport(true).MonitorProgress(true),
[]interface{}{float64(1.5), "user", "password"},
)
ts.NotNil(task)
ts.Nil(err)
if err != nil {
return
}
fn := func(float64, string, string) {}
ts.Nil(ts.m.Prepare("test", fn))
{
// encode
b, err := ts.m.EncodeTask(fn, task)
ts.Nil(err)
ts.NotNil(b)
// decode
if err == nil {
// provide a fake function as a reference of fingerprint
t, err := ts.m.DecodeTask(nil, fn, b)
ts.Nil(err)
ts.NotNil(t)
if t != nil {
ts.True(t.almostEqual(task))
ts.True(t.Option().GetIgnoreReport())
}
}
}
// nil case
{
_, err := ts.m.EncodeTask(fn, nil)
ts.NotNil(err)
}
}
func (ts *MarshallerTestSuite) TestReport() {
task, err := composeTask("test", DefaultOption().IgnoreReport(true).MonitorProgress(true), nil)
ts.Nil(err)
if err != nil {
return
}
fn := func() (a float64, b, c string) { return }
ts.Nil(ts.m.Prepare("test", fn))
{
report, err := task.composeReport(
Status.Sent,
[]interface{}{float64(2.5), "user", "password"},
errors.New("test error"),
)
ts.Nil(err)
// encode
b, err := ts.m.EncodeReport(fn, report)
ts.Nil(err)
ts.NotNil(b)
// decode
if err == nil {
// provide a fake function as a reference of fingerprint
r, err := ts.m.DecodeReport(nil, fn, b)
ts.Nil(err)
ts.NotNil(r)
if r != nil {
ts.Equal(report, r)
ts.True(r.Option().GetIgnoreReport())
}
}
}
// nil case
{
_, err := ts.m.EncodeReport(fn, nil)
ts.NotNil(err)
}
}
//
// JSON
//
type jsonMarshallerTestSuite struct {
MarshallerTestSuite
}
func TestJsonMarshallerSuite(t *testing.T) {
suite.Run(t, &jsonMarshallerTestSuite{
MarshallerTestSuite{
m: &JsonMarshaller{},
},
})
}
//
// Gob
//
type gobMarshallerTestSuite struct {
MarshallerTestSuite
}
func TestGobMarshallerSuite(t *testing.T) {
suite.Run(t, &gobMarshallerTestSuite{
MarshallerTestSuite{
m: &GobMarshaller{},
},
})
}
//
// JsonSafe
//
type jsonSafeMarshallerTestSuite struct {
MarshallerTestSuite
}
func TestJsonSafeMarshallerSuite(t *testing.T) {
suite.Run(t, &jsonSafeMarshallerTestSuite{
MarshallerTestSuite{
m: &CustomMarshaller{Codec: &JSONSafeCodec{}},
},
})
}
//
// CustomMarshaller
//
func TestCustomMarshaller(t *testing.T) {
ass := assert.New(t)
fn := func(n int) (count int) { return }
task, err := composeTask("TestCustomMarshaller", nil, []interface{}{1})
ass.Nil(err)
if err != nil {
return
}
// nil Codec
i := &CustomMarshaller{}
bs, err := i.EncodeTask(fn, task)
ass.NotNil(err)
ass.Nil(bs)
report, err := task.composeReport(Status.Success, []interface{}{1}, nil)
ass.Nil(err)
if err != nil {
return
}
bs, err = i.EncodeReport(fn, report)
ass.NotNil(err)
ass.Nil(bs)
}