@@ -5,229 +5,187 @@ import (
5
5
"testing"
6
6
)
7
7
8
- func TestNoArgs (t * testing.T ) {
9
- c := & Command {Use : "c" , Args : NoArgs , Run : emptyRun }
8
+ func getCommand (args PositionalArgs , withValid bool ) * Command {
9
+ c := & Command {
10
+ Use : "c" ,
11
+ Args : args ,
12
+ Run : emptyRun ,
13
+ }
14
+ if withValid {
15
+ c .ValidArgs = []string {"one" , "two" , "three" }
16
+ }
17
+ return c
18
+ }
10
19
11
- output , err := executeCommand ( c )
20
+ func expectSuccess ( output string , err error , t * testing. T ) {
12
21
if output != "" {
13
- t .Errorf ("Unexpected string : %v" , output )
22
+ t .Errorf ("Unexpected output : %v" , output )
14
23
}
15
24
if err != nil {
16
25
t .Fatalf ("Unexpected error: %v" , err )
17
26
}
18
27
}
19
28
20
- func TestNoArgsWithArgs (t * testing.T ) {
21
- c := & Command {Use : "c" , Args : NoArgs , Run : emptyRun }
22
-
23
- _ , err := executeCommand (c , "illegal" )
29
+ func validWithInvalidArgs (err error , t * testing.T ) {
24
30
if err == nil {
25
31
t .Fatal ("Expected an error" )
26
32
}
33
+ got := err .Error ()
34
+ expected := `invalid argument "a" for "c"`
35
+ if got != expected {
36
+ t .Errorf ("Expected: %q, got: %q" , expected , got )
37
+ }
38
+ }
27
39
40
+ func noArgsWithArgs (err error , t * testing.T ) {
41
+ if err == nil {
42
+ t .Fatal ("Expected an error" )
43
+ }
28
44
got := err .Error ()
29
45
expected := `unknown command "illegal" for "c"`
30
46
if got != expected {
31
47
t .Errorf ("Expected: %q, got: %q" , expected , got )
32
48
}
33
49
}
34
50
35
- func TestOnlyValidArgs (t * testing.T ) {
36
- c := & Command {
37
- Use : "c" ,
38
- Args : OnlyValidArgs ,
39
- ValidArgs : []string {"one" , "two" },
40
- Run : emptyRun ,
51
+ func minimumNArgsWithLessArgs (err error , t * testing.T ) {
52
+ if err == nil {
53
+ t .Fatal ("Expected an error" )
54
+ }
55
+ got := err .Error ()
56
+ expected := "requires at least 2 arg(s), only received 1"
57
+ if got != expected {
58
+ t .Fatalf ("Expected %q, got %q" , expected , got )
41
59
}
60
+ }
42
61
43
- output , err := executeCommand ( c , "one" , "two" )
44
- if output != "" {
45
- t .Errorf ( "Unexpected output: %v" , output )
62
+ func maximumNArgsWithMoreArgs ( err error , t * testing. T ) {
63
+ if err == nil {
64
+ t .Fatal ( "Expected an error" )
46
65
}
47
- if err != nil {
48
- t .Fatalf ("Unexpected error: %v" , err )
66
+ got := err .Error ()
67
+ expected := "accepts at most 2 arg(s), received 3"
68
+ if got != expected {
69
+ t .Fatalf ("Expected %q, got %q" , expected , got )
49
70
}
50
71
}
51
72
52
- func TestOnlyValidArgsWithInvalidArgs (t * testing.T ) {
53
- c := & Command {
54
- Use : "c" ,
55
- Args : OnlyValidArgs ,
56
- ValidArgs : []string {"one" , "two" },
57
- Run : emptyRun ,
73
+ func exactArgsWithInvalidCount (err error , t * testing.T ) {
74
+ if err == nil {
75
+ t .Fatal ("Expected an error" )
76
+ }
77
+ got := err .Error ()
78
+ expected := "accepts 2 arg(s), received 3"
79
+ if got != expected {
80
+ t .Fatalf ("Expected %q, got %q" , expected , got )
58
81
}
82
+ }
59
83
60
- _ , err := executeCommand ( c , "three" )
84
+ func rangeArgsWithInvalidCount ( err error , t * testing. T ) {
61
85
if err == nil {
62
86
t .Fatal ("Expected an error" )
63
87
}
64
-
65
88
got := err .Error ()
66
- expected := `invalid argument "three" for "c"`
89
+ expected := "accepts between 2 and 4 arg(s), received 1"
67
90
if got != expected {
68
- t .Errorf ("Expected: %q, got: %q" , expected , got )
91
+ t .Fatalf ("Expected %q, got %q" , expected , got )
69
92
}
70
93
}
71
94
95
+ func TestNoArgs (t * testing.T ) {
96
+ c := getCommand (NoArgs , false )
97
+ output , err := executeCommand (c )
98
+ expectSuccess (output , err , t )
99
+ }
100
+
101
+ func TestNoArgsWithArgs (t * testing.T ) {
102
+ c := getCommand (NoArgs , false )
103
+ _ , err := executeCommand (c , "illegal" )
104
+ noArgsWithArgs (err , t )
105
+ }
106
+
107
+ func TestOnlyValidArgs (t * testing.T ) {
108
+ c := getCommand (OnlyValidArgs , true )
109
+ output , err := executeCommand (c , "one" , "two" )
110
+ expectSuccess (output , err , t )
111
+ }
112
+
113
+ func TestOnlyValidArgsWithInvalidArgs (t * testing.T ) {
114
+ c := getCommand (OnlyValidArgs , true )
115
+ _ , err := executeCommand (c , "a" )
116
+ validWithInvalidArgs (err , t )
117
+ }
118
+
72
119
func TestArbitraryArgs (t * testing.T ) {
73
- c := & Command { Use : "c" , Args : ArbitraryArgs , Run : emptyRun }
120
+ c := getCommand ( ArbitraryArgs , false )
74
121
output , err := executeCommand (c , "a" , "b" )
75
- if output != "" {
76
- t .Errorf ("Unexpected output: %v" , output )
77
- }
78
- if err != nil {
79
- t .Errorf ("Unexpected error: %v" , err )
80
- }
122
+ expectSuccess (output , err , t )
81
123
}
82
124
83
125
func TestMinimumNArgs (t * testing.T ) {
84
- c := & Command { Use : "c" , Args : MinimumNArgs (2 ), Run : emptyRun }
126
+ c := getCommand ( MinimumNArgs (2 ), false )
85
127
output , err := executeCommand (c , "a" , "b" , "c" )
86
- if output != "" {
87
- t .Errorf ("Unexpected output: %v" , output )
88
- }
89
- if err != nil {
90
- t .Errorf ("Unexpected error: %v" , err )
91
- }
128
+ expectSuccess (output , err , t )
92
129
}
93
130
94
131
func TestMinimumNArgsWithLessArgs (t * testing.T ) {
95
- c := & Command { Use : "c" , Args : MinimumNArgs (2 ), Run : emptyRun }
132
+ c := getCommand ( MinimumNArgs (2 ), false )
96
133
_ , err := executeCommand (c , "a" )
97
-
98
- if err == nil {
99
- t .Fatal ("Expected an error" )
100
- }
101
-
102
- got := err .Error ()
103
- expected := "requires at least 2 arg(s), only received 1"
104
- if got != expected {
105
- t .Fatalf ("Expected %q, got %q" , expected , got )
106
- }
134
+ minimumNArgsWithLessArgs (err , t )
107
135
}
108
136
109
137
func TestMaximumNArgs (t * testing.T ) {
110
- c := & Command { Use : "c" , Args : MaximumNArgs (3 ), Run : emptyRun }
138
+ c := getCommand ( MaximumNArgs (3 ), false )
111
139
output , err := executeCommand (c , "a" , "b" )
112
- if output != "" {
113
- t .Errorf ("Unexpected output: %v" , output )
114
- }
115
- if err != nil {
116
- t .Errorf ("Unexpected error: %v" , err )
117
- }
140
+ expectSuccess (output , err , t )
118
141
}
119
142
120
143
func TestMaximumNArgsWithMoreArgs (t * testing.T ) {
121
- c := & Command { Use : "c" , Args : MaximumNArgs (2 ), Run : emptyRun }
144
+ c := getCommand ( MaximumNArgs (2 ), false )
122
145
_ , err := executeCommand (c , "a" , "b" , "c" )
123
-
124
- if err == nil {
125
- t .Fatal ("Expected an error" )
126
- }
127
-
128
- got := err .Error ()
129
- expected := "accepts at most 2 arg(s), received 3"
130
- if got != expected {
131
- t .Fatalf ("Expected %q, got %q" , expected , got )
132
- }
146
+ maximumNArgsWithMoreArgs (err , t )
133
147
}
134
148
135
149
func TestExactArgs (t * testing.T ) {
136
- c := & Command { Use : "c" , Args : ExactArgs (3 ), Run : emptyRun }
150
+ c := getCommand ( ExactArgs (3 ), false )
137
151
output , err := executeCommand (c , "a" , "b" , "c" )
138
- if output != "" {
139
- t .Errorf ("Unexpected output: %v" , output )
140
- }
141
- if err != nil {
142
- t .Errorf ("Unexpected error: %v" , err )
143
- }
152
+ expectSuccess (output , err , t )
144
153
}
145
154
146
155
func TestExactArgsWithInvalidCount (t * testing.T ) {
147
- c := & Command { Use : "c" , Args : ExactArgs (2 ), Run : emptyRun }
156
+ c := getCommand ( ExactArgs (2 ), false )
148
157
_ , err := executeCommand (c , "a" , "b" , "c" )
149
-
150
- if err == nil {
151
- t .Fatal ("Expected an error" )
152
- }
153
-
154
- got := err .Error ()
155
- expected := "accepts 2 arg(s), received 3"
156
- if got != expected {
157
- t .Fatalf ("Expected %q, got %q" , expected , got )
158
- }
158
+ exactArgsWithInvalidCount (err , t )
159
159
}
160
160
161
161
func TestExactValidArgs (t * testing.T ) {
162
- c := & Command {Use : "c" , Args : ExactValidArgs (3 ), ValidArgs : []string {"a" , "b" , "c" }, Run : emptyRun }
163
- output , err := executeCommand (c , "a" , "b" , "c" )
164
- if output != "" {
165
- t .Errorf ("Unexpected output: %v" , output )
166
- }
167
- if err != nil {
168
- t .Errorf ("Unexpected error: %v" , err )
169
- }
162
+ c := getCommand (ExactValidArgs (3 ), true )
163
+ output , err := executeCommand (c , "three" , "one" , "two" )
164
+ expectSuccess (output , err , t )
170
165
}
171
166
172
167
func TestExactValidArgsWithInvalidCount (t * testing.T ) {
173
- c := & Command {Use : "c" , Args : ExactValidArgs (2 ), Run : emptyRun }
174
- _ , err := executeCommand (c , "a" , "b" , "c" )
175
-
176
- if err == nil {
177
- t .Fatal ("Expected an error" )
178
- }
179
-
180
- got := err .Error ()
181
- expected := "accepts 2 arg(s), received 3"
182
- if got != expected {
183
- t .Fatalf ("Expected %q, got %q" , expected , got )
184
- }
168
+ c := getCommand (ExactValidArgs (2 ), false )
169
+ _ , err := executeCommand (c , "three" , "one" , "two" )
170
+ exactArgsWithInvalidCount (err , t )
185
171
}
186
172
187
173
func TestExactValidArgsWithInvalidArgs (t * testing.T ) {
188
- c := & Command {
189
- Use : "c" ,
190
- Args : ExactValidArgs (1 ),
191
- ValidArgs : []string {"one" , "two" },
192
- Run : emptyRun ,
193
- }
194
-
195
- _ , err := executeCommand (c , "three" )
196
- if err == nil {
197
- t .Fatal ("Expected an error" )
198
- }
199
-
200
- got := err .Error ()
201
- expected := `invalid argument "three" for "c"`
202
- if got != expected {
203
- t .Errorf ("Expected: %q, got: %q" , expected , got )
204
- }
174
+ c := getCommand (ExactValidArgs (3 ), true )
175
+ _ , err := executeCommand (c , "three" , "a" , "two" )
176
+ validWithInvalidArgs (err , t )
205
177
}
206
178
207
179
func TestRangeArgs (t * testing.T ) {
208
- c := & Command { Use : "c" , Args : RangeArgs (2 , 4 ), Run : emptyRun }
180
+ c := getCommand ( RangeArgs (2 , 4 ), false )
209
181
output , err := executeCommand (c , "a" , "b" , "c" )
210
- if output != "" {
211
- t .Errorf ("Unexpected output: %v" , output )
212
- }
213
- if err != nil {
214
- t .Errorf ("Unexpected error: %v" , err )
215
- }
182
+ expectSuccess (output , err , t )
216
183
}
217
184
218
185
func TestRangeArgsWithInvalidCount (t * testing.T ) {
219
- c := & Command { Use : "c" , Args : RangeArgs (2 , 4 ), Run : emptyRun }
186
+ c := getCommand ( RangeArgs (2 , 4 ), false )
220
187
_ , err := executeCommand (c , "a" )
221
-
222
- if err == nil {
223
- t .Fatal ("Expected an error" )
224
- }
225
-
226
- got := err .Error ()
227
- expected := "accepts between 2 and 4 arg(s), received 1"
228
- if got != expected {
229
- t .Fatalf ("Expected %q, got %q" , expected , got )
230
- }
188
+ rangeArgsWithInvalidCount (err , t )
231
189
}
232
190
233
191
func TestRootTakesNoArgs (t * testing.T ) {
0 commit comments