Skip to content

Commit

Permalink
Unit testing for internal.Duration Unmarshal
Browse files Browse the repository at this point in the history
closes #1926
  • Loading branch information
sparrc committed Oct 25, 2016
1 parent 662db7a commit f729fa9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
6 changes: 6 additions & 0 deletions internal/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ type Duration struct {
func (d *Duration) UnmarshalTOML(b []byte) 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"
if uq, err := strconv.Unquote(string(b)); err == nil && len(uq) > 0 {
d.Duration, err = time.ParseDuration(uq)
Expand Down
19 changes: 19 additions & 0 deletions internal/internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,22 @@ func TestRandomSleep(t *testing.T) {
elapsed = time.Since(s)
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)
}

0 comments on commit f729fa9

Please sign in to comment.