forked from uber-go/dig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
param_test.go
207 lines (175 loc) · 5.44 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
// Copyright (c) 2018 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package dig
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 }))
require.NoError(t, err)
assert.Panics(t, func() {
p.Build(New())
})
}
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{}))
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 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
}
_, err := newParamObject(reflect.TypeOf(in{}))
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)`)
})
}
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",
},
}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
_, err := newParamObject(reflect.TypeOf(tt.shape))
require.Error(t, err, "expected failure")
assert.Contains(t, err.Error(), tt.wantErr)
})
}
}
func TestParamVisitorChecksEverything(t *testing.T) {
type params struct {
In
ReaderAt io.ReaderAt
}
typeOfReader := reflect.TypeOf((*io.Reader)(nil)).Elem()
typeOfWriter := reflect.TypeOf((*io.Writer)(nil)).Elem()
pl, err := newParamList(reflect.TypeOf(func(io.Reader, params, io.Writer) {
t.Fatalf("this function should not be called")
}))
require.NoError(t, err)
idx := 0
walkParam(pl, paramVisitorFunc(func(p param) bool {
defer func() { idx++ }()
switch idx {
case 0:
_, ok := p.(paramList)
assert.True(t, ok, "expected paramList, got %T", p)
case 1:
ps, ok := p.(paramSingle)
assert.True(t, ok, "expected paramSingle, got %T", p)
assert.Equal(t, typeOfReader, ps.Type, "first parameter didn't match")
case 2:
_, ok := p.(paramObject)
assert.True(t, ok, "expected paramObject, got %T", p)
return false // don't recurse
case 3:
ps, ok := p.(paramSingle)
assert.True(t, ok, "expected paramSingle, got %T", p)
assert.Equal(t, typeOfWriter, ps.Type, "third parameter didn't match")
default:
t.Errorf("unexpected call to visitor with %v", p)
}
return true
}))
assert.Equal(t, 4, idx, "visitor wasn't called four times")
}