args_test: style

This commit is contained in:
umarcor 2019-03-19 20:30:05 +01:00
parent d4d0679f4b
commit d507a44e7a

View file

@ -6,7 +6,7 @@ import (
"testing" "testing"
) )
func getCommand(args PositionalArgs, withValid bool) *Command { func newCommand(args PositionalArgs, withValid bool) *Command {
c := &Command{ c := &Command{
Use: "c", Use: "c",
Args: args, Args: args,
@ -31,23 +31,15 @@ func expectError(err error, t *testing.T, ex string) {
if err == nil { if err == nil {
t.Fatal("Expected an error") t.Fatal("Expected an error")
} }
got := err.Error() expected := map[string]string{
"valid": `invalid argument "a" for "c"`,
expected := `invalid argument "a" for "c"` "no": `unknown command "one" for "c"`,
switch ex { "min": "requires at least 2 arg(s), only received 1",
case "no": "max": "accepts at most 2 arg(s), received 3",
expected = `unknown command "one" for "c"` "exact": "accepts 2 arg(s), received 3",
case "min": "range": "accepts between 2 and 4 arg(s), received 1",
expected = "requires at least 2 arg(s), only received 1" }[ex]
case "max": if got := err.Error(); got != expected {
expected = "accepts at most 2 arg(s), received 3"
case "exact":
expected = "accepts 2 arg(s), received 3"
case "range":
expected = "accepts between 2 and 4 arg(s), received 1"
}
if got != expected {
t.Errorf("Expected: %q, got: %q", expected, got) t.Errorf("Expected: %q, got: %q", expected, got)
} }
} }
@ -55,193 +47,163 @@ func expectError(err error, t *testing.T, ex string) {
// NoArgs // NoArgs
func TestNoArgs(t *testing.T) { func TestNoArgs(t *testing.T) {
c := getCommand(NoArgs, false) o, e := executeCommand(newCommand(NoArgs, false))
output, err := executeCommand(c) expectSuccess(o, e, t)
expectSuccess(output, err, t)
} }
func TestNoArgsWithArgs(t *testing.T) { func TestNoArgs_WithArgs(t *testing.T) {
c := getCommand(NoArgs, false) _, e := executeCommand(newCommand(NoArgs, false), "one")
_, err := executeCommand(c, "one") expectError(e, t, "no")
expectError(err, t, "no")
} }
func TestNoArgsWithArgsWithValid(t *testing.T) { func TestNoArgs_WithArgs_WithValid(t *testing.T) {
c := getCommand(NoArgs, true) _, e := executeCommand(newCommand(NoArgs, true), "one")
_, err := executeCommand(c, "one") expectError(e, t, "no")
expectError(err, t, "no")
} }
// ArbitraryArgs // ArbitraryArgs
func TestArbitraryArgs(t *testing.T) { func TestArbitraryArgs(t *testing.T) {
c := getCommand(ArbitraryArgs, false) o, e := executeCommand(newCommand(ArbitraryArgs, false), "a", "b")
output, err := executeCommand(c, "a", "b") expectSuccess(o, e, t)
expectSuccess(output, err, t)
} }
func TestArbitraryArgsWithValid(t *testing.T) { func TestArbitraryArgs_WithValid(t *testing.T) {
c := getCommand(ArbitraryArgs, true) o, e := executeCommand(newCommand(ArbitraryArgs, true), "one", "two")
output, err := executeCommand(c, "one", "two") expectSuccess(o, e, t)
expectSuccess(output, err, t)
} }
func TestArbitraryArgsWithValidWithInvalidArgs(t *testing.T) { func TestArbitraryArgs_WithValid_WithInvalidArgs(t *testing.T) {
c := getCommand(ArbitraryArgs, true) _, e := executeCommand(newCommand(ArbitraryArgs, true), "a")
_, err := executeCommand(c, "a") expectError(e, t, "valid")
expectError(err, t, "valid")
} }
// MinimumNArgs // MinimumNArgs
func TestMinimumNArgs(t *testing.T) { func TestMinimumNArgs(t *testing.T) {
c := getCommand(MinimumNArgs(2), false) o, e := executeCommand(newCommand(MinimumNArgs(2), false), "a", "b", "c")
output, err := executeCommand(c, "a", "b", "c") expectSuccess(o, e, t)
expectSuccess(output, err, t)
} }
func TestMinimumNArgsWithValid(t *testing.T) { func TestMinimumNArgs_WithValid(t *testing.T) {
c := getCommand(MinimumNArgs(2), true) o, e := executeCommand(newCommand(MinimumNArgs(2), true), "one", "three")
output, err := executeCommand(c, "one", "three") expectSuccess(o, e, t)
expectSuccess(output, err, t)
} }
func TestMinimumNArgsWithValidWithInvalidArgs(t *testing.T) { func TestMinimumNArgs_WithValid_WithInvalidArgs(t *testing.T) {
c := getCommand(MinimumNArgs(2), true) _, e := executeCommand(newCommand(MinimumNArgs(2), true), "a", "b")
_, err := executeCommand(c, "a", "b") expectError(e, t, "valid")
expectError(err, t, "valid")
} }
func TestMinimumNArgsWithLessArgs(t *testing.T) { func TestMinimumNArgs_WithLessArgs(t *testing.T) {
c := getCommand(MinimumNArgs(2), false) _, e := executeCommand(newCommand(MinimumNArgs(2), false), "a")
_, err := executeCommand(c, "a") expectError(e, t, "min")
expectError(err, t, "min")
} }
func TestMinimumNArgsWithLessArgsWithValid(t *testing.T) { func TestMinimumNArgs_WithValid_WithLessArgs(t *testing.T) {
c := getCommand(MinimumNArgs(2), true) _, e := executeCommand(newCommand(MinimumNArgs(2), true), "one")
_, err := executeCommand(c, "one") expectError(e, t, "min")
expectError(err, t, "min")
} }
func TestMinimumNArgsWithLessArgsWithValidWithInvalidArgs(t *testing.T) { func TestMinimumNArgs_WithValid_WithLessArgsWithInvalidArgs(t *testing.T) {
c := getCommand(MinimumNArgs(2), true) _, e := executeCommand(newCommand(MinimumNArgs(2), true), "a")
_, err := executeCommand(c, "a") expectError(e, t, "valid")
expectError(err, t, "valid")
} }
// MaximumNArgs // MaximumNArgs
func TestMaximumNArgs(t *testing.T) { func TestMaximumNArgs(t *testing.T) {
c := getCommand(MaximumNArgs(3), false) o, e := executeCommand(newCommand(MaximumNArgs(3), false), "a", "b")
output, err := executeCommand(c, "a", "b") expectSuccess(o, e, t)
expectSuccess(output, err, t)
} }
func TestMaximumNArgsWithValid(t *testing.T) { func TestMaximumNArgs_WithValid(t *testing.T) {
c := getCommand(MaximumNArgs(2), true) o, e := executeCommand(newCommand(MaximumNArgs(2), true), "one", "three")
output, err := executeCommand(c, "one", "three") expectSuccess(o, e, t)
expectSuccess(output, err, t)
} }
func TestMaximumNArgsWithValidWithInvalidArgs(t *testing.T) { func TestMaximumNArgs_WithValid_WithInvalidArgs(t *testing.T) {
c := getCommand(MaximumNArgs(2), true) _, e := executeCommand(newCommand(MaximumNArgs(2), true), "a", "b")
_, err := executeCommand(c, "a", "b") expectError(e, t, "valid")
expectError(err, t, "valid")
} }
func TestMaximumNArgsWithMoreArgs(t *testing.T) { func TestMaximumNArgs_WithMoreArgs(t *testing.T) {
c := getCommand(MaximumNArgs(2), false) _, e := executeCommand(newCommand(MaximumNArgs(2), false), "a", "b", "c")
_, err := executeCommand(c, "a", "b", "c") expectError(e, t, "max")
expectError(err, t, "max")
} }
func TestMaximumNArgsWithMoreArgsWithValid(t *testing.T) { func TestMaximumNArgs_WithValid_WithMoreArgs(t *testing.T) {
c := getCommand(MaximumNArgs(2), true) _, e := executeCommand(newCommand(MaximumNArgs(2), true), "one", "three", "two")
_, err := executeCommand(c, "one", "three", "two") expectError(e, t, "max")
expectError(err, t, "max")
} }
func TestMaximumNArgsWithMoreArgsWithValidWithInvalidArgs(t *testing.T) { func TestMaximumNArgs_WithValid_WithMoreArgsWithInvalidArgs(t *testing.T) {
c := getCommand(MaximumNArgs(2), true) _, e := executeCommand(newCommand(MaximumNArgs(2), true), "a", "b", "c")
_, err := executeCommand(c, "a", "b", "c") expectError(e, t, "valid")
expectError(err, t, "valid")
} }
// ExactArgs // ExactArgs
func TestExactArgs(t *testing.T) { func TestExactArgs(t *testing.T) {
c := getCommand(ExactArgs(3), false) o, e := executeCommand(newCommand(ExactArgs(3), false), "a", "b", "c")
output, err := executeCommand(c, "a", "b", "c") expectSuccess(o, e, t)
expectSuccess(output, err, t)
} }
func TestExactArgsWithValid(t *testing.T) { func TestExactArgs_WithValid(t *testing.T) {
c := getCommand(ExactArgs(3), true) o, e := executeCommand(newCommand(ExactArgs(3), true), "three", "one", "two")
output, err := executeCommand(c, "three", "one", "two") expectSuccess(o, e, t)
expectSuccess(output, err, t)
} }
func TestExactArgsWithValidWithInvalidArgs(t *testing.T) { func TestExactArgs_WithValid_WithInvalidArgs(t *testing.T) {
c := getCommand(ExactArgs(3), true) _, e := executeCommand(newCommand(ExactArgs(3), true), "three", "a", "two")
_, err := executeCommand(c, "three", "a", "two") expectError(e, t, "valid")
expectError(err, t, "valid")
} }
func TestExactArgsWithInvalidCount(t *testing.T) { func TestExactArgs_WithInvalidCount(t *testing.T) {
c := getCommand(ExactArgs(2), false) _, e := executeCommand(newCommand(ExactArgs(2), false), "a", "b", "c")
_, err := executeCommand(c, "a", "b", "c") expectError(e, t, "exact")
expectError(err, t, "exact")
} }
func TestExactArgsWithInvalidCountWithValid(t *testing.T) { func TestExactArgs_WithValid_WithInvalidCount(t *testing.T) {
c := getCommand(ExactArgs(2), true) _, e := executeCommand(newCommand(ExactArgs(2), true), "three", "one", "two")
_, err := executeCommand(c, "three", "one", "two") expectError(e, t, "exact")
expectError(err, t, "exact")
} }
func TestExactArgsWithInvalidCountWithValidWithInvalidArgs(t *testing.T) { func TestExactArgs_WithValid_WithInvalidCountWithInvalidArgs(t *testing.T) {
c := getCommand(ExactArgs(2), true) _, e := executeCommand(newCommand(ExactArgs(2), true), "three", "a", "two")
_, err := executeCommand(c, "three", "a", "two") expectError(e, t, "valid")
expectError(err, t, "valid")
} }
// RangeArgs // RangeArgs
func TestRangeArgs(t *testing.T) { func TestRangeArgs(t *testing.T) {
c := getCommand(RangeArgs(2, 4), false) o, e := executeCommand(newCommand(RangeArgs(2, 4), false), "a", "b", "c")
output, err := executeCommand(c, "a", "b", "c") expectSuccess(o, e, t)
expectSuccess(output, err, t)
} }
func TestRangeArgsWithValid(t *testing.T) { func TestRangeArgs_WithValid(t *testing.T) {
c := getCommand(RangeArgs(2, 4), true) o, e := executeCommand(newCommand(RangeArgs(2, 4), true), "three", "one", "two")
output, err := executeCommand(c, "three", "one", "two") expectSuccess(o, e, t)
expectSuccess(output, err, t)
} }
func TestRangeArgsWithValidWithInvalidArgs(t *testing.T) { func TestRangeArgs_WithValid_WithInvalidArgs(t *testing.T) {
c := getCommand(RangeArgs(2, 4), true) _, e := executeCommand(newCommand(RangeArgs(2, 4), true), "three", "a", "two")
_, err := executeCommand(c, "three", "a", "two") expectError(e, t, "valid")
expectError(err, t, "valid")
} }
func TestRangeArgsWithInvalidCount(t *testing.T) { func TestRangeArgs_WithInvalidCount(t *testing.T) {
c := getCommand(RangeArgs(2, 4), false) _, e := executeCommand(newCommand(RangeArgs(2, 4), false), "a")
_, err := executeCommand(c, "a") expectError(e, t, "range")
expectError(err, t, "range")
} }
func TestRangeArgsWithInvalidCountWithValid(t *testing.T) { func TestRangeArgs_WithValid_WithInvalidCount(t *testing.T) {
c := getCommand(RangeArgs(2, 4), true) _, e := executeCommand(newCommand(RangeArgs(2, 4), true), "two")
_, err := executeCommand(c, "two") expectError(e, t, "range")
expectError(err, t, "range")
} }
func TestRangeArgsWithInvalidCountWithValidWithInvalidArgs(t *testing.T) { func TestRangeArgs_WithValid_WithInvalidCountWithInvalidArgs(t *testing.T) {
c := getCommand(RangeArgs(2, 4), true) _, e := executeCommand(newCommand(RangeArgs(2, 4), true), "a")
_, err := executeCommand(c, "a") expectError(e, t, "valid")
expectError(err, t, "valid")
} }
// Takes(No)Args // Takes(No)Args
@ -352,87 +314,32 @@ func TestMatchAll(t *testing.T) {
// DEPRECATED // DEPRECATED
func TestOnlyValidArgs(t *testing.T) { func TestDEPRECATED_OnlyValidArgs(t *testing.T) {
c := &Command{ o, e := executeCommand(newCommand(OnlyValidArgs, true), "one", "two")
Use: "c", expectSuccess(o, e, t)
Args: OnlyValidArgs,
ValidArgs: []string{"one", "two"},
Run: emptyRun,
}
output, err := executeCommand(c, "one", "two")
if output != "" {
t.Errorf("Unexpected output: %v", output)
}
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
} }
func TestOnlyValidArgsWithInvalidArgs(t *testing.T) { func TestDEPRECATED_OnlyValidArgs_WithInvalidArgs(t *testing.T) {
c := &Command{ _, e := executeCommand(newCommand(OnlyValidArgs, true), "a")
Use: "c", expectError(e, t, "valid")
Args: OnlyValidArgs,
ValidArgs: []string{"one", "two"},
Run: emptyRun,
}
_, err := executeCommand(c, "three")
if err == nil {
t.Fatal("Expected an error")
}
got := err.Error()
expected := `invalid argument "three" for "c"`
if got != expected {
t.Errorf("Expected: %q, got: %q", expected, got)
}
} }
func TestExactValidArgs(t *testing.T) { func TestDEPRECATED_ExactValidArgs(t *testing.T) {
c := &Command{Use: "c", Args: ExactValidArgs(3), ValidArgs: []string{"a", "b", "c"}, Run: emptyRun} // Note that the order is not required to be the same:
output, err := executeCommand(c, "a", "b", "c") // Definition: "one", "two", "three"
if output != "" { // Execution: "two", "three", "one"
t.Errorf("Unexpected output: %v", output) o, e := executeCommand(newCommand(ExactValidArgs(3), true), "two", "three", "one")
} expectSuccess(o, e, t)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
} }
func TestExactValidArgsWithInvalidCount(t *testing.T) { func TestDEPRECATED_ExactValidArgs_WithInvalidCount(t *testing.T) {
c := &Command{Use: "c", Args: ExactValidArgs(2), Run: emptyRun} _, e := executeCommand(newCommand(ExactValidArgs(2), true), "two", "three", "one")
_, err := executeCommand(c, "a", "b", "c") expectError(e, t, "exact")
if err == nil {
t.Fatal("Expected an error")
}
got := err.Error()
expected := "accepts 2 arg(s), received 3"
if got != expected {
t.Fatalf("Expected %q, got %q", expected, got)
}
} }
func TestExactValidArgsWithInvalidArgs(t *testing.T) { func TestDEPRECATED_ExactValidArgs_WithInvalidArgs(t *testing.T) {
c := &Command{ _, e := executeCommand(newCommand(ExactValidArgs(2), true), "two", "a")
Use: "c", expectError(e, t, "valid")
Args: ExactValidArgs(1),
ValidArgs: []string{"one", "two"},
Run: emptyRun,
}
_, err := executeCommand(c, "three")
if err == nil {
t.Fatal("Expected an error")
}
got := err.Error()
expected := `invalid argument "three" for "c"`
if got != expected {
t.Errorf("Expected: %q, got: %q", expected, got)
}
} }
// This test make sure we keep backwards-compatibility with respect // This test make sure we keep backwards-compatibility with respect