mirror of
https://github.com/spf13/viper
synced 2024-12-23 03:57:01 +00:00
Added TestUnmarshalKeyExact() to test UnmarshalKeyExact().
This commit is contained in:
parent
6d96b16143
commit
eb4fbe638f
1 changed files with 31 additions and 0 deletions
|
@ -287,6 +287,37 @@ func TestUnmarshalExact(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestUnmarshalKeyExact(t *testing.T) {
|
||||||
|
type duration struct {
|
||||||
|
Delay time.Duration
|
||||||
|
}
|
||||||
|
|
||||||
|
type item struct {
|
||||||
|
Name string
|
||||||
|
Delay time.Duration
|
||||||
|
Nested duration
|
||||||
|
}
|
||||||
|
|
||||||
|
config := `invalid_but_irrelevant=true
|
||||||
|
[[parent]]
|
||||||
|
error=true
|
||||||
|
delay="100ms"
|
||||||
|
[parent.nested]
|
||||||
|
delay="200ms"
|
||||||
|
`
|
||||||
|
initConfig("toml", config)
|
||||||
|
|
||||||
|
var items []item
|
||||||
|
err := v.UnmarshalKeyExact("parent", &items)
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("UnmarshalKeyExact should error when populating a struct from a conf that contains unused fields")
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(t, 1, len(items))
|
||||||
|
assert.Equal(t, 100*time.Millisecond, items[0].Delay)
|
||||||
|
assert.Equal(t, 200*time.Millisecond, items[0].Nested.Delay)
|
||||||
|
}
|
||||||
|
|
||||||
func TestOverrides(t *testing.T) {
|
func TestOverrides(t *testing.T) {
|
||||||
Set("age", 40)
|
Set("age", 40)
|
||||||
assert.Equal(t, 40, Get("age"))
|
assert.Equal(t, 40, Get("age"))
|
||||||
|
|
Loading…
Reference in a new issue