forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprovider_desc_test.go
106 lines (99 loc) · 2.51 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
package depinject_test
import (
"reflect"
"testing"
"github.com/cosmos/cosmos-sdk/depinject"
)
type StructIn struct {
depinject.In
X int
Y float64 `optional:"true"`
}
type BadOptional struct {
depinject.In
X int `optional:"foo"`
}
type StructOut struct {
depinject.Out
X string
Y []byte
}
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 []depinject.ProviderInput
wantOut []depinject.ProviderOutput
wantErr bool
}{
{
"simple args",
func(x int, y float64) (string, []byte) { return "", nil },
[]depinject.ProviderInput{{Type: intType}, {Type: float64Type}},
[]depinject.ProviderOutput{{Type: stringType}, {Type: bytesTyp}},
false,
},
{
"simple args with error",
func(x int, y float64) (string, []byte, error) { return "", nil, nil },
[]depinject.ProviderInput{{Type: intType}, {Type: float64Type}},
[]depinject.ProviderOutput{{Type: stringType}, {Type: bytesTyp}},
false,
},
{
"struct in and out",
func(_ float32, _ StructIn, _ byte) (int16, StructOut, int32, error) {
return int16(0), StructOut{}, int32(0), nil
},
[]depinject.ProviderInput{{Type: float32Type}, {Type: intType}, {Type: float64Type, Optional: true}, {Type: byteTyp}},
[]depinject.ProviderOutput{{Type: int16Type}, {Type: stringType}, {Type: bytesTyp}, {Type: int32Type}},
false,
},
{
"error bad position",
func() (error, int) { return nil, 0 },
nil,
nil,
true,
},
{
"bad optional",
func(_ BadOptional) int { return 0 },
nil,
nil,
true,
},
{
"variadic",
func(...float64) int { return 0 },
nil,
nil,
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := depinject.ExtractProviderDescriptor(tt.ctr)
if (err != nil) != tt.wantErr {
t.Errorf("ExtractProviderDescriptor() error = %v, wantErr %v", err, tt.wantErr)
return
}
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)
}
})
}
}