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
Provenance
- Commit
9e1d6f1- Tree
305abfa- Author
Unai Martinez-Corral <38422348+umarcor@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
62a72cd
1 file changed+103 added−145 removed
modified:args_test.go+103 added-145 removed
3 unmodified lines4 "strings"5 "testing"6 )7 8-func TestNoArgs(t *testing.T) {9- c := &Command{Use: "c", Args: NoArgs, Run: emptyRun}8+func 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+}19 11- output, err := executeCommand(c)20+func 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 }28 20-func TestNoArgsWithArgs(t *testing.T) {21- c := &Command{Use: "c", Args: NoArgs, Run: emptyRun}22-23- _, err := executeCommand(c, "illegal")29+func 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+}39 40+func 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 }50 35-func TestOnlyValidArgs(t *testing.T) {36- c := &Command{37- Use: "c",38- Args: OnlyValidArgs,39- ValidArgs: []string{"one", "two"},40- Run: emptyRun,51+func 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+}61 43- output, err := executeCommand(c, "one", "two")44- if output != "" {45- t.Errorf("Unexpected output: %v", output)62+func 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 }72 52-func TestOnlyValidArgsWithInvalidArgs(t *testing.T) {53- c := &Command{54- Use: "c",55- Args: OnlyValidArgs,56- ValidArgs: []string{"one", "two"},57- Run: emptyRun,73+func 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+}83 60- _, err := executeCommand(c, "three")84+func rangeArgsWithInvalidCount(err error, t *testing.T) {85 if err == nil {86 t.Fatal("Expected an error")87 }64-88 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 }94 95+func TestNoArgs(t *testing.T) {96+ c := getCommand(NoArgs, false)97+ output, err := executeCommand(c)98+ expectSuccess(output, err, t)99+}100+101+func TestNoArgsWithArgs(t *testing.T) {102+ c := getCommand(NoArgs, false)103+ _, err := executeCommand(c, "illegal")104+ noArgsWithArgs(err, t)105+}106+107+func TestOnlyValidArgs(t *testing.T) {108+ c := getCommand(OnlyValidArgs, true)109+ output, err := executeCommand(c, "one", "two")110+ expectSuccess(output, err, t)111+}112+113+func TestOnlyValidArgsWithInvalidArgs(t *testing.T) {114+ c := getCommand(OnlyValidArgs, true)115+ _, err := executeCommand(c, "a")116+ validWithInvalidArgs(err, t)117+}118+119 func 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 }124 125 func 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 }130 131 func 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")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- }134+ minimumNArgsWithLessArgs(err, t)135 }136 137 func 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 }142 143 func 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")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- }146+ maximumNArgsWithMoreArgs(err, t)147 }148 149 func 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 }154 155 func 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")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- }158+ exactArgsWithInvalidCount(err, t)159 }160 161 func 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 }166 167 func TestExactValidArgsWithInvalidCount(t *testing.T) {173- c := &Command{Use: "c", Args: ExactValidArgs(2), Run: emptyRun}174- _, err := executeCommand(c, "a", "b", "c")175-176- if err == nil {177- t.Fatal("Expected an error")178- }179-180- 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 }172 173 func TestExactValidArgsWithInvalidArgs(t *testing.T) {188- c := &Command{189- Use: "c",190- Args: ExactValidArgs(1),191- ValidArgs: []string{"one", "two"},192- Run: emptyRun,193- }194-195- _, err := executeCommand(c, "three")196- if err == nil {197- t.Fatal("Expected an error")198- }199-200- 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 }178 179 func 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 }184 185 func 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")221-222- if err == nil {223- t.Fatal("Expected an error")224- }225-226- 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 }190 191 func TestRootTakesNoArgs(t *testing.T) {192 rootCmd := &Command{Use: "root", Run: emptyRun}53 unmodified lines