-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathflags_test.go
208 lines (187 loc) · 7.89 KB
/
flags_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
package clistruct
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/urfave/cli"
)
func TestFlagsFromStructWithTags(t *testing.T) {
type custom struct{}
sample := struct {
Bool bool `name:"bool" type:"bool" usage:"hello"`
BoolT bool `name:"boolt" type:"boolt" usage:"hello"`
UInt uint `name:"uint" type:"uint" usage:"hello" value:"1"`
UInt64 uint64 `name:"uint64" type:"uint64" usage:"hello" value:"1"`
Int int `name:"int" type:"int" usage:"hello" value:"1"`
Int64 int64 `name:"int64" type:"int64" usage:"hello" value:"-1"`
Float64 float64 `name:"float64" type:"float64" usage:"hello" value:"1.5"`
IntSlice []int `name:"intslice" type:"intslice" usage:"hello" value:"1,2,3,-1"`
Int64Slice []int64 `name:"int64slice" type:"int64slice" usage:"hello" value:"1,2,3,-1"`
String string `name:"string" type:"string" usage:"hello" value:"some string"`
StringSlice []string `name:"stringslice" type:"stringslice" usage:"hello" value:"some,string,slice"`
Duration time.Duration `name:"duration" type:"duration" usage:"hello" value:"2h1m10s"`
Custom custom `name:"custom" type:"generic" usage:"hello"`
}{}
flags := []cli.Flag{
cli.BoolFlag{Name: "bool", Usage: "hello"},
cli.BoolTFlag{Name: "boolt", Usage: "hello"},
cli.UintFlag{Name: "uint", Usage: "hello", Value: uint(1)},
cli.Uint64Flag{Name: "uint64", Usage: "hello", Value: uint64(1)},
cli.IntFlag{Name: "int", Usage: "hello", Value: 1},
cli.Int64Flag{Name: "int64", Usage: "hello", Value: int64(-1)},
cli.Float64Flag{Name: "float64", Usage: "hello", Value: float64(1.5)},
cli.IntSliceFlag{Name: "intslice", Usage: "hello", Value: &cli.IntSlice{1, 2, 3, -1}},
cli.Int64SliceFlag{Name: "int64slice", Usage: "hello", Value: &cli.Int64Slice{1, 2, 3, -1}},
cli.StringFlag{Name: "string", Usage: "hello", Value: "some string"},
cli.StringSliceFlag{Name: "stringslice", Usage: "hello", Value: &cli.StringSlice{"some", "string", "slice"}},
cli.DurationFlag{Name: "duration", Usage: "hello", Value: (2 * 60 * time.Minute) + (time.Minute) + (10 * time.Second)},
cli.GenericFlag{Name: "custom", Usage: "hello"},
}
result, err := FlagsFromStruct(&sample)
if err != nil {
t.Error(err)
return
}
assert.EqualValues(t, flags, result)
}
func TestFlagsFromStructWithoutNameTags(t *testing.T) {
type custom struct{}
sample := struct {
Bool bool `type:"bool" usage:"hello"`
BoolT bool `type:"boolt" usage:"hello"`
UInt uint `type:"uint" usage:"hello" value:"1"`
UInt64 uint64 `type:"uint64" usage:"hello" value:"1"`
Int int `type:"int" usage:"hello" value:"1"`
Int64 int64 `type:"int64" usage:"hello" value:"-1"`
Float64 float64 `type:"float64" usage:"hello" value:"1.5"`
IntSlice []int `type:"intslice" usage:"hello" value:"1,2,3,-1"`
Int64Slice []int64 `type:"int64slice" usage:"hello" value:"1,2,3,-1"`
String string `type:"string" usage:"hello" value:"some string"`
StringSlice []string `type:"stringslice" usage:"hello" value:"some,string,slice"`
Duration time.Duration `type:"duration" usage:"hello" value:"2h1m10s"`
Custom custom `type:"generic" usage:"hello"`
}{}
flags := []cli.Flag{
cli.BoolFlag{Name: "bool", Usage: "hello"},
cli.BoolTFlag{Name: "boolt", Usage: "hello"},
cli.UintFlag{Name: "uint", Usage: "hello", Value: uint(1)},
cli.Uint64Flag{Name: "uint64", Usage: "hello", Value: uint64(1)},
cli.IntFlag{Name: "int", Usage: "hello", Value: 1},
cli.Int64Flag{Name: "int64", Usage: "hello", Value: int64(-1)},
cli.Float64Flag{Name: "float64", Usage: "hello", Value: float64(1.5)},
cli.IntSliceFlag{Name: "intslice", Usage: "hello", Value: &cli.IntSlice{1, 2, 3, -1}},
cli.Int64SliceFlag{Name: "int64slice", Usage: "hello", Value: &cli.Int64Slice{1, 2, 3, -1}},
cli.StringFlag{Name: "string", Usage: "hello", Value: "some string"},
cli.StringSliceFlag{Name: "stringslice", Usage: "hello", Value: &cli.StringSlice{"some", "string", "slice"}},
cli.DurationFlag{Name: "duration", Usage: "hello", Value: (2 * 60 * time.Minute) + (time.Minute) + (10 * time.Second)},
cli.GenericFlag{Name: "custom", Usage: "hello"},
}
result, err := FlagsFromStruct(&sample)
if err != nil {
t.Error(err)
return
}
assert.EqualValues(t, flags, result)
}
func TestFlagsFromStructBoolHasNoValue(t *testing.T) {
sample := struct {
Bool bool `name:"bool" type:"bool" usage:"hello" value:"true"`
}{}
result, err := FlagsFromStruct(&sample)
assert.NotNil(t, err)
switch err.(type) {
case *ErrFlagTypeCanNotHaveValue:
default:
t.Error(err)
return
}
assert.EqualValues(t, ([]cli.Flag)(nil), result)
}
func TestFlagsFromStructBoolThasNoValue(t *testing.T) {
sample := struct {
BoolT bool `name:"boolt" type:"boolt" usage:"hello" value:"true"`
}{}
result, err := FlagsFromStruct(&sample)
assert.NotNil(t, err)
switch err.(type) {
case *ErrFlagTypeCanNotHaveValue:
default:
t.Error(err)
return
}
assert.EqualValues(t, ([]cli.Flag)(nil), result)
}
func TestFlagsRoStructWithTags(t *testing.T) {
// FIXME: generic types is nil in .cli for some reason oO
//type custom struct{}
type Sample struct {
Bool bool `name:"bool" type:"bool" usage:"hello"`
BoolT bool `name:"boolt" type:"boolt" usage:"hello"`
UInt uint `name:"uint" type:"uint" usage:"hello" value:"1"`
UInt64 uint64 `name:"uint64" type:"uint64" usage:"hello" value:"1"`
Int int `name:"int" type:"int" usage:"hello" value:"1"`
Int64 int64 `name:"int64" type:"int64" usage:"hello" value:"-1"`
Float64 float64 `name:"float64" type:"float64" usage:"hello" value:"1.5"`
IntSlice []int `name:"intslice" type:"intslice" usage:"hello" value:"1,2,3,-1"`
Int64Slice []int64 `name:"int64slice" type:"int64slice" usage:"hello" value:"1,2,3,-1"`
String string `name:"string" type:"string" usage:"hello" value:"some string"`
StringSlice []string `name:"stringslice" type:"stringslice" usage:"hello" value:"some,string,slice"`
Duration time.Duration `name:"duration" type:"duration" usage:"hello" value:"2h1m10s"`
//Custom custom `name:"custom" type:"generic" usage:"hello"`
}
sample := &Sample{}
flags, err := FlagsFromStruct(&sample)
if err != nil {
t.Error(err)
return
}
ch := make(chan *cli.Context, 1)
defer close(ch)
app := cli.NewApp()
app.Flags = flags
app.Action = func(context *cli.Context) error {
ch <- context
return nil
}
err = app.Run(
// XXX: First argument is a program name, so it is empty.
[]string{
"",
"--bool",
"--uint", "10",
"--uint64", "10",
"--int", "10",
"--int64", "-10",
"--float64", "10.05",
"--intslice", "5", "--intslice", "4",
"--int64slice", "5", "--int64slice", "4",
"--string", "string some",
"--stringslice", "and", "--stringslice", "others",
"--duration", "1h",
},
)
if err != nil {
t.Error(err)
return
}
err = FlagsToStruct(<-ch, sample)
if err != nil {
t.Error(err)
return
}
expectedSample := &Sample{
Bool: true,
BoolT: true,
UInt: 10,
UInt64: 10,
Int: 10,
Int64: -10,
Float64: 10.05,
IntSlice: []int{1, 2, 3, -1, 5, 4},
Int64Slice: []int64{1, 2, 3, -1, 5, 4},
String: "string some",
StringSlice: []string{"some", "string", "slice", "and", "others"},
Duration: time.Hour,
}
assert.EqualValues(t, expectedSample, sample)
}