@@ -2,7 +2,6 @@ package cli
22
33import (
44 "context"
5- "errors"
65 "testing"
76 "time"
87
@@ -100,60 +99,95 @@ func TestArgumentsRootCommand(t *testing.T) {
10099}
101100
102101func TestArgumentsSubcommand (t * testing.T ) {
103- cmd := buildMinimalTestCommand ()
104- var ifval int64
105- var svals []string
106- var tvals []time.Time
107- cmd .Commands = []* Command {
102+ tests := []struct {
103+ name string
104+ args []string
105+ expectedIval int64
106+ expectedSvals []string
107+ expectedTVals []time.Time
108+ errStr string
109+ }{
108110 {
109- Name : "subcmd" ,
110- Flags : []Flag {
111- & Int64Flag {
112- Name : "foo" ,
113- Value : 10 ,
114- Destination : & ifval ,
115- },
116- },
117- Arguments : []Argument {
118- & TimestampArgs {
119- Name : "ta" ,
120- Min : 1 ,
121- Max : 1 ,
122- Destination : & tvals ,
123- Config : TimestampConfig {
124- Layouts : []string {time .RFC3339 },
125- },
126- },
127- & StringArgs {
128- Name : "sa" ,
129- Min : 1 ,
130- Max : 3 ,
131- Destination : & svals ,
132- },
133- },
111+ name : "insuff args" ,
112+ args : []string {"foo" , "subcmd" , "2006-01-02T15:04:05Z" },
113+ errStr : "sufficient count of arg sa not provided, given 0 expected 1" ,
114+ },
115+ {
116+ name : "set sval and tval" ,
117+ args : []string {"foo" , "subcmd" , "2006-01-02T15:04:05Z" , "fubar" },
118+ expectedIval : 10 ,
119+ expectedTVals : []time.Time {time .Date (2006 , time .January , 2 , 15 , 4 , 5 , 0 , time .UTC )},
120+ expectedSvals : []string {"fubar" },
121+ },
122+ {
123+ name : "set sval, tval and ival" ,
124+ args : []string {"foo" , "subcmd" , "--foo" , "100" , "2006-01-02T15:04:05Z" , "fubar" , "some" },
125+ expectedIval : 100 ,
126+ expectedTVals : []time.Time {time .Date (2006 , time .January , 2 , 15 , 4 , 5 , 0 , time .UTC )},
127+ expectedSvals : []string {"fubar" , "some" },
134128 },
135129 }
136130
137- numUsageErrors := 0
138- cmd .Commands [0 ].OnUsageError = func (ctx context.Context , cmd * Command , err error , isSubcommand bool ) error {
139- numUsageErrors ++
140- return err
141- }
131+ for _ , test := range tests {
132+ t .Run (test .name , func (t * testing.T ) {
133+ cmd := buildMinimalTestCommand ()
134+ var ival int64
135+ var svals []string
136+ var tvals []time.Time
137+ cmd .Commands = []* Command {
138+ {
139+ Name : "subcmd" ,
140+ Flags : []Flag {
141+ & IntFlag {
142+ Name : "foo" ,
143+ Value : 10 ,
144+ Destination : & ival ,
145+ },
146+ },
147+ Arguments : []Argument {
148+ & TimestampArgs {
149+ Name : "ta" ,
150+ Min : 1 ,
151+ Max : 1 ,
152+ Destination : & tvals ,
153+ Config : TimestampConfig {
154+ Layouts : []string {time .RFC3339 },
155+ },
156+ },
157+ & StringArgs {
158+ Name : "sa" ,
159+ Min : 1 ,
160+ Max : 3 ,
161+ Destination : & svals ,
162+ },
163+ },
164+ },
165+ }
142166
143- require .Error (t , errors .New ("sufficient count of arg sa not provided, given 0 expected 1" ), cmd .Run (context .Background (), []string {"foo" , "subcmd" , "2006-01-02T15:04:05Z" }))
144- require .Equal (t , 1 , numUsageErrors )
167+ numUsageErrors := 0
168+ cmd .Commands [0 ].OnUsageError = func (ctx context.Context , cmd * Command , err error , isSubcommand bool ) error {
169+ numUsageErrors ++
170+ return err
171+ }
172+
173+ err := cmd .Run (buildTestContext (t ), test .args )
145174
146- tvals = []time.Time {}
147- require .NoError (t , cmd .Run (context .Background (), []string {"foo" , "subcmd" , "2006-01-02T15:04:05Z" , "fubar" }))
148- require .Equal (t , []time.Time {time .Date (2006 , time .January , 2 , 15 , 4 , 5 , 0 , time .UTC )}, tvals )
149- require .Equal (t , []string {"fubar" }, svals )
175+ r := require .New (t )
150176
151- tvals = []time.Time {}
152- svals = []string {}
153- require .NoError (t , cmd .Run (context .Background (), []string {"foo" , "subcmd" , "--foo" , "100" , "2006-01-02T15:04:05Z" , "fubar" , "some" }))
154- require .Equal (t , int64 (100 ), ifval )
155- require .Equal (t , []time.Time {time .Date (2006 , time .January , 2 , 15 , 4 , 5 , 0 , time .UTC )}, tvals )
156- require .Equal (t , []string {"fubar" , "some" }, svals )
177+ if test .errStr != "" {
178+ r .ErrorContains (err , test .errStr )
179+ r .Equal (1 , numUsageErrors )
180+ } else {
181+ if test .expectedSvals != nil {
182+ r .Equal (test .expectedSvals , svals )
183+ }
184+ if test .expectedTVals != nil {
185+ r .Equal (test .expectedTVals , tvals )
186+ }
187+ r .Equal (test .expectedIval , ival )
188+ }
189+ })
190+ }
157191}
158192
159193func TestArgsUsage (t * testing.T ) {
@@ -222,26 +256,49 @@ func TestArgsUsage(t *testing.T) {
222256}
223257
224258func TestSingleOptionalArg (t * testing.T ) {
225- cmd := buildMinimalTestCommand ()
226- var s1 []string
227- arg := & StringArgs {
228- Min : 0 ,
229- Max : 1 ,
230- Destination : & s1 ,
231- }
232- cmd .Arguments = []Argument {
233- arg ,
259+ tests := []struct {
260+ name string
261+ args []string
262+ argValue string
263+ exp []string
264+ }{
265+ {
266+ name : "no args" ,
267+ args : []string {"foo" },
268+ exp : nil ,
269+ },
270+ /*{
271+ name: "no arg with def value",
272+ args: []string{"foo"},
273+ exp: []string{"bar"},
274+ },*/
275+ {
276+ name : "one arg" ,
277+ args : []string {"foo" , "zbar" },
278+ exp : []string {"zbar" },
279+ },
234280 }
235281
236- require .NoError (t , cmd .Run (context .Background (), []string {"foo" }))
237- require .Equal (t , []string {}, s1 )
238-
239- /*arg.Value = "bar"
240- require.NoError(t, cmd.Run(context.Background(), []string{"foo"}))
241- require.Equal(t, "bar", s1)*/
282+ for _ , test := range tests {
283+ t .Run (test .name , func (t * testing.T ) {
284+ cmd := buildMinimalTestCommand ()
285+ var s1 []string
286+ arg := & StringArgs {
287+ Min : 0 ,
288+ Max : 1 ,
289+ Value : test .argValue ,
290+ Destination : & s1 ,
291+ }
292+ cmd .Arguments = []Argument {
293+ arg ,
294+ }
242295
243- require .NoError (t , cmd .Run (context .Background (), []string {"foo" , "zbar" }))
244- require .Equal (t , []string {"zbar" }, s1 )
296+ err := cmd .Run (buildTestContext (t ), test .args ) //
297+ r := require .New (t )
298+ r .NoError (err )
299+ r .Equal (test .exp , s1 )
300+ })
301+ }
245302}
246303
247304func TestUnboundedArgs (t * testing.T ) {
0 commit comments