1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
11 check "gopkg.in/check.v1"
14 var _ = check.Suite(&DurationSuite{})
16 type DurationSuite struct{}
18 func (s *DurationSuite) TestMarshalJSON(c *check.C) {
22 err := json.Unmarshal([]byte(`{"D":"1.234s"}`), &d)
23 c.Check(err, check.IsNil)
24 c.Check(d.D, check.Equals, Duration(time.Second+234*time.Millisecond))
25 buf, err := json.Marshal(d)
26 c.Check(err, check.IsNil)
27 c.Check(string(buf), check.Equals, `{"D":"1.234s"}`)
29 for _, trial := range []struct {
40 {360610, "100h10m10s"},
42 buf, err := json.Marshal(Duration(time.Duration(trial.seconds) * time.Second))
43 c.Check(err, check.IsNil)
44 c.Check(string(buf), check.Equals, `"`+trial.out+`"`)
48 func (s *DurationSuite) TestUnmarshalJSON(c *check.C) {
52 err := json.Unmarshal([]byte(`{"D":1.234}`), &d)
53 c.Check(err, check.ErrorMatches, `.*missing unit in duration "?1\.234"?`)
54 err = json.Unmarshal([]byte(`{"D":"1.234"}`), &d)
55 c.Check(err, check.ErrorMatches, `.*missing unit in duration "?1\.234"?`)
56 err = json.Unmarshal([]byte(`{"D":"1"}`), &d)
57 c.Check(err, check.ErrorMatches, `.*missing unit in duration "?1"?`)
58 err = json.Unmarshal([]byte(`{"D":"foobar"}`), &d)
59 c.Check(err, check.ErrorMatches, `.*invalid duration "?foobar"?`)
60 err = json.Unmarshal([]byte(`{"D":"60s"}`), &d)
61 c.Check(err, check.IsNil)
62 c.Check(d.D.Duration(), check.Equals, time.Minute)