forked from streamrail/vast-1
-
Notifications
You must be signed in to change notification settings - Fork 3
/
duration_test.go
48 lines (41 loc) · 1.61 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
package vast
import (
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/extensions/table"
. "github.com/onsi/gomega"
)
var _ = Describe("Duration", func() {
DescribeTable("marshal",
func(d Duration, exp string) {
b, err := d.MarshalText()
Expect(err).NotTo(HaveOccurred())
Expect(string(b)).To(Equal(exp))
},
Entry("00:00:00", Duration(0), "00:00:00"),
Entry("00:00:00.002", Duration(2*time.Millisecond), "00:00:00.002"),
Entry("00:00:02", Duration(2*time.Second), "00:00:02"),
Entry("00:02:00", Duration(2*time.Minute), "00:02:00"),
Entry("02:00:00", Duration(2*time.Hour), "02:00:00"),
)
DescribeTable("unmarshal",
func(s string, exp Duration) {
d := new(Duration)
Expect(d.UnmarshalText([]byte(s))).To(Succeed())
Expect(*d).To(Equal(exp))
},
Entry("00:00:00", "00:00:00", Duration(0)),
Entry("00:00:00.002", "00:00:00.002", Duration(2*time.Millisecond)),
Entry("00:00:02", "00:00:02", Duration(2*time.Second)),
Entry("00:02:00", "00:02:00", Duration(2*time.Minute)),
Entry("02:00:00", "02:00:00", Duration(2*time.Hour)),
)
It("should fail to unmarshal bad inputs", func() {
d := new(Duration)
Expect(d.UnmarshalText([]byte("00:00:60"))).To(MatchError("invalid duration: 00:00:60"))
Expect(d.UnmarshalText([]byte("00:60:00"))).To(MatchError("invalid duration: 00:60:00"))
Expect(d.UnmarshalText([]byte("00:00:00.-1"))).To(MatchError("invalid duration: 00:00:00.-1"))
Expect(d.UnmarshalText([]byte("00:00:00.1000"))).To(MatchError("invalid duration: 00:00:00.1000"))
Expect(d.UnmarshalText([]byte("00h01m"))).To(MatchError("invalid duration: 00h01m"))
})
})