@@ -5,229 +5,187 @@ import (
55 "testing"
66)
77
8- func TestNoArgs (t * testing.T ) {
9- c := & Command {Use : "c" , Args : NoArgs , Run : emptyRun }
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+ }
1019
11- output , err := executeCommand ( c )
20+ func expectSuccess ( output string , err error , t * testing. T ) {
1221 if output != "" {
13- t .Errorf ("Unexpected string : %v" , output )
22+ t .Errorf ("Unexpected output : %v" , output )
1423 }
1524 if err != nil {
1625 t .Fatalf ("Unexpected error: %v" , err )
1726 }
1827}
1928
20- func TestNoArgsWithArgs (t * testing.T ) {
21- c := & Command {Use : "c" , Args : NoArgs , Run : emptyRun }
22-
23- _ , err := executeCommand (c , "illegal" )
29+ func validWithInvalidArgs (err error , t * testing.T ) {
2430 if err == nil {
2531 t .Fatal ("Expected an error" )
2632 }
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+ }
38+ }
2739
40+ func noArgsWithArgs (err error , t * testing.T ) {
41+ if err == nil {
42+ t .Fatal ("Expected an error" )
43+ }
2844 got := err .Error ()
2945 expected := `unknown command "illegal" for "c"`
3046 if got != expected {
3147 t .Errorf ("Expected: %q, got: %q" , expected , got )
3248 }
3349}
3450
35- func TestOnlyValidArgs (t * testing.T ) {
36- c := & Command {
37- Use : "c" ,
38- Args : OnlyValidArgs ,
39- ValidArgs : []string {"one" , "two" },
40- Run : emptyRun ,
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 )
4159 }
60+ }
4261
43- output , err := executeCommand ( c , "one" , "two" )
44- if output != "" {
45- t .Errorf ( "Unexpected output: %v" , output )
62+ func maximumNArgsWithMoreArgs ( err error , t * testing. T ) {
63+ if err == nil {
64+ t .Fatal ( "Expected an error" )
4665 }
47- if err != nil {
48- t .Fatalf ("Unexpected error: %v" , err )
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 )
4970 }
5071}
5172
52- func TestOnlyValidArgsWithInvalidArgs (t * testing.T ) {
53- c := & Command {
54- Use : "c" ,
55- Args : OnlyValidArgs ,
56- ValidArgs : []string {"one" , "two" },
57- Run : emptyRun ,
73+ func exactArgsWithInvalidCount (err error , t * testing.T ) {
74+ if err == nil {
75+ t .Fatal ("Expected an error" )
76+ }
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 )
5881 }
82+ }
5983
60- _ , err := executeCommand ( c , "three" )
84+ func rangeArgsWithInvalidCount ( err error , t * testing. T ) {
6185 if err == nil {
6286 t .Fatal ("Expected an error" )
6387 }
64-
6588 got := err .Error ()
66- expected := `invalid argument "three" 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+
72119func TestArbitraryArgs (t * testing.T ) {
73- c := & Command { Use : "c" , Args : ArbitraryArgs , Run : emptyRun }
120+ c := getCommand ( ArbitraryArgs , false )
74121 output , err := executeCommand (c , "a" , "b" )
75- if output != "" {
76- t .Errorf ("Unexpected output: %v" , output )
77- }
78- if err != nil {
79- t .Errorf ("Unexpected error: %v" , err )
80- }
122+ expectSuccess (output , err , t )
81123}
82124
83125func TestMinimumNArgs (t * testing.T ) {
84- c := & Command { Use : "c" , Args : MinimumNArgs (2 ), Run : emptyRun }
126+ c := getCommand ( MinimumNArgs (2 ), false )
85127 output , err := executeCommand (c , "a" , "b" , "c" )
86- if output != "" {
87- t .Errorf ("Unexpected output: %v" , output )
88- }
89- if err != nil {
90- t .Errorf ("Unexpected error: %v" , err )
91- }
128+ expectSuccess (output , err , t )
92129}
93130
94131func TestMinimumNArgsWithLessArgs (t * testing.T ) {
95- c := & Command { Use : "c" , Args : MinimumNArgs (2 ), Run : emptyRun }
132+ c := getCommand ( MinimumNArgs (2 ), false )
96133 _ , err := executeCommand (c , "a" )
97-
98- if err == nil {
99- t .Fatal ("Expected an error" )
100- }
101-
102- got := err .Error ()
103- expected := "requires at least 2 arg(s), only received 1"
104- if got != expected {
105- t .Fatalf ("Expected %q, got %q" , expected , got )
106- }
134+ minimumNArgsWithLessArgs (err , t )
107135}
108136
109137func TestMaximumNArgs (t * testing.T ) {
110- c := & Command { Use : "c" , Args : MaximumNArgs (3 ), Run : emptyRun }
138+ c := getCommand ( MaximumNArgs (3 ), false )
111139 output , err := executeCommand (c , "a" , "b" )
112- if output != "" {
113- t .Errorf ("Unexpected output: %v" , output )
114- }
115- if err != nil {
116- t .Errorf ("Unexpected error: %v" , err )
117- }
140+ expectSuccess (output , err , t )
118141}
119142
120143func TestMaximumNArgsWithMoreArgs (t * testing.T ) {
121- c := & Command { Use : "c" , Args : MaximumNArgs (2 ), Run : emptyRun }
144+ c := getCommand ( MaximumNArgs (2 ), false )
122145 _ , err := executeCommand (c , "a" , "b" , "c" )
123-
124- if err == nil {
125- t .Fatal ("Expected an error" )
126- }
127-
128- got := err .Error ()
129- expected := "accepts at most 2 arg(s), received 3"
130- if got != expected {
131- t .Fatalf ("Expected %q, got %q" , expected , got )
132- }
146+ maximumNArgsWithMoreArgs (err , t )
133147}
134148
135149func TestExactArgs (t * testing.T ) {
136- c := & Command { Use : "c" , Args : ExactArgs (3 ), Run : emptyRun }
150+ c := getCommand ( ExactArgs (3 ), false )
137151 output , err := executeCommand (c , "a" , "b" , "c" )
138- if output != "" {
139- t .Errorf ("Unexpected output: %v" , output )
140- }
141- if err != nil {
142- t .Errorf ("Unexpected error: %v" , err )
143- }
152+ expectSuccess (output , err , t )
144153}
145154
146155func TestExactArgsWithInvalidCount (t * testing.T ) {
147- c := & Command { Use : "c" , Args : ExactArgs (2 ), Run : emptyRun }
156+ c := getCommand ( ExactArgs (2 ), false )
148157 _ , err := executeCommand (c , "a" , "b" , "c" )
149-
150- if err == nil {
151- t .Fatal ("Expected an error" )
152- }
153-
154- got := err .Error ()
155- expected := "accepts 2 arg(s), received 3"
156- if got != expected {
157- t .Fatalf ("Expected %q, got %q" , expected , got )
158- }
158+ exactArgsWithInvalidCount (err , t )
159159}
160160
161161func TestExactValidArgs (t * testing.T ) {
162- c := & Command {Use : "c" , Args : ExactValidArgs (3 ), ValidArgs : []string {"a" , "b" , "c" }, Run : emptyRun }
163- output , err := executeCommand (c , "a" , "b" , "c" )
164- if output != "" {
165- t .Errorf ("Unexpected output: %v" , output )
166- }
167- if err != nil {
168- t .Errorf ("Unexpected error: %v" , err )
169- }
162+ c := getCommand (ExactValidArgs (3 ), true )
163+ output , err := executeCommand (c , "three" , "one" , "two" )
164+ expectSuccess (output , err , t )
170165}
171166
172167func TestExactValidArgsWithInvalidCount (t * testing.T ) {
173- c := & Command {Use : "c" , Args : ExactValidArgs (2 ), Run : emptyRun }
174- _ , err := executeCommand (c , "a" , "b" , "c" )
175-
176- if err == nil {
177- t .Fatal ("Expected an error" )
178- }
179-
180- got := err .Error ()
181- expected := "accepts 2 arg(s), received 3"
182- if got != expected {
183- t .Fatalf ("Expected %q, got %q" , expected , got )
184- }
168+ c := getCommand (ExactValidArgs (2 ), false )
169+ _ , err := executeCommand (c , "three" , "one" , "two" )
170+ exactArgsWithInvalidCount (err , t )
185171}
186172
187173func TestExactValidArgsWithInvalidArgs (t * testing.T ) {
188- c := & Command {
189- Use : "c" ,
190- Args : ExactValidArgs (1 ),
191- ValidArgs : []string {"one" , "two" },
192- Run : emptyRun ,
193- }
194-
195- _ , err := executeCommand (c , "three" )
196- if err == nil {
197- t .Fatal ("Expected an error" )
198- }
199-
200- got := err .Error ()
201- expected := `invalid argument "three" for "c"`
202- if got != expected {
203- t .Errorf ("Expected: %q, got: %q" , expected , got )
204- }
174+ c := getCommand (ExactValidArgs (3 ), true )
175+ _ , err := executeCommand (c , "three" , "a" , "two" )
176+ validWithInvalidArgs (err , t )
205177}
206178
207179func TestRangeArgs (t * testing.T ) {
208- c := & Command { Use : "c" , Args : RangeArgs (2 , 4 ), Run : emptyRun }
180+ c := getCommand ( RangeArgs (2 , 4 ), false )
209181 output , err := executeCommand (c , "a" , "b" , "c" )
210- if output != "" {
211- t .Errorf ("Unexpected output: %v" , output )
212- }
213- if err != nil {
214- t .Errorf ("Unexpected error: %v" , err )
215- }
182+ expectSuccess (output , err , t )
216183}
217184
218185func TestRangeArgsWithInvalidCount (t * testing.T ) {
219- c := & Command { Use : "c" , Args : RangeArgs (2 , 4 ), Run : emptyRun }
186+ c := getCommand ( RangeArgs (2 , 4 ), false )
220187 _ , err := executeCommand (c , "a" )
221-
222- if err == nil {
223- t .Fatal ("Expected an error" )
224- }
225-
226- got := err .Error ()
227- expected := "accepts between 2 and 4 arg(s), received 1"
228- if got != expected {
229- t .Fatalf ("Expected %q, got %q" , expected , got )
230- }
188+ rangeArgsWithInvalidCount (err , t )
231189}
232190
233191func TestRootTakesNoArgs (t * testing.T ) {
0 commit comments