-
Notifications
You must be signed in to change notification settings - Fork 0
/
broccoli_test.go
295 lines (252 loc) · 8.23 KB
/
broccoli_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
288
289
290
291
292
293
294
295
package broccoli
import (
"math"
"reflect"
"testing"
)
func TestBindArgs(t *testing.T) {
t.Run("test-no-args", func(t *testing.T) {
type NoArgsApp struct {
_ struct{} `version:"1.0.0" command:"NoArgsApp" about:"This is a test app"`
}
var app NoArgsApp
args, _, err := Bind(&app, []string{})
if err != nil {
t.Error(err)
}
if len(args) != 0 {
t.Errorf("expected 0 args, got %d", len(args))
}
})
t.Run("test-flags", func(t *testing.T) {
type LongFlagApp struct {
_ struct{} `version:"1.0.0" command:"LongFlagApp" about:"This is a test app"`
FirstName string `flag:"name" about:"Your first name"`
LastName string `flag:"last" about:"Your last name"`
Age int `flag:"age" about:"Your age"`
}
var app LongFlagApp
args, _, err := Bind(&app, []string{"--name", "John", "--last", "Doe", "--age", "42"})
if err != nil {
t.Error(err)
}
if len(args) != 0 {
t.Errorf("expected 0 args, got %d\n%v", len(args), args)
}
if app.FirstName != "John" {
t.Errorf("expected first name to be 'John', got '%s'", app.FirstName)
}
if app.LastName != "Doe" {
t.Errorf("expected last name to be 'Doe', got '%s'", app.LastName)
}
if app.Age != 42 {
t.Errorf("expected age to be 42, got %d", app.Age)
}
})
t.Run("test-required-flags", func(t *testing.T) {
type RequiredFlagsApp struct {
_ struct{} `version:"1.0.0" command:"RequiredFlagsApp" about:"This is a test app"`
Name string `flag:"name" alias:"n" required:"true" about:"Your first name"`
Age int `flag:"age" alias:"a" required:"true" about:"Your age"`
}
var app RequiredFlagsApp
_, _, err := Bind(&app, []string{})
if err == nil {
t.Error("expected ErrMissingRequiredField, got nil")
}
})
t.Run("test-default-flags", func(t *testing.T) {
type DefaultFlagsApp struct {
_ struct{} `version:"1.0.0" command:"DefaultFlagsApp" about:"This is a test app"`
Name string `flag:"name" alias:"n" required:"true" about:"Your first name" default:"John"`
Age int `flag:"age" alias:"a" required:"true" about:"Your age" default:"42"`
}
var app DefaultFlagsApp
args, _, err := Bind(&app, []string{})
if err != nil {
t.Error(err)
}
if len(args) != 0 {
t.Errorf("expected 0 args, got %d", len(args))
}
if app.Name != "John" {
t.Errorf("expected name to be 'John', got '%s'", app.Name)
}
if app.Age != 42 {
t.Errorf("expected age to be 42, got %d", app.Age)
}
})
t.Run("test-subcommand", func(t *testing.T) {
type AddApp struct {
_ struct{} `version:"1.0.0" command:"add" about:"This is a test app"`
A int `flag:"a" alias:"a" required:"true" about:"A"`
B int `flag:"b" alias:"b" required:"true" about:"B"`
}
type SubcommandApp struct {
_ struct{} `version:"1.0.0" command:"SubcommandApp" about:"This is a test app"`
Name string `flag:"name" alias:"n" required:"true" about:"Your first name" default:"John"`
Age int `flag:"age" alias:"a" required:"true" about:"Your age" default:"42"`
Add *AddApp `subcommand:"add" about:"Add two numbers"`
}
var app SubcommandApp
args, _, err := Bind(&app, []string{"add", "--a", "1", "--b", "2"})
if err != nil {
t.Error(err)
}
if len(args) != 0 {
t.Errorf("expected 0 args, got %d", len(args))
}
if app.Add == nil {
t.Error("expected Add to be non-nil")
}
if app.Add.A != 1 {
t.Errorf("expected Add.A to be 1, got %d", app.Add.A)
}
if app.Add.B != 2 {
t.Errorf("expected Add.B to be 2, got %d", app.Add.B)
}
})
t.Run("test-args", func(t *testing.T) {
type TestApp struct {
_ struct{} `version:"1.0.0" command:"LongFlagApp" about:"This is a test app"`
FirstName string `flag:"name" about:"Your first name"`
Age int `flag:"age" about:"Your age"`
}
var app TestApp
args, _, err := Bind(&app, []string{"--name", "John Doe", "--age", "42", "extra", "args"})
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(args, []string{"extra", "args"}) {
t.Errorf("expected args to be ['extra', 'args'], got %v", args)
}
})
t.Run("test-alias", func(t *testing.T) {
type TestApp struct {
_ struct{} `version:"1.0.0" command:"LongFlagApp" about:"This is a test app"`
FirstName string `flag:"name" alias:"n" about:"Your first name"`
Age int `flag:"age" alias:"a" about:"Your age"`
}
var app TestApp
args, _, err := Bind(&app, []string{"-n", "John Doe", "--age", "42", "extra", "args"})
if err != nil {
t.Error(err)
}
expectedArgs := []string{"extra", "args"}
if !reflect.DeepEqual(args, expectedArgs) {
t.Errorf("expected args to be %v, got %v", expectedArgs, args)
}
if app.FirstName != "John Doe" {
t.Errorf("expected first name to be 'John Doe', got '%s'", app.FirstName)
}
if app.Age != 42 {
t.Errorf("expected age to be 42, got %d", app.Age)
}
})
t.Run("test-boolean-flags", func(t *testing.T) {
type TestApp struct {
_ struct{} `version:"1.0.0" command:"LongFlagApp" about:"This is a test app"`
Bool0 bool `flag:"bool0" about:"A boolean flag"`
Bool1 bool `flag:"bool1" about:"A boolean flag"`
Bool2 bool `flag:"bool2" about:"A boolean flag"`
}
var app TestApp
args, _, err := Bind(&app, []string{"--bool0", "--!bool1", "--bool2"})
if err != nil {
t.Error(err)
}
if len(args) != 0 {
t.Errorf("expected 0 args, got %d", len(args))
}
if !app.Bool0 {
t.Error("expected Bool0 to be true")
}
if app.Bool1 {
t.Error("expected Bool1 to be false")
}
if !app.Bool2 {
t.Error("expected Bool2 to be false")
}
})
t.Run("test-default-boolean-flags", func(t *testing.T) {
type TestApp struct {
_ struct{} `version:"1.0.0" command:"LongFlagApp" about:"This is a test app"`
Bool0 bool `flag:"bool0" about:"A boolean flag" default:"true" alias:"b0"`
Bool1 bool `flag:"bool1" about:"A boolean flag" default:"false" alias:"b1"`
Bool2 bool `flag:"bool2" about:"A boolean flag" default:"true" alias:"b2"`
}
var app TestApp
args, _, err := Bind(&app, []string{"-!b0", "-b1", "-b2"})
if err != nil {
t.Error(err)
}
if len(args) != 0 {
t.Errorf("expected 0 args, got %d", len(args))
}
if app.Bool0 {
t.Error("expected Bool0 to be false")
}
if !app.Bool1 {
t.Error("expected Bool1 to be true")
}
if !app.Bool2 {
t.Error("expected Bool2 to be true")
}
})
t.Run("test-flags", func(t *testing.T) {
type TestApp struct {
_ struct{} `version:"1.0.0" command:"LongFlagApp" about:"This is a test app"`
Name string `flag:"name" about:"Your name" required:"true"`
Age int `flag:"age" about:"Your age" required:"true"`
Addr *string `flag:"addr" about:"Your address"`
Zip *uint64 `flag:"zip" about:"Your zip code"`
Height float64 `flag:"height" about:"Your height"`
Weight float32 `flag:"weight" about:"Your weight"`
IsDeveloper bool `flag:"dev" alias:"d" about:"Are you a developer?" default:"false"`
Clothes []string `flag:"clothes" alias:"c" about:"Your clothes" required:"true"`
}
var app TestApp
args, _, err := Bind(&app, []string{"--name", "John Doe",
"--age", "42",
"--addr", "123 Main St",
"--zip", "12345",
"--height", "1.78",
"--weight", "80.5",
"--dev",
"-c", "shirt,pants,hat",
"extra", "args"})
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(args, []string{"extra", "args"}) {
t.Errorf("expected args to be ['extra', 'args'], got %v", args)
}
if app.Name != "John Doe" {
t.Errorf("expected name to be 'John Doe', got '%s'", app.Name)
}
if app.Age != 42 {
t.Errorf("expected age to be 42, got %d", app.Age)
}
if *app.Addr != "123 Main St" {
t.Errorf("expected addr to be '123 Main St', got '%s'", *app.Addr)
}
if *app.Zip != 12345 {
t.Errorf("expected zip to be 12345, got %d", *app.Zip)
}
if !floatCompare(app.Height, 1.78) {
t.Errorf("expected height to be 1.78, got %f", app.Height)
}
if !floatCompare(float64(app.Weight), 80.5) {
t.Errorf("expected weight to be 80.5, got %f", app.Weight)
}
if !app.IsDeveloper {
t.Error("expected IsDeveloper to be true")
}
if !reflect.DeepEqual(app.Clothes, []string{"shirt", "pants", "hat"}) {
t.Errorf("expected clothes to be ['shirt', 'pants', 'hat'], got %v", app.Clothes)
}
})
}
func floatCompare(a, b float64) bool {
return math.Abs(a-b) < 0.00001
}