Add unit test for GenericFlag Destination parsing

The test checks if Destination provided in GenericFlag is being set as
expected.
This commit is contained in:
Jakub Nowakowski 2022-07-26 16:16:43 +02:00 committed by Dan Buch
parent 01bdec784f
commit b82e628617
Signed by: meatballhat
GPG Key ID: A12F782281063434

View File

@ -2710,6 +2710,53 @@ func TestParseGeneric(t *testing.T) {
}).Run([]string{"run", "-s", "10,20"})
}
type genericType struct {
s []string
}
func (g *genericType) Set(value string) error {
g.s = strings.Split(value, "-")
return nil
}
func (g *genericType) String() string {
return strings.Join(g.s, "-")
}
func TestParseDestinationGeneric(t *testing.T) {
expectedString := "abc1-123d"
expectedGeneric := &genericType{[]string{"abc1", "123d"}}
dest := &genericType{}
_ = (&App{
Flags: []Flag{
&GenericFlag{
Name: "dest",
Destination: dest,
},
},
Action: func(ctx *Context) error {
if !reflect.DeepEqual(dest, expectedGeneric) {
t.Errorf(
"expected destination generic: %+v, actual: %+v",
expectedGeneric,
dest,
)
}
if dest.String() != expectedString {
t.Errorf(
"expected destination string: %s, actual: %s",
expectedString,
dest.String(),
)
}
return nil
},
}).Run([]string{"run", "--dest", expectedString})
}
func TestParseGenericFromEnv(t *testing.T) {
defer resetEnv(os.Environ())
os.Clearenv()