Skip to content

Commit

Permalink
tl test suite option accepts time without unit suffix (assumes to be …
Browse files Browse the repository at this point in the history
…seconds).
  • Loading branch information
kuredoro committed Oct 16, 2021
1 parent bec05e0 commit 9a75a53
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
4 changes: 2 additions & 2 deletions scan_inputs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ foo= bar
func(t *testing.T) {
text := `= foo
foo= aaa
tl=10.0
tl=10.a
===
===
Expand All @@ -467,7 +467,7 @@ by the way...
errsWant := []error{
&cptest.LineRangeError{1, []string{"= foo"}, cptest.KeyMissing},
&cptest.LineRangeError{2, []string{"foo= aaa"}, &cptest.FieldError{"foo", cptest.ErrUnknownField}},
&cptest.LineRangeError{3, []string{"tl=10.0"}, &cptest.FieldError{"tl", &cptest.NotValueOfTypeError{"PositiveDuration", "10.0"}}},
&cptest.LineRangeError{3, []string{"tl=10.a"}, &cptest.FieldError{"tl", &cptest.NotValueOfTypeError{"PositiveDuration", "10.a"}}},
&cptest.LineRangeError{7, []string{"extra=love"}, &cptest.TestError{1, cptest.IOSeparatorMissing}},
&cptest.LineRangeError{9, []string{"oh = and", "by the way..."}, &cptest.TestError{2, cptest.IOSeparatorMissing}},
}
Expand Down
8 changes: 8 additions & 0 deletions string_map_unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ func (d *PositiveDuration) UnmarshalText(b []byte) error {
return ErrNegativePositiveDuration
}

if err != nil {
num, err := strconv.ParseFloat(string(b), 64)
if err == nil {
*d = PositiveDuration{time.Duration(num * float64(time.Second))}
return nil
}
}

*d = PositiveDuration{dur}
return err
}
Expand Down
16 changes: 16 additions & 0 deletions string_map_unmarshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,14 @@ func TestPositiveDuration(t *testing.T) {
td.Cmp(t, dur.Duration, time.Second)
})

t.Run("one second without suffix", func(t *testing.T) {
dur := &cptest.PositiveDuration{}
err := dur.UnmarshalText([]byte("1"))

td.CmpNoError(t, err)
td.Cmp(t, dur.Duration, time.Second)
})

t.Run("ten seconds", func(t *testing.T) {
dur := &cptest.PositiveDuration{}
err := dur.UnmarshalText([]byte("10s"))
Expand All @@ -652,6 +660,14 @@ func TestPositiveDuration(t *testing.T) {
td.Cmp(t, dur.Duration, 10*time.Second)
})

t.Run("ten and half seconds without suffix", func(t *testing.T) {
dur := &cptest.PositiveDuration{}
err := dur.UnmarshalText([]byte("10.5"))

td.CmpNoError(t, err)
td.Cmp(t, dur.Duration, 10500 * time.Millisecond)
})

t.Run("1 and half milliseconds", func(t *testing.T) {
dur := &cptest.PositiveDuration{}
err := dur.UnmarshalText([]byte("1.5ms"))
Expand Down

0 comments on commit 9a75a53

Please sign in to comment.