Skip to content
Myr

args_test: add helper functions (#1426)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* args_test: add helper function expectSuccess * args_test: add helper function getCommand * args_test: add additional helper functions * noArgsWithArgs * validWithInvalidArgs * minimumNArgsWithLessArgs * maximumNArgsWithMoreArgs * exactArgsWithInvalidCount * rangeArgsWithInvalidCount

authored by Unai Martinez-Corral · committed by GitHub · parent 62a72cd · Browse files · 1 file+103 added−145 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+103 added−145 removed

  • other:+103 added−145 removed

1 file+103 added−145 removed

modified:args_test.go+103 added-145 removed
3 unmodified lines
4 "strings"5 "testing"6)78func TestNoArgs(t *testing.T) {9 c := &Command{Use: "c", Args: NoArgs, Run: emptyRun}8func getCommand(args PositionalArgs, withValid bool) *Command {9 c := &Command{10 Use: "c",11 Args: args,12 Run: emptyRun,13 }14 if withValid {15 c.ValidArgs = []string{"one", "two", "three"}16 }17 return c18}1911 output, err := executeCommand(c)20func expectSuccess(output string, err error, t *testing.T) {21 if output != "" {13 t.Errorf("Unexpected string: %v", output)22 t.Errorf("Unexpected output: %v", output)23 }24 if err != nil {25 t.Fatalf("Unexpected error: %v", err)26 }27}2820func TestNoArgsWithArgs(t *testing.T) {21 c := &Command{Use: "c", Args: NoArgs, Run: emptyRun}2223 _, err := executeCommand(c, "illegal")29func validWithInvalidArgs(err error, t *testing.T) {30 if err == nil {31 t.Fatal("Expected an error")32 }33 got := err.Error()34 expected := `invalid argument "a" for "c"`35 if got != expected {36 t.Errorf("Expected: %q, got: %q", expected, got)37 }38}3940func noArgsWithArgs(err error, t *testing.T) {41 if err == nil {42 t.Fatal("Expected an error")43 }44 got := err.Error()45 expected := `unknown command "illegal" for "c"`46 if got != expected {47 t.Errorf("Expected: %q, got: %q", expected, got)48 }49}5035func TestOnlyValidArgs(t *testing.T) {36 c := &Command{37 Use: "c",38 Args: OnlyValidArgs,39 ValidArgs: []string{"one", "two"},40 Run: emptyRun,51func minimumNArgsWithLessArgs(err error, t *testing.T) {52 if err == nil {53 t.Fatal("Expected an error")54 }55 got := err.Error()56 expected := "requires at least 2 arg(s), only received 1"57 if got != expected {58 t.Fatalf("Expected %q, got %q", expected, got)59 }60}6143 output, err := executeCommand(c, "one", "two")44 if output != "" {45 t.Errorf("Unexpected output: %v", output)62func maximumNArgsWithMoreArgs(err error, t *testing.T) {63 if err == nil {64 t.Fatal("Expected an error")65 }47 if err != nil {48 t.Fatalf("Unexpected error: %v", err)66 got := err.Error()67 expected := "accepts at most 2 arg(s), received 3"68 if got != expected {69 t.Fatalf("Expected %q, got %q", expected, got)70 }71}7252func TestOnlyValidArgsWithInvalidArgs(t *testing.T) {53 c := &Command{54 Use: "c",55 Args: OnlyValidArgs,56 ValidArgs: []string{"one", "two"},57 Run: emptyRun,73func exactArgsWithInvalidCount(err error, t *testing.T) {74 if err == nil {75 t.Fatal("Expected an error")76 }77 got := err.Error()78 expected := "accepts 2 arg(s), received 3"79 if got != expected {80 t.Fatalf("Expected %q, got %q", expected, got)81 }82}8360 _, err := executeCommand(c, "three")84func rangeArgsWithInvalidCount(err error, t *testing.T) {85 if err == nil {86 t.Fatal("Expected an error")87 }6488 got := err.Error()66 expected := `invalid argument "three" for "c"`89 expected := "accepts between 2 and 4 arg(s), received 1"90 if got != expected {68 t.Errorf("Expected: %q, got: %q", expected, got)91 t.Fatalf("Expected %q, got %q", expected, got)92 }93}9495func TestNoArgs(t *testing.T) {96 c := getCommand(NoArgs, false)97 output, err := executeCommand(c)98 expectSuccess(output, err, t)99}100101func TestNoArgsWithArgs(t *testing.T) {102 c := getCommand(NoArgs, false)103 _, err := executeCommand(c, "illegal")104 noArgsWithArgs(err, t)105}106107func TestOnlyValidArgs(t *testing.T) {108 c := getCommand(OnlyValidArgs, true)109 output, err := executeCommand(c, "one", "two")110 expectSuccess(output, err, t)111}112113func TestOnlyValidArgsWithInvalidArgs(t *testing.T) {114 c := getCommand(OnlyValidArgs, true)115 _, err := executeCommand(c, "a")116 validWithInvalidArgs(err, t)117}118119func TestArbitraryArgs(t *testing.T) {73 c := &Command{Use: "c", Args: ArbitraryArgs, Run: emptyRun}120 c := getCommand(ArbitraryArgs, false)121 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 }122 expectSuccess(output, err, t)123}124125func TestMinimumNArgs(t *testing.T) {84 c := &Command{Use: "c", Args: MinimumNArgs(2), Run: emptyRun}126 c := getCommand(MinimumNArgs(2), false)127 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 }128 expectSuccess(output, err, t)129}130131func TestMinimumNArgsWithLessArgs(t *testing.T) {95 c := &Command{Use: "c", Args: MinimumNArgs(2), Run: emptyRun}132 c := getCommand(MinimumNArgs(2), false)133 _, 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 }134 minimumNArgsWithLessArgs(err, t)135}136137func TestMaximumNArgs(t *testing.T) {110 c := &Command{Use: "c", Args: MaximumNArgs(3), Run: emptyRun}138 c := getCommand(MaximumNArgs(3), false)139 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 }140 expectSuccess(output, err, t)141}142143func TestMaximumNArgsWithMoreArgs(t *testing.T) {121 c := &Command{Use: "c", Args: MaximumNArgs(2), Run: emptyRun}144 c := getCommand(MaximumNArgs(2), false)145 _, 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 }146 maximumNArgsWithMoreArgs(err, t)147}148149func TestExactArgs(t *testing.T) {136 c := &Command{Use: "c", Args: ExactArgs(3), Run: emptyRun}150 c := getCommand(ExactArgs(3), false)151 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 }152 expectSuccess(output, err, t)153}154155func TestExactArgsWithInvalidCount(t *testing.T) {147 c := &Command{Use: "c", Args: ExactArgs(2), Run: emptyRun}156 c := getCommand(ExactArgs(2), false)157 _, 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 }158 exactArgsWithInvalidCount(err, t)159}160161func TestExactValidArgs(t *testing.T) {162 c := &Command{Use: "c", Args: ExactValidArgs(3), ValidArgs: []string{"a", "b", "c"}, 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 }162 c := getCommand(ExactValidArgs(3), true)163 output, err := executeCommand(c, "three", "one", "two")164 expectSuccess(output, err, t)165}166167func TestExactValidArgsWithInvalidCount(t *testing.T) {173 c := &Command{Use: "c", Args: ExactValidArgs(2), Run: emptyRun}174 _, err := executeCommand(c, "a", "b", "c")175176 if err == nil {177 t.Fatal("Expected an error")178 }179180 got := err.Error()181 expected := "accepts 2 arg(s), received 3"182 if got != expected {183 t.Fatalf("Expected %q, got %q", expected, got)184 }168 c := getCommand(ExactValidArgs(2), false)169 _, err := executeCommand(c, "three", "one", "two")170 exactArgsWithInvalidCount(err, t)171}172173func TestExactValidArgsWithInvalidArgs(t *testing.T) {188 c := &Command{189 Use: "c",190 Args: ExactValidArgs(1),191 ValidArgs: []string{"one", "two"},192 Run: emptyRun,193 }194195 _, err := executeCommand(c, "three")196 if err == nil {197 t.Fatal("Expected an error")198 }199200 got := err.Error()201 expected := `invalid argument "three" for "c"`202 if got != expected {203 t.Errorf("Expected: %q, got: %q", expected, got)204 }174 c := getCommand(ExactValidArgs(3), true)175 _, err := executeCommand(c, "three", "a", "two")176 validWithInvalidArgs(err, t)177}178179func TestRangeArgs(t *testing.T) {208 c := &Command{Use: "c", Args: RangeArgs(2, 4), Run: emptyRun}180 c := getCommand(RangeArgs(2, 4), false)181 output, err := executeCommand(c, "a", "b", "c")210 if output != "" {211 t.Errorf("Unexpected output: %v", output)212 }213 if err != nil {214 t.Errorf("Unexpected error: %v", err)215 }182 expectSuccess(output, err, t)183}184185func TestRangeArgsWithInvalidCount(t *testing.T) {219 c := &Command{Use: "c", Args: RangeArgs(2, 4), Run: emptyRun}186 c := getCommand(RangeArgs(2, 4), false)187 _, err := executeCommand(c, "a")221222 if err == nil {223 t.Fatal("Expected an error")224 }225226 got := err.Error()227 expected := "accepts between 2 and 4 arg(s), received 1"228 if got != expected {229 t.Fatalf("Expected %q, got %q", expected, got)230 }188 rangeArgsWithInvalidCount(err, t)189}190191func TestRootTakesNoArgs(t *testing.T) {192 rootCmd := &Command{Use: "root", Run: emptyRun}
53 unmodified lines