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

Function TestBindPFlagStringToString

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

Source from the content-addressed store, hash-verified

1320}
1321
1322func TestBindPFlagStringToString(t *testing.T) {
1323 tests := []struct {
1324 Expected map[string]string
1325 Value string
1326 }{
1327 {map[string]string{}, ""},
1328 {map[string]string{"yo": "hi"}, "yo=hi"},
1329 {map[string]string{"yo": "hi", "oh": "hi=there"}, "yo=hi,oh=hi=there"},
1330 {map[string]string{"yo": ""}, "yo="},
1331 {map[string]string{"yo": "", "oh": "hi=there"}, "yo=,oh=hi=there"},
1332 }
1333
1334 v := New() // create independent Viper object
1335 defaultVal := map[string]string{}
1336 v.SetDefault("stringtostring", defaultVal)
1337
1338 for _, testValue := range tests {
1339 flagSet := pflag.NewFlagSet("test", pflag.ContinueOnError)
1340 flagSet.StringToString("stringtostring", testValue.Expected, "test")
1341
1342 for _, changed := range []bool{true, false} {
1343 flagSet.VisitAll(func(f *pflag.Flag) {
1344 f.Value.Set(testValue.Value)
1345 f.Changed = changed
1346 })
1347
1348 err := v.BindPFlags(flagSet)
1349 require.NoError(t, err, "error binding flag set")
1350
1351 type TestMap struct {
1352 StringToString map[string]string
1353 }
1354 val := &TestMap{}
1355 err = v.Unmarshal(val)
1356 require.NoError(t, err, "cannot unmarshal")
1357 if changed {
1358 assert.Equal(t, testValue.Expected, val.StringToString)
1359 } else {
1360 assert.Equal(t, defaultVal, val.StringToString)
1361 }
1362 }
1363 }
1364}
1365
1366func TestBindPFlagStringToInt(t *testing.T) {
1367 tests := []struct {

Callers

nothing calls this directly

Calls 6

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

Tested by

no test coverage detected