Skip to content

Commit

Permalink
args_test: add additional helper functions
Browse files Browse the repository at this point in the history
* noArgsWithArgs
* validWithInvalidArgs
* minimumNArgsWithLessArgs
* maximumNArgsWithMoreArgs
* exactArgsWithInvalidCount
* rangeArgsWithInvalidCount
  • Loading branch information
umarcor committed Nov 15, 2021
1 parent 9895902 commit 4120f6e
Showing 1 changed file with 76 additions and 83 deletions.
159 changes: 76 additions & 83 deletions args_test.go
Expand Up @@ -26,49 +26,96 @@ 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 {
t.Errorf("Expected: %q, got: %q", expected, got)
}
}

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

0 comments on commit 4120f6e

Please sign in to comment.