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

Function TestBindPFlagStringToInt

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

Source from the content-addressed store, hash-verified

1364}
1365
1366func TestBindPFlagStringToInt(t *testing.T) {
1367 tests := []struct {
1368 Expected map[string]int
1369 Value string
1370 }{
1371 {map[string]int{"yo": 1, "oh": 21}, "yo=1,oh=21"},
1372 {map[string]int{"yo": 100000000, "oh": 0}, "yo=100000000,oh=0"},
1373 {map[string]int{}, "yo=2,oh=21.0"},
1374 {map[string]int{}, "yo=,oh=20.99"},
1375 {map[string]int{}, "yo=,oh="},
1376 }
1377
1378 v := New() // create independent Viper object
1379 defaultVal := map[string]int{}
1380 v.SetDefault("stringtoint", defaultVal)
1381
1382 for _, testValue := range tests {
1383 flagSet := pflag.NewFlagSet("test", pflag.ContinueOnError)
1384 flagSet.StringToInt("stringtoint", testValue.Expected, "test")
1385
1386 for _, changed := range []bool{true, false} {
1387 flagSet.VisitAll(func(f *pflag.Flag) {
1388 f.Value.Set(testValue.Value)
1389 f.Changed = changed
1390 })
1391
1392 err := v.BindPFlags(flagSet)
1393 require.NoError(t, err, "error binding flag set")
1394
1395 type TestMap struct {
1396 StringToInt map[string]int
1397 }
1398 val := &TestMap{}
1399 err = v.Unmarshal(val)
1400 require.NoError(t, err, "cannot unmarshal")
1401 if changed {
1402 assert.Equal(t, testValue.Expected, val.StringToInt)
1403 } else {
1404 assert.Equal(t, defaultVal, val.StringToInt)
1405 }
1406 }
1407 }
1408}
1409
1410func TestBoundCaseSensitivity(t *testing.T) {
1411 v := New()

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