@cryptotaxi247 / kubo / commits / 93f253e00

parse_test: add testFail() to simplify tests

License: MIT Signed-off-by: Christian Couder <chriscool@tuxfamily.org>

Christian Couder committed May 3, 2015 at 20:18 UTC 93f253e00b11921f593f21b001acd5f4c597d005
1 file changed +14 -32
commands/cli/parse_test.go
+14 -32
@@ -173,38 +173,26 @@ func TestArgumentParsing(t *testing.T) {
173 }
174 }
175
176 - test([]string{"noarg"}, nil, []string{})
177 -
178 - _, _, _, err := Parse([]string{"noarg", "value!"}, nil, rootCmd)
179 - if err == nil {
180 - t.Error("Should have failed (provided an arg, but command didn't define any)")
176 + testFail := func(cmd words, msg string) {
177 + _, _, _, err := Parse(cmd, nil, rootCmd)
178 + if err == nil {
179 + t.Errorf("Should have failed: %v", msg)
180 + }
181 }
182
183 - test([]string{"onearg", "value!"}, nil, []string{"value!"})
183 + test([]string{"noarg"}, nil, []string{})
184 + testFail([]string{"noarg", "value!"}, "provided an arg, but command didn't define any")
185
185 - _, _, _, err = Parse([]string{"onearg"}, nil, rootCmd)
186 - if err == nil {
187 - t.Error("Should have failed (didn't provide any args, arg is required)")
188 - }
186 + test([]string{"onearg", "value!"}, nil, []string{"value!"})
187 + testFail([]string{"onearg"}, "didn't provide any args, arg is required")
188
189 test([]string{"twoargs", "value1", "value2"}, nil, []string{"value1", "value2"})
191 -
192 - _, _, _, err = Parse([]string{"twoargs", "value!"}, nil, rootCmd)
193 - if err == nil {
194 - t.Error("Should have failed (only provided 1 arg, needs 2)")
195 - }
196 - _, _, _, err = Parse([]string{"twoargs"}, nil, rootCmd)
197 - if err == nil {
198 - t.Error("Should have failed (didn't provide any args, 2 required)")
199 - }
190 + testFail([]string{"twoargs", "value!"}, "only provided 1 arg, needs 2")
191 + testFail([]string{"twoargs"}, "didn't provide any args, 2 required")
192
193 test([]string{"variadic", "value!"}, nil, []string{"value!"})
194 test([]string{"variadic", "value1", "value2", "value3"}, nil, []string{"value1", "value2", "value3"})
203 -
204 - _, _, _, err = Parse([]string{"variadic"}, nil, rootCmd)
205 - if err == nil {
206 - t.Error("Should have failed (didn't provide any args, 1 required)")
207 - }
195 + testFail([]string{"variadic"}, "didn't provide any args, 1 required")
196
197 test([]string{"optional", "value!"}, nil, []string{"value!"})
198 test([]string{"optional"}, nil, []string{})
@@ -212,14 +200,8 @@ func TestArgumentParsing(t *testing.T) {
200 test([]string{"reversedoptional", "value1", "value2"}, nil, []string{"value1", "value2"})
201 test([]string{"reversedoptional", "value!"}, nil, []string{"value!"})
202
215 - _, _, _, err = Parse([]string{"reversedoptional"}, nil, rootCmd)
216 - if err == nil {
217 - t.Error("Should have failed (didn't provide any args, 1 required)")
218 - }
219 - _, _, _, err = Parse([]string{"reversedoptional", "value1", "value2", "value3"}, nil, rootCmd)
220 - if err == nil {
221 - t.Error("Should have failed (provided too many args, only takes 1)")
222 - }
203 + testFail([]string{"reversedoptional"}, "didn't provide any args, 1 required")
204 + testFail([]string{"reversedoptional", "value1", "value2", "value3"}, "provided too many args, only takes 1")
205
206 // Use a temp file to simulate stdin
207 fileToSimulateStdin := func(t *testing.T, content string) (*os.File) {