@@ -5,6 +5,18 @@ import (
5
5
"testing"
6
6
)
7
7
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
+
8
20
func expectSuccess (output string , err error , t * testing.T ) {
9
21
if output != "" {
10
22
t .Errorf ("Unexpected output: %v" , output )
@@ -15,13 +27,13 @@ func expectSuccess(output string, err error, t *testing.T) {
15
27
}
16
28
17
29
func TestNoArgs (t * testing.T ) {
18
- c := & Command { Use : "c" , Args : NoArgs , Run : emptyRun }
30
+ c := getCommand ( NoArgs , false )
19
31
output , err := executeCommand (c )
20
32
expectSuccess (output , err , t )
21
33
}
22
34
23
35
func TestNoArgsWithArgs (t * testing.T ) {
24
- c := & Command { Use : "c" , Args : NoArgs , Run : emptyRun }
36
+ c := getCommand ( NoArgs , false )
25
37
26
38
_ , err := executeCommand (c , "illegal" )
27
39
if err == nil {
@@ -36,51 +48,41 @@ func TestNoArgsWithArgs(t *testing.T) {
36
48
}
37
49
38
50
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 )
45
52
46
53
output , err := executeCommand (c , "one" , "two" )
47
54
expectSuccess (output , err , t )
48
55
}
49
56
50
57
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 )
57
59
58
- _ , err := executeCommand (c , "three " )
60
+ _ , err := executeCommand (c , "a " )
59
61
if err == nil {
60
62
t .Fatal ("Expected an error" )
61
63
}
62
64
63
65
got := err .Error ()
64
- expected := `invalid argument "three " for "c"`
66
+ expected := `invalid argument "a " for "c"`
65
67
if got != expected {
66
68
t .Errorf ("Expected: %q, got: %q" , expected , got )
67
69
}
68
70
}
69
71
70
72
func TestArbitraryArgs (t * testing.T ) {
71
- c := & Command { Use : "c" , Args : ArbitraryArgs , Run : emptyRun }
73
+ c := getCommand ( ArbitraryArgs , false )
72
74
output , err := executeCommand (c , "a" , "b" )
73
75
expectSuccess (output , err , t )
74
76
}
75
77
76
78
func TestMinimumNArgs (t * testing.T ) {
77
- c := & Command { Use : "c" , Args : MinimumNArgs (2 ), Run : emptyRun }
79
+ c := getCommand ( MinimumNArgs (2 ), false )
78
80
output , err := executeCommand (c , "a" , "b" , "c" )
79
81
expectSuccess (output , err , t )
80
82
}
81
83
82
84
func TestMinimumNArgsWithLessArgs (t * testing.T ) {
83
- c := & Command { Use : "c" , Args : MinimumNArgs (2 ), Run : emptyRun }
85
+ c := getCommand ( MinimumNArgs (2 ), false )
84
86
_ , err := executeCommand (c , "a" )
85
87
86
88
if err == nil {
@@ -95,13 +97,13 @@ func TestMinimumNArgsWithLessArgs(t *testing.T) {
95
97
}
96
98
97
99
func TestMaximumNArgs (t * testing.T ) {
98
- c := & Command { Use : "c" , Args : MaximumNArgs (3 ), Run : emptyRun }
100
+ c := getCommand ( MaximumNArgs (3 ), false )
99
101
output , err := executeCommand (c , "a" , "b" )
100
102
expectSuccess (output , err , t )
101
103
}
102
104
103
105
func TestMaximumNArgsWithMoreArgs (t * testing.T ) {
104
- c := & Command { Use : "c" , Args : MaximumNArgs (2 ), Run : emptyRun }
106
+ c := getCommand ( MaximumNArgs (2 ), false )
105
107
_ , err := executeCommand (c , "a" , "b" , "c" )
106
108
107
109
if err == nil {
@@ -116,13 +118,13 @@ func TestMaximumNArgsWithMoreArgs(t *testing.T) {
116
118
}
117
119
118
120
func TestExactArgs (t * testing.T ) {
119
- c := & Command { Use : "c" , Args : ExactArgs (3 ), Run : emptyRun }
121
+ c := getCommand ( ExactArgs (3 ), false )
120
122
output , err := executeCommand (c , "a" , "b" , "c" )
121
123
expectSuccess (output , err , t )
122
124
}
123
125
124
126
func TestExactArgsWithInvalidCount (t * testing.T ) {
125
- c := & Command { Use : "c" , Args : ExactArgs (2 ), Run : emptyRun }
127
+ c := getCommand ( ExactArgs (2 ), false )
126
128
_ , err := executeCommand (c , "a" , "b" , "c" )
127
129
128
130
if err == nil {
@@ -137,14 +139,14 @@ func TestExactArgsWithInvalidCount(t *testing.T) {
137
139
}
138
140
139
141
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 " )
142
144
expectSuccess (output , err , t )
143
145
}
144
146
145
147
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 " )
148
150
149
151
if err == nil {
150
152
t .Fatal ("Expected an error" )
@@ -158,33 +160,28 @@ func TestExactValidArgsWithInvalidCount(t *testing.T) {
158
160
}
159
161
160
162
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 )
167
164
168
- _ , err := executeCommand (c , "three " )
165
+ _ , err := executeCommand (c , "a " )
169
166
if err == nil {
170
167
t .Fatal ("Expected an error" )
171
168
}
172
169
173
170
got := err .Error ()
174
- expected := `invalid argument "three " for "c"`
171
+ expected := `invalid argument "a " for "c"`
175
172
if got != expected {
176
173
t .Errorf ("Expected: %q, got: %q" , expected , got )
177
174
}
178
175
}
179
176
180
177
func TestRangeArgs (t * testing.T ) {
181
- c := & Command { Use : "c" , Args : RangeArgs (2 , 4 ), Run : emptyRun }
178
+ c := getCommand ( RangeArgs (2 , 4 ), false )
182
179
output , err := executeCommand (c , "a" , "b" , "c" )
183
180
expectSuccess (output , err , t )
184
181
}
185
182
186
183
func TestRangeArgsWithInvalidCount (t * testing.T ) {
187
- c := & Command { Use : "c" , Args : RangeArgs (2 , 4 ), Run : emptyRun }
184
+ c := getCommand ( RangeArgs (2 , 4 ), false )
188
185
_ , err := executeCommand (c , "a" )
189
186
190
187
if err == nil {
0 commit comments