Skip to content

Commit f872d88

Browse files
committed
add expectError helper function to args_test
1 parent 5576bd3 commit f872d88

File tree

1 file changed

+32
-75
lines changed

1 file changed

+32
-75
lines changed

args_test.go

Lines changed: 32 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -26,74 +26,31 @@ func expectSuccess(output string, err error, t *testing.T) {
2626
}
2727
}
2828

29-
func validWithInvalidArgs(err error, t *testing.T) {
29+
func expectError(err error, t *testing.T, ex string) {
3030
if err == nil {
3131
t.Fatal("Expected an error")
3232
}
33-
3433
got := err.Error()
34+
3535
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"
3847
}
39-
}
4048

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"`
4749
if got != expected {
4850
t.Errorf("Expected: %q, got: %q", expected, got)
4951
}
5052
}
5153

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-
9754
// NoArgs
9855

9956
func TestNoArgs(t *testing.T) {
@@ -105,13 +62,13 @@ func TestNoArgs(t *testing.T) {
10562
func TestNoArgsWithArgs(t *testing.T) {
10663
c := getCommand(NoArgs, false)
10764
_, err := executeCommand(c, "one")
108-
noArgsWithArgs(err, t)
65+
expectError(err, t, "no")
10966
}
11067

11168
func TestNoArgsWithArgsWithValid(t *testing.T) {
11269
c := getCommand(NoArgs, true)
11370
_, err := executeCommand(c, "one")
114-
noArgsWithArgs(err, t)
71+
expectError(err, t, "no")
11572
}
11673

11774
// ArbitraryArgs
@@ -131,7 +88,7 @@ func TestArbitraryArgsWithValid(t *testing.T) {
13188
func TestArbitraryArgsWithValidWithInvalidArgs(t *testing.T) {
13289
c := getCommand(ArbitraryArgs, true)
13390
_, err := executeCommand(c, "a")
134-
validWithInvalidArgs(err, t)
91+
expectError(err, t, "valid")
13592
}
13693

13794
// MinimumNArgs
@@ -151,25 +108,25 @@ func TestMinimumNArgsWithValid(t *testing.T) {
151108
func TestMinimumNArgsWithValidWithInvalidArgs(t *testing.T) {
152109
c := getCommand(MinimumNArgs(2), true)
153110
_, err := executeCommand(c, "a", "b")
154-
validWithInvalidArgs(err, t)
111+
expectError(err, t, "valid")
155112
}
156113

157114
func TestMinimumNArgsWithLessArgs(t *testing.T) {
158115
c := getCommand(MinimumNArgs(2), false)
159116
_, err := executeCommand(c, "a")
160-
minimumNArgsWithLessArgs(err, t)
117+
expectError(err, t, "min")
161118
}
162119

163120
func TestMinimumNArgsWithLessArgsWithValid(t *testing.T) {
164121
c := getCommand(MinimumNArgs(2), true)
165122
_, err := executeCommand(c, "one")
166-
minimumNArgsWithLessArgs(err, t)
123+
expectError(err, t, "min")
167124
}
168125

169126
func TestMinimumNArgsWithLessArgsWithValidWithInvalidArgs(t *testing.T) {
170127
c := getCommand(MinimumNArgs(2), true)
171128
_, err := executeCommand(c, "a")
172-
validWithInvalidArgs(err, t)
129+
expectError(err, t, "valid")
173130
}
174131

175132
// MaximumNArgs
@@ -189,25 +146,25 @@ func TestMaximumNArgsWithValid(t *testing.T) {
189146
func TestMaximumNArgsWithValidWithInvalidArgs(t *testing.T) {
190147
c := getCommand(MaximumNArgs(2), true)
191148
_, err := executeCommand(c, "a", "b")
192-
validWithInvalidArgs(err, t)
149+
expectError(err, t, "valid")
193150
}
194151

195152
func TestMaximumNArgsWithMoreArgs(t *testing.T) {
196153
c := getCommand(MaximumNArgs(2), false)
197154
_, err := executeCommand(c, "a", "b", "c")
198-
maximumNArgsWithMoreArgs(err, t)
155+
expectError(err, t, "max")
199156
}
200157

201158
func TestMaximumNArgsWithMoreArgsWithValid(t *testing.T) {
202159
c := getCommand(MaximumNArgs(2), true)
203160
_, err := executeCommand(c, "one", "three", "two")
204-
maximumNArgsWithMoreArgs(err, t)
161+
expectError(err, t, "max")
205162
}
206163

207164
func TestMaximumNArgsWithMoreArgsWithValidWithInvalidArgs(t *testing.T) {
208165
c := getCommand(MaximumNArgs(2), true)
209166
_, err := executeCommand(c, "a", "b", "c")
210-
validWithInvalidArgs(err, t)
167+
expectError(err, t, "valid")
211168
}
212169

213170
// ExactArgs
@@ -227,25 +184,25 @@ func TestExactArgsWithValid(t *testing.T) {
227184
func TestExactArgsWithValidWithInvalidArgs(t *testing.T) {
228185
c := getCommand(ExactArgs(3), true)
229186
_, err := executeCommand(c, "three", "a", "two")
230-
validWithInvalidArgs(err, t)
187+
expectError(err, t, "valid")
231188
}
232189

233190
func TestExactArgsWithInvalidCount(t *testing.T) {
234191
c := getCommand(ExactArgs(2), false)
235192
_, err := executeCommand(c, "a", "b", "c")
236-
exactArgsWithInvalidCount(err, t)
193+
expectError(err, t, "exact")
237194
}
238195

239196
func TestExactArgsWithInvalidCountWithValid(t *testing.T) {
240197
c := getCommand(ExactArgs(2), true)
241198
_, err := executeCommand(c, "three", "one", "two")
242-
exactArgsWithInvalidCount(err, t)
199+
expectError(err, t, "exact")
243200
}
244201

245202
func TestExactArgsWithInvalidCountWithValidWithInvalidArgs(t *testing.T) {
246203
c := getCommand(ExactArgs(2), true)
247204
_, err := executeCommand(c, "three", "a", "two")
248-
validWithInvalidArgs(err, t)
205+
expectError(err, t, "valid")
249206
}
250207

251208
// RangeArgs
@@ -265,25 +222,25 @@ func TestRangeArgsWithValid(t *testing.T) {
265222
func TestRangeArgsWithValidWithInvalidArgs(t *testing.T) {
266223
c := getCommand(RangeArgs(2, 4), true)
267224
_, err := executeCommand(c, "three", "a", "two")
268-
validWithInvalidArgs(err, t)
225+
expectError(err, t, "valid")
269226
}
270227

271228
func TestRangeArgsWithInvalidCount(t *testing.T) {
272229
c := getCommand(RangeArgs(2, 4), false)
273230
_, err := executeCommand(c, "a")
274-
rangeArgsWithInvalidCount(err, t)
231+
expectError(err, t, "range")
275232
}
276233

277234
func TestRangeArgsWithInvalidCountWithValid(t *testing.T) {
278235
c := getCommand(RangeArgs(2, 4), true)
279236
_, err := executeCommand(c, "two")
280-
rangeArgsWithInvalidCount(err, t)
237+
expectError(err, t, "range")
281238
}
282239

283240
func TestRangeArgsWithInvalidCountWithValidWithInvalidArgs(t *testing.T) {
284241
c := getCommand(RangeArgs(2, 4), true)
285242
_, err := executeCommand(c, "a")
286-
validWithInvalidArgs(err, t)
243+
expectError(err, t, "valid")
287244
}
288245

289246
// Takes(No)Args

0 commit comments

Comments
 (0)