forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 1
/
provider_desc_test.go
168 lines (148 loc) · 3.52 KB
/
provider_desc_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
package depinject
import (
"reflect"
"testing"
"gotest.tools/v3/assert"
"cosmossdk.io/depinject/internal/codegen"
"cosmossdk.io/depinject/internal/graphviz"
)
type StructIn struct {
In
X int
Y float64 `optional:"true"`
}
type BadOptional struct {
In
X int `optional:"foo"`
}
type StructOut struct {
Out
X string
Y []byte
}
func privateProvider(int, float64) (string, []byte) { return "", nil }
func PrivateInAndOut(containerConfig) *container { return nil } //revive:disable:unexported-return
func InternalInAndOut(graphviz.Attributes) *codegen.FileGen { return nil }
type SomeStruct struct{}
func (SomeStruct) privateMethod() int { return 0 }
func SimpleArgs(int, float64) (string, []byte) { return "", nil }
func SimpleArgsWithError(int, float64) (string, []byte, error) { return "", nil, nil }
func StructInAndOut(_ float32, _ StructIn, _ byte) (int16, StructOut, int32, error) {
return int16(0), StructOut{}, int32(0), nil
}
func BadErrorPosition() (error, int) { return nil, 0 } //nolint:stylecheck // Deliberately has error as first of multiple arguments.
func BadOptionalFn(_ BadOptional) int { return 0 }
func Variadic(...float64) int { return 0 }
func TestExtractProviderDescriptor(t *testing.T) {
var (
intType = reflect.TypeOf(0)
int16Type = reflect.TypeOf(int16(0))
int32Type = reflect.TypeOf(int32(0))
float32Type = reflect.TypeOf(float32(0.0))
float64Type = reflect.TypeOf(0.0)
stringType = reflect.TypeOf("")
byteTyp = reflect.TypeOf(byte(0))
bytesTyp = reflect.TypeOf([]byte{})
)
tests := []struct {
name string
ctr interface{}
wantIn []providerInput
wantOut []providerOutput
wantErr string
}{
{
"private",
privateProvider,
nil,
nil,
"function must be exported",
},
{
"private method",
SomeStruct.privateMethod,
nil,
nil,
"function must be exported",
},
{
"private in and out",
PrivateInAndOut,
nil,
nil,
"type must be exported",
},
{
"internal in and out",
InternalInAndOut,
nil,
nil,
"internal",
},
{
"struct",
SomeStruct{},
nil,
nil,
"expected a Func type",
},
{
"simple args",
SimpleArgs,
[]providerInput{{Type: intType}, {Type: float64Type}},
[]providerOutput{{Type: stringType}, {Type: bytesTyp}},
"",
},
{
"simple args with error",
SimpleArgsWithError,
[]providerInput{{Type: intType}, {Type: float64Type}},
[]providerOutput{{Type: stringType}, {Type: bytesTyp}},
"",
},
{
"struct in and out",
StructInAndOut,
[]providerInput{{Type: float32Type}, {Type: intType}, {Type: float64Type, Optional: true}, {Type: byteTyp}},
[]providerOutput{{Type: int16Type}, {Type: stringType}, {Type: bytesTyp}, {Type: int32Type}},
"",
},
{
"error bad position",
BadErrorPosition,
nil,
nil,
"error parameter is not last parameter",
},
{
"bad optional",
BadOptionalFn,
nil,
nil,
"bad optional tag",
},
{
"variadic",
Variadic,
nil,
nil,
"variadic function can't be used",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := extractProviderDescriptor(tt.ctr)
if tt.wantErr != "" {
assert.ErrorContains(t, err, tt.wantErr)
} else {
assert.NilError(t, err)
if !reflect.DeepEqual(got.Inputs, tt.wantIn) {
t.Errorf("extractProviderDescriptor() got = %v, want %v", got.Inputs, tt.wantIn)
}
if !reflect.DeepEqual(got.Outputs, tt.wantOut) {
t.Errorf("extractProviderDescriptor() got = %v, want %v", got.Outputs, tt.wantOut)
}
}
})
}
}