-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparam_test.go
215 lines (172 loc) · 4.82 KB
/
param_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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package antidig
import (
"io"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestParamListBuild(t *testing.T) {
p, err := newParamList(reflect.TypeOf(func() io.Writer { return nil }), newScope())
require.NoError(t, err)
assert.Panics(t, func() {
p.Build(newScope())
})
}
func TestParamObjectSuccess(t *testing.T) {
type type1 struct{}
type type2 struct{}
type type3 struct{}
type in struct {
In
T1 type1
T2 type2 `optional:"true"`
T3 type3 `name:"foo"`
Nested struct {
In
A string
B int32
} `name:"bar"`
}
po, err := newParamObject(reflect.TypeOf(in{}), newScope())
require.NoError(t, err)
require.Len(t, po.Fields, 4)
t.Run("no tags", func(t *testing.T) {
require.Equal(t, "T1", po.Fields[0].FieldName)
t1, ok := po.Fields[0].Param.(paramSingle)
require.True(t, ok, "T1 must be a paramSingle")
assert.Empty(t, t1.Name)
assert.False(t, t1.Optional)
})
t.Run("optional field", func(t *testing.T) {
require.Equal(t, "T2", po.Fields[1].FieldName)
t2, ok := po.Fields[1].Param.(paramSingle)
require.True(t, ok, "T2 must be a paramSingle")
assert.Empty(t, t2.Name)
assert.True(t, t2.Optional)
})
t.Run("named value", func(t *testing.T) {
require.Equal(t, "T3", po.Fields[2].FieldName)
t3, ok := po.Fields[2].Param.(paramSingle)
require.True(t, ok, "T3 must be a paramSingle")
assert.Equal(t, "foo", t3.Name)
assert.False(t, t3.Optional)
})
t.Run("tags don't apply to nested dig.In", func(t *testing.T) {
require.Equal(t, "Nested", po.Fields[3].FieldName)
nested, ok := po.Fields[3].Param.(paramObject)
require.True(t, ok, "Nested must be a paramObject")
assert.Len(t, nested.Fields, 2)
a, ok := nested.Fields[0].Param.(paramSingle)
require.True(t, ok, "Nested.A must be a paramSingle")
assert.Empty(t, a.Name, "Nested.A must not have a name")
})
}
func TestParamObjectWithUnexportedFieldsSuccess(t *testing.T) {
type type1 struct{}
type type2 struct{}
type in struct {
In `ignore-unexported:"true"`
T1 type1
t2 type2
}
_ = in{}.t2 // unused
po, err := newParamObject(reflect.TypeOf(in{}), newScope())
require.NoError(t, err)
require.Len(t, po.Fields, 1)
require.Equal(t, "T1", po.Fields[0].FieldName)
t1, ok := po.Fields[0].Param.(paramSingle)
require.True(t, ok, "T1 must be a paramSingle")
assert.Empty(t, t1.Name)
assert.False(t, t1.Optional)
}
func TestParamObjectFailure(t *testing.T) {
t.Run("unexported field gets an error", func(t *testing.T) {
type A struct{}
type in struct {
In
A1 A
a2 A
}
_ = in{}.a2 // unused but needed
_, err := newParamObject(reflect.TypeOf(in{}), newScope())
require.Error(t, err)
assert.Contains(t, err.Error(),
`bad field "a2" of dig.in: unexported fields not allowed in dig.In, did you mean to export "a2" (dig.A)`)
})
t.Run("unexported field with empty tag value gets an error", func(t *testing.T) {
type A struct{}
type in struct {
In `ignore-unexported:""`
A1 A
a2 A
}
_ = in{}.a2 // unused but needed
_, err := newParamObject(reflect.TypeOf(in{}), newScope())
require.Error(t, err)
assert.Contains(t, err.Error(),
`bad field "a2" of dig.in: unexported fields not allowed in dig.In, did you mean to export "a2" (dig.A)`)
})
t.Run("unexported field with invalid tag value gets an error", func(t *testing.T) {
type A struct{}
type in struct {
In `ignore-unexported:"foo"`
A1 A
a2 A
}
_ = in{}.a2 // unused but needed
_, err := newParamObject(reflect.TypeOf(in{}), newScope())
require.Error(t, err)
assert.Contains(t, err.Error(),
`invalid value "foo" for "ignore-unexported" tag on field In: strconv.ParseBool: parsing "foo": invalid syntax`)
})
}
func TestParamGroupSliceErrors(t *testing.T) {
tests := []struct {
desc string
shape interface{}
wantErr string
}{
{
desc: "non-slice type are disallowed",
shape: struct {
In
Foo string `group:"foo"`
}{},
wantErr: "value groups may be consumed as slices only: " +
`field "Foo" (string) is not a slice`,
},
{
desc: "cannot provide name for a group",
shape: struct {
In
Foo []string `group:"foo" name:"bar"`
}{},
wantErr: "cannot use named values with value groups: " +
`name:"bar" requested with group:"foo"`,
},
{
desc: "cannot be optional",
shape: struct {
In
Foo []string `group:"foo" optional:"true"`
}{},
wantErr: "value groups cannot be optional",
},
{
desc: "no flatten in In",
shape: struct {
In
Foo []string `group:"foo,flatten"`
}{},
wantErr: "cannot use flatten in parameter value groups",
},
}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
_, err := newParamObject(reflect.TypeOf(tt.shape), newScope())
require.Error(t, err, "expected failure")
assert.Contains(t, err.Error(), tt.wantErr)
})
}
}