Skip to content

Commit

Permalink
Moved shared resources out of the loops in TestBindPFlagsStringSlice()
Browse files Browse the repository at this point in the history
Common code and resources put out of the loops, to improve
efficiency and readability.
  • Loading branch information
benoitmasson authored and bep committed Nov 7, 2018
1 parent 69647fb commit ae103d7
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions viper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -613,23 +613,25 @@ func TestBindPFlags(t *testing.T) {
}

func TestBindPFlagsStringSlice(t *testing.T) {
defaultVal := []string{"default"}

for _, testValue := range []struct {
tests := []struct {
Expected []string
Value string
}{
{[]string{}, ""},
{[]string{"jeden"}, "jeden"},
{[]string{"dwa", "trzy"}, "dwa,trzy"},
{[]string{"cztery", "piec , szesc"}, "cztery,\"piec , szesc\""}} {
{[]string{"cztery", "piec , szesc"}, "cztery,\"piec , szesc\""},
}

for _, changed := range []bool{true, false} {
v := New() // create independent Viper object
v.SetDefault("stringslice", defaultVal)
v := New() // create independent Viper object
defaultVal := []string{"default"}
v.SetDefault("stringslice", defaultVal)

flagSet := pflag.NewFlagSet("test", pflag.ContinueOnError)
flagSet.StringSlice("stringslice", testValue.Expected, "test")
for _, testValue := range tests {
flagSet := pflag.NewFlagSet("test", pflag.ContinueOnError)
flagSet.StringSlice("stringslice", testValue.Expected, "test")

for _, changed := range []bool{true, false} {
flagSet.VisitAll(func(f *pflag.Flag) {
f.Value.Set(testValue.Value)
f.Changed = changed
Expand Down

0 comments on commit ae103d7

Please sign in to comment.