@@ -1,87 +0,0 @@
1−package options
2−
3−import (
4− "encoding/json"
5− "errors"
6− "time"
7−
8− . "github.com/onsi/ginkgo/v2"
9− . "github.com/onsi/gomega"
10−)
11−
12−var _ = Describe("Common", func() {
13− Context("Duration", func() {
14− type marshalJSONTableInput struct {
15− duration Duration
16− expectedJSON string
17− }
18−
19− DescribeTable("MarshalJSON",
20− func(in marshalJSONTableInput) {
21− data, err := in.duration.MarshalJSON()
22− Expect(err).ToNot(HaveOccurred())
23− Expect(string(data)).To(Equal(in.expectedJSON))
24−
25− var d Duration
26− Expect(json.Unmarshal(data, &d)).To(Succeed())
27− Expect(d).To(Equal(in.duration))
28− },
29− Entry("30 seconds", marshalJSONTableInput{
30− duration: Duration(30 * time.Second),
31− expectedJSON: "\"30s\"",
32− }),
33− Entry("1 minute", marshalJSONTableInput{
34− duration: Duration(1 * time.Minute),
35− expectedJSON: "\"1m0s\"",
36− }),
37− Entry("1 hour 15 minutes", marshalJSONTableInput{
38− duration: Duration(75 * time.Minute),
39− expectedJSON: "\"1h15m0s\"",
40− }),
41− Entry("A zero Duration", marshalJSONTableInput{
42− duration: Duration(0),
43− expectedJSON: "\"0s\"",
44− }),
45− )
46−
47− type unmarshalJSONTableInput struct {
48− json string
49− expectedErr error
50− expectedDuration Duration
51− }
52−
53− DescribeTable("UnmarshalJSON",
54− func(in unmarshalJSONTableInput) {
55− // A duration must be initialised pointer before UnmarshalJSON will work.
56− zero := Duration(0)
57− d := &zero
58−
59− err := d.UnmarshalJSON([]byte(in.json))
60− if in.expectedErr != nil {
61− Expect(err).To(MatchError(in.expectedErr.Error()))
62− } else {
63− Expect(err).ToNot(HaveOccurred())
64− }
65− Expect(d).ToNot(BeNil())
66− Expect(*d).To(Equal(in.expectedDuration))
67− },
68− Entry("1m", unmarshalJSONTableInput{
69− json: "\"1m\"",
70− expectedDuration: Duration(1 * time.Minute),
71− }),
72− Entry("30s", unmarshalJSONTableInput{
73− json: "\"30s\"",
74− expectedDuration: Duration(30 * time.Second),
75− }),
76− Entry("1h15m", unmarshalJSONTableInput{
77− json: "\"1h15m\"",
78− expectedDuration: Duration(75 * time.Minute),
79− }),
80− Entry("am", unmarshalJSONTableInput{
81− json: "\"am\"",
82− expectedErr: errors.New("time: invalid duration \"am\""),
83− expectedDuration: Duration(0),
84− }),
85− )
86− })
87−})