@@ -8,16 +8,44 @@ import (
8
8
"testing"
9
9
)
10
10
11
- func TestCommandSet_Add (t * testing.T ) {
11
+ type handlerFunc func (* Cmd ) error
12
+
13
+ func (h handlerFunc ) Handle (c * Cmd ) error { return h (c ) }
14
+
15
+ func TestCmdSet_Add (t * testing.T ) {
12
16
cmd := & CmdSet {}
13
- names := []string {"name" , "andAnotherOne" , "anotherName" }
14
- for _ , v := range names {
15
- if c := cmd .Add ("" , flag .NewFlagSet (v , flag .ContinueOnError ), nil , false ); cmd .commands [v ] != c {
16
- t .Errorf ("%v has not been added to commandSet" , v )
17
+
18
+ cmds := []Cmd {
19
+ {
20
+ "info for one" ,
21
+ flag .NewFlagSet ("one" , flag .ContinueOnError ),
22
+ false ,
23
+ handlerFunc (func (c * Cmd ) error { return nil }),
24
+ },
25
+ {
26
+ "info for two" ,
27
+ flag .NewFlagSet ("two" , flag .ContinueOnError ),
28
+ true ,
29
+ handlerFunc (func (c * Cmd ) error { return nil }),
30
+ },
31
+ {
32
+ "info for three" ,
33
+ flag .NewFlagSet ("three" , flag .ContinueOnError ),
34
+ false ,
35
+ handlerFunc (func (c * Cmd ) error { return nil }),
36
+ },
37
+ }
38
+ for _ , v := range cmds {
39
+ c := cmd .Add (v .Info , v .FlagSet , v .Handler , v .AllowArgs )
40
+ if cmd .commands [v .FlagSet .Name ()] != c {
41
+ t .Errorf ("%v has not been added to commandSet" , v .FlagSet .Name ())
42
+ }
43
+ if c .AllowArgs != v .AllowArgs || c .FlagSet != v .FlagSet || c .Handler == nil || c .Info != v .Info {
44
+ t .Errorf ("added %v but got %v" , v , * c )
17
45
}
18
46
}
19
47
20
- expectedLen := len (names [ 1 ] )
48
+ expectedLen := len (cmds [ 2 ]. FlagSet . Name () )
21
49
if cmd .cmdNameLength != expectedLen {
22
50
t .Errorf ("expected cmdNameLength length to be %v, got %v" , expectedLen , cmd .cmdNameLength )
23
51
}
@@ -28,22 +56,22 @@ func TestCommandSet_Add(t *testing.T) {
28
56
29
57
}
30
58
31
- func TestCommandSet_AddEmptyName (t * testing.T ) {
59
+ func TestCmdSet_AddEmptyName (t * testing.T ) {
32
60
cmd := & CmdSet {}
33
61
defer func () { recover () }()
34
62
cmd .Add ("" , flag .NewFlagSet ("" , flag .ContinueOnError ), nil , false )
35
63
t .Errorf ("expected empty name to panic" )
36
64
}
37
65
38
- func TestCommandSet_AddExistingName (t * testing.T ) {
66
+ func TestCmdSet_AddExistingName (t * testing.T ) {
39
67
cmd := & CmdSet {}
40
68
defer func () { recover () }()
41
69
cmd .Add ("name" , flag .NewFlagSet ("" , flag .ContinueOnError ), nil , false )
42
70
cmd .Add ("name" , flag .NewFlagSet ("" , flag .ContinueOnError ), nil , false )
43
71
t .Errorf ("expected empty name to panic" )
44
72
}
45
73
46
- func TestCommandSet_Visit (t * testing.T ) {
74
+ func TestCmdSet_Visit (t * testing.T ) {
47
75
cmd := & CmdSet {}
48
76
sets := map [* flag.FlagSet ]bool {
49
77
flag .NewFlagSet ("a" , flag .ContinueOnError ): false ,
@@ -63,7 +91,7 @@ func TestCommandSet_Visit(t *testing.T) {
63
91
64
92
}
65
93
66
- func TestCommandSet_PrintUsage (t * testing.T ) {
94
+ func TestCmdSet_PrintUsage (t * testing.T ) {
67
95
68
96
emptyCmd := & CmdSet {output : & strings.Builder {}}
69
97
@@ -106,7 +134,7 @@ func TestCommandSet_PrintUsage(t *testing.T) {
106
134
}
107
135
}
108
136
109
- func TestCommandSet_Parse (t * testing.T ) {
137
+ func TestCmdSet_Parse (t * testing.T ) {
110
138
cmd := & CmdSet {}
111
139
112
140
var av string
@@ -123,7 +151,7 @@ func TestCommandSet_Parse(t *testing.T) {
123
151
124
152
}
125
153
126
- func TestCommandSet_ParseError (t * testing.T ) {
154
+ func TestCmdSet_ParseError (t * testing.T ) {
127
155
cmd := & CmdSet {}
128
156
cmd .Add ("" , flag .NewFlagSet ("a" , flag .ContinueOnError ), nil , false )
129
157
@@ -158,7 +186,7 @@ func TestCommandSet_ParseError(t *testing.T) {
158
186
t .Errorf ("expected panic, got %v" , err )
159
187
}()
160
188
161
- c := exec .Command (os .Args [0 ], "-test.run=TestCommandSet_ParseError " )
189
+ c := exec .Command (os .Args [0 ], "-test.run=TestCmdSet_ParseError " )
162
190
c .Env = append (os .Environ (), "EXIT=" + tt .execFlag )
163
191
err := c .Run ()
164
192
if e , ok := err .(* exec.ExitError ); ! ok || e .ExitCode () != 2 {
@@ -172,15 +200,15 @@ func TestCommandSet_ParseError(t *testing.T) {
172
200
}
173
201
}
174
202
175
- func TestCommandSet_ParseHelp (t * testing.T ) {
203
+ func TestCmdSet_ParseHelp (t * testing.T ) {
176
204
cmd := & CmdSet {}
177
205
178
206
if os .Getenv ("EXIT" ) == "HELP" {
179
207
cmd .Parse ([]string {"-HeLP" }, flag .ExitOnError )
180
208
return
181
209
}
182
210
183
- c := exec .Command (os .Args [0 ], "-test.run=TestCommandSet_ParseHelp " )
211
+ c := exec .Command (os .Args [0 ], "-test.run=TestCmdSet_ParseHelp " )
184
212
c .Env = append (os .Environ (), "EXIT=HELP" )
185
213
186
214
if err := c .Run (); err != nil {
0 commit comments