Skip to content

Commit 4293ff9

Browse files
committed
args_test: add helper function getCommand
1 parent 20a0e77 commit 4293ff9

File tree

1 file changed

+34
-37
lines changed

1 file changed

+34
-37
lines changed

args_test.go

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@ import (
55
"testing"
66
)
77

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+
}
19+
820
func expectSuccess(output string, err error, t *testing.T) {
921
if output != "" {
1022
t.Errorf("Unexpected output: %v", output)
@@ -15,13 +27,13 @@ func expectSuccess(output string, err error, t *testing.T) {
1527
}
1628

1729
func TestNoArgs(t *testing.T) {
18-
c := &Command{Use: "c", Args: NoArgs, Run: emptyRun}
30+
c := getCommand(NoArgs, false)
1931
output, err := executeCommand(c)
2032
expectSuccess(output, err, t)
2133
}
2234

2335
func TestNoArgsWithArgs(t *testing.T) {
24-
c := &Command{Use: "c", Args: NoArgs, Run: emptyRun}
36+
c := getCommand(NoArgs, false)
2537

2638
_, err := executeCommand(c, "illegal")
2739
if err == nil {
@@ -36,51 +48,41 @@ func TestNoArgsWithArgs(t *testing.T) {
3648
}
3749

3850
func TestOnlyValidArgs(t *testing.T) {
39-
c := &Command{
40-
Use: "c",
41-
Args: OnlyValidArgs,
42-
ValidArgs: []string{"one", "two"},
43-
Run: emptyRun,
44-
}
51+
c := getCommand(OnlyValidArgs, true)
4552

4653
output, err := executeCommand(c, "one", "two")
4754
expectSuccess(output, err, t)
4855
}
4956

5057
func TestOnlyValidArgsWithInvalidArgs(t *testing.T) {
51-
c := &Command{
52-
Use: "c",
53-
Args: OnlyValidArgs,
54-
ValidArgs: []string{"one", "two"},
55-
Run: emptyRun,
56-
}
58+
c := getCommand(OnlyValidArgs, true)
5759

58-
_, err := executeCommand(c, "three")
60+
_, err := executeCommand(c, "a")
5961
if err == nil {
6062
t.Fatal("Expected an error")
6163
}
6264

6365
got := err.Error()
64-
expected := `invalid argument "three" for "c"`
66+
expected := `invalid argument "a" for "c"`
6567
if got != expected {
6668
t.Errorf("Expected: %q, got: %q", expected, got)
6769
}
6870
}
6971

7072
func TestArbitraryArgs(t *testing.T) {
71-
c := &Command{Use: "c", Args: ArbitraryArgs, Run: emptyRun}
73+
c := getCommand(ArbitraryArgs, false)
7274
output, err := executeCommand(c, "a", "b")
7375
expectSuccess(output, err, t)
7476
}
7577

7678
func TestMinimumNArgs(t *testing.T) {
77-
c := &Command{Use: "c", Args: MinimumNArgs(2), Run: emptyRun}
79+
c := getCommand(MinimumNArgs(2), false)
7880
output, err := executeCommand(c, "a", "b", "c")
7981
expectSuccess(output, err, t)
8082
}
8183

8284
func TestMinimumNArgsWithLessArgs(t *testing.T) {
83-
c := &Command{Use: "c", Args: MinimumNArgs(2), Run: emptyRun}
85+
c := getCommand(MinimumNArgs(2), false)
8486
_, err := executeCommand(c, "a")
8587

8688
if err == nil {
@@ -95,13 +97,13 @@ func TestMinimumNArgsWithLessArgs(t *testing.T) {
9597
}
9698

9799
func TestMaximumNArgs(t *testing.T) {
98-
c := &Command{Use: "c", Args: MaximumNArgs(3), Run: emptyRun}
100+
c := getCommand(MaximumNArgs(3), false)
99101
output, err := executeCommand(c, "a", "b")
100102
expectSuccess(output, err, t)
101103
}
102104

103105
func TestMaximumNArgsWithMoreArgs(t *testing.T) {
104-
c := &Command{Use: "c", Args: MaximumNArgs(2), Run: emptyRun}
106+
c := getCommand(MaximumNArgs(2), false)
105107
_, err := executeCommand(c, "a", "b", "c")
106108

107109
if err == nil {
@@ -116,13 +118,13 @@ func TestMaximumNArgsWithMoreArgs(t *testing.T) {
116118
}
117119

118120
func TestExactArgs(t *testing.T) {
119-
c := &Command{Use: "c", Args: ExactArgs(3), Run: emptyRun}
121+
c := getCommand(ExactArgs(3), false)
120122
output, err := executeCommand(c, "a", "b", "c")
121123
expectSuccess(output, err, t)
122124
}
123125

124126
func TestExactArgsWithInvalidCount(t *testing.T) {
125-
c := &Command{Use: "c", Args: ExactArgs(2), Run: emptyRun}
127+
c := getCommand(ExactArgs(2), false)
126128
_, err := executeCommand(c, "a", "b", "c")
127129

128130
if err == nil {
@@ -137,14 +139,14 @@ func TestExactArgsWithInvalidCount(t *testing.T) {
137139
}
138140

139141
func TestExactValidArgs(t *testing.T) {
140-
c := &Command{Use: "c", Args: ExactValidArgs(3), ValidArgs: []string{"a", "b", "c"}, Run: emptyRun}
141-
output, err := executeCommand(c, "a", "b", "c")
142+
c := getCommand(ExactValidArgs(3), true)
143+
output, err := executeCommand(c, "two", "three", "one")
142144
expectSuccess(output, err, t)
143145
}
144146

145147
func TestExactValidArgsWithInvalidCount(t *testing.T) {
146-
c := &Command{Use: "c", Args: ExactValidArgs(2), Run: emptyRun}
147-
_, err := executeCommand(c, "a", "b", "c")
148+
c := getCommand(ExactValidArgs(2), false)
149+
_, err := executeCommand(c, "two", "three", "one")
148150

149151
if err == nil {
150152
t.Fatal("Expected an error")
@@ -158,33 +160,28 @@ func TestExactValidArgsWithInvalidCount(t *testing.T) {
158160
}
159161

160162
func TestExactValidArgsWithInvalidArgs(t *testing.T) {
161-
c := &Command{
162-
Use: "c",
163-
Args: ExactValidArgs(1),
164-
ValidArgs: []string{"one", "two"},
165-
Run: emptyRun,
166-
}
163+
c := getCommand(ExactValidArgs(1), true)
167164

168-
_, err := executeCommand(c, "three")
165+
_, err := executeCommand(c, "a")
169166
if err == nil {
170167
t.Fatal("Expected an error")
171168
}
172169

173170
got := err.Error()
174-
expected := `invalid argument "three" for "c"`
171+
expected := `invalid argument "a" for "c"`
175172
if got != expected {
176173
t.Errorf("Expected: %q, got: %q", expected, got)
177174
}
178175
}
179176

180177
func TestRangeArgs(t *testing.T) {
181-
c := &Command{Use: "c", Args: RangeArgs(2, 4), Run: emptyRun}
178+
c := getCommand(RangeArgs(2, 4), false)
182179
output, err := executeCommand(c, "a", "b", "c")
183180
expectSuccess(output, err, t)
184181
}
185182

186183
func TestRangeArgsWithInvalidCount(t *testing.T) {
187-
c := &Command{Use: "c", Args: RangeArgs(2, 4), Run: emptyRun}
184+
c := getCommand(RangeArgs(2, 4), false)
188185
_, err := executeCommand(c, "a")
189186

190187
if err == nil {

0 commit comments

Comments
 (0)