-
Notifications
You must be signed in to change notification settings - Fork 0
/
parameters_test.go
287 lines (271 loc) · 7.61 KB
/
parameters_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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package yagclif
import (
"os"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
type validStruct struct {
A int
B string `yagclif:"shortname:sb;description:foo;default:3"`
C bool `yagclif:"shortname:sc"`
}
var validStructType = reflect.TypeOf(validStruct{})
func TestNewParameters(t *testing.T) {
type faultyStruct struct {
a int
b string `yagclif:"something"`
}
var faultyStructType = reflect.TypeOf(faultyStruct{})
t.Run("returns value", func(t *testing.T) {
params, err := newParameters(validStructType)
assert.Nil(t, err)
assert.Equal(t, 3, len(params))
assert.Equal(t, "A", params[0].name)
assert.Equal(t, 0, params[0].index)
assert.Equal(t, "B", params[1].name)
assert.Equal(t, "foo", params[1].description)
assert.Equal(t, "3", params[1].defaultValue)
assert.False(t, params[1].mandatory)
assert.Equal(t, 1, params[1].index)
assert.Equal(t, "C", params[2].name)
assert.False(t, params[2].mandatory)
assert.Equal(t, 2, params[2].index)
})
t.Run("returns error", func(t *testing.T) {
t.Run("new parameters error", func(t *testing.T) {
params, err := newParameters(faultyStructType)
assert.NotNil(t, err)
assert.Nil(t, params)
})
t.Run("non struct type", func(t *testing.T) {
params, err := newParameters(reflect.TypeOf(""))
assert.NotNil(t, err)
assert.Nil(t, params)
})
t.Run("non valid tags", func(t *testing.T) {
type foo struct {
field1 bool `yagclif:"shortname:sb"`
field2 bool `yagclif:"shortname:sb"`
}
params, err := newParameters(reflect.TypeOf(foo{}))
assert.NotNil(t, err)
assert.Nil(t, params)
})
t.Run("non valid recursion", func(t *testing.T) {
type foo2 struct {
faultyStruct
}
params, err := newParameters(reflect.TypeOf(foo2{}))
assert.NotNil(t, err)
assert.Nil(t, params)
})
})
}
type inheritanceTestStruct struct {
validStruct
D string `yagclif:"shortname:sd;description:foo;default:3"`
E bool `yagclif:"shortname:se"`
}
var inheritanceTestStructType = reflect.TypeOf(inheritanceTestStruct{})
func TestNewParametersInheritance(t *testing.T) {
t.Run("returns value", func(t *testing.T) {
params, err := newParameters(inheritanceTestStructType)
assert.Nil(t, err)
assert.Equal(t, 5, len(params))
assert.Equal(t, "A", params[0].name)
assert.Equal(t, 0, params[0].index)
})
}
func TestFind(t *testing.T) {
params, err := newParameters(validStructType)
assert.Nil(t, err)
t.Run("finds", func(t *testing.T) {
param := params.find("--a")
assert.NotNil(t, param)
})
t.Run("does not find", func(t *testing.T) {
param := params.find("-a")
assert.Nil(t, param)
})
}
func TestParamsGetHelp(t *testing.T) {
params, err := newParameters(validStructType)
assert.Nil(t, err)
help := params.getHelp()
assert.Len(t, help, 3)
}
func TestAssignDefault(t *testing.T) {
t.Run("works", func(t *testing.T) {
type Foo struct {
Solution int `yagclif:"default:42"`
OtherSolution int
Problem int `yagclif:"default:52;env:TestAssignDefault_Problem"`
NoProblem int `yagclif:"omit"`
OtherProblem int `yagclif:"default:51;env:TestAssignDefault_Problem_empty_key"`
}
os.Setenv("TestAssignDefault_Problem", "53")
fooInstance := Foo{}
params, err := newParameters(reflect.TypeOf(fooInstance))
assert.Nil(t, err)
assert.NotNil(t, params)
err = params.assignDefaults(&fooInstance)
assert.Nil(t, err)
assert.Equal(t, Foo{
Solution: 42,
Problem: 53,
NoProblem: 0,
OtherSolution: 0,
OtherProblem: 51,
}, fooInstance)
os.Setenv("TestAssignDefault_Problem_empty_key", "toto")
fooInstance = Foo{}
params, err = newParameters(reflect.TypeOf(fooInstance))
assert.NotNil(t, err)
assert.Equal(t, Foo{
Solution: 0,
Problem: 0,
NoProblem: 0,
OtherSolution: 0,
OtherProblem: 0,
}, fooInstance)
os.Setenv("TestAssignDefault_Problem_empty_key", "toto")
type BadFoo struct {
Solution os.File `yagclif:"default:toto"`
}
badfooInstance := BadFoo{}
params, err = newParameters(reflect.TypeOf(badfooInstance))
assert.NotNil(t, err)
assert.Equal(t, BadFoo{}, badfooInstance)
})
t.Run("returns errors", func(t *testing.T) {
type Foo struct {
Solution int
}
fooInstance := Foo{}
params, err := newParameters(reflect.TypeOf(fooInstance))
assert.Nil(t, err)
assert.NotNil(t, params)
// stub an unparselable value
params[0].defaultValue = "hello"
err = params.assignDefaults(&fooInstance)
assert.NotNil(t, err)
})
}
func TestCheckForMissingMandatory(t *testing.T) {
params := parameters{
¶meter{
used: true,
mandatory: true,
},
¶meter{
used: true,
mandatory: true,
},
}
t.Run("false negative", func(t *testing.T) {
err := params.checkForMissingMandatory()
assert.Nil(t, err)
})
t.Run("false positive", func(t *testing.T) {
params[1].used = false
err := params.checkForMissingMandatory()
assert.NotNil(t, err)
})
}
func TestParseArguments(t *testing.T) {
t.Run("works", func(t *testing.T) {
params, err := newParameters(validStructType)
assert.Nil(t, err)
testStruct := reflect.New(validStructType).Interface()
remaining, err := params.ParseArguments(testStruct, []string{"hello", "--b", "world", "-sc", "!"})
assert.Nil(t, err)
assert.Equal(t, []string{"hello", "!"}, remaining)
assert.Equal(t, &validStruct{
B: "world",
C: true,
}, testStruct)
})
t.Run("error at executing callback", func(t *testing.T) {
type foo struct {
bar int
}
fooType := reflect.TypeOf(foo{})
testStruct := &foo{}
params, err := newParameters(fooType)
assert.Nil(t, err)
remaining, err := params.ParseArguments(testStruct, []string{"--bar", "notanumber"})
assert.Nil(t, remaining)
assert.NotNil(t, err)
})
t.Run("error on missing mandatory", func(t *testing.T) {
type Foo struct {
Foo int `yagclif:"mandatory"`
}
fooType := reflect.TypeOf(Foo{})
testStruct := &Foo{}
params, err := newParameters(fooType)
assert.Nil(t, err)
remaining, err := params.ParseArguments(testStruct, []string{})
assert.Nil(t, remaining)
assert.NotNil(t, err)
})
t.Run("error in setter callback", func(t *testing.T) {
type Foo struct {
A int `yagclif:"mandatory"`
}
fooType := reflect.TypeOf(Foo{})
testStruct := &Foo{}
params, err := newParameters(fooType)
assert.Nil(t, err)
remaining, err := params.ParseArguments(testStruct, []string{"--a", "42", "--a", "43"})
assert.Nil(t, remaining)
assert.NotNil(t, err)
})
}
func TestParse(t *testing.T) {
t.Run("works", func(t *testing.T) {
testStruct := &validStruct{}
os.Args = []string{"main", "hello", "--b", "world", "!"}
remaining, err := Parse(testStruct)
assert.Nil(t, err)
assert.Equal(t, []string{"hello", "!"}, remaining)
})
t.Run("return err with bad struct", func(t *testing.T) {
type faultyStruct struct {
a int
b string `yagclif:"something"`
}
testStruct := &faultyStruct{}
os.Args = []string{"main", "hello", "-b", "world", "!"}
remaining, err := Parse(testStruct)
assert.Nil(t, remaining)
assert.NotNil(t, err)
})
t.Run("return err with bad params", func(t *testing.T) {
type goodStruct struct {
a int
b int `yagclif:"mandatory"`
}
testStruct := &goodStruct{}
os.Args = []string{"main", "-c", "hello", "!"}
remaining, err := Parse(testStruct)
assert.Nil(t, remaining)
assert.NotNil(t, err)
})
t.Run("return err with bad struct", func(t *testing.T) {
type goodStruct struct {
a int
b int `yagclif:"mandatory"`
}
type randomStruct struct {
}
testStruct := &goodStruct{}
os.Args = []string{"main", "-b", "hello", "!"}
remaining, err := Parse(testStruct)
assert.Nil(t, remaining)
assert.NotNil(t, err)
})
}
func TestSetterCallBack(t *testing.T) {
}