mirror of https://github.com/spf13/viper.git
Add test for top level keys
This commit is contained in:
parent
4c6d781515
commit
6816bca321
|
@ -44,6 +44,23 @@ var yamlExampleWithExtras = []byte(`Existing: true
|
|||
Bogus: true
|
||||
`)
|
||||
|
||||
var yamlTopLevelKeys = []byte(`
|
||||
restuarants:
|
||||
types:
|
||||
sushi:
|
||||
name: sushi2go
|
||||
rating: 10
|
||||
price: average
|
||||
italian:
|
||||
name: "pizza place"
|
||||
rating: 7
|
||||
price: average
|
||||
thai:
|
||||
name: "thai garden"
|
||||
rating: 9
|
||||
price: high
|
||||
`)
|
||||
|
||||
type testUnmarshalExtra struct {
|
||||
Existing bool
|
||||
}
|
||||
|
@ -462,6 +479,35 @@ func TestAllKeysWithEnv(t *testing.T) {
|
|||
assert.Equal(t, expectedKeys, keys)
|
||||
}
|
||||
|
||||
func TestTopLevelKeys(t *testing.T) {
|
||||
v := New()
|
||||
v.SetConfigType("yaml")
|
||||
r := bytes.NewReader(yamlTopLevelKeys)
|
||||
err := v.ReadConfig(r)
|
||||
assert.NoError(t, err)
|
||||
fmt.Printf("%#v\n", v.config)
|
||||
s := v.Sub("restuarants.types")
|
||||
for _, i := range s.TopLevelKeys() {
|
||||
ss := s.Sub(i)
|
||||
switch i {
|
||||
case "sushi":
|
||||
assert.Equal(t, ss.GetString("name"), "sushi2go")
|
||||
assert.Equal(t, ss.GetString("price"), "average")
|
||||
assert.Equal(t, ss.GetInt("rating"), 10)
|
||||
case "italian":
|
||||
assert.Equal(t, ss.GetString("name"), "pizza place")
|
||||
assert.Equal(t, ss.GetString("price"), "average")
|
||||
assert.Equal(t, ss.GetInt("rating"), 7)
|
||||
case "thai":
|
||||
assert.Equal(t, ss.GetString("name"), "thai garden")
|
||||
assert.Equal(t, ss.GetString("price"), "high")
|
||||
assert.Equal(t, ss.GetInt("rating"), 9)
|
||||
default:
|
||||
t.Fatalf("unexpected key, %q", i)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAliasesOfAliases(t *testing.T) {
|
||||
Set("Title", "Checking Case")
|
||||
RegisterAlias("Foo", "Bar")
|
||||
|
|
Loading…
Reference in New Issue