From ae103d7e593e371c69e832d5eb3347e2b80cbbc9 Mon Sep 17 00:00:00 2001 From: Benoit Masson Date: Mon, 17 Apr 2017 18:52:44 +0200 Subject: [PATCH] Moved shared resources out of the loops in TestBindPFlagsStringSlice() Common code and resources put out of the loops, to improve efficiency and readability. --- viper_test.go | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/viper_test.go b/viper_test.go index 21b49c9..fa3903e 100644 --- a/viper_test.go +++ b/viper_test.go @@ -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\""}, + } + + v := New() // create independent Viper object + defaultVal := []string{"default"} + v.SetDefault("stringslice", defaultVal) + + for _, testValue := range tests { + flagSet := pflag.NewFlagSet("test", pflag.ContinueOnError) + flagSet.StringSlice("stringslice", testValue.Expected, "test") for _, changed := range []bool{true, false} { - v := New() // create independent Viper object - v.SetDefault("stringslice", defaultVal) - - flagSet := pflag.NewFlagSet("test", pflag.ContinueOnError) - flagSet.StringSlice("stringslice", testValue.Expected, "test") flagSet.VisitAll(func(f *pflag.Flag) { f.Value.Set(testValue.Value) f.Changed = changed