MCPcopy
hub / github.com/spf13/viper / TestBindPFlagsStringSlice

Function TestBindPFlagsStringSlice

viper_test.go:1125–1167  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1123}
1124
1125func TestBindPFlagsStringSlice(t *testing.T) {
1126 tests := []struct {
1127 Expected []string
1128 Value string
1129 }{
1130 {[]string{}, ""},
1131 {[]string{"jeden"}, "jeden"},
1132 {[]string{"dwa", "trzy"}, "dwa,trzy"},
1133 {[]string{"cztery", "piec , szesc"}, "cztery,\"piec , szesc\""},
1134 }
1135
1136 v := New() // create independent Viper object
1137 defaultVal := []string{"default"}
1138 v.SetDefault("stringslice", defaultVal)
1139
1140 for _, testValue := range tests {
1141 flagSet := pflag.NewFlagSet("test", pflag.ContinueOnError)
1142 flagSet.StringSlice("stringslice", testValue.Expected, "test")
1143
1144 for _, changed := range []bool{true, false} {
1145 flagSet.VisitAll(func(f *pflag.Flag) {
1146 f.Value.Set(testValue.Value)
1147 f.Changed = changed
1148 })
1149
1150 err := v.BindPFlags(flagSet)
1151 require.NoError(t, err, "error binding flag set")
1152
1153 type TestStr struct {
1154 StringSlice []string
1155 }
1156 val := &TestStr{}
1157 err = v.Unmarshal(val)
1158 require.NoError(t, err, "cannot unmarshal")
1159 if changed {
1160 assert.Equal(t, testValue.Expected, val.StringSlice)
1161 assert.Equal(t, testValue.Expected, v.Get("stringslice"))
1162 } else {
1163 assert.Equal(t, defaultVal, val.StringSlice)
1164 }
1165 }
1166 }
1167}
1168
1169func TestBindPFlagsStringArray(t *testing.T) {
1170 tests := []struct {

Callers

nothing calls this directly

Calls 7

NewFunction · 0.85
SetDefaultMethod · 0.80
BindPFlagsMethod · 0.80
UnmarshalMethod · 0.80
VisitAllMethod · 0.65
GetMethod · 0.65
SetMethod · 0.45

Tested by

no test coverage detected