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(string(buf), check.Equals, `{"D":"1.234s"}`)
28 for _, trial := range []struct {
39 {360610, "100h10m10s"},
41 buf, err := json.Marshal(Duration(time.Duration(trial.seconds) * time.Second))
42 c.Check(err, check.IsNil)
43 c.Check(string(buf), check.Equals, `"`+trial.out+`"`)
47 func (s *DurationSuite) TestUnmarshalJSON(c *check.C) {
51 err := json.Unmarshal([]byte(`{"D":1.234}`), &d)
52 c.Check(err, check.ErrorMatches, `missing unit in duration 1.234`)
53 err = json.Unmarshal([]byte(`{"D":"1.234"}`), &d)
54 c.Check(err, check.ErrorMatches, `.*missing unit in duration 1.234`)
55 err = json.Unmarshal([]byte(`{"D":"1"}`), &d)
56 c.Check(err, check.ErrorMatches, `.*missing unit in duration 1`)
57 err = json.Unmarshal([]byte(`{"D":"foobar"}`), &d)
58 c.Check(err, check.ErrorMatches, `.*invalid duration foobar`)
59 err = json.Unmarshal([]byte(`{"D":"60s"}`), &d)
60 c.Check(err, check.IsNil)
61 c.Check(d.D.Duration(), check.Equals, time.Minute)