update tests
This commit is contained in:
parent
f6777bf4bf
commit
550ed20ea4
104
context_test.go
104
context_test.go
@ -401,3 +401,107 @@ func TestContext_GlobalSet(t *testing.T) {
|
||||
expect(t, c.GlobalInt("int"), 1)
|
||||
expect(t, c.GlobalIsSet("int"), true)
|
||||
}
|
||||
|
||||
func TestCheckRequiredFlags(t *testing.T) {
|
||||
tdata := []struct {
|
||||
testCase string
|
||||
parseInput []string
|
||||
flags []Flag
|
||||
expectedAnError bool
|
||||
}{
|
||||
{
|
||||
testCase: "empty",
|
||||
},
|
||||
{
|
||||
testCase: "optional",
|
||||
flags: []Flag{
|
||||
StringFlag{Name: "optionalFlag"},
|
||||
},
|
||||
},
|
||||
{
|
||||
testCase: "required",
|
||||
flags: []Flag{
|
||||
StringFlag{Name: "requiredFlag", Required: true},
|
||||
},
|
||||
expectedAnError: true,
|
||||
},
|
||||
{
|
||||
testCase: "required_and_present",
|
||||
flags: []Flag{
|
||||
StringFlag{Name: "requiredFlag", Required: true},
|
||||
},
|
||||
parseInput: []string{"--requiredFlag", "myinput"},
|
||||
},
|
||||
{
|
||||
testCase: "required_and_optional",
|
||||
flags: []Flag{
|
||||
StringFlag{Name: "requiredFlag", Required: true},
|
||||
StringFlag{Name: "optionalFlag"},
|
||||
},
|
||||
expectedAnError: true,
|
||||
},
|
||||
{
|
||||
testCase: "required_and_optional_and_optional_present",
|
||||
flags: []Flag{
|
||||
StringFlag{Name: "requiredFlag", Required: true},
|
||||
StringFlag{Name: "optionalFlag"},
|
||||
},
|
||||
parseInput: []string{"--optionalFlag", "myinput"},
|
||||
expectedAnError: true,
|
||||
},
|
||||
{
|
||||
testCase: "required_and_optional_and_required_present",
|
||||
flags: []Flag{
|
||||
StringFlag{Name: "requiredFlag", Required: true},
|
||||
StringFlag{Name: "optionalFlag"},
|
||||
},
|
||||
parseInput: []string{"--requiredFlag", "myinput"},
|
||||
},
|
||||
{
|
||||
testCase: "two_required",
|
||||
flags: []Flag{
|
||||
StringFlag{Name: "requiredFlag", Required: true},
|
||||
StringFlag{Name: "requiredFlagTwo", Required: true},
|
||||
},
|
||||
expectedAnError: true,
|
||||
},
|
||||
{
|
||||
testCase: "two_required_and_one_present",
|
||||
flags: []Flag{
|
||||
StringFlag{Name: "requiredFlag", Required: true},
|
||||
StringFlag{Name: "requiredFlagTwo", Required: true},
|
||||
},
|
||||
parseInput: []string{"--requiredFlag", "myinput"},
|
||||
expectedAnError: true,
|
||||
},
|
||||
{
|
||||
testCase: "two_required_and_both_present",
|
||||
flags: []Flag{
|
||||
StringFlag{Name: "requiredFlag", Required: true},
|
||||
StringFlag{Name: "requiredFlagTwo", Required: true},
|
||||
},
|
||||
parseInput: []string{"--requiredFlag", "myinput", "--requiredFlagTwo", "myinput"},
|
||||
},
|
||||
}
|
||||
for _, test := range tdata {
|
||||
t.Run(test.testCase, func(t *testing.T) {
|
||||
// setup
|
||||
set := flag.NewFlagSet("test", 0)
|
||||
for _, flags := range test.flags {
|
||||
flags.Apply(set)
|
||||
}
|
||||
set.Parse(test.parseInput)
|
||||
|
||||
// logic under test
|
||||
err := checkRequiredFlags(test.flags, set)
|
||||
|
||||
// assertions
|
||||
if test.expectedAnError && err == nil {
|
||||
t.Errorf("expected an error, but there was none")
|
||||
}
|
||||
if !test.expectedAnError && err != nil {
|
||||
t.Errorf("did not expected an error, but there was one: %s", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
55
flag_test.go
55
flag_test.go
@ -1,7 +1,6 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
@ -1353,57 +1352,3 @@ func TestFlagFromFile(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestContext_CheckRequiredFlagsSuccess(t *testing.T) {
|
||||
flags := []Flag{
|
||||
StringFlag{
|
||||
Name: "required",
|
||||
Required: true,
|
||||
},
|
||||
StringFlag{
|
||||
Name: "optional",
|
||||
},
|
||||
}
|
||||
|
||||
set := flag.NewFlagSet("test", 0)
|
||||
for _, f := range flags {
|
||||
f.Apply(set)
|
||||
}
|
||||
|
||||
e := set.Parse([]string{"--required", "foo"})
|
||||
if e != nil {
|
||||
t.Errorf("Expected no error parsing but there was one: %s", e)
|
||||
}
|
||||
|
||||
err := checkRequiredFlags(flags, set)
|
||||
if err != nil {
|
||||
t.Error("Expected flag parsing to be successful")
|
||||
}
|
||||
}
|
||||
|
||||
func TestContext_CheckRequiredFlagsFailure(t *testing.T) {
|
||||
flags := []Flag{
|
||||
StringFlag{
|
||||
Name: "required",
|
||||
Required: true,
|
||||
},
|
||||
StringFlag{
|
||||
Name: "optional",
|
||||
},
|
||||
}
|
||||
|
||||
set := flag.NewFlagSet("test", 0)
|
||||
for _, f := range flags {
|
||||
f.Apply(set)
|
||||
}
|
||||
|
||||
e := set.Parse([]string{"--optional", "foo"})
|
||||
if e != nil {
|
||||
t.Errorf("Expected no error parsing but there was one: %s", e)
|
||||
}
|
||||
|
||||
err := checkRequiredFlags(flags, set)
|
||||
if err == nil {
|
||||
t.Error("Expected flag parsing to be unsuccessful")
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user