Skip to content

Commit

Permalink
Add unit test for GenericFlag Destination parsing
Browse files Browse the repository at this point in the history
The test checks if Destination provided in GenericFlag is being set as
expected.
  • Loading branch information
nkuba committed Jul 26, 2022
1 parent fb7958b commit 341be3b
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions flag_test.go
Expand Up @@ -2165,6 +2165,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()
Expand Down

0 comments on commit 341be3b

Please sign in to comment.