From 0fd7782b66cf6799a80b79c2eb7edc5b1c9292a0 Mon Sep 17 00:00:00 2001 From: umarcor Date: Mon, 18 Mar 2019 23:20:48 +0100 Subject: [PATCH 1/3] args_test: add helper function expectSuccess --- args_test.go | 62 +++++++++++++--------------------------------------- 1 file changed, 15 insertions(+), 47 deletions(-) diff --git a/args_test.go b/args_test.go index c81b212ec..a44075a3d 100644 --- a/args_test.go +++ b/args_test.go @@ -5,18 +5,21 @@ import ( "testing" ) -func TestNoArgs(t *testing.T) { - c := &Command{Use: "c", Args: NoArgs, Run: emptyRun} - - output, err := executeCommand(c) +func expectSuccess(output string, err error, t *testing.T) { if output != "" { - t.Errorf("Unexpected string: %v", output) + t.Errorf("Unexpected output: %v", output) } if err != nil { t.Fatalf("Unexpected error: %v", err) } } +func TestNoArgs(t *testing.T) { + c := &Command{Use: "c", Args: NoArgs, Run: emptyRun} + output, err := executeCommand(c) + expectSuccess(output, err, t) +} + func TestNoArgsWithArgs(t *testing.T) { c := &Command{Use: "c", Args: NoArgs, Run: emptyRun} @@ -41,12 +44,7 @@ func TestOnlyValidArgs(t *testing.T) { } output, err := executeCommand(c, "one", "two") - if output != "" { - t.Errorf("Unexpected output: %v", output) - } - if err != nil { - t.Fatalf("Unexpected error: %v", err) - } + expectSuccess(output, err, t) } func TestOnlyValidArgsWithInvalidArgs(t *testing.T) { @@ -72,23 +70,13 @@ func TestOnlyValidArgsWithInvalidArgs(t *testing.T) { func TestArbitraryArgs(t *testing.T) { c := &Command{Use: "c", Args: ArbitraryArgs, Run: emptyRun} output, err := executeCommand(c, "a", "b") - if output != "" { - t.Errorf("Unexpected output: %v", output) - } - if err != nil { - t.Errorf("Unexpected error: %v", err) - } + expectSuccess(output, err, t) } func TestMinimumNArgs(t *testing.T) { c := &Command{Use: "c", Args: MinimumNArgs(2), Run: emptyRun} output, err := executeCommand(c, "a", "b", "c") - if output != "" { - t.Errorf("Unexpected output: %v", output) - } - if err != nil { - t.Errorf("Unexpected error: %v", err) - } + expectSuccess(output, err, t) } func TestMinimumNArgsWithLessArgs(t *testing.T) { @@ -109,12 +97,7 @@ func TestMinimumNArgsWithLessArgs(t *testing.T) { func TestMaximumNArgs(t *testing.T) { c := &Command{Use: "c", Args: MaximumNArgs(3), Run: emptyRun} output, err := executeCommand(c, "a", "b") - if output != "" { - t.Errorf("Unexpected output: %v", output) - } - if err != nil { - t.Errorf("Unexpected error: %v", err) - } + expectSuccess(output, err, t) } func TestMaximumNArgsWithMoreArgs(t *testing.T) { @@ -135,12 +118,7 @@ func TestMaximumNArgsWithMoreArgs(t *testing.T) { func TestExactArgs(t *testing.T) { c := &Command{Use: "c", Args: ExactArgs(3), Run: emptyRun} output, err := executeCommand(c, "a", "b", "c") - if output != "" { - t.Errorf("Unexpected output: %v", output) - } - if err != nil { - t.Errorf("Unexpected error: %v", err) - } + expectSuccess(output, err, t) } func TestExactArgsWithInvalidCount(t *testing.T) { @@ -161,12 +139,7 @@ func TestExactArgsWithInvalidCount(t *testing.T) { func TestExactValidArgs(t *testing.T) { c := &Command{Use: "c", Args: ExactValidArgs(3), ValidArgs: []string{"a", "b", "c"}, Run: emptyRun} output, err := executeCommand(c, "a", "b", "c") - if output != "" { - t.Errorf("Unexpected output: %v", output) - } - if err != nil { - t.Errorf("Unexpected error: %v", err) - } + expectSuccess(output, err, t) } func TestExactValidArgsWithInvalidCount(t *testing.T) { @@ -207,12 +180,7 @@ func TestExactValidArgsWithInvalidArgs(t *testing.T) { func TestRangeArgs(t *testing.T) { c := &Command{Use: "c", Args: RangeArgs(2, 4), Run: emptyRun} output, err := executeCommand(c, "a", "b", "c") - if output != "" { - t.Errorf("Unexpected output: %v", output) - } - if err != nil { - t.Errorf("Unexpected error: %v", err) - } + expectSuccess(output, err, t) } func TestRangeArgsWithInvalidCount(t *testing.T) { From 989590274f9d0bfe0c943a82b0e8283f414a61ae Mon Sep 17 00:00:00 2001 From: umarcor Date: Mon, 18 Mar 2019 23:32:12 +0100 Subject: [PATCH 2/3] args_test: add helper function getCommand --- args_test.go | 71 +++++++++++++++++++++++++--------------------------- 1 file changed, 34 insertions(+), 37 deletions(-) diff --git a/args_test.go b/args_test.go index a44075a3d..2db6191b4 100644 --- a/args_test.go +++ b/args_test.go @@ -5,6 +5,18 @@ import ( "testing" ) +func getCommand(args PositionalArgs, withValid bool) *Command { + c := &Command{ + Use: "c", + Args: args, + Run: emptyRun, + } + if withValid { + c.ValidArgs = []string{"one", "two", "three"} + } + return c +} + func expectSuccess(output string, err error, t *testing.T) { if output != "" { t.Errorf("Unexpected output: %v", output) @@ -15,13 +27,13 @@ func expectSuccess(output string, err error, t *testing.T) { } func TestNoArgs(t *testing.T) { - c := &Command{Use: "c", Args: NoArgs, Run: emptyRun} + c := getCommand(NoArgs, false) output, err := executeCommand(c) expectSuccess(output, err, t) } func TestNoArgsWithArgs(t *testing.T) { - c := &Command{Use: "c", Args: NoArgs, Run: emptyRun} + c := getCommand(NoArgs, false) _, err := executeCommand(c, "illegal") if err == nil { @@ -36,51 +48,41 @@ func TestNoArgsWithArgs(t *testing.T) { } func TestOnlyValidArgs(t *testing.T) { - c := &Command{ - Use: "c", - Args: OnlyValidArgs, - ValidArgs: []string{"one", "two"}, - Run: emptyRun, - } + c := getCommand(OnlyValidArgs, true) output, err := executeCommand(c, "one", "two") expectSuccess(output, err, t) } func TestOnlyValidArgsWithInvalidArgs(t *testing.T) { - c := &Command{ - Use: "c", - Args: OnlyValidArgs, - ValidArgs: []string{"one", "two"}, - Run: emptyRun, - } + c := getCommand(OnlyValidArgs, true) - _, err := executeCommand(c, "three") + _, err := executeCommand(c, "a") if err == nil { t.Fatal("Expected an error") } got := err.Error() - expected := `invalid argument "three" for "c"` + expected := `invalid argument "a" for "c"` if got != expected { t.Errorf("Expected: %q, got: %q", expected, got) } } func TestArbitraryArgs(t *testing.T) { - c := &Command{Use: "c", Args: ArbitraryArgs, Run: emptyRun} + c := getCommand(ArbitraryArgs, false) output, err := executeCommand(c, "a", "b") expectSuccess(output, err, t) } func TestMinimumNArgs(t *testing.T) { - c := &Command{Use: "c", Args: MinimumNArgs(2), Run: emptyRun} + c := getCommand(MinimumNArgs(2), false) output, err := executeCommand(c, "a", "b", "c") expectSuccess(output, err, t) } func TestMinimumNArgsWithLessArgs(t *testing.T) { - c := &Command{Use: "c", Args: MinimumNArgs(2), Run: emptyRun} + c := getCommand(MinimumNArgs(2), false) _, err := executeCommand(c, "a") if err == nil { @@ -95,13 +97,13 @@ func TestMinimumNArgsWithLessArgs(t *testing.T) { } func TestMaximumNArgs(t *testing.T) { - c := &Command{Use: "c", Args: MaximumNArgs(3), Run: emptyRun} + c := getCommand(MaximumNArgs(3), false) output, err := executeCommand(c, "a", "b") expectSuccess(output, err, t) } func TestMaximumNArgsWithMoreArgs(t *testing.T) { - c := &Command{Use: "c", Args: MaximumNArgs(2), Run: emptyRun} + c := getCommand(MaximumNArgs(2), false) _, err := executeCommand(c, "a", "b", "c") if err == nil { @@ -116,13 +118,13 @@ func TestMaximumNArgsWithMoreArgs(t *testing.T) { } func TestExactArgs(t *testing.T) { - c := &Command{Use: "c", Args: ExactArgs(3), Run: emptyRun} + c := getCommand(ExactArgs(3), false) output, err := executeCommand(c, "a", "b", "c") expectSuccess(output, err, t) } func TestExactArgsWithInvalidCount(t *testing.T) { - c := &Command{Use: "c", Args: ExactArgs(2), Run: emptyRun} + c := getCommand(ExactArgs(2), false) _, err := executeCommand(c, "a", "b", "c") if err == nil { @@ -137,14 +139,14 @@ func TestExactArgsWithInvalidCount(t *testing.T) { } func TestExactValidArgs(t *testing.T) { - c := &Command{Use: "c", Args: ExactValidArgs(3), ValidArgs: []string{"a", "b", "c"}, Run: emptyRun} - output, err := executeCommand(c, "a", "b", "c") + c := getCommand(ExactValidArgs(3), true) + output, err := executeCommand(c, "two", "three", "one") expectSuccess(output, err, t) } func TestExactValidArgsWithInvalidCount(t *testing.T) { - c := &Command{Use: "c", Args: ExactValidArgs(2), Run: emptyRun} - _, err := executeCommand(c, "a", "b", "c") + c := getCommand(ExactValidArgs(2), false) + _, err := executeCommand(c, "two", "three", "one") if err == nil { t.Fatal("Expected an error") @@ -158,33 +160,28 @@ func TestExactValidArgsWithInvalidCount(t *testing.T) { } func TestExactValidArgsWithInvalidArgs(t *testing.T) { - c := &Command{ - Use: "c", - Args: ExactValidArgs(1), - ValidArgs: []string{"one", "two"}, - Run: emptyRun, - } + c := getCommand(ExactValidArgs(1), true) - _, err := executeCommand(c, "three") + _, err := executeCommand(c, "a") if err == nil { t.Fatal("Expected an error") } got := err.Error() - expected := `invalid argument "three" for "c"` + expected := `invalid argument "a" for "c"` if got != expected { t.Errorf("Expected: %q, got: %q", expected, got) } } func TestRangeArgs(t *testing.T) { - c := &Command{Use: "c", Args: RangeArgs(2, 4), Run: emptyRun} + c := getCommand(RangeArgs(2, 4), false) output, err := executeCommand(c, "a", "b", "c") expectSuccess(output, err, t) } func TestRangeArgsWithInvalidCount(t *testing.T) { - c := &Command{Use: "c", Args: RangeArgs(2, 4), Run: emptyRun} + c := getCommand(RangeArgs(2, 4), false) _, err := executeCommand(c, "a") if err == nil { From 4120f6e4dc253ffd4239189c72c3bbb165bb414d Mon Sep 17 00:00:00 2001 From: umarcor Date: Mon, 18 Mar 2019 23:45:36 +0100 Subject: [PATCH 3/3] args_test: add additional helper functions * noArgsWithArgs * validWithInvalidArgs * minimumNArgsWithLessArgs * maximumNArgsWithMoreArgs * exactArgsWithInvalidCount * rangeArgsWithInvalidCount --- args_test.go | 159 ++++++++++++++++++++++++--------------------------- 1 file changed, 76 insertions(+), 83 deletions(-) diff --git a/args_test.go b/args_test.go index 2db6191b4..0c25b97af 100644 --- a/args_test.go +++ b/args_test.go @@ -26,20 +26,21 @@ func expectSuccess(output string, err error, t *testing.T) { } } -func TestNoArgs(t *testing.T) { - c := getCommand(NoArgs, false) - output, err := executeCommand(c) - expectSuccess(output, err, t) +func validWithInvalidArgs(err error, t *testing.T) { + if err == nil { + t.Fatal("Expected an error") + } + got := err.Error() + expected := `invalid argument "a" for "c"` + if got != expected { + t.Errorf("Expected: %q, got: %q", expected, got) + } } -func TestNoArgsWithArgs(t *testing.T) { - c := getCommand(NoArgs, false) - - _, err := executeCommand(c, "illegal") +func noArgsWithArgs(err error, t *testing.T) { if err == nil { t.Fatal("Expected an error") } - got := err.Error() expected := `unknown command "illegal" for "c"` if got != expected { @@ -47,28 +48,74 @@ func TestNoArgsWithArgs(t *testing.T) { } } -func TestOnlyValidArgs(t *testing.T) { - c := getCommand(OnlyValidArgs, true) - - output, err := executeCommand(c, "one", "two") - expectSuccess(output, err, t) +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 TestOnlyValidArgsWithInvalidArgs(t *testing.T) { - c := getCommand(OnlyValidArgs, true) +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) + } +} - _, err := executeCommand(c, "a") +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 := `invalid argument "a" for "c"` + expected := "accepts between 2 and 4 arg(s), received 1" if got != expected { - t.Errorf("Expected: %q, got: %q", expected, got) + t.Fatalf("Expected %q, got %q", expected, got) } } +func TestNoArgs(t *testing.T) { + c := getCommand(NoArgs, false) + output, err := executeCommand(c) + expectSuccess(output, err, t) +} + +func TestNoArgsWithArgs(t *testing.T) { + c := getCommand(NoArgs, false) + _, err := executeCommand(c, "illegal") + noArgsWithArgs(err, t) +} + +func TestOnlyValidArgs(t *testing.T) { + c := getCommand(OnlyValidArgs, true) + output, err := executeCommand(c, "one", "two") + expectSuccess(output, err, t) +} + +func TestOnlyValidArgsWithInvalidArgs(t *testing.T) { + c := getCommand(OnlyValidArgs, true) + _, err := executeCommand(c, "a") + validWithInvalidArgs(err, t) +} + func TestArbitraryArgs(t *testing.T) { c := getCommand(ArbitraryArgs, false) output, err := executeCommand(c, "a", "b") @@ -84,16 +131,7 @@ func TestMinimumNArgs(t *testing.T) { func TestMinimumNArgsWithLessArgs(t *testing.T) { c := getCommand(MinimumNArgs(2), false) _, err := executeCommand(c, "a") - - 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) - } + minimumNArgsWithLessArgs(err, t) } func TestMaximumNArgs(t *testing.T) { @@ -105,16 +143,7 @@ func TestMaximumNArgs(t *testing.T) { func TestMaximumNArgsWithMoreArgs(t *testing.T) { c := getCommand(MaximumNArgs(2), false) _, err := executeCommand(c, "a", "b", "c") - - 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) - } + maximumNArgsWithMoreArgs(err, t) } func TestExactArgs(t *testing.T) { @@ -126,52 +155,25 @@ func TestExactArgs(t *testing.T) { func TestExactArgsWithInvalidCount(t *testing.T) { c := getCommand(ExactArgs(2), false) _, err := executeCommand(c, "a", "b", "c") - - 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) - } + exactArgsWithInvalidCount(err, t) } func TestExactValidArgs(t *testing.T) { c := getCommand(ExactValidArgs(3), true) - output, err := executeCommand(c, "two", "three", "one") + output, err := executeCommand(c, "three", "one", "two") expectSuccess(output, err, t) } func TestExactValidArgsWithInvalidCount(t *testing.T) { c := getCommand(ExactValidArgs(2), false) - _, err := executeCommand(c, "two", "three", "one") - - 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) - } + _, err := executeCommand(c, "three", "one", "two") + exactArgsWithInvalidCount(err, t) } func TestExactValidArgsWithInvalidArgs(t *testing.T) { - c := getCommand(ExactValidArgs(1), true) - - _, err := executeCommand(c, "a") - if err == nil { - t.Fatal("Expected an error") - } - - got := err.Error() - expected := `invalid argument "a" for "c"` - if got != expected { - t.Errorf("Expected: %q, got: %q", expected, got) - } + c := getCommand(ExactValidArgs(3), true) + _, err := executeCommand(c, "three", "a", "two") + validWithInvalidArgs(err, t) } func TestRangeArgs(t *testing.T) { @@ -183,16 +185,7 @@ func TestRangeArgs(t *testing.T) { func TestRangeArgsWithInvalidCount(t *testing.T) { c := getCommand(RangeArgs(2, 4), false) _, err := executeCommand(c, "a") - - 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) - } + rangeArgsWithInvalidCount(err, t) } func TestRootTakesNoArgs(t *testing.T) {