@@ -26,49 +26,96 @@ func expectSuccess(output string, err error, t *testing.T) {
26
26
}
27
27
}
28
28
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
+ }
33
38
}
34
39
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 ) {
39
41
if err == nil {
40
42
t .Fatal ("Expected an error" )
41
43
}
42
-
43
44
got := err .Error ()
44
45
expected := `unknown command "illegal" for "c"`
45
46
if got != expected {
46
47
t .Errorf ("Expected: %q, got: %q" , expected , got )
47
48
}
48
49
}
49
50
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
+ }
55
60
}
56
61
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
+ }
59
72
60
- _ , err := executeCommand ( c , "a" )
73
+ func exactArgsWithInvalidCount ( err error , t * testing. T ) {
61
74
if err == nil {
62
75
t .Fatal ("Expected an error" )
63
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 )
81
+ }
82
+ }
64
83
84
+ func rangeArgsWithInvalidCount (err error , t * testing.T ) {
85
+ if err == nil {
86
+ t .Fatal ("Expected an error" )
87
+ }
65
88
got := err .Error ()
66
- expected := `invalid argument "a" for "c"`
89
+ expected := "accepts between 2 and 4 arg(s), received 1"
67
90
if got != expected {
68
- t .Errorf ("Expected: %q, got: %q" , expected , got )
91
+ t .Fatalf ("Expected %q, got %q" , expected , got )
69
92
}
70
93
}
71
94
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
+
72
119
func TestArbitraryArgs (t * testing.T ) {
73
120
c := getCommand (ArbitraryArgs , false )
74
121
output , err := executeCommand (c , "a" , "b" )
@@ -84,16 +131,7 @@ func TestMinimumNArgs(t *testing.T) {
84
131
func TestMinimumNArgsWithLessArgs (t * testing.T ) {
85
132
c := getCommand (MinimumNArgs (2 ), false )
86
133
_ , 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 )
97
135
}
98
136
99
137
func TestMaximumNArgs (t * testing.T ) {
@@ -105,16 +143,7 @@ func TestMaximumNArgs(t *testing.T) {
105
143
func TestMaximumNArgsWithMoreArgs (t * testing.T ) {
106
144
c := getCommand (MaximumNArgs (2 ), false )
107
145
_ , 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 )
118
147
}
119
148
120
149
func TestExactArgs (t * testing.T ) {
@@ -126,52 +155,25 @@ func TestExactArgs(t *testing.T) {
126
155
func TestExactArgsWithInvalidCount (t * testing.T ) {
127
156
c := getCommand (ExactArgs (2 ), false )
128
157
_ , 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 )
139
159
}
140
160
141
161
func TestExactValidArgs (t * testing.T ) {
142
162
c := getCommand (ExactValidArgs (3 ), true )
143
- output , err := executeCommand (c , "two " , "three " , "one " )
163
+ output , err := executeCommand (c , "three " , "one " , "two " )
144
164
expectSuccess (output , err , t )
145
165
}
146
166
147
167
func TestExactValidArgsWithInvalidCount (t * testing.T ) {
148
168
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 )
160
171
}
161
172
162
173
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 )
175
177
}
176
178
177
179
func TestRangeArgs (t * testing.T ) {
@@ -183,16 +185,7 @@ func TestRangeArgs(t *testing.T) {
183
185
func TestRangeArgsWithInvalidCount (t * testing.T ) {
184
186
c := getCommand (RangeArgs (2 , 4 ), false )
185
187
_ , 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 )
196
189
}
197
190
198
191
func TestRootTakesNoArgs (t * testing.T ) {
0 commit comments