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

Function TestBindPFlagsStringArray

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

Source from the content-addressed store, hash-verified

1167}
1168
1169func TestBindPFlagsStringArray(t *testing.T) {
1170 tests := []struct {
1171 Expected []string
1172 Value string
1173 }{
1174 {[]string{}, ""},
1175 {[]string{"jeden"}, "jeden"},
1176 {[]string{"dwa,trzy"}, "dwa,trzy"},
1177 {[]string{"cztery,\"piec , szesc\""}, "cztery,\"piec , szesc\""},
1178 }
1179
1180 v := New() // create independent Viper object
1181 defaultVal := []string{"default"}
1182 v.SetDefault("stringarray", defaultVal)
1183
1184 for _, testValue := range tests {
1185 flagSet := pflag.NewFlagSet("test", pflag.ContinueOnError)
1186 flagSet.StringArray("stringarray", testValue.Expected, "test")
1187
1188 for _, changed := range []bool{true, false} {
1189 flagSet.VisitAll(func(f *pflag.Flag) {
1190 f.Value.Set(testValue.Value)
1191 f.Changed = changed
1192 })
1193
1194 err := v.BindPFlags(flagSet)
1195 require.NoError(t, err, "error binding flag set")
1196
1197 type TestStr struct {
1198 StringArray []string
1199 }
1200 val := &TestStr{}
1201 err = v.Unmarshal(val)
1202 require.NoError(t, err, "cannot unmarshal")
1203 if changed {
1204 assert.Equal(t, testValue.Expected, val.StringArray)
1205 assert.Equal(t, testValue.Expected, v.Get("stringarray"))
1206 } else {
1207 assert.Equal(t, defaultVal, val.StringArray)
1208 }
1209 }
1210 }
1211}
1212
1213func TestBindPFlagsSlices(t *testing.T) {
1214 set := pflag.NewFlagSet("test", pflag.ContinueOnError)

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