-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathduration_test.go
79 lines (71 loc) · 1.59 KB
/
duration_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package clock_test
import (
"encoding/json"
"testing"
"github.com/mailgun/holster/v4/clock"
"github.com/stretchr/testify/suite"
)
type DurationSuite struct {
suite.Suite
}
func TestDurationSuite(t *testing.T) {
suite.Run(t, new(DurationSuite))
}
func (s *DurationSuite) TestNewOk() {
for _, v := range []interface{}{
42 * clock.Second,
int(42000000000),
int64(42000000000),
42000000000.,
"42s",
[]byte("42s"),
} {
d, err := clock.NewDurationJSON(v)
s.Nil(err)
s.Equal(42*clock.Second, d.Duration)
}
}
func (s *DurationSuite) TestNewError() {
for _, tc := range []struct {
v interface{}
errMsg string
}{{
v: "foo",
errMsg: "while parsing string: time: invalid duration \"foo\"",
}, {
v: []byte("foo"),
errMsg: "while parsing []byte: time: invalid duration \"foo\"",
}, {
v: true,
errMsg: "bad type bool",
}} {
d, err := clock.NewDurationJSON(tc.v)
s.Equal(tc.errMsg, err.Error())
s.Equal(clock.DurationJSON{}, d)
}
}
func (s *DurationSuite) TestUnmarshal() {
for _, v := range []string{
`{"foo": 42000000000}`,
`{"foo": 0.42e11}`,
`{"foo": "42s"}`,
} {
var withDuration struct {
Foo clock.DurationJSON `json:"foo"`
}
err := json.Unmarshal([]byte(v), &withDuration)
s.Nil(err)
s.Equal(42*clock.Second, withDuration.Foo.Duration)
}
}
func (s *DurationSuite) TestMarshalling() {
d, err := clock.NewDurationJSON(42 * clock.Second)
s.Nil(err)
encoded, err := d.MarshalJSON()
s.Nil(err)
var decoded clock.DurationJSON
err = decoded.UnmarshalJSON(encoded)
s.Nil(err)
s.Equal(d, decoded)
s.Equal("42s", decoded.String())
}