Skip to content
Myr

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

1 file+241 added−0 removed

added:args_test.go+241 added
1package cobra23import (4 "strings"5 "testing"6)78func TestNoArgs(t *testing.T) {9 c := &Command{Use: "c", Args: NoArgs, Run: emptyRun}1011 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}1920func TestNoArgsWithArgs(t *testing.T) {21 c := &Command{Use: "c", Args: NoArgs, Run: emptyRun}2223 _, err := executeCommand(c, "illegal")24 if err == nil {25 t.Fatal("Expected an error")26 }2728 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}3435func TestOnlyValidArgs(t *testing.T) {36 c := &Command{37 Use: "c",38 Args: OnlyValidArgs,39 ValidArgs: []string{"one", "two"},40 Run: emptyRun,41 }4243 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}5152func TestOnlyValidArgsWithInvalidArgs(t *testing.T) {53 c := &Command{54 Use: "c",55 Args: OnlyValidArgs,56 ValidArgs: []string{"one", "two"},57 Run: emptyRun,58 }5960 _, err := executeCommand(c, "three")61 if err == nil {62 t.Fatal("Expected an error")63 }6465 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}7172func 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}8283func 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}9394func TestMinimumNArgsWithLessArgs(t *testing.T) {95 c := &Command{Use: "c", Args: MinimumNArgs(2), Run: emptyRun}96 _, err := executeCommand(c, "a")9798 if err == nil {99 t.Fatal("Expected an error")100 }101102 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}108109func 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}119120func TestMaximumNArgsWithMoreArgs(t *testing.T) {121 c := &Command{Use: "c", Args: MaximumNArgs(2), Run: emptyRun}122 _, err := executeCommand(c, "a", "b", "c")123124 if err == nil {125 t.Fatal("Expected an error")126 }127128 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}134135func 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}145146func TestExactArgsWithInvalidCount(t *testing.T) {147 c := &Command{Use: "c", Args: ExactArgs(2), Run: emptyRun}148 _, err := executeCommand(c, "a", "b", "c")149150 if err == nil {151 t.Fatal("Expected an error")152 }153154 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}160161func 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}171172func TestRangeArgsWithInvalidCount(t *testing.T) {173 c := &Command{Use: "c", Args: RangeArgs(2, 4), Run: emptyRun}174 _, err := executeCommand(c, "a")175176 if err == nil {177 t.Fatal("Expected an error")178 }179180 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}186187func TestRootTakesNoArgs(t *testing.T) {188 rootCmd := &Command{Use: "root", Run: emptyRun}189 childCmd := &Command{Use: "child", Run: emptyRun}190 rootCmd.AddCommand(childCmd)191192 _, err := executeCommand(rootCmd, "illegal", "args")193 if err == nil {194 t.Fatal("Expected an error")195 }196197 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}203204func TestRootTakesArgs(t *testing.T) {205 rootCmd := &Command{Use: "root", Args: ArbitraryArgs, Run: emptyRun}206 childCmd := &Command{Use: "child", Run: emptyRun}207 rootCmd.AddCommand(childCmd)208209 _, err := executeCommand(rootCmd, "legal", "args")210 if err != nil {211 t.Errorf("Unexpected error: %v", err)212 }213}214215func TestChildTakesNoArgs(t *testing.T) {216 rootCmd := &Command{Use: "root", Run: emptyRun}217 childCmd := &Command{Use: "child", Args: NoArgs, Run: emptyRun}218 rootCmd.AddCommand(childCmd)219220 _, err := executeCommand(rootCmd, "child", "illegal", "args")221 if err == nil {222 t.Fatal("Expected an error")223 }224225 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}231232func TestChildTakesArgs(t *testing.T) {233 rootCmd := &Command{Use: "root", Run: emptyRun}234 childCmd := &Command{Use: "child", Args: ArbitraryArgs, Run: emptyRun}235 rootCmd.AddCommand(childCmd)236237 _, err := executeCommand(rootCmd, "child", "legal", "args")238 if err != nil {239 t.Fatalf("Unexpected error: %v", err)240 }241}