From 34e0cf567cb509f4bb82b27cd778a46b3c04c9d1 Mon Sep 17 00:00:00 2001 From: umarcor Date: Mon, 28 Mar 2022 02:22:38 +0200 Subject: [PATCH 1/3] args_test: add expectError helper function --- args_test.go | 136 +++++++++++++++++++-------------------------------- 1 file changed, 50 insertions(+), 86 deletions(-) diff --git a/args_test.go b/args_test.go index fd2c3b41..ee367d07 100644 --- a/args_test.go +++ b/args_test.go @@ -41,70 +41,34 @@ func expectSuccess(output string, err error, t *testing.T) { } } -func validOnlyWithInvalidArgs(err error, t *testing.T) { +func expectErrorWithArg(err error, t *testing.T, ex string, arg string) { if err == nil { t.Fatal("Expected an error") } - got := err.Error() - expected := `invalid argument "a" for "c"` - if got != expected { + + var expected string + switch ex { + case "validOnlyWithInvalidArgs": + expected = `invalid argument "a" for "c"` + case "noArgsWithArgs": + expected = `unknown command "` + arg + `" for "c"` + case "minimumNArgsWithLessArgs": + expected = "requires at least 2 arg(s), only received 1" + case "maximumNArgsWithMoreArgs": + expected = "accepts at most 2 arg(s), received 3" + case "exactArgsWithInvalidCount": + expected = "accepts 2 arg(s), received 3" + case "rangeArgsWithInvalidCount": + expected = "accepts between 2 and 4 arg(s), received 1" + } + + if got := err.Error(); got != expected { t.Errorf("Expected: %q, got: %q", expected, got) } } -func noArgsWithArgs(err error, t *testing.T, arg string) { - if err == nil { - t.Fatal("Expected an error") - } - got := err.Error() - expected := `unknown command "` + arg + `" for "c"` - if got != expected { - t.Errorf("Expected: %q, got: %q", expected, got) - } -} - -func minimumNArgsWithLessArgs(err error, t *testing.T) { - if err == nil { - t.Fatal("Expected an error") - } - got := err.Error() - expected := "requires at least 2 arg(s), only received 1" - if got != expected { - t.Fatalf("Expected %q, got %q", expected, got) - } -} - -func maximumNArgsWithMoreArgs(err error, t *testing.T) { - if err == nil { - t.Fatal("Expected an error") - } - got := err.Error() - expected := "accepts at most 2 arg(s), received 3" - if got != expected { - t.Fatalf("Expected %q, got %q", expected, got) - } -} - -func exactArgsWithInvalidCount(err error, t *testing.T) { - 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 rangeArgsWithInvalidCount(err error, t *testing.T) { - if err == nil { - t.Fatal("Expected an error") - } - got := err.Error() - expected := "accepts between 2 and 4 arg(s), received 1" - if got != expected { - t.Fatalf("Expected %q, got %q", expected, got) - } +func expectError(err error, t *testing.T, ex string) { + expectErrorWithArg(err, t, ex, "") } // NoArgs @@ -118,25 +82,25 @@ func TestNoArgs(t *testing.T) { func TestNoArgs_WithArgs(t *testing.T) { c := getCommand(NoArgs, false) _, err := executeCommand(c, "one") - noArgsWithArgs(err, t, "one") + expectErrorWithArg(err, t, "noArgsWithArgs", "one") } func TestNoArgs_WithValid_WithArgs(t *testing.T) { c := getCommand(NoArgs, true) _, err := executeCommand(c, "one") - noArgsWithArgs(err, t, "one") + expectErrorWithArg(err, t, "noArgsWithArgs", "one") } func TestNoArgs_WithValid_WithInvalidArgs(t *testing.T) { c := getCommand(NoArgs, true) _, err := executeCommand(c, "a") - noArgsWithArgs(err, t, "a") + expectErrorWithArg(err, t, "noArgsWithArgs", "a") } func TestNoArgs_WithValidOnly_WithInvalidArgs(t *testing.T) { c := getCommand(MatchAll(OnlyValidArgs, NoArgs), true) _, err := executeCommand(c, "a") - validOnlyWithInvalidArgs(err, t) + expectError(err, t, "validOnlyWithInvalidArgs") } // OnlyValidArgs @@ -150,7 +114,7 @@ func TestOnlyValidArgs(t *testing.T) { func TestOnlyValidArgs_WithInvalidArgs(t *testing.T) { c := getCommand(OnlyValidArgs, true) _, err := executeCommand(c, "a") - validOnlyWithInvalidArgs(err, t) + expectError(err, t, "validOnlyWithInvalidArgs") } // ArbitraryArgs @@ -176,7 +140,7 @@ func TestArbitraryArgs_WithValid_WithInvalidArgs(t *testing.T) { func TestArbitraryArgs_WithValidOnly_WithInvalidArgs(t *testing.T) { c := getCommand(MatchAll(OnlyValidArgs, ArbitraryArgs), true) _, err := executeCommand(c, "a") - validOnlyWithInvalidArgs(err, t) + expectError(err, t, "validOnlyWithInvalidArgs") } // MinimumNArgs @@ -202,31 +166,31 @@ func TestMinimumNArgs_WithValid__WithInvalidArgs(t *testing.T) { func TestMinimumNArgs_WithValidOnly_WithInvalidArgs(t *testing.T) { c := getCommand(MatchAll(OnlyValidArgs, MinimumNArgs(2)), true) _, err := executeCommand(c, "a", "b") - validOnlyWithInvalidArgs(err, t) + expectError(err, t, "validOnlyWithInvalidArgs") } func TestMinimumNArgs_WithLessArgs(t *testing.T) { c := getCommand(MinimumNArgs(2), false) _, err := executeCommand(c, "a") - minimumNArgsWithLessArgs(err, t) + expectError(err, t, "minimumNArgsWithLessArgs") } func TestMinimumNArgs_WithLessArgs_WithValid(t *testing.T) { c := getCommand(MinimumNArgs(2), true) _, err := executeCommand(c, "one") - minimumNArgsWithLessArgs(err, t) + expectError(err, t, "minimumNArgsWithLessArgs") } func TestMinimumNArgs_WithLessArgs_WithValid_WithInvalidArgs(t *testing.T) { c := getCommand(MinimumNArgs(2), true) _, err := executeCommand(c, "a") - minimumNArgsWithLessArgs(err, t) + expectError(err, t, "minimumNArgsWithLessArgs") } func TestMinimumNArgs_WithLessArgs_WithValidOnly_WithInvalidArgs(t *testing.T) { c := getCommand(MatchAll(OnlyValidArgs, MinimumNArgs(2)), true) _, err := executeCommand(c, "a") - validOnlyWithInvalidArgs(err, t) + expectError(err, t, "validOnlyWithInvalidArgs") } // MaximumNArgs @@ -252,31 +216,31 @@ func TestMaximumNArgs_WithValid_WithInvalidArgs(t *testing.T) { func TestMaximumNArgs_WithValidOnly_WithInvalidArgs(t *testing.T) { c := getCommand(MatchAll(OnlyValidArgs, MaximumNArgs(2)), true) _, err := executeCommand(c, "a", "b") - validOnlyWithInvalidArgs(err, t) + expectError(err, t, "validOnlyWithInvalidArgs") } func TestMaximumNArgs_WithMoreArgs(t *testing.T) { c := getCommand(MaximumNArgs(2), false) _, err := executeCommand(c, "a", "b", "c") - maximumNArgsWithMoreArgs(err, t) + expectError(err, t, "maximumNArgsWithMoreArgs") } func TestMaximumNArgs_WithMoreArgs_WithValid(t *testing.T) { c := getCommand(MaximumNArgs(2), true) _, err := executeCommand(c, "one", "three", "two") - maximumNArgsWithMoreArgs(err, t) + expectError(err, t, "maximumNArgsWithMoreArgs") } func TestMaximumNArgs_WithMoreArgs_WithValid_WithInvalidArgs(t *testing.T) { c := getCommand(MaximumNArgs(2), true) _, err := executeCommand(c, "a", "b", "c") - maximumNArgsWithMoreArgs(err, t) + expectError(err, t, "maximumNArgsWithMoreArgs") } func TestMaximumNArgs_WithMoreArgs_WithValidOnly_WithInvalidArgs(t *testing.T) { c := getCommand(MatchAll(OnlyValidArgs, MaximumNArgs(2)), true) _, err := executeCommand(c, "a", "b", "c") - validOnlyWithInvalidArgs(err, t) + expectError(err, t, "validOnlyWithInvalidArgs") } // ExactArgs @@ -302,31 +266,31 @@ func TestExactArgs_WithValid_WithInvalidArgs(t *testing.T) { func TestExactArgs_WithValidOnly_WithInvalidArgs(t *testing.T) { c := getCommand(MatchAll(OnlyValidArgs, ExactArgs(3)), true) _, err := executeCommand(c, "three", "a", "two") - validOnlyWithInvalidArgs(err, t) + expectError(err, t, "validOnlyWithInvalidArgs") } func TestExactArgs_WithInvalidCount(t *testing.T) { c := getCommand(ExactArgs(2), false) _, err := executeCommand(c, "a", "b", "c") - exactArgsWithInvalidCount(err, t) + expectError(err, t, "exactArgsWithInvalidCount") } func TestExactArgs_WithInvalidCount_WithValid(t *testing.T) { c := getCommand(ExactArgs(2), true) _, err := executeCommand(c, "three", "one", "two") - exactArgsWithInvalidCount(err, t) + expectError(err, t, "exactArgsWithInvalidCount") } func TestExactArgs_WithInvalidCount_WithValid_WithInvalidArgs(t *testing.T) { c := getCommand(ExactArgs(2), true) _, err := executeCommand(c, "three", "a", "two") - exactArgsWithInvalidCount(err, t) + expectError(err, t, "exactArgsWithInvalidCount") } func TestExactArgs_WithInvalidCount_WithValidOnly_WithInvalidArgs(t *testing.T) { c := getCommand(MatchAll(OnlyValidArgs, ExactArgs(2)), true) _, err := executeCommand(c, "three", "a", "two") - validOnlyWithInvalidArgs(err, t) + expectError(err, t, "validOnlyWithInvalidArgs") } // RangeArgs @@ -352,31 +316,31 @@ func TestRangeArgs_WithValid_WithInvalidArgs(t *testing.T) { func TestRangeArgs_WithValidOnly_WithInvalidArgs(t *testing.T) { c := getCommand(MatchAll(OnlyValidArgs, RangeArgs(2, 4)), true) _, err := executeCommand(c, "three", "a", "two") - validOnlyWithInvalidArgs(err, t) + expectError(err, t, "validOnlyWithInvalidArgs") } func TestRangeArgs_WithInvalidCount(t *testing.T) { c := getCommand(RangeArgs(2, 4), false) _, err := executeCommand(c, "a") - rangeArgsWithInvalidCount(err, t) + expectError(err, t, "rangeArgsWithInvalidCount") } func TestRangeArgs_WithInvalidCount_WithValid(t *testing.T) { c := getCommand(RangeArgs(2, 4), true) _, err := executeCommand(c, "two") - rangeArgsWithInvalidCount(err, t) + expectError(err, t, "rangeArgsWithInvalidCount") } func TestRangeArgs_WithInvalidCount_WithValid_WithInvalidArgs(t *testing.T) { c := getCommand(RangeArgs(2, 4), true) _, err := executeCommand(c, "a") - rangeArgsWithInvalidCount(err, t) + expectError(err, t, "rangeArgsWithInvalidCount") } func TestRangeArgs_WithInvalidCount_WithValidOnly_WithInvalidArgs(t *testing.T) { c := getCommand(MatchAll(OnlyValidArgs, RangeArgs(2, 4)), true) _, err := executeCommand(c, "a") - validOnlyWithInvalidArgs(err, t) + expectError(err, t, "validOnlyWithInvalidArgs") } // Takes(No)Args @@ -496,19 +460,19 @@ func TestExactValidArgs(t *testing.T) { func TestExactValidArgs_WithInvalidCount(t *testing.T) { c := getCommand(ExactValidArgs(2), false) _, err := executeCommand(c, "three", "one", "two") - exactArgsWithInvalidCount(err, t) + expectError(err, t, "exactArgsWithInvalidCount") } func TestExactValidArgs_WithInvalidCount_WithInvalidArgs(t *testing.T) { c := getCommand(ExactValidArgs(2), true) _, err := executeCommand(c, "three", "a", "two") - exactArgsWithInvalidCount(err, t) + expectError(err, t, "exactArgsWithInvalidCount") } func TestExactValidArgs_WithInvalidArgs(t *testing.T) { c := getCommand(ExactValidArgs(2), true) _, err := executeCommand(c, "three", "a") - validOnlyWithInvalidArgs(err, t) + expectError(err, t, "validOnlyWithInvalidArgs") } // This test make sure we keep backwards-compatibility with respect From 1684df23b3ef5f99246fcd16e438f39192125817 Mon Sep 17 00:00:00 2001 From: umarcor Date: Mon, 28 Mar 2022 02:29:31 +0200 Subject: [PATCH 2/3] args_test: use ExpectedErrors enum instead of strings --- args_test.go | 85 +++++++++++++++++++++++++++++----------------------- 1 file changed, 48 insertions(+), 37 deletions(-) diff --git a/args_test.go b/args_test.go index ee367d07..1221ee40 100644 --- a/args_test.go +++ b/args_test.go @@ -41,24 +41,35 @@ func expectSuccess(output string, err error, t *testing.T) { } } -func expectErrorWithArg(err error, t *testing.T, ex string, arg string) { +type ExpectedErrors int64 + +const ( + OnlyValid_WithInvalidArgs ExpectedErrors = iota + NoArgs_WithArgs + MinimumNArgs_WithLessArgs + MaximumNArgs_WithMoreArgs + ExactArgs_WithInvalidCount + RangeArgs_WithInvalidCount +) + +func expectErrorWithArg(err error, t *testing.T, ex ExpectedErrors, arg string) { if err == nil { t.Fatal("Expected an error") } var expected string switch ex { - case "validOnlyWithInvalidArgs": + case OnlyValid_WithInvalidArgs: expected = `invalid argument "a" for "c"` - case "noArgsWithArgs": + case NoArgs_WithArgs: expected = `unknown command "` + arg + `" for "c"` - case "minimumNArgsWithLessArgs": + case MinimumNArgs_WithLessArgs: expected = "requires at least 2 arg(s), only received 1" - case "maximumNArgsWithMoreArgs": + case MaximumNArgs_WithMoreArgs: expected = "accepts at most 2 arg(s), received 3" - case "exactArgsWithInvalidCount": + case ExactArgs_WithInvalidCount: expected = "accepts 2 arg(s), received 3" - case "rangeArgsWithInvalidCount": + case RangeArgs_WithInvalidCount: expected = "accepts between 2 and 4 arg(s), received 1" } @@ -67,7 +78,7 @@ func expectErrorWithArg(err error, t *testing.T, ex string, arg string) { } } -func expectError(err error, t *testing.T, ex string) { +func expectError(err error, t *testing.T, ex ExpectedErrors) { expectErrorWithArg(err, t, ex, "") } @@ -82,25 +93,25 @@ func TestNoArgs(t *testing.T) { func TestNoArgs_WithArgs(t *testing.T) { c := getCommand(NoArgs, false) _, err := executeCommand(c, "one") - expectErrorWithArg(err, t, "noArgsWithArgs", "one") + expectErrorWithArg(err, t, NoArgs_WithArgs, "one") } func TestNoArgs_WithValid_WithArgs(t *testing.T) { c := getCommand(NoArgs, true) _, err := executeCommand(c, "one") - expectErrorWithArg(err, t, "noArgsWithArgs", "one") + expectErrorWithArg(err, t, NoArgs_WithArgs, "one") } func TestNoArgs_WithValid_WithInvalidArgs(t *testing.T) { c := getCommand(NoArgs, true) _, err := executeCommand(c, "a") - expectErrorWithArg(err, t, "noArgsWithArgs", "a") + expectErrorWithArg(err, t, NoArgs_WithArgs, "a") } func TestNoArgs_WithValidOnly_WithInvalidArgs(t *testing.T) { c := getCommand(MatchAll(OnlyValidArgs, NoArgs), true) _, err := executeCommand(c, "a") - expectError(err, t, "validOnlyWithInvalidArgs") + expectError(err, t, OnlyValid_WithInvalidArgs) } // OnlyValidArgs @@ -114,7 +125,7 @@ func TestOnlyValidArgs(t *testing.T) { func TestOnlyValidArgs_WithInvalidArgs(t *testing.T) { c := getCommand(OnlyValidArgs, true) _, err := executeCommand(c, "a") - expectError(err, t, "validOnlyWithInvalidArgs") + expectError(err, t, OnlyValid_WithInvalidArgs) } // ArbitraryArgs @@ -140,7 +151,7 @@ func TestArbitraryArgs_WithValid_WithInvalidArgs(t *testing.T) { func TestArbitraryArgs_WithValidOnly_WithInvalidArgs(t *testing.T) { c := getCommand(MatchAll(OnlyValidArgs, ArbitraryArgs), true) _, err := executeCommand(c, "a") - expectError(err, t, "validOnlyWithInvalidArgs") + expectError(err, t, OnlyValid_WithInvalidArgs) } // MinimumNArgs @@ -166,31 +177,31 @@ func TestMinimumNArgs_WithValid__WithInvalidArgs(t *testing.T) { func TestMinimumNArgs_WithValidOnly_WithInvalidArgs(t *testing.T) { c := getCommand(MatchAll(OnlyValidArgs, MinimumNArgs(2)), true) _, err := executeCommand(c, "a", "b") - expectError(err, t, "validOnlyWithInvalidArgs") + expectError(err, t, OnlyValid_WithInvalidArgs) } func TestMinimumNArgs_WithLessArgs(t *testing.T) { c := getCommand(MinimumNArgs(2), false) _, err := executeCommand(c, "a") - expectError(err, t, "minimumNArgsWithLessArgs") + expectError(err, t, MinimumNArgs_WithLessArgs) } func TestMinimumNArgs_WithLessArgs_WithValid(t *testing.T) { c := getCommand(MinimumNArgs(2), true) _, err := executeCommand(c, "one") - expectError(err, t, "minimumNArgsWithLessArgs") + expectError(err, t, MinimumNArgs_WithLessArgs) } func TestMinimumNArgs_WithLessArgs_WithValid_WithInvalidArgs(t *testing.T) { c := getCommand(MinimumNArgs(2), true) _, err := executeCommand(c, "a") - expectError(err, t, "minimumNArgsWithLessArgs") + expectError(err, t, MinimumNArgs_WithLessArgs) } func TestMinimumNArgs_WithLessArgs_WithValidOnly_WithInvalidArgs(t *testing.T) { c := getCommand(MatchAll(OnlyValidArgs, MinimumNArgs(2)), true) _, err := executeCommand(c, "a") - expectError(err, t, "validOnlyWithInvalidArgs") + expectError(err, t, OnlyValid_WithInvalidArgs) } // MaximumNArgs @@ -216,31 +227,31 @@ func TestMaximumNArgs_WithValid_WithInvalidArgs(t *testing.T) { func TestMaximumNArgs_WithValidOnly_WithInvalidArgs(t *testing.T) { c := getCommand(MatchAll(OnlyValidArgs, MaximumNArgs(2)), true) _, err := executeCommand(c, "a", "b") - expectError(err, t, "validOnlyWithInvalidArgs") + expectError(err, t, OnlyValid_WithInvalidArgs) } func TestMaximumNArgs_WithMoreArgs(t *testing.T) { c := getCommand(MaximumNArgs(2), false) _, err := executeCommand(c, "a", "b", "c") - expectError(err, t, "maximumNArgsWithMoreArgs") + expectError(err, t, MaximumNArgs_WithMoreArgs) } func TestMaximumNArgs_WithMoreArgs_WithValid(t *testing.T) { c := getCommand(MaximumNArgs(2), true) _, err := executeCommand(c, "one", "three", "two") - expectError(err, t, "maximumNArgsWithMoreArgs") + expectError(err, t, MaximumNArgs_WithMoreArgs) } func TestMaximumNArgs_WithMoreArgs_WithValid_WithInvalidArgs(t *testing.T) { c := getCommand(MaximumNArgs(2), true) _, err := executeCommand(c, "a", "b", "c") - expectError(err, t, "maximumNArgsWithMoreArgs") + expectError(err, t, MaximumNArgs_WithMoreArgs) } func TestMaximumNArgs_WithMoreArgs_WithValidOnly_WithInvalidArgs(t *testing.T) { c := getCommand(MatchAll(OnlyValidArgs, MaximumNArgs(2)), true) _, err := executeCommand(c, "a", "b", "c") - expectError(err, t, "validOnlyWithInvalidArgs") + expectError(err, t, OnlyValid_WithInvalidArgs) } // ExactArgs @@ -266,31 +277,31 @@ func TestExactArgs_WithValid_WithInvalidArgs(t *testing.T) { func TestExactArgs_WithValidOnly_WithInvalidArgs(t *testing.T) { c := getCommand(MatchAll(OnlyValidArgs, ExactArgs(3)), true) _, err := executeCommand(c, "three", "a", "two") - expectError(err, t, "validOnlyWithInvalidArgs") + expectError(err, t, OnlyValid_WithInvalidArgs) } func TestExactArgs_WithInvalidCount(t *testing.T) { c := getCommand(ExactArgs(2), false) _, err := executeCommand(c, "a", "b", "c") - expectError(err, t, "exactArgsWithInvalidCount") + expectError(err, t, ExactArgs_WithInvalidCount) } func TestExactArgs_WithInvalidCount_WithValid(t *testing.T) { c := getCommand(ExactArgs(2), true) _, err := executeCommand(c, "three", "one", "two") - expectError(err, t, "exactArgsWithInvalidCount") + expectError(err, t, ExactArgs_WithInvalidCount) } func TestExactArgs_WithInvalidCount_WithValid_WithInvalidArgs(t *testing.T) { c := getCommand(ExactArgs(2), true) _, err := executeCommand(c, "three", "a", "two") - expectError(err, t, "exactArgsWithInvalidCount") + expectError(err, t, ExactArgs_WithInvalidCount) } func TestExactArgs_WithInvalidCount_WithValidOnly_WithInvalidArgs(t *testing.T) { c := getCommand(MatchAll(OnlyValidArgs, ExactArgs(2)), true) _, err := executeCommand(c, "three", "a", "two") - expectError(err, t, "validOnlyWithInvalidArgs") + expectError(err, t, OnlyValid_WithInvalidArgs) } // RangeArgs @@ -316,31 +327,31 @@ func TestRangeArgs_WithValid_WithInvalidArgs(t *testing.T) { func TestRangeArgs_WithValidOnly_WithInvalidArgs(t *testing.T) { c := getCommand(MatchAll(OnlyValidArgs, RangeArgs(2, 4)), true) _, err := executeCommand(c, "three", "a", "two") - expectError(err, t, "validOnlyWithInvalidArgs") + expectError(err, t, OnlyValid_WithInvalidArgs) } func TestRangeArgs_WithInvalidCount(t *testing.T) { c := getCommand(RangeArgs(2, 4), false) _, err := executeCommand(c, "a") - expectError(err, t, "rangeArgsWithInvalidCount") + expectError(err, t, RangeArgs_WithInvalidCount) } func TestRangeArgs_WithInvalidCount_WithValid(t *testing.T) { c := getCommand(RangeArgs(2, 4), true) _, err := executeCommand(c, "two") - expectError(err, t, "rangeArgsWithInvalidCount") + expectError(err, t, RangeArgs_WithInvalidCount) } func TestRangeArgs_WithInvalidCount_WithValid_WithInvalidArgs(t *testing.T) { c := getCommand(RangeArgs(2, 4), true) _, err := executeCommand(c, "a") - expectError(err, t, "rangeArgsWithInvalidCount") + expectError(err, t, RangeArgs_WithInvalidCount) } func TestRangeArgs_WithInvalidCount_WithValidOnly_WithInvalidArgs(t *testing.T) { c := getCommand(MatchAll(OnlyValidArgs, RangeArgs(2, 4)), true) _, err := executeCommand(c, "a") - expectError(err, t, "validOnlyWithInvalidArgs") + expectError(err, t, OnlyValid_WithInvalidArgs) } // Takes(No)Args @@ -460,19 +471,19 @@ func TestExactValidArgs(t *testing.T) { func TestExactValidArgs_WithInvalidCount(t *testing.T) { c := getCommand(ExactValidArgs(2), false) _, err := executeCommand(c, "three", "one", "two") - expectError(err, t, "exactArgsWithInvalidCount") + expectError(err, t, ExactArgs_WithInvalidCount) } func TestExactValidArgs_WithInvalidCount_WithInvalidArgs(t *testing.T) { c := getCommand(ExactValidArgs(2), true) _, err := executeCommand(c, "three", "a", "two") - expectError(err, t, "exactArgsWithInvalidCount") + expectError(err, t, ExactArgs_WithInvalidCount) } func TestExactValidArgs_WithInvalidArgs(t *testing.T) { c := getCommand(ExactValidArgs(2), true) _, err := executeCommand(c, "three", "a") - expectError(err, t, "validOnlyWithInvalidArgs") + expectError(err, t, OnlyValid_WithInvalidArgs) } // This test make sure we keep backwards-compatibility with respect From 8d10b3fcb99ef23ce0970e426998c130b95b0495 Mon Sep 17 00:00:00 2001 From: umarcor Date: Mon, 28 Mar 2022 02:39:27 +0200 Subject: [PATCH 3/3] args_test: style --- args_test.go | 242 ++++++++++++++++++++------------------------------- 1 file changed, 92 insertions(+), 150 deletions(-) diff --git a/args_test.go b/args_test.go index 1221ee40..2c255af0 100644 --- a/args_test.go +++ b/args_test.go @@ -20,7 +20,7 @@ import ( "testing" ) -func getCommand(args PositionalArgs, withValid bool) *Command { +func newCommand(args PositionalArgs, withValid bool) *Command { c := &Command{ Use: "c", Args: args, @@ -44,35 +44,26 @@ func expectSuccess(output string, err error, t *testing.T) { type ExpectedErrors int64 const ( - OnlyValid_WithInvalidArgs ExpectedErrors = iota - NoArgs_WithArgs - MinimumNArgs_WithLessArgs - MaximumNArgs_WithMoreArgs - ExactArgs_WithInvalidCount - RangeArgs_WithInvalidCount + OnlyValidWithInvalidArgs ExpectedErrors = iota + NoArgsWithArgs + MinimumNArgsWithLessArgs + MaximumNArgsWithMoreArgs + ExactArgsWithInvalidCount + RangeArgsWithInvalidCount ) func expectErrorWithArg(err error, t *testing.T, ex ExpectedErrors, arg string) { if err == nil { t.Fatal("Expected an error") } - - var expected string - switch ex { - case OnlyValid_WithInvalidArgs: - expected = `invalid argument "a" for "c"` - case NoArgs_WithArgs: - expected = `unknown command "` + arg + `" for "c"` - case MinimumNArgs_WithLessArgs: - expected = "requires at least 2 arg(s), only received 1" - case MaximumNArgs_WithMoreArgs: - expected = "accepts at most 2 arg(s), received 3" - case ExactArgs_WithInvalidCount: - expected = "accepts 2 arg(s), received 3" - case RangeArgs_WithInvalidCount: - expected = "accepts between 2 and 4 arg(s), received 1" - } - + expected := map[ExpectedErrors]string{ + OnlyValidWithInvalidArgs: `invalid argument "a" for "c"`, + NoArgsWithArgs: `unknown command "` + arg + `" for "c"`, + MinimumNArgsWithLessArgs: "requires at least 2 arg(s), only received 1", + MaximumNArgsWithMoreArgs: "accepts at most 2 arg(s), received 3", + ExactArgsWithInvalidCount: "accepts 2 arg(s), received 3", + RangeArgsWithInvalidCount: "accepts between 2 and 4 arg(s), received 1", + }[ex] if got := err.Error(); got != expected { t.Errorf("Expected: %q, got: %q", expected, got) } @@ -85,273 +76,230 @@ func expectError(err error, t *testing.T, ex ExpectedErrors) { // NoArgs func TestNoArgs(t *testing.T) { - c := getCommand(NoArgs, false) - output, err := executeCommand(c) + output, err := executeCommand(newCommand(NoArgs, false)) expectSuccess(output, err, t) } func TestNoArgs_WithArgs(t *testing.T) { - c := getCommand(NoArgs, false) - _, err := executeCommand(c, "one") - expectErrorWithArg(err, t, NoArgs_WithArgs, "one") + _, err := executeCommand(newCommand(NoArgs, false), "one") + expectErrorWithArg(err, t, NoArgsWithArgs, "one") } func TestNoArgs_WithValid_WithArgs(t *testing.T) { - c := getCommand(NoArgs, true) - _, err := executeCommand(c, "one") - expectErrorWithArg(err, t, NoArgs_WithArgs, "one") + _, err := executeCommand(newCommand(NoArgs, true), "one") + expectErrorWithArg(err, t, NoArgsWithArgs, "one") } func TestNoArgs_WithValid_WithInvalidArgs(t *testing.T) { - c := getCommand(NoArgs, true) - _, err := executeCommand(c, "a") - expectErrorWithArg(err, t, NoArgs_WithArgs, "a") + _, err := executeCommand(newCommand(NoArgs, true), "a") + expectErrorWithArg(err, t, NoArgsWithArgs, "a") } func TestNoArgs_WithValidOnly_WithInvalidArgs(t *testing.T) { - c := getCommand(MatchAll(OnlyValidArgs, NoArgs), true) - _, err := executeCommand(c, "a") - expectError(err, t, OnlyValid_WithInvalidArgs) + _, err := executeCommand(newCommand(MatchAll(OnlyValidArgs, NoArgs), true), "a") + expectError(err, t, OnlyValidWithInvalidArgs) } // OnlyValidArgs func TestOnlyValidArgs(t *testing.T) { - c := getCommand(OnlyValidArgs, true) - output, err := executeCommand(c, "one", "two") + output, err := executeCommand(newCommand(OnlyValidArgs, true), "one", "two") expectSuccess(output, err, t) } func TestOnlyValidArgs_WithInvalidArgs(t *testing.T) { - c := getCommand(OnlyValidArgs, true) - _, err := executeCommand(c, "a") - expectError(err, t, OnlyValid_WithInvalidArgs) + _, err := executeCommand(newCommand(OnlyValidArgs, true), "a") + expectError(err, t, OnlyValidWithInvalidArgs) } // ArbitraryArgs func TestArbitraryArgs(t *testing.T) { - c := getCommand(ArbitraryArgs, false) - output, err := executeCommand(c, "a", "b") + output, err := executeCommand(newCommand(ArbitraryArgs, false), "a", "b") expectSuccess(output, err, t) } func TestArbitraryArgs_WithValid(t *testing.T) { - c := getCommand(ArbitraryArgs, true) - output, err := executeCommand(c, "one", "two") + output, err := executeCommand(newCommand(ArbitraryArgs, true), "one", "two") expectSuccess(output, err, t) } func TestArbitraryArgs_WithValid_WithInvalidArgs(t *testing.T) { - c := getCommand(ArbitraryArgs, true) - output, err := executeCommand(c, "a") + output, err := executeCommand(newCommand(ArbitraryArgs, true), "a") expectSuccess(output, err, t) } func TestArbitraryArgs_WithValidOnly_WithInvalidArgs(t *testing.T) { - c := getCommand(MatchAll(OnlyValidArgs, ArbitraryArgs), true) - _, err := executeCommand(c, "a") - expectError(err, t, OnlyValid_WithInvalidArgs) + _, err := executeCommand(newCommand(MatchAll(OnlyValidArgs, ArbitraryArgs), true), "a") + expectError(err, t, OnlyValidWithInvalidArgs) } // MinimumNArgs func TestMinimumNArgs(t *testing.T) { - c := getCommand(MinimumNArgs(2), false) - output, err := executeCommand(c, "a", "b", "c") + output, err := executeCommand(newCommand(MinimumNArgs(2), false), "a", "b", "c") expectSuccess(output, err, t) } func TestMinimumNArgs_WithValid(t *testing.T) { - c := getCommand(MinimumNArgs(2), true) - output, err := executeCommand(c, "one", "three") + output, err := executeCommand(newCommand(MinimumNArgs(2), true), "one", "three") expectSuccess(output, err, t) } func TestMinimumNArgs_WithValid__WithInvalidArgs(t *testing.T) { - c := getCommand(MinimumNArgs(2), true) - output, err := executeCommand(c, "a", "b") + output, err := executeCommand(newCommand(MinimumNArgs(2), true), "a", "b") expectSuccess(output, err, t) } func TestMinimumNArgs_WithValidOnly_WithInvalidArgs(t *testing.T) { - c := getCommand(MatchAll(OnlyValidArgs, MinimumNArgs(2)), true) - _, err := executeCommand(c, "a", "b") - expectError(err, t, OnlyValid_WithInvalidArgs) + _, err := executeCommand(newCommand(MatchAll(OnlyValidArgs, MinimumNArgs(2)), true), "a", "b") + expectError(err, t, OnlyValidWithInvalidArgs) } func TestMinimumNArgs_WithLessArgs(t *testing.T) { - c := getCommand(MinimumNArgs(2), false) - _, err := executeCommand(c, "a") - expectError(err, t, MinimumNArgs_WithLessArgs) + _, err := executeCommand(newCommand(MinimumNArgs(2), false), "a") + expectError(err, t, MinimumNArgsWithLessArgs) } func TestMinimumNArgs_WithLessArgs_WithValid(t *testing.T) { - c := getCommand(MinimumNArgs(2), true) - _, err := executeCommand(c, "one") - expectError(err, t, MinimumNArgs_WithLessArgs) + _, err := executeCommand(newCommand(MinimumNArgs(2), true), "one") + expectError(err, t, MinimumNArgsWithLessArgs) } func TestMinimumNArgs_WithLessArgs_WithValid_WithInvalidArgs(t *testing.T) { - c := getCommand(MinimumNArgs(2), true) - _, err := executeCommand(c, "a") - expectError(err, t, MinimumNArgs_WithLessArgs) + _, err := executeCommand(newCommand(MinimumNArgs(2), true), "a") + expectError(err, t, MinimumNArgsWithLessArgs) } func TestMinimumNArgs_WithLessArgs_WithValidOnly_WithInvalidArgs(t *testing.T) { - c := getCommand(MatchAll(OnlyValidArgs, MinimumNArgs(2)), true) - _, err := executeCommand(c, "a") - expectError(err, t, OnlyValid_WithInvalidArgs) + _, err := executeCommand(newCommand(MatchAll(OnlyValidArgs, MinimumNArgs(2)), true), "a") + expectError(err, t, OnlyValidWithInvalidArgs) } // MaximumNArgs func TestMaximumNArgs(t *testing.T) { - c := getCommand(MaximumNArgs(3), false) - output, err := executeCommand(c, "a", "b") + output, err := executeCommand(newCommand(MaximumNArgs(3), false), "a", "b") expectSuccess(output, err, t) } func TestMaximumNArgs_WithValid(t *testing.T) { - c := getCommand(MaximumNArgs(2), true) - output, err := executeCommand(c, "one", "three") + output, err := executeCommand(newCommand(MaximumNArgs(2), true), "one", "three") expectSuccess(output, err, t) } func TestMaximumNArgs_WithValid_WithInvalidArgs(t *testing.T) { - c := getCommand(MaximumNArgs(2), true) - output, err := executeCommand(c, "a", "b") + output, err := executeCommand(newCommand(MaximumNArgs(2), true), "a", "b") expectSuccess(output, err, t) } func TestMaximumNArgs_WithValidOnly_WithInvalidArgs(t *testing.T) { - c := getCommand(MatchAll(OnlyValidArgs, MaximumNArgs(2)), true) - _, err := executeCommand(c, "a", "b") - expectError(err, t, OnlyValid_WithInvalidArgs) + _, err := executeCommand(newCommand(MatchAll(OnlyValidArgs, MaximumNArgs(2)), true), "a", "b") + expectError(err, t, OnlyValidWithInvalidArgs) } func TestMaximumNArgs_WithMoreArgs(t *testing.T) { - c := getCommand(MaximumNArgs(2), false) - _, err := executeCommand(c, "a", "b", "c") - expectError(err, t, MaximumNArgs_WithMoreArgs) + _, err := executeCommand(newCommand(MaximumNArgs(2), false), "a", "b", "c") + expectError(err, t, MaximumNArgsWithMoreArgs) } func TestMaximumNArgs_WithMoreArgs_WithValid(t *testing.T) { - c := getCommand(MaximumNArgs(2), true) - _, err := executeCommand(c, "one", "three", "two") - expectError(err, t, MaximumNArgs_WithMoreArgs) + _, err := executeCommand(newCommand(MaximumNArgs(2), true), "one", "three", "two") + expectError(err, t, MaximumNArgsWithMoreArgs) } func TestMaximumNArgs_WithMoreArgs_WithValid_WithInvalidArgs(t *testing.T) { - c := getCommand(MaximumNArgs(2), true) - _, err := executeCommand(c, "a", "b", "c") - expectError(err, t, MaximumNArgs_WithMoreArgs) + _, err := executeCommand(newCommand(MaximumNArgs(2), true), "a", "b", "c") + expectError(err, t, MaximumNArgsWithMoreArgs) } func TestMaximumNArgs_WithMoreArgs_WithValidOnly_WithInvalidArgs(t *testing.T) { - c := getCommand(MatchAll(OnlyValidArgs, MaximumNArgs(2)), true) - _, err := executeCommand(c, "a", "b", "c") - expectError(err, t, OnlyValid_WithInvalidArgs) + _, err := executeCommand(newCommand(MatchAll(OnlyValidArgs, MaximumNArgs(2)), true), "a", "b", "c") + expectError(err, t, OnlyValidWithInvalidArgs) } // ExactArgs func TestExactArgs(t *testing.T) { - c := getCommand(ExactArgs(3), false) - output, err := executeCommand(c, "a", "b", "c") + output, err := executeCommand(newCommand(ExactArgs(3), false), "a", "b", "c") expectSuccess(output, err, t) } func TestExactArgs_WithValid(t *testing.T) { - c := getCommand(ExactArgs(3), true) - output, err := executeCommand(c, "three", "one", "two") + output, err := executeCommand(newCommand(ExactArgs(3), true), "three", "one", "two") expectSuccess(output, err, t) } func TestExactArgs_WithValid_WithInvalidArgs(t *testing.T) { - c := getCommand(ExactArgs(3), true) - output, err := executeCommand(c, "three", "a", "two") + output, err := executeCommand(newCommand(ExactArgs(3), true), "three", "a", "two") expectSuccess(output, err, t) } func TestExactArgs_WithValidOnly_WithInvalidArgs(t *testing.T) { - c := getCommand(MatchAll(OnlyValidArgs, ExactArgs(3)), true) - _, err := executeCommand(c, "three", "a", "two") - expectError(err, t, OnlyValid_WithInvalidArgs) + _, err := executeCommand(newCommand(MatchAll(OnlyValidArgs, ExactArgs(3)), true), "three", "a", "two") + expectError(err, t, OnlyValidWithInvalidArgs) } func TestExactArgs_WithInvalidCount(t *testing.T) { - c := getCommand(ExactArgs(2), false) - _, err := executeCommand(c, "a", "b", "c") - expectError(err, t, ExactArgs_WithInvalidCount) + _, err := executeCommand(newCommand(ExactArgs(2), false), "a", "b", "c") + expectError(err, t, ExactArgsWithInvalidCount) } func TestExactArgs_WithInvalidCount_WithValid(t *testing.T) { - c := getCommand(ExactArgs(2), true) - _, err := executeCommand(c, "three", "one", "two") - expectError(err, t, ExactArgs_WithInvalidCount) + _, err := executeCommand(newCommand(ExactArgs(2), true), "three", "one", "two") + expectError(err, t, ExactArgsWithInvalidCount) } func TestExactArgs_WithInvalidCount_WithValid_WithInvalidArgs(t *testing.T) { - c := getCommand(ExactArgs(2), true) - _, err := executeCommand(c, "three", "a", "two") - expectError(err, t, ExactArgs_WithInvalidCount) + _, err := executeCommand(newCommand(ExactArgs(2), true), "three", "a", "two") + expectError(err, t, ExactArgsWithInvalidCount) } func TestExactArgs_WithInvalidCount_WithValidOnly_WithInvalidArgs(t *testing.T) { - c := getCommand(MatchAll(OnlyValidArgs, ExactArgs(2)), true) - _, err := executeCommand(c, "three", "a", "two") - expectError(err, t, OnlyValid_WithInvalidArgs) + _, err := executeCommand(newCommand(MatchAll(OnlyValidArgs, ExactArgs(2)), true), "three", "a", "two") + expectError(err, t, OnlyValidWithInvalidArgs) } // RangeArgs func TestRangeArgs(t *testing.T) { - c := getCommand(RangeArgs(2, 4), false) - output, err := executeCommand(c, "a", "b", "c") + output, err := executeCommand(newCommand(RangeArgs(2, 4), false), "a", "b", "c") expectSuccess(output, err, t) } func TestRangeArgs_WithValid(t *testing.T) { - c := getCommand(RangeArgs(2, 4), true) - output, err := executeCommand(c, "three", "one", "two") + output, err := executeCommand(newCommand(RangeArgs(2, 4), true), "three", "one", "two") expectSuccess(output, err, t) } func TestRangeArgs_WithValid_WithInvalidArgs(t *testing.T) { - c := getCommand(RangeArgs(2, 4), true) - output, err := executeCommand(c, "three", "a", "two") + output, err := executeCommand(newCommand(RangeArgs(2, 4), true), "three", "a", "two") expectSuccess(output, err, t) } func TestRangeArgs_WithValidOnly_WithInvalidArgs(t *testing.T) { - c := getCommand(MatchAll(OnlyValidArgs, RangeArgs(2, 4)), true) - _, err := executeCommand(c, "three", "a", "two") - expectError(err, t, OnlyValid_WithInvalidArgs) + _, err := executeCommand(newCommand(MatchAll(OnlyValidArgs, RangeArgs(2, 4)), true), "three", "a", "two") + expectError(err, t, OnlyValidWithInvalidArgs) } func TestRangeArgs_WithInvalidCount(t *testing.T) { - c := getCommand(RangeArgs(2, 4), false) - _, err := executeCommand(c, "a") - expectError(err, t, RangeArgs_WithInvalidCount) + _, err := executeCommand(newCommand(RangeArgs(2, 4), false), "a") + expectError(err, t, RangeArgsWithInvalidCount) } func TestRangeArgs_WithInvalidCount_WithValid(t *testing.T) { - c := getCommand(RangeArgs(2, 4), true) - _, err := executeCommand(c, "two") - expectError(err, t, RangeArgs_WithInvalidCount) + _, err := executeCommand(newCommand(RangeArgs(2, 4), true), "two") + expectError(err, t, RangeArgsWithInvalidCount) } func TestRangeArgs_WithInvalidCount_WithValid_WithInvalidArgs(t *testing.T) { - c := getCommand(RangeArgs(2, 4), true) - _, err := executeCommand(c, "a") - expectError(err, t, RangeArgs_WithInvalidCount) + _, err := executeCommand(newCommand(RangeArgs(2, 4), true), "a") + expectError(err, t, RangeArgsWithInvalidCount) } func TestRangeArgs_WithInvalidCount_WithValidOnly_WithInvalidArgs(t *testing.T) { - c := getCommand(MatchAll(OnlyValidArgs, RangeArgs(2, 4)), true) - _, err := executeCommand(c, "a") - expectError(err, t, OnlyValid_WithInvalidArgs) + _, err := executeCommand(newCommand(MatchAll(OnlyValidArgs, RangeArgs(2, 4)), true), "a") + expectError(err, t, OnlyValidWithInvalidArgs) } // Takes(No)Args @@ -463,27 +411,23 @@ func TestMatchAll(t *testing.T) { // DEPRECATED func TestExactValidArgs(t *testing.T) { - c := getCommand(ExactValidArgs(3), true) - output, err := executeCommand(c, "three", "one", "two") + output, err := executeCommand(newCommand(ExactValidArgs(3), true), "three", "one", "two") expectSuccess(output, err, t) } func TestExactValidArgs_WithInvalidCount(t *testing.T) { - c := getCommand(ExactValidArgs(2), false) - _, err := executeCommand(c, "three", "one", "two") - expectError(err, t, ExactArgs_WithInvalidCount) + _, err := executeCommand(newCommand(ExactValidArgs(2), false), "three", "one", "two") + expectError(err, t, ExactArgsWithInvalidCount) } func TestExactValidArgs_WithInvalidCount_WithInvalidArgs(t *testing.T) { - c := getCommand(ExactValidArgs(2), true) - _, err := executeCommand(c, "three", "a", "two") - expectError(err, t, ExactArgs_WithInvalidCount) + _, err := executeCommand(newCommand(ExactValidArgs(2), true), "three", "a", "two") + expectError(err, t, ExactArgsWithInvalidCount) } func TestExactValidArgs_WithInvalidArgs(t *testing.T) { - c := getCommand(ExactValidArgs(2), true) - _, err := executeCommand(c, "three", "a") - expectError(err, t, OnlyValid_WithInvalidArgs) + _, err := executeCommand(newCommand(ExactValidArgs(2), true), "three", "a") + expectError(err, t, OnlyValidWithInvalidArgs) } // This test make sure we keep backwards-compatibility with respect @@ -491,9 +435,7 @@ func TestExactValidArgs_WithInvalidArgs(t *testing.T) { // It makes sure the root command accepts arguments if it does not have // sub-commands. func TestLegacyArgsRootAcceptsArgs(t *testing.T) { - rootCmd := &Command{Use: "root", Args: nil, Run: emptyRun} - - _, err := executeCommand(rootCmd, "somearg") + _, err := executeCommand(&Command{Use: "root", Args: nil, Run: emptyRun}, "somearg") if err != nil { t.Fatalf("Unexpected error: %v", err) }