Skip to content

Commit 9a89c0c

Browse files
committed
args_test: add additional helper functions (spf13#1426)
* noArgsWithArgs * validWithInvalidArgs * minimumNArgsWithLessArgs * maximumNArgsWithMoreArgs * exactArgsWithInvalidCount * rangeArgsWithInvalidCount
1 parent 7e08701 commit 9a89c0c

File tree

1 file changed

+76
-83
lines changed

1 file changed

+76
-83
lines changed

args_test.go

+76-83
Original file line numberDiff line numberDiff line change
@@ -26,49 +26,96 @@ func expectSuccess(output string, err error, t *testing.T) {
2626
}
2727
}
2828

29-
func TestNoArgs(t *testing.T) {
30-
c := getCommand(NoArgs, false)
31-
output, err := executeCommand(c)
32-
expectSuccess(output, err, t)
29+
func validWithInvalidArgs(err error, t *testing.T) {
30+
if err == nil {
31+
t.Fatal("Expected an error")
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+
}
3338
}
3439

35-
func TestNoArgsWithArgs(t *testing.T) {
36-
c := getCommand(NoArgs, false)
37-
38-
_, err := executeCommand(c, "illegal")
40+
func noArgsWithArgs(err error, t *testing.T) {
3941
if err == nil {
4042
t.Fatal("Expected an error")
4143
}
42-
4344
got := err.Error()
4445
expected := `unknown command "illegal" for "c"`
4546
if got != expected {
4647
t.Errorf("Expected: %q, got: %q", expected, got)
4748
}
4849
}
4950

50-
func TestOnlyValidArgs(t *testing.T) {
51-
c := getCommand(OnlyValidArgs, true)
52-
53-
output, err := executeCommand(c, "one", "two")
54-
expectSuccess(output, err, t)
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)
59+
}
5560
}
5661

57-
func TestOnlyValidArgsWithInvalidArgs(t *testing.T) {
58-
c := getCommand(OnlyValidArgs, true)
62+
func maximumNArgsWithMoreArgs(err error, t *testing.T) {
63+
if err == nil {
64+
t.Fatal("Expected an error")
65+
}
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)
70+
}
71+
}
5972

60-
_, err := executeCommand(c, "a")
73+
func exactArgsWithInvalidCount(err error, t *testing.T) {
6174
if err == nil {
6275
t.Fatal("Expected an error")
6376
}
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)
81+
}
82+
}
6483

84+
func rangeArgsWithInvalidCount(err error, t *testing.T) {
85+
if err == nil {
86+
t.Fatal("Expected an error")
87+
}
6588
got := err.Error()
66-
expected := `invalid argument "a" for "c"`
89+
expected := "accepts between 2 and 4 arg(s), received 1"
6790
if got != expected {
68-
t.Errorf("Expected: %q, got: %q", expected, got)
91+
t.Fatalf("Expected %q, got %q", expected, got)
6992
}
7093
}
7194

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+
72119
func TestArbitraryArgs(t *testing.T) {
73120
c := getCommand(ArbitraryArgs, false)
74121
output, err := executeCommand(c, "a", "b")
@@ -84,16 +131,7 @@ func TestMinimumNArgs(t *testing.T) {
84131
func TestMinimumNArgsWithLessArgs(t *testing.T) {
85132
c := getCommand(MinimumNArgs(2), false)
86133
_, err := executeCommand(c, "a")
87-
88-
if err == nil {
89-
t.Fatal("Expected an error")
90-
}
91-
92-
got := err.Error()
93-
expected := "requires at least 2 arg(s), only received 1"
94-
if got != expected {
95-
t.Fatalf("Expected %q, got %q", expected, got)
96-
}
134+
minimumNArgsWithLessArgs(err, t)
97135
}
98136

99137
func TestMaximumNArgs(t *testing.T) {
@@ -105,16 +143,7 @@ func TestMaximumNArgs(t *testing.T) {
105143
func TestMaximumNArgsWithMoreArgs(t *testing.T) {
106144
c := getCommand(MaximumNArgs(2), false)
107145
_, err := executeCommand(c, "a", "b", "c")
108-
109-
if err == nil {
110-
t.Fatal("Expected an error")
111-
}
112-
113-
got := err.Error()
114-
expected := "accepts at most 2 arg(s), received 3"
115-
if got != expected {
116-
t.Fatalf("Expected %q, got %q", expected, got)
117-
}
146+
maximumNArgsWithMoreArgs(err, t)
118147
}
119148

120149
func TestExactArgs(t *testing.T) {
@@ -126,52 +155,25 @@ func TestExactArgs(t *testing.T) {
126155
func TestExactArgsWithInvalidCount(t *testing.T) {
127156
c := getCommand(ExactArgs(2), false)
128157
_, err := executeCommand(c, "a", "b", "c")
129-
130-
if err == nil {
131-
t.Fatal("Expected an error")
132-
}
133-
134-
got := err.Error()
135-
expected := "accepts 2 arg(s), received 3"
136-
if got != expected {
137-
t.Fatalf("Expected %q, got %q", expected, got)
138-
}
158+
exactArgsWithInvalidCount(err, t)
139159
}
140160

141161
func TestExactValidArgs(t *testing.T) {
142162
c := getCommand(ExactValidArgs(3), true)
143-
output, err := executeCommand(c, "two", "three", "one")
163+
output, err := executeCommand(c, "three", "one", "two")
144164
expectSuccess(output, err, t)
145165
}
146166

147167
func TestExactValidArgsWithInvalidCount(t *testing.T) {
148168
c := getCommand(ExactValidArgs(2), false)
149-
_, err := executeCommand(c, "two", "three", "one")
150-
151-
if err == nil {
152-
t.Fatal("Expected an error")
153-
}
154-
155-
got := err.Error()
156-
expected := "accepts 2 arg(s), received 3"
157-
if got != expected {
158-
t.Fatalf("Expected %q, got %q", expected, got)
159-
}
169+
_, err := executeCommand(c, "three", "one", "two")
170+
exactArgsWithInvalidCount(err, t)
160171
}
161172

162173
func TestExactValidArgsWithInvalidArgs(t *testing.T) {
163-
c := getCommand(ExactValidArgs(1), true)
164-
165-
_, err := executeCommand(c, "a")
166-
if err == nil {
167-
t.Fatal("Expected an error")
168-
}
169-
170-
got := err.Error()
171-
expected := `invalid argument "a" for "c"`
172-
if got != expected {
173-
t.Errorf("Expected: %q, got: %q", expected, got)
174-
}
174+
c := getCommand(ExactValidArgs(3), true)
175+
_, err := executeCommand(c, "three", "a", "two")
176+
validWithInvalidArgs(err, t)
175177
}
176178

177179
func TestRangeArgs(t *testing.T) {
@@ -183,16 +185,7 @@ func TestRangeArgs(t *testing.T) {
183185
func TestRangeArgsWithInvalidCount(t *testing.T) {
184186
c := getCommand(RangeArgs(2, 4), false)
185187
_, err := executeCommand(c, "a")
186-
187-
if err == nil {
188-
t.Fatal("Expected an error")
189-
}
190-
191-
got := err.Error()
192-
expected := "accepts between 2 and 4 arg(s), received 1"
193-
if got != expected {
194-
t.Fatalf("Expected %q, got %q", expected, got)
195-
}
188+
rangeArgsWithInvalidCount(err, t)
196189
}
197190

198191
func TestRootTakesNoArgs(t *testing.T) {

0 commit comments

Comments
 (0)