@@ -26,74 +26,31 @@ func expectSuccess(output string, err error, t *testing.T) {
26
26
}
27
27
}
28
28
29
- func validWithInvalidArgs (err error , t * testing.T ) {
29
+ func expectError (err error , t * testing.T , ex string ) {
30
30
if err == nil {
31
31
t .Fatal ("Expected an error" )
32
32
}
33
-
34
33
got := err .Error ()
34
+
35
35
expected := `invalid argument "a" for "c"`
36
- if got != expected {
37
- t .Errorf ("Expected: %q, got: %q" , expected , got )
36
+ switch ex {
37
+ case "no" :
38
+ expected = `unknown command "one" for "c"`
39
+ case "min" :
40
+ expected = "requires at least 2 arg(s), only received 1"
41
+ case "max" :
42
+ expected = "accepts at most 2 arg(s), received 3"
43
+ case "exact" :
44
+ expected = "accepts 2 arg(s), received 3"
45
+ case "range" :
46
+ expected = "accepts between 2 and 4 arg(s), received 1"
38
47
}
39
- }
40
48
41
- func noArgsWithArgs (err error , t * testing.T ) {
42
- if err == nil {
43
- t .Fatal ("Expected an error" )
44
- }
45
- got := err .Error ()
46
- expected := `unknown command "one" for "c"`
47
49
if got != expected {
48
50
t .Errorf ("Expected: %q, got: %q" , expected , got )
49
51
}
50
52
}
51
53
52
- func minimumNArgsWithLessArgs (err error , t * testing.T ) {
53
- if err == nil {
54
- t .Fatal ("Expected an error" )
55
- }
56
- got := err .Error ()
57
- expected := "requires at least 2 arg(s), only received 1"
58
- if got != expected {
59
- t .Fatalf ("Expected %q, got %q" , expected , got )
60
- }
61
- }
62
-
63
- func maximumNArgsWithMoreArgs (err error , t * testing.T ) {
64
- if err == nil {
65
- t .Fatal ("Expected an error" )
66
- }
67
-
68
- got := err .Error ()
69
- expected := "accepts at most 2 arg(s), received 3"
70
- if got != expected {
71
- t .Fatalf ("Expected %q, got %q" , expected , got )
72
- }
73
- }
74
-
75
- func exactArgsWithInvalidCount (err error , t * testing.T ) {
76
- if err == nil {
77
- t .Fatal ("Expected an error" )
78
- }
79
- got := err .Error ()
80
- expected := "accepts 2 arg(s), received 3"
81
- if got != expected {
82
- t .Fatalf ("Expected %q, got %q" , expected , got )
83
- }
84
- }
85
-
86
- func rangeArgsWithInvalidCount (err error , t * testing.T ) {
87
- if err == nil {
88
- t .Fatal ("Expected an error" )
89
- }
90
- got := err .Error ()
91
- expected := "accepts between 2 and 4 arg(s), received 1"
92
- if got != expected {
93
- t .Fatalf ("Expected %q, got %q" , expected , got )
94
- }
95
- }
96
-
97
54
// NoArgs
98
55
99
56
func TestNoArgs (t * testing.T ) {
@@ -105,13 +62,13 @@ func TestNoArgs(t *testing.T) {
105
62
func TestNoArgsWithArgs (t * testing.T ) {
106
63
c := getCommand (NoArgs , false )
107
64
_ , err := executeCommand (c , "one" )
108
- noArgsWithArgs (err , t )
65
+ expectError (err , t , "no" )
109
66
}
110
67
111
68
func TestNoArgsWithArgsWithValid (t * testing.T ) {
112
69
c := getCommand (NoArgs , true )
113
70
_ , err := executeCommand (c , "one" )
114
- noArgsWithArgs (err , t )
71
+ expectError (err , t , "no" )
115
72
}
116
73
117
74
// ArbitraryArgs
@@ -131,7 +88,7 @@ func TestArbitraryArgsWithValid(t *testing.T) {
131
88
func TestArbitraryArgsWithValidWithInvalidArgs (t * testing.T ) {
132
89
c := getCommand (ArbitraryArgs , true )
133
90
_ , err := executeCommand (c , "a" )
134
- validWithInvalidArgs (err , t )
91
+ expectError (err , t , "valid" )
135
92
}
136
93
137
94
// MinimumNArgs
@@ -151,25 +108,25 @@ func TestMinimumNArgsWithValid(t *testing.T) {
151
108
func TestMinimumNArgsWithValidWithInvalidArgs (t * testing.T ) {
152
109
c := getCommand (MinimumNArgs (2 ), true )
153
110
_ , err := executeCommand (c , "a" , "b" )
154
- validWithInvalidArgs (err , t )
111
+ expectError (err , t , "valid" )
155
112
}
156
113
157
114
func TestMinimumNArgsWithLessArgs (t * testing.T ) {
158
115
c := getCommand (MinimumNArgs (2 ), false )
159
116
_ , err := executeCommand (c , "a" )
160
- minimumNArgsWithLessArgs (err , t )
117
+ expectError (err , t , "min" )
161
118
}
162
119
163
120
func TestMinimumNArgsWithLessArgsWithValid (t * testing.T ) {
164
121
c := getCommand (MinimumNArgs (2 ), true )
165
122
_ , err := executeCommand (c , "one" )
166
- minimumNArgsWithLessArgs (err , t )
123
+ expectError (err , t , "min" )
167
124
}
168
125
169
126
func TestMinimumNArgsWithLessArgsWithValidWithInvalidArgs (t * testing.T ) {
170
127
c := getCommand (MinimumNArgs (2 ), true )
171
128
_ , err := executeCommand (c , "a" )
172
- validWithInvalidArgs (err , t )
129
+ expectError (err , t , "valid" )
173
130
}
174
131
175
132
// MaximumNArgs
@@ -189,25 +146,25 @@ func TestMaximumNArgsWithValid(t *testing.T) {
189
146
func TestMaximumNArgsWithValidWithInvalidArgs (t * testing.T ) {
190
147
c := getCommand (MaximumNArgs (2 ), true )
191
148
_ , err := executeCommand (c , "a" , "b" )
192
- validWithInvalidArgs (err , t )
149
+ expectError (err , t , "valid" )
193
150
}
194
151
195
152
func TestMaximumNArgsWithMoreArgs (t * testing.T ) {
196
153
c := getCommand (MaximumNArgs (2 ), false )
197
154
_ , err := executeCommand (c , "a" , "b" , "c" )
198
- maximumNArgsWithMoreArgs (err , t )
155
+ expectError (err , t , "max" )
199
156
}
200
157
201
158
func TestMaximumNArgsWithMoreArgsWithValid (t * testing.T ) {
202
159
c := getCommand (MaximumNArgs (2 ), true )
203
160
_ , err := executeCommand (c , "one" , "three" , "two" )
204
- maximumNArgsWithMoreArgs (err , t )
161
+ expectError (err , t , "max" )
205
162
}
206
163
207
164
func TestMaximumNArgsWithMoreArgsWithValidWithInvalidArgs (t * testing.T ) {
208
165
c := getCommand (MaximumNArgs (2 ), true )
209
166
_ , err := executeCommand (c , "a" , "b" , "c" )
210
- validWithInvalidArgs (err , t )
167
+ expectError (err , t , "valid" )
211
168
}
212
169
213
170
// ExactArgs
@@ -227,25 +184,25 @@ func TestExactArgsWithValid(t *testing.T) {
227
184
func TestExactArgsWithValidWithInvalidArgs (t * testing.T ) {
228
185
c := getCommand (ExactArgs (3 ), true )
229
186
_ , err := executeCommand (c , "three" , "a" , "two" )
230
- validWithInvalidArgs (err , t )
187
+ expectError (err , t , "valid" )
231
188
}
232
189
233
190
func TestExactArgsWithInvalidCount (t * testing.T ) {
234
191
c := getCommand (ExactArgs (2 ), false )
235
192
_ , err := executeCommand (c , "a" , "b" , "c" )
236
- exactArgsWithInvalidCount (err , t )
193
+ expectError (err , t , "exact" )
237
194
}
238
195
239
196
func TestExactArgsWithInvalidCountWithValid (t * testing.T ) {
240
197
c := getCommand (ExactArgs (2 ), true )
241
198
_ , err := executeCommand (c , "three" , "one" , "two" )
242
- exactArgsWithInvalidCount (err , t )
199
+ expectError (err , t , "exact" )
243
200
}
244
201
245
202
func TestExactArgsWithInvalidCountWithValidWithInvalidArgs (t * testing.T ) {
246
203
c := getCommand (ExactArgs (2 ), true )
247
204
_ , err := executeCommand (c , "three" , "a" , "two" )
248
- validWithInvalidArgs (err , t )
205
+ expectError (err , t , "valid" )
249
206
}
250
207
251
208
// RangeArgs
@@ -265,25 +222,25 @@ func TestRangeArgsWithValid(t *testing.T) {
265
222
func TestRangeArgsWithValidWithInvalidArgs (t * testing.T ) {
266
223
c := getCommand (RangeArgs (2 , 4 ), true )
267
224
_ , err := executeCommand (c , "three" , "a" , "two" )
268
- validWithInvalidArgs (err , t )
225
+ expectError (err , t , "valid" )
269
226
}
270
227
271
228
func TestRangeArgsWithInvalidCount (t * testing.T ) {
272
229
c := getCommand (RangeArgs (2 , 4 ), false )
273
230
_ , err := executeCommand (c , "a" )
274
- rangeArgsWithInvalidCount (err , t )
231
+ expectError (err , t , "range" )
275
232
}
276
233
277
234
func TestRangeArgsWithInvalidCountWithValid (t * testing.T ) {
278
235
c := getCommand (RangeArgs (2 , 4 ), true )
279
236
_ , err := executeCommand (c , "two" )
280
- rangeArgsWithInvalidCount (err , t )
237
+ expectError (err , t , "range" )
281
238
}
282
239
283
240
func TestRangeArgsWithInvalidCountWithValidWithInvalidArgs (t * testing.T ) {
284
241
c := getCommand (RangeArgs (2 , 4 ), true )
285
242
_ , err := executeCommand (c , "a" )
286
- validWithInvalidArgs (err , t )
243
+ expectError (err , t , "valid" )
287
244
}
288
245
289
246
// Takes(No)Args
0 commit comments