Add tests for args.go
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Albert Nigmatzianov · committed by Albert Nigmatzianov · parent f634327 · Browse files · 1 file·+241 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+241 added−0 removed
- other:+241 added−0 removed
Provenance
- Commit
18eefcd- Tree
65461b0- Author
Albert Nigmatzianov <albertnigma@gmail.com>- Committer
Albert Nigmatzianov <albertnigma@gmail.com>- Parent
f634327
1 file changed+241 added−0 removed
added:args_test.go+241 added
1+package cobra2+3+import (4+ "strings"5+ "testing"6+)7+8+func TestNoArgs(t *testing.T) {9+ c := &Command{Use: "c", Args: NoArgs, Run: emptyRun}10+11+ output, err := executeCommand(c)12+ if output != "" {13+ t.Errorf("Unexpected string: %v", output)14+ }15+ if err != nil {16+ t.Fatalf("Unexpected error: %v", err)17+ }18+}19+20+func TestNoArgsWithArgs(t *testing.T) {21+ c := &Command{Use: "c", Args: NoArgs, Run: emptyRun}22+23+ _, err := executeCommand(c, "illegal")24+ if err == nil {25+ t.Fatal("Expected an error")26+ }27+28+ got := err.Error()29+ expected := `unknown command "illegal" for "c"`30+ if got != expected {31+ t.Errorf("Expected: %q, got: %q", expected, got)32+ }33+}34+35+func TestOnlyValidArgs(t *testing.T) {36+ c := &Command{37+ Use: "c",38+ Args: OnlyValidArgs,39+ ValidArgs: []string{"one", "two"},40+ Run: emptyRun,41+ }42+43+ output, err := executeCommand(c, "one", "two")44+ if output != "" {45+ t.Errorf("Unexpected output: %v", output)46+ }47+ if err != nil {48+ t.Fatalf("Unexpected error: %v", err)49+ }50+}51+52+func TestOnlyValidArgsWithInvalidArgs(t *testing.T) {53+ c := &Command{54+ Use: "c",55+ Args: OnlyValidArgs,56+ ValidArgs: []string{"one", "two"},57+ Run: emptyRun,58+ }59+60+ _, err := executeCommand(c, "three")61+ if err == nil {62+ t.Fatal("Expected an error")63+ }64+65+ got := err.Error()66+ expected := `invalid argument "three" for "c"`67+ if got != expected {68+ t.Errorf("Expected: %q, got: %q", expected, got)69+ }70+}71+72+func TestArbitraryArgs(t *testing.T) {73+ c := &Command{Use: "c", Args: ArbitraryArgs, Run: emptyRun}74+ output, err := executeCommand(c, "a", "b")75+ if output != "" {76+ t.Errorf("Unexpected output: %v", output)77+ }78+ if err != nil {79+ t.Errorf("Unexpected error: %v", err)80+ }81+}82+83+func TestMinimumNArgs(t *testing.T) {84+ c := &Command{Use: "c", Args: MinimumNArgs(2), Run: emptyRun}85+ output, err := executeCommand(c, "a", "b", "c")86+ if output != "" {87+ t.Errorf("Unexpected output: %v", output)88+ }89+ if err != nil {90+ t.Errorf("Unexpected error: %v", err)91+ }92+}93+94+func TestMinimumNArgsWithLessArgs(t *testing.T) {95+ c := &Command{Use: "c", Args: MinimumNArgs(2), Run: emptyRun}96+ _, err := executeCommand(c, "a")97+98+ if err == nil {99+ t.Fatal("Expected an error")100+ }101+102+ got := err.Error()103+ expected := "requires at least 2 arg(s), only received 1"104+ if got != expected {105+ t.Fatalf("Expected %q, got %q", expected, got)106+ }107+}108+109+func TestMaximumNArgs(t *testing.T) {110+ c := &Command{Use: "c", Args: MaximumNArgs(3), Run: emptyRun}111+ output, err := executeCommand(c, "a", "b")112+ if output != "" {113+ t.Errorf("Unexpected output: %v", output)114+ }115+ if err != nil {116+ t.Errorf("Unexpected error: %v", err)117+ }118+}119+120+func TestMaximumNArgsWithMoreArgs(t *testing.T) {121+ c := &Command{Use: "c", Args: MaximumNArgs(2), Run: emptyRun}122+ _, err := executeCommand(c, "a", "b", "c")123+124+ if err == nil {125+ t.Fatal("Expected an error")126+ }127+128+ got := err.Error()129+ expected := "accepts at most 2 arg(s), received 3"130+ if got != expected {131+ t.Fatalf("Expected %q, got %q", expected, got)132+ }133+}134+135+func TestExactArgs(t *testing.T) {136+ c := &Command{Use: "c", Args: ExactArgs(3), Run: emptyRun}137+ output, err := executeCommand(c, "a", "b", "c")138+ if output != "" {139+ t.Errorf("Unexpected output: %v", output)140+ }141+ if err != nil {142+ t.Errorf("Unexpected error: %v", err)143+ }144+}145+146+func TestExactArgsWithInvalidCount(t *testing.T) {147+ c := &Command{Use: "c", Args: ExactArgs(2), Run: emptyRun}148+ _, err := executeCommand(c, "a", "b", "c")149+150+ if err == nil {151+ t.Fatal("Expected an error")152+ }153+154+ got := err.Error()155+ expected := "accepts 2 arg(s), received 3"156+ if got != expected {157+ t.Fatalf("Expected %q, got %q", expected, got)158+ }159+}160+161+func TestRangeArgs(t *testing.T) {162+ c := &Command{Use: "c", Args: RangeArgs(2, 4), Run: emptyRun}163+ output, err := executeCommand(c, "a", "b", "c")164+ if output != "" {165+ t.Errorf("Unexpected output: %v", output)166+ }167+ if err != nil {168+ t.Errorf("Unexpected error: %v", err)169+ }170+}171+172+func TestRangeArgsWithInvalidCount(t *testing.T) {173+ c := &Command{Use: "c", Args: RangeArgs(2, 4), Run: emptyRun}174+ _, err := executeCommand(c, "a")175+176+ if err == nil {177+ t.Fatal("Expected an error")178+ }179+180+ got := err.Error()181+ expected := "accepts between 2 and 4 arg(s), received 1"182+ if got != expected {183+ t.Fatalf("Expected %q, got %q", expected, got)184+ }185+}186+187+func TestRootTakesNoArgs(t *testing.T) {188+ rootCmd := &Command{Use: "root", Run: emptyRun}189+ childCmd := &Command{Use: "child", Run: emptyRun}190+ rootCmd.AddCommand(childCmd)191+192+ _, err := executeCommand(rootCmd, "illegal", "args")193+ if err == nil {194+ t.Fatal("Expected an error")195+ }196+197+ got := err.Error()198+ expected := `unknown command "illegal" for "root"`199+ if !strings.Contains(got, expected) {200+ t.Errorf("expected %q, got %q", expected, got)201+ }202+}203+204+func TestRootTakesArgs(t *testing.T) {205+ rootCmd := &Command{Use: "root", Args: ArbitraryArgs, Run: emptyRun}206+ childCmd := &Command{Use: "child", Run: emptyRun}207+ rootCmd.AddCommand(childCmd)208+209+ _, err := executeCommand(rootCmd, "legal", "args")210+ if err != nil {211+ t.Errorf("Unexpected error: %v", err)212+ }213+}214+215+func TestChildTakesNoArgs(t *testing.T) {216+ rootCmd := &Command{Use: "root", Run: emptyRun}217+ childCmd := &Command{Use: "child", Args: NoArgs, Run: emptyRun}218+ rootCmd.AddCommand(childCmd)219+220+ _, err := executeCommand(rootCmd, "child", "illegal", "args")221+ if err == nil {222+ t.Fatal("Expected an error")223+ }224+225+ got := err.Error()226+ expected := `unknown command "illegal" for "root child"`227+ if !strings.Contains(got, expected) {228+ t.Errorf("expected %q, got %q", expected, got)229+ }230+}231+232+func TestChildTakesArgs(t *testing.T) {233+ rootCmd := &Command{Use: "root", Run: emptyRun}234+ childCmd := &Command{Use: "child", Args: ArbitraryArgs, Run: emptyRun}235+ rootCmd.AddCommand(childCmd)236+237+ _, err := executeCommand(rootCmd, "child", "legal", "args")238+ if err != nil {239+ t.Fatalf("Unexpected error: %v", err)240+ }241+}