mirror of
https://github.com/spf13/cobra
synced 2025-04-11 01:07:18 +00:00
Merge 8d10b3fcb9
into ceb39aba25
This commit is contained in:
commit
967b73d0ce
1 changed files with 101 additions and 184 deletions
285
args_test.go
285
args_test.go
|
@ -20,7 +20,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,
|
||||||
|
@ -41,342 +41,265 @@ func expectSuccess(output string, err error, t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func validOnlyWithInvalidArgs(err error, t *testing.T) {
|
type ExpectedErrors int64
|
||||||
|
|
||||||
|
const (
|
||||||
|
OnlyValidWithInvalidArgs ExpectedErrors = iota
|
||||||
|
NoArgsWithArgs
|
||||||
|
MinimumNArgsWithLessArgs
|
||||||
|
MaximumNArgsWithMoreArgs
|
||||||
|
ExactArgsWithInvalidCount
|
||||||
|
RangeArgsWithInvalidCount
|
||||||
|
)
|
||||||
|
|
||||||
|
func expectErrorWithArg(err error, t *testing.T, ex ExpectedErrors, arg string) {
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatal("Expected an error")
|
t.Fatal("Expected an error")
|
||||||
}
|
}
|
||||||
got := err.Error()
|
expected := map[ExpectedErrors]string{
|
||||||
expected := `invalid argument "a" for "c"`
|
OnlyValidWithInvalidArgs: `invalid argument "a" for "c"`,
|
||||||
if got != expected {
|
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)
|
t.Errorf("Expected: %q, got: %q", expected, got)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func noArgsWithArgs(err error, t *testing.T, arg string) {
|
func expectError(err error, t *testing.T, ex ExpectedErrors) {
|
||||||
if err == nil {
|
expectErrorWithArg(err, t, ex, "")
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NoArgs
|
// NoArgs
|
||||||
|
|
||||||
func TestNoArgs(t *testing.T) {
|
func TestNoArgs(t *testing.T) {
|
||||||
c := getCommand(NoArgs, false)
|
output, err := executeCommand(newCommand(NoArgs, false))
|
||||||
output, err := executeCommand(c)
|
|
||||||
expectSuccess(output, err, t)
|
expectSuccess(output, err, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNoArgs_WithArgs(t *testing.T) {
|
func TestNoArgs_WithArgs(t *testing.T) {
|
||||||
c := getCommand(NoArgs, false)
|
_, err := executeCommand(newCommand(NoArgs, false), "one")
|
||||||
_, err := executeCommand(c, "one")
|
expectErrorWithArg(err, t, NoArgsWithArgs, "one")
|
||||||
noArgsWithArgs(err, t, "one")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNoArgs_WithValid_WithArgs(t *testing.T) {
|
func TestNoArgs_WithValid_WithArgs(t *testing.T) {
|
||||||
c := getCommand(NoArgs, true)
|
_, err := executeCommand(newCommand(NoArgs, true), "one")
|
||||||
_, err := executeCommand(c, "one")
|
expectErrorWithArg(err, t, NoArgsWithArgs, "one")
|
||||||
noArgsWithArgs(err, t, "one")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNoArgs_WithValid_WithInvalidArgs(t *testing.T) {
|
func TestNoArgs_WithValid_WithInvalidArgs(t *testing.T) {
|
||||||
c := getCommand(NoArgs, true)
|
_, err := executeCommand(newCommand(NoArgs, true), "a")
|
||||||
_, err := executeCommand(c, "a")
|
expectErrorWithArg(err, t, NoArgsWithArgs, "a")
|
||||||
noArgsWithArgs(err, t, "a")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNoArgs_WithValidOnly_WithInvalidArgs(t *testing.T) {
|
func TestNoArgs_WithValidOnly_WithInvalidArgs(t *testing.T) {
|
||||||
c := getCommand(MatchAll(OnlyValidArgs, NoArgs), true)
|
_, err := executeCommand(newCommand(MatchAll(OnlyValidArgs, NoArgs), true), "a")
|
||||||
_, err := executeCommand(c, "a")
|
expectError(err, t, OnlyValidWithInvalidArgs)
|
||||||
validOnlyWithInvalidArgs(err, t)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// OnlyValidArgs
|
// OnlyValidArgs
|
||||||
|
|
||||||
func TestOnlyValidArgs(t *testing.T) {
|
func TestOnlyValidArgs(t *testing.T) {
|
||||||
c := getCommand(OnlyValidArgs, true)
|
output, err := executeCommand(newCommand(OnlyValidArgs, true), "one", "two")
|
||||||
output, err := executeCommand(c, "one", "two")
|
|
||||||
expectSuccess(output, err, t)
|
expectSuccess(output, err, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestOnlyValidArgs_WithInvalidArgs(t *testing.T) {
|
func TestOnlyValidArgs_WithInvalidArgs(t *testing.T) {
|
||||||
c := getCommand(OnlyValidArgs, true)
|
_, err := executeCommand(newCommand(OnlyValidArgs, true), "a")
|
||||||
_, err := executeCommand(c, "a")
|
expectError(err, t, OnlyValidWithInvalidArgs)
|
||||||
validOnlyWithInvalidArgs(err, t)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ArbitraryArgs
|
// ArbitraryArgs
|
||||||
|
|
||||||
func TestArbitraryArgs(t *testing.T) {
|
func TestArbitraryArgs(t *testing.T) {
|
||||||
c := getCommand(ArbitraryArgs, false)
|
output, err := executeCommand(newCommand(ArbitraryArgs, false), "a", "b")
|
||||||
output, err := executeCommand(c, "a", "b")
|
|
||||||
expectSuccess(output, err, t)
|
expectSuccess(output, err, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestArbitraryArgs_WithValid(t *testing.T) {
|
func TestArbitraryArgs_WithValid(t *testing.T) {
|
||||||
c := getCommand(ArbitraryArgs, true)
|
output, err := executeCommand(newCommand(ArbitraryArgs, true), "one", "two")
|
||||||
output, err := executeCommand(c, "one", "two")
|
|
||||||
expectSuccess(output, err, t)
|
expectSuccess(output, err, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestArbitraryArgs_WithValid_WithInvalidArgs(t *testing.T) {
|
func TestArbitraryArgs_WithValid_WithInvalidArgs(t *testing.T) {
|
||||||
c := getCommand(ArbitraryArgs, true)
|
output, err := executeCommand(newCommand(ArbitraryArgs, true), "a")
|
||||||
output, err := executeCommand(c, "a")
|
|
||||||
expectSuccess(output, err, t)
|
expectSuccess(output, err, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestArbitraryArgs_WithValidOnly_WithInvalidArgs(t *testing.T) {
|
func TestArbitraryArgs_WithValidOnly_WithInvalidArgs(t *testing.T) {
|
||||||
c := getCommand(MatchAll(OnlyValidArgs, ArbitraryArgs), true)
|
_, err := executeCommand(newCommand(MatchAll(OnlyValidArgs, ArbitraryArgs), true), "a")
|
||||||
_, err := executeCommand(c, "a")
|
expectError(err, t, OnlyValidWithInvalidArgs)
|
||||||
validOnlyWithInvalidArgs(err, t)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// MinimumNArgs
|
// MinimumNArgs
|
||||||
|
|
||||||
func TestMinimumNArgs(t *testing.T) {
|
func TestMinimumNArgs(t *testing.T) {
|
||||||
c := getCommand(MinimumNArgs(2), false)
|
output, err := executeCommand(newCommand(MinimumNArgs(2), false), "a", "b", "c")
|
||||||
output, err := executeCommand(c, "a", "b", "c")
|
|
||||||
expectSuccess(output, err, t)
|
expectSuccess(output, err, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMinimumNArgs_WithValid(t *testing.T) {
|
func TestMinimumNArgs_WithValid(t *testing.T) {
|
||||||
c := getCommand(MinimumNArgs(2), true)
|
output, err := executeCommand(newCommand(MinimumNArgs(2), true), "one", "three")
|
||||||
output, err := executeCommand(c, "one", "three")
|
|
||||||
expectSuccess(output, err, t)
|
expectSuccess(output, err, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMinimumNArgs_WithValid__WithInvalidArgs(t *testing.T) {
|
func TestMinimumNArgs_WithValid__WithInvalidArgs(t *testing.T) {
|
||||||
c := getCommand(MinimumNArgs(2), true)
|
output, err := executeCommand(newCommand(MinimumNArgs(2), true), "a", "b")
|
||||||
output, err := executeCommand(c, "a", "b")
|
|
||||||
expectSuccess(output, err, t)
|
expectSuccess(output, err, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMinimumNArgs_WithValidOnly_WithInvalidArgs(t *testing.T) {
|
func TestMinimumNArgs_WithValidOnly_WithInvalidArgs(t *testing.T) {
|
||||||
c := getCommand(MatchAll(OnlyValidArgs, MinimumNArgs(2)), true)
|
_, err := executeCommand(newCommand(MatchAll(OnlyValidArgs, MinimumNArgs(2)), true), "a", "b")
|
||||||
_, err := executeCommand(c, "a", "b")
|
expectError(err, t, OnlyValidWithInvalidArgs)
|
||||||
validOnlyWithInvalidArgs(err, t)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMinimumNArgs_WithLessArgs(t *testing.T) {
|
func TestMinimumNArgs_WithLessArgs(t *testing.T) {
|
||||||
c := getCommand(MinimumNArgs(2), false)
|
_, err := executeCommand(newCommand(MinimumNArgs(2), false), "a")
|
||||||
_, err := executeCommand(c, "a")
|
expectError(err, t, MinimumNArgsWithLessArgs)
|
||||||
minimumNArgsWithLessArgs(err, t)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMinimumNArgs_WithLessArgs_WithValid(t *testing.T) {
|
func TestMinimumNArgs_WithLessArgs_WithValid(t *testing.T) {
|
||||||
c := getCommand(MinimumNArgs(2), true)
|
_, err := executeCommand(newCommand(MinimumNArgs(2), true), "one")
|
||||||
_, err := executeCommand(c, "one")
|
expectError(err, t, MinimumNArgsWithLessArgs)
|
||||||
minimumNArgsWithLessArgs(err, t)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMinimumNArgs_WithLessArgs_WithValid_WithInvalidArgs(t *testing.T) {
|
func TestMinimumNArgs_WithLessArgs_WithValid_WithInvalidArgs(t *testing.T) {
|
||||||
c := getCommand(MinimumNArgs(2), true)
|
_, err := executeCommand(newCommand(MinimumNArgs(2), true), "a")
|
||||||
_, err := executeCommand(c, "a")
|
expectError(err, t, MinimumNArgsWithLessArgs)
|
||||||
minimumNArgsWithLessArgs(err, t)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMinimumNArgs_WithLessArgs_WithValidOnly_WithInvalidArgs(t *testing.T) {
|
func TestMinimumNArgs_WithLessArgs_WithValidOnly_WithInvalidArgs(t *testing.T) {
|
||||||
c := getCommand(MatchAll(OnlyValidArgs, MinimumNArgs(2)), true)
|
_, err := executeCommand(newCommand(MatchAll(OnlyValidArgs, MinimumNArgs(2)), true), "a")
|
||||||
_, err := executeCommand(c, "a")
|
expectError(err, t, OnlyValidWithInvalidArgs)
|
||||||
validOnlyWithInvalidArgs(err, t)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// MaximumNArgs
|
// MaximumNArgs
|
||||||
|
|
||||||
func TestMaximumNArgs(t *testing.T) {
|
func TestMaximumNArgs(t *testing.T) {
|
||||||
c := getCommand(MaximumNArgs(3), false)
|
output, err := executeCommand(newCommand(MaximumNArgs(3), false), "a", "b")
|
||||||
output, err := executeCommand(c, "a", "b")
|
|
||||||
expectSuccess(output, err, t)
|
expectSuccess(output, err, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMaximumNArgs_WithValid(t *testing.T) {
|
func TestMaximumNArgs_WithValid(t *testing.T) {
|
||||||
c := getCommand(MaximumNArgs(2), true)
|
output, err := executeCommand(newCommand(MaximumNArgs(2), true), "one", "three")
|
||||||
output, err := executeCommand(c, "one", "three")
|
|
||||||
expectSuccess(output, err, t)
|
expectSuccess(output, err, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMaximumNArgs_WithValid_WithInvalidArgs(t *testing.T) {
|
func TestMaximumNArgs_WithValid_WithInvalidArgs(t *testing.T) {
|
||||||
c := getCommand(MaximumNArgs(2), true)
|
output, err := executeCommand(newCommand(MaximumNArgs(2), true), "a", "b")
|
||||||
output, err := executeCommand(c, "a", "b")
|
|
||||||
expectSuccess(output, err, t)
|
expectSuccess(output, err, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMaximumNArgs_WithValidOnly_WithInvalidArgs(t *testing.T) {
|
func TestMaximumNArgs_WithValidOnly_WithInvalidArgs(t *testing.T) {
|
||||||
c := getCommand(MatchAll(OnlyValidArgs, MaximumNArgs(2)), true)
|
_, err := executeCommand(newCommand(MatchAll(OnlyValidArgs, MaximumNArgs(2)), true), "a", "b")
|
||||||
_, err := executeCommand(c, "a", "b")
|
expectError(err, t, OnlyValidWithInvalidArgs)
|
||||||
validOnlyWithInvalidArgs(err, t)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMaximumNArgs_WithMoreArgs(t *testing.T) {
|
func TestMaximumNArgs_WithMoreArgs(t *testing.T) {
|
||||||
c := getCommand(MaximumNArgs(2), false)
|
_, err := executeCommand(newCommand(MaximumNArgs(2), false), "a", "b", "c")
|
||||||
_, err := executeCommand(c, "a", "b", "c")
|
expectError(err, t, MaximumNArgsWithMoreArgs)
|
||||||
maximumNArgsWithMoreArgs(err, t)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMaximumNArgs_WithMoreArgs_WithValid(t *testing.T) {
|
func TestMaximumNArgs_WithMoreArgs_WithValid(t *testing.T) {
|
||||||
c := getCommand(MaximumNArgs(2), true)
|
_, err := executeCommand(newCommand(MaximumNArgs(2), true), "one", "three", "two")
|
||||||
_, err := executeCommand(c, "one", "three", "two")
|
expectError(err, t, MaximumNArgsWithMoreArgs)
|
||||||
maximumNArgsWithMoreArgs(err, t)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMaximumNArgs_WithMoreArgs_WithValid_WithInvalidArgs(t *testing.T) {
|
func TestMaximumNArgs_WithMoreArgs_WithValid_WithInvalidArgs(t *testing.T) {
|
||||||
c := getCommand(MaximumNArgs(2), true)
|
_, err := executeCommand(newCommand(MaximumNArgs(2), true), "a", "b", "c")
|
||||||
_, err := executeCommand(c, "a", "b", "c")
|
expectError(err, t, MaximumNArgsWithMoreArgs)
|
||||||
maximumNArgsWithMoreArgs(err, t)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMaximumNArgs_WithMoreArgs_WithValidOnly_WithInvalidArgs(t *testing.T) {
|
func TestMaximumNArgs_WithMoreArgs_WithValidOnly_WithInvalidArgs(t *testing.T) {
|
||||||
c := getCommand(MatchAll(OnlyValidArgs, MaximumNArgs(2)), true)
|
_, err := executeCommand(newCommand(MatchAll(OnlyValidArgs, MaximumNArgs(2)), true), "a", "b", "c")
|
||||||
_, err := executeCommand(c, "a", "b", "c")
|
expectError(err, t, OnlyValidWithInvalidArgs)
|
||||||
validOnlyWithInvalidArgs(err, t)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ExactArgs
|
// ExactArgs
|
||||||
|
|
||||||
func TestExactArgs(t *testing.T) {
|
func TestExactArgs(t *testing.T) {
|
||||||
c := getCommand(ExactArgs(3), false)
|
output, err := executeCommand(newCommand(ExactArgs(3), false), "a", "b", "c")
|
||||||
output, err := executeCommand(c, "a", "b", "c")
|
|
||||||
expectSuccess(output, err, t)
|
expectSuccess(output, err, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestExactArgs_WithValid(t *testing.T) {
|
func TestExactArgs_WithValid(t *testing.T) {
|
||||||
c := getCommand(ExactArgs(3), true)
|
output, err := executeCommand(newCommand(ExactArgs(3), true), "three", "one", "two")
|
||||||
output, err := executeCommand(c, "three", "one", "two")
|
|
||||||
expectSuccess(output, err, t)
|
expectSuccess(output, err, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestExactArgs_WithValid_WithInvalidArgs(t *testing.T) {
|
func TestExactArgs_WithValid_WithInvalidArgs(t *testing.T) {
|
||||||
c := getCommand(ExactArgs(3), true)
|
output, err := executeCommand(newCommand(ExactArgs(3), true), "three", "a", "two")
|
||||||
output, err := executeCommand(c, "three", "a", "two")
|
|
||||||
expectSuccess(output, err, t)
|
expectSuccess(output, err, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestExactArgs_WithValidOnly_WithInvalidArgs(t *testing.T) {
|
func TestExactArgs_WithValidOnly_WithInvalidArgs(t *testing.T) {
|
||||||
c := getCommand(MatchAll(OnlyValidArgs, ExactArgs(3)), true)
|
_, err := executeCommand(newCommand(MatchAll(OnlyValidArgs, ExactArgs(3)), true), "three", "a", "two")
|
||||||
_, err := executeCommand(c, "three", "a", "two")
|
expectError(err, t, OnlyValidWithInvalidArgs)
|
||||||
validOnlyWithInvalidArgs(err, t)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestExactArgs_WithInvalidCount(t *testing.T) {
|
func TestExactArgs_WithInvalidCount(t *testing.T) {
|
||||||
c := getCommand(ExactArgs(2), false)
|
_, err := executeCommand(newCommand(ExactArgs(2), false), "a", "b", "c")
|
||||||
_, err := executeCommand(c, "a", "b", "c")
|
expectError(err, t, ExactArgsWithInvalidCount)
|
||||||
exactArgsWithInvalidCount(err, t)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestExactArgs_WithInvalidCount_WithValid(t *testing.T) {
|
func TestExactArgs_WithInvalidCount_WithValid(t *testing.T) {
|
||||||
c := getCommand(ExactArgs(2), true)
|
_, err := executeCommand(newCommand(ExactArgs(2), true), "three", "one", "two")
|
||||||
_, err := executeCommand(c, "three", "one", "two")
|
expectError(err, t, ExactArgsWithInvalidCount)
|
||||||
exactArgsWithInvalidCount(err, t)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestExactArgs_WithInvalidCount_WithValid_WithInvalidArgs(t *testing.T) {
|
func TestExactArgs_WithInvalidCount_WithValid_WithInvalidArgs(t *testing.T) {
|
||||||
c := getCommand(ExactArgs(2), true)
|
_, err := executeCommand(newCommand(ExactArgs(2), true), "three", "a", "two")
|
||||||
_, err := executeCommand(c, "three", "a", "two")
|
expectError(err, t, ExactArgsWithInvalidCount)
|
||||||
exactArgsWithInvalidCount(err, t)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestExactArgs_WithInvalidCount_WithValidOnly_WithInvalidArgs(t *testing.T) {
|
func TestExactArgs_WithInvalidCount_WithValidOnly_WithInvalidArgs(t *testing.T) {
|
||||||
c := getCommand(MatchAll(OnlyValidArgs, ExactArgs(2)), true)
|
_, err := executeCommand(newCommand(MatchAll(OnlyValidArgs, ExactArgs(2)), true), "three", "a", "two")
|
||||||
_, err := executeCommand(c, "three", "a", "two")
|
expectError(err, t, OnlyValidWithInvalidArgs)
|
||||||
validOnlyWithInvalidArgs(err, t)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// RangeArgs
|
// RangeArgs
|
||||||
|
|
||||||
func TestRangeArgs(t *testing.T) {
|
func TestRangeArgs(t *testing.T) {
|
||||||
c := getCommand(RangeArgs(2, 4), false)
|
output, err := executeCommand(newCommand(RangeArgs(2, 4), false), "a", "b", "c")
|
||||||
output, err := executeCommand(c, "a", "b", "c")
|
|
||||||
expectSuccess(output, err, t)
|
expectSuccess(output, err, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRangeArgs_WithValid(t *testing.T) {
|
func TestRangeArgs_WithValid(t *testing.T) {
|
||||||
c := getCommand(RangeArgs(2, 4), true)
|
output, err := executeCommand(newCommand(RangeArgs(2, 4), true), "three", "one", "two")
|
||||||
output, err := executeCommand(c, "three", "one", "two")
|
|
||||||
expectSuccess(output, err, t)
|
expectSuccess(output, err, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRangeArgs_WithValid_WithInvalidArgs(t *testing.T) {
|
func TestRangeArgs_WithValid_WithInvalidArgs(t *testing.T) {
|
||||||
c := getCommand(RangeArgs(2, 4), true)
|
output, err := executeCommand(newCommand(RangeArgs(2, 4), true), "three", "a", "two")
|
||||||
output, err := executeCommand(c, "three", "a", "two")
|
|
||||||
expectSuccess(output, err, t)
|
expectSuccess(output, err, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRangeArgs_WithValidOnly_WithInvalidArgs(t *testing.T) {
|
func TestRangeArgs_WithValidOnly_WithInvalidArgs(t *testing.T) {
|
||||||
c := getCommand(MatchAll(OnlyValidArgs, RangeArgs(2, 4)), true)
|
_, err := executeCommand(newCommand(MatchAll(OnlyValidArgs, RangeArgs(2, 4)), true), "three", "a", "two")
|
||||||
_, err := executeCommand(c, "three", "a", "two")
|
expectError(err, t, OnlyValidWithInvalidArgs)
|
||||||
validOnlyWithInvalidArgs(err, t)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRangeArgs_WithInvalidCount(t *testing.T) {
|
func TestRangeArgs_WithInvalidCount(t *testing.T) {
|
||||||
c := getCommand(RangeArgs(2, 4), false)
|
_, err := executeCommand(newCommand(RangeArgs(2, 4), false), "a")
|
||||||
_, err := executeCommand(c, "a")
|
expectError(err, t, RangeArgsWithInvalidCount)
|
||||||
rangeArgsWithInvalidCount(err, t)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRangeArgs_WithInvalidCount_WithValid(t *testing.T) {
|
func TestRangeArgs_WithInvalidCount_WithValid(t *testing.T) {
|
||||||
c := getCommand(RangeArgs(2, 4), true)
|
_, err := executeCommand(newCommand(RangeArgs(2, 4), true), "two")
|
||||||
_, err := executeCommand(c, "two")
|
expectError(err, t, RangeArgsWithInvalidCount)
|
||||||
rangeArgsWithInvalidCount(err, t)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRangeArgs_WithInvalidCount_WithValid_WithInvalidArgs(t *testing.T) {
|
func TestRangeArgs_WithInvalidCount_WithValid_WithInvalidArgs(t *testing.T) {
|
||||||
c := getCommand(RangeArgs(2, 4), true)
|
_, err := executeCommand(newCommand(RangeArgs(2, 4), true), "a")
|
||||||
_, err := executeCommand(c, "a")
|
expectError(err, t, RangeArgsWithInvalidCount)
|
||||||
rangeArgsWithInvalidCount(err, t)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRangeArgs_WithInvalidCount_WithValidOnly_WithInvalidArgs(t *testing.T) {
|
func TestRangeArgs_WithInvalidCount_WithValidOnly_WithInvalidArgs(t *testing.T) {
|
||||||
c := getCommand(MatchAll(OnlyValidArgs, RangeArgs(2, 4)), true)
|
_, err := executeCommand(newCommand(MatchAll(OnlyValidArgs, RangeArgs(2, 4)), true), "a")
|
||||||
_, err := executeCommand(c, "a")
|
expectError(err, t, OnlyValidWithInvalidArgs)
|
||||||
validOnlyWithInvalidArgs(err, t)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Takes(No)Args
|
// Takes(No)Args
|
||||||
|
@ -488,27 +411,23 @@ func TestMatchAll(t *testing.T) {
|
||||||
// DEPRECATED
|
// DEPRECATED
|
||||||
|
|
||||||
func TestExactValidArgs(t *testing.T) {
|
func TestExactValidArgs(t *testing.T) {
|
||||||
c := getCommand(ExactValidArgs(3), true)
|
output, err := executeCommand(newCommand(ExactValidArgs(3), true), "three", "one", "two")
|
||||||
output, err := executeCommand(c, "three", "one", "two")
|
|
||||||
expectSuccess(output, err, t)
|
expectSuccess(output, err, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestExactValidArgs_WithInvalidCount(t *testing.T) {
|
func TestExactValidArgs_WithInvalidCount(t *testing.T) {
|
||||||
c := getCommand(ExactValidArgs(2), false)
|
_, err := executeCommand(newCommand(ExactValidArgs(2), false), "three", "one", "two")
|
||||||
_, err := executeCommand(c, "three", "one", "two")
|
expectError(err, t, ExactArgsWithInvalidCount)
|
||||||
exactArgsWithInvalidCount(err, t)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestExactValidArgs_WithInvalidCount_WithInvalidArgs(t *testing.T) {
|
func TestExactValidArgs_WithInvalidCount_WithInvalidArgs(t *testing.T) {
|
||||||
c := getCommand(ExactValidArgs(2), true)
|
_, err := executeCommand(newCommand(ExactValidArgs(2), true), "three", "a", "two")
|
||||||
_, err := executeCommand(c, "three", "a", "two")
|
expectError(err, t, ExactArgsWithInvalidCount)
|
||||||
exactArgsWithInvalidCount(err, t)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestExactValidArgs_WithInvalidArgs(t *testing.T) {
|
func TestExactValidArgs_WithInvalidArgs(t *testing.T) {
|
||||||
c := getCommand(ExactValidArgs(2), true)
|
_, err := executeCommand(newCommand(ExactValidArgs(2), true), "three", "a")
|
||||||
_, err := executeCommand(c, "three", "a")
|
expectError(err, t, OnlyValidWithInvalidArgs)
|
||||||
validOnlyWithInvalidArgs(err, t)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// This test make sure we keep backwards-compatibility with respect
|
// This test make sure we keep backwards-compatibility with respect
|
||||||
|
@ -516,9 +435,7 @@ func TestExactValidArgs_WithInvalidArgs(t *testing.T) {
|
||||||
// It makes sure the root command accepts arguments if it does not have
|
// It makes sure the root command accepts arguments if it does not have
|
||||||
// sub-commands.
|
// sub-commands.
|
||||||
func TestLegacyArgsRootAcceptsArgs(t *testing.T) {
|
func TestLegacyArgsRootAcceptsArgs(t *testing.T) {
|
||||||
rootCmd := &Command{Use: "root", Args: nil, Run: emptyRun}
|
_, err := executeCommand(&Command{Use: "root", Args: nil, Run: emptyRun}, "somearg")
|
||||||
|
|
||||||
_, err := executeCommand(rootCmd, "somearg")
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Unexpected error: %v", err)
|
t.Fatalf("Unexpected error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue