@@ -3,6 +3,7 @@ package toml
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
@@ -3976,3 +3977,59 @@ func TestGithubIssue431(t *testing.T) {
|
||||
t.Errorf("UnmarshalTOML should not have been called")
|
||||
}
|
||||
}
|
||||
|
||||
type durationString struct {
|
||||
time.Duration
|
||||
}
|
||||
|
||||
func (d *durationString) UnmarshalTOML(v interface{}) error {
|
||||
d.Duration = 10 * time.Second
|
||||
return nil
|
||||
}
|
||||
|
||||
type config437Error struct {
|
||||
}
|
||||
|
||||
func (e *config437Error) UnmarshalTOML(v interface{}) error {
|
||||
return errors.New("expected")
|
||||
}
|
||||
|
||||
type config437 struct {
|
||||
HTTP struct {
|
||||
PingTimeout durationString `toml:"PingTimeout"`
|
||||
ErrorField config437Error
|
||||
} `toml:"HTTP"`
|
||||
}
|
||||
|
||||
func TestGithubIssue437(t *testing.T) {
|
||||
src := `
|
||||
[HTTP]
|
||||
PingTimeout = "32m"
|
||||
`
|
||||
cfg := &config437{}
|
||||
cfg.HTTP.PingTimeout = durationString{time.Second}
|
||||
|
||||
r := strings.NewReader(src)
|
||||
err := NewDecoder(r).Decode(cfg)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected errors %s", err)
|
||||
}
|
||||
expected := durationString{10 * time.Second}
|
||||
if cfg.HTTP.PingTimeout != expected {
|
||||
t.Fatalf("expected '%s', got '%s'", expected, cfg.HTTP.PingTimeout)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLeafUnmarshalerError(t *testing.T) {
|
||||
src := `
|
||||
[HTTP]
|
||||
ErrorField = "foo"
|
||||
`
|
||||
cfg := &config437{}
|
||||
|
||||
r := strings.NewReader(src)
|
||||
err := NewDecoder(r).Decode(cfg)
|
||||
if err == nil {
|
||||
t.Fatalf("error was expected")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user