Skip to content

Commit

Permalink
args_test: add helper function expectSuccess
Browse files Browse the repository at this point in the history
  • Loading branch information
umarcor committed Jul 1, 2021
1 parent 07861c8 commit 4ffea91
Showing 1 changed file with 15 additions and 47 deletions.
62 changes: 15 additions & 47 deletions args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 4ffea91

Please sign in to comment.