Unit testing for internal.Duration Unmarshal

closes #1926
This commit is contained in:
Cameron Sparr 2016-10-25 13:11:32 +01:00
parent 662db7a944
commit f729fa990d
2 changed files with 25 additions and 0 deletions

View File

@ -36,6 +36,12 @@ type Duration struct {
func (d *Duration) UnmarshalTOML(b []byte) error { func (d *Duration) UnmarshalTOML(b []byte) error {
var err error var err error
// see if we can straight convert it
d.Duration, err = time.ParseDuration(string(b))
if err == nil {
return nil
}
// Parse string duration, ie, "1s" // Parse string duration, ie, "1s"
if uq, err := strconv.Unquote(string(b)); err == nil && len(uq) > 0 { if uq, err := strconv.Unquote(string(b)); err == nil && len(uq) > 0 {
d.Duration, err = time.ParseDuration(uq) d.Duration, err = time.ParseDuration(uq)

View File

@ -131,3 +131,22 @@ func TestRandomSleep(t *testing.T) {
elapsed = time.Since(s) elapsed = time.Since(s)
assert.True(t, elapsed < time.Millisecond*150) assert.True(t, elapsed < time.Millisecond*150)
} }
func TestDuration(t *testing.T) {
var d Duration
d.UnmarshalTOML([]byte(`"1s"`))
assert.Equal(t, time.Second, d.Duration)
d = Duration{}
d.UnmarshalTOML([]byte(`1s`))
assert.Equal(t, time.Second, d.Duration)
d = Duration{}
d.UnmarshalTOML([]byte(`10`))
assert.Equal(t, 10*time.Second, d.Duration)
d = Duration{}
d.UnmarshalTOML([]byte(`1.5`))
assert.Equal(t, time.Second, d.Duration)
}