forked from mission-liao/dingo
/
invoker_lazy_test.go
131 lines (110 loc) · 2.73 KB
/
invoker_lazy_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
package dingo
import (
"encoding/gob"
"reflect"
"testing"
"github.com/stretchr/testify/suite"
)
//
// gob
//
type invokerLazyGobTestSuite struct {
InvokerTestSuite
}
func TestInvokerLazyGobSuite(t *testing.T) {
gob.Register(map[string]int{})
gob.Register(testEmbed{})
gob.Register(testEmbedWithCollision{})
gob.Register(TestStruct{})
gob.Register(map[string]*TestStruct{})
gob.Register([]*TestStruct{})
suite.Run(t, &invokerLazyGobTestSuite{
InvokerTestSuite{
ivk: &LazyInvoker{},
convert: ioGOB,
},
})
}
//
// test cases
//
func (s *invokerLazyGobTestSuite) TestMap2Struct() {
s.InvokerTestSuite._testMap2Struct(map[string]*TestStruct{
"a": &TestStruct{Name: "Mary", Count: 11},
"b": &TestStruct{Name: "Bob", Count: 10},
"c": &TestStruct{Name: "Tom", Count: 12},
"d": &TestStruct{}, // gob didn't allow nil value as struct
})
}
func (s *invokerLazyGobTestSuite) TestSliceOfStruct() {
s.InvokerTestSuite._testSliceOfStruct([]*TestStruct{
&TestStruct{Name: "Mary", Count: 11},
&TestStruct{Name: "Bob", Count: 10},
&TestStruct{Name: "Tom", Count: 12},
&TestStruct{},
})
}
func (s *invokerLazyGobTestSuite) TestReturn() {
// struct & *struct would be marshalled to the same
// stuff by god, we need to take care of that.
fn := func() (a *TestStruct, b *testEmbed) { return }
v, err := s.convert(fn, &TestStruct{}, &testEmbed{})
s.Nil(err)
v, err = s.ivk.Return(fn, v)
s.Nil(err)
// check each type
_, ok := v[0].(*TestStruct)
s.True(ok)
_, ok = v[1].(*testEmbed)
s.True(ok)
}
//
// json-safe
//
func ioJSONSafe() func(f interface{}, args ...interface{}) (v []interface{}, err error) {
m := &JSONSafeCodec{}
return func(f interface{}, args ...interface{}) (v []interface{}, err error) {
// encode
bs, err := m.encode(args)
if err != nil {
return
}
// decode
funcT := reflect.TypeOf(f)
v, err = m.decode(bs, func(i int) reflect.Type {
// for invoker, we mainly focus on input
return funcT.In(i)
})
return
}
}
type invokerLazyJsonSafeTestSuite struct {
InvokerTestSuite
}
func TestInvokerLazyJsonSafeSuite(t *testing.T) {
suite.Run(t, &invokerLazyJsonSafeTestSuite{
InvokerTestSuite{
ivk: &LazyInvoker{},
convert: ioJSONSafe(),
},
})
}
//
// test cases
//
func (s *invokerLazyJsonSafeTestSuite) TestMap2Struct() {
s.InvokerTestSuite._testMap2Struct(map[string]*TestStruct{
"a": &TestStruct{Name: "Mary", Count: 11},
"b": &TestStruct{Name: "Bob", Count: 10},
"c": &TestStruct{Name: "Tom", Count: 12},
"d": nil,
})
}
func (s *invokerLazyJsonSafeTestSuite) TestSliceOfStruct() {
s.InvokerTestSuite._testSliceOfStruct([]*TestStruct{
&TestStruct{Name: "Mary", Count: 11},
&TestStruct{Name: "Bob", Count: 10},
&TestStruct{Name: "Tom", Count: 12},
nil,
})
}