mirror of
https://github.com/spf13/cobra
synced 2024-11-24 22:57:12 +00:00
Refactor args_test helpers
This commit is contained in:
parent
b073425b8a
commit
917f1bc97c
1 changed files with 19 additions and 44 deletions
63
args_test.go
63
args_test.go
|
@ -33,6 +33,7 @@ func getCommand(args PositionalArgs, withValid bool) *Command {
|
||||||
}
|
}
|
||||||
|
|
||||||
func expectSuccess(output string, err error, t *testing.T) {
|
func expectSuccess(output string, err error, t *testing.T) {
|
||||||
|
t.Helper()
|
||||||
if output != "" {
|
if output != "" {
|
||||||
t.Errorf("Unexpected output: %v", output)
|
t.Errorf("Unexpected output: %v", output)
|
||||||
}
|
}
|
||||||
|
@ -40,71 +41,45 @@ func expectSuccess(output string, err error, t *testing.T) {
|
||||||
t.Fatalf("Unexpected error: %v", err)
|
t.Fatalf("Unexpected error: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
func expectError(expected string, err error, t *testing.T) {
|
||||||
func validOnlyWithInvalidArgs(err error, t *testing.T) {
|
t.Helper()
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatal("Expected an error")
|
t.Fatal("Expected an error")
|
||||||
}
|
}
|
||||||
got := err.Error()
|
got := err.Error()
|
||||||
expected := `invalid argument "a" for "c"`
|
|
||||||
if got != expected {
|
if got != expected {
|
||||||
t.Errorf("Expected: %q, got: %q", expected, got)
|
t.Fatalf("Expected %q, got %q", expected, got)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func validOnlyWithInvalidArgs(err error, t *testing.T) {
|
||||||
|
t.Helper()
|
||||||
|
expectError(`invalid argument "a" for "c"`, err, t)
|
||||||
|
}
|
||||||
|
|
||||||
func noArgsWithArgs(err error, t *testing.T, arg string) {
|
func noArgsWithArgs(err error, t *testing.T, arg string) {
|
||||||
if err == nil {
|
t.Helper()
|
||||||
t.Fatal("Expected an error")
|
expectError(`unknown command "`+arg+`" for "c"`, err, t)
|
||||||
}
|
|
||||||
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) {
|
func minimumNArgsWithLessArgs(err error, t *testing.T) {
|
||||||
if err == nil {
|
t.Helper()
|
||||||
t.Fatal("Expected an error")
|
expectError("requires at least 2 arg(s), only received 1", err, t)
|
||||||
}
|
|
||||||
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) {
|
func maximumNArgsWithMoreArgs(err error, t *testing.T) {
|
||||||
if err == nil {
|
t.Helper()
|
||||||
t.Fatal("Expected an error")
|
expectError("accepts at most 2 arg(s), received 3", err, t)
|
||||||
}
|
|
||||||
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) {
|
func exactArgsWithInvalidCount(err error, t *testing.T) {
|
||||||
if err == nil {
|
t.Helper()
|
||||||
t.Fatal("Expected an error")
|
expectError("accepts 2 arg(s), received 3", err, t)
|
||||||
}
|
|
||||||
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) {
|
func rangeArgsWithInvalidCount(err error, t *testing.T) {
|
||||||
if err == nil {
|
t.Helper()
|
||||||
t.Fatal("Expected an error")
|
expectError("accepts between 2 and 4 arg(s), received 1", err, t)
|
||||||
}
|
|
||||||
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
|
||||||
|
|
Loading…
Reference in a new issue