-
Notifications
You must be signed in to change notification settings - Fork 45
/
flag_test.go
216 lines (188 loc) · 7.28 KB
/
flag_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
package utils
import (
"testing"
"go.viam.com/test"
)
func TestParseFlags(t *testing.T) {
t.Run("simple", func(t *testing.T) {
test.That(t, ParseFlags(nil, nil), test.ShouldBeNil)
test.That(t, ParseFlags([]string{"1"}, nil), test.ShouldBeNil)
err := ParseFlags([]string{"1"}, 1)
test.That(t, err, test.ShouldNotBeNil)
test.That(t, err.Error(), test.ShouldContainSubstring, "struct")
s := struct{}{}
err = ParseFlags([]string{"1"}, s)
test.That(t, err, test.ShouldNotBeNil)
test.That(t, err.Error(), test.ShouldContainSubstring, "addressable")
test.That(t, ParseFlags([]string{"1"}, &s), test.ShouldBeNil)
var ps1 parseStruct1
test.That(t, ParseFlags([]string{"1"}, &ps1), test.ShouldBeNil)
test.That(t, ps1.A, test.ShouldEqual, "")
test.That(t, ps1.b, test.ShouldEqual, "")
var ps2 parseStruct2
ps2Old := ps2
test.That(t, ParseFlags([]string{"1"}, &ps2), test.ShouldBeNil)
test.That(t, ps2, test.ShouldResemble, ps2Old)
test.That(t, ParseFlags([]string{"1", "--a=5"}, &ps2), test.ShouldBeNil)
test.That(t, ps2, test.ShouldNotResemble, ps2Old)
test.That(t, ps2.A, test.ShouldEqual, "5")
err = ParseFlags([]string{"1", "--a=5", "--b=6"}, &ps2)
test.That(t, err, test.ShouldNotBeNil)
test.That(t, err.Error(), test.ShouldContainSubstring, "defined")
test.That(t, ps2, test.ShouldNotResemble, ps2Old)
test.That(t, ps2.A, test.ShouldEqual, "5")
test.That(t, ps2.b, test.ShouldEqual, "")
err = ParseFlags([]string{"1", "--a=5", "--c=foo"}, &ps2)
test.That(t, err, test.ShouldNotBeNil)
test.That(t, err.Error(), test.ShouldContainSubstring, "parse")
err = ParseFlags([]string{"1", "--a=5", "--d=foo"}, &ps2)
test.That(t, err, test.ShouldNotBeNil)
test.That(t, err.Error(), test.ShouldContainSubstring, "parse")
})
t.Run("NetPortFlag", func(t *testing.T) {
var psn parseStructNet
test.That(t, ParseFlags([]string{"1", "--a=2"}, &psn), test.ShouldBeNil)
test.That(t, psn, test.ShouldResemble, parseStructNet{
A: NetPortFlag(2),
})
test.That(t, ParseFlags([]string{"1", "--a=0"}, &psn), test.ShouldBeNil)
test.That(t, psn, test.ShouldResemble, parseStructNet{
A: NetPortFlag(0),
})
test.That(t, ParseFlags([]string{"1"}, &psn), test.ShouldBeNil)
test.That(t, psn, test.ShouldResemble, parseStructNet{
A: NetPortFlag(5555),
})
err := ParseFlags([]string{"1", "--a=-1"}, &psn)
test.That(t, err, test.ShouldNotBeNil)
test.That(t, err.Error(), test.ShouldContainSubstring, "syntax")
err = ParseFlags([]string{"1", "--a=100000"}, &psn)
test.That(t, err, test.ShouldNotBeNil)
test.That(t, err.Error(), test.ShouldContainSubstring, "range")
test.That(t, ParseFlags([]string{"1", "--b=2", "--b=3"}, &psn), test.ShouldBeNil)
test.That(t, psn, test.ShouldResemble, parseStructNet{
A: NetPortFlag(5555),
B: []NetPortFlag{2, 3},
})
err = ParseFlags([]string{"1", "--b=2", "--b=foo"}, &psn)
test.That(t, err, test.ShouldNotBeNil)
test.That(t, err.Error(), test.ShouldContainSubstring, "syntax")
err = ParseFlags([]string{"1", "--b=2", "--b=100000"}, &psn)
test.That(t, err, test.ShouldNotBeNil)
test.That(t, err.Error(), test.ShouldContainSubstring, "range")
psn = parseStructNet{}
test.That(t, ParseFlags([]string{"1", "--d=2"}, &psn), test.ShouldBeNil)
nf := NetPortFlag(2)
test.That(t, psn, test.ShouldResemble, parseStructNet{
A: NetPortFlag(5555),
D: &nf,
})
err = ParseFlags([]string{"1", "--d=foo"}, &psn)
test.That(t, err, test.ShouldNotBeNil)
test.That(t, err.Error(), test.ShouldContainSubstring, "syntax")
})
t.Run("flag options", func(t *testing.T) {
var ps3 parseStruct3
err := ParseFlags([]string{"1", "--A=hey there"}, &ps3)
test.That(t, err, test.ShouldNotBeNil)
test.That(t, err.Error(), test.ShouldContainSubstring, "Usage")
err = ParseFlags([]string{"1", "--A=hey there", "--b=one"}, &ps3)
test.That(t, err, test.ShouldNotBeNil)
test.That(t, err.Error(), test.ShouldContainSubstring, "Usage")
err = ParseFlags([]string{"1", "--A=hey there", "--b=one", "--c=2"}, &ps3)
test.That(t, err, test.ShouldBeNil)
test.That(t, ps3.A, test.ShouldEqual, "hey there")
test.That(t, ps3.B, test.ShouldEqual, "one")
test.That(t, ps3.C, test.ShouldEqual, 2)
test.That(t, ps3.D, test.ShouldEqual, 1)
test.That(t, ps3.E, test.ShouldEqual, "whatisup")
test.That(t, ps3.F, test.ShouldBeTrue)
err = ParseFlags([]string{"1", "--a=hey there"}, &parseStruct4{})
test.That(t, err, test.ShouldNotBeNil)
test.That(t, err.Error(), test.ShouldContainSubstring, "syntax")
err = ParseFlags([]string{"1", "--a=hey there"}, &parseStruct5{})
test.That(t, err, test.ShouldNotBeNil)
test.That(t, err.Error(), test.ShouldContainSubstring, "syntax")
err = ParseFlags([]string{"1", "--A=hey there", "--b=one", "--c=2", "5"}, &ps3)
test.That(t, err, test.ShouldNotBeNil)
test.That(t, err.Error(), test.ShouldContainSubstring, "syntax")
err = ParseFlags([]string{"1", "--A=hey there", "--b=one", "--c=2", "false"}, &ps3)
test.That(t, err, test.ShouldBeNil)
test.That(t, ps3.A, test.ShouldEqual, "hey there")
test.That(t, ps3.B, test.ShouldEqual, "one")
test.That(t, ps3.C, test.ShouldEqual, 2)
test.That(t, ps3.D, test.ShouldEqual, 1)
test.That(t, ps3.E, test.ShouldEqual, "whatisup")
test.That(t, ps3.F, test.ShouldBeFalse)
err = ParseFlags([]string{"1", "--A=hey there", "--b=one", "--c=2", "false", "more"}, &ps3)
test.That(t, err, test.ShouldNotBeNil)
test.That(t, err.Error(), test.ShouldContainSubstring, "unspecified")
var ps6 parseStruct6
err = ParseFlags([]string{"1", "true", "two", "three", "four"}, &ps6)
test.That(t, err, test.ShouldBeNil)
test.That(t, ps6.A, test.ShouldBeTrue)
test.That(t, ps6.B, test.ShouldEqual, "two")
test.That(t, NewStringSet(ps6.Extra...), test.ShouldResemble, NewStringSet("three", "four"))
var ps7 parseStruct7
err = ParseFlags([]string{"1", "true", "two", "three", "four"}, &ps7)
test.That(t, err.Error(), test.ShouldContainSubstring, "more than one extra")
test.That(t, err.Error(), test.ShouldContainSubstring, "Extra")
test.That(t, err.Error(), test.ShouldContainSubstring, "Extra2")
})
}
type parseStruct1 struct {
A string
b string
}
type parseStruct2 struct {
A string `flag:"a"`
b string `flag:"b"`
C int `flag:"c"`
D bool `flag:"d"`
}
type stringFlag string
func (sf *stringFlag) String() string {
return string(*sf)
}
func (sf *stringFlag) Set(val string) error {
if val != "" {
*sf = stringFlag(val)
return nil
}
*sf = stringFlag("whatisup")
return nil
}
func (sf *stringFlag) Get() interface{} {
return string(*sf)
}
type parseStruct3 struct {
A string `flag:""`
B string `flag:"b,required,default=foo,usage=hello alice"`
C int `flag:"c,required,usage=hello bob"`
D int `flag:"d,default=1,usage=hello charlie"`
E stringFlag `flag:"e,default="`
F bool `flag:"0,default=true"`
}
type parseStruct4 struct {
A int `flag:",default=foo"`
}
type parseStruct5 struct {
A bool `flag:",default=foo"`
}
type parseStruct6 struct {
A bool `flag:"0"`
B string `flag:"1"`
Extra []string `flag:",extra"`
}
type parseStruct7 struct {
A bool `flag:"0"`
B string `flag:"1"`
Extra []string `flag:",extra"`
Extra2 []string `flag:",extra"`
}
type parseStructNet struct {
A NetPortFlag `flag:"a,default=5555"`
B []NetPortFlag `flag:"b"`
C NetPortFlag `flag:"c"`
D *NetPortFlag `flag:"d"`
}