-
Notifications
You must be signed in to change notification settings - Fork 0
/
duration_test.go
68 lines (55 loc) · 1.32 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
package cast
import (
. "github.com/smartystreets/goconvey/convey"
"testing"
"time"
)
type NotDuration struct{}
func TestDurationCast(t *testing.T) {
Convey("Cast string to duration", t, func() {
val := "1h2m3s"
castVal, _ := Duration(val)
dur, _ := time.ParseDuration(val)
So(castVal, ShouldEqual, dur)
})
Convey("Cast string int to duration", t, func() {
val := "123"
castVal, _ := Duration(val)
dur := time.Duration(123)
So(castVal, ShouldEqual, dur)
})
Convey("Cast duration int to duration", t, func() {
val := time.Duration(123)
castVal, _ := Duration(val)
dur := time.Duration(123)
So(castVal, ShouldEqual, dur)
})
Convey("Cast invalid string to duration", t, func() {
val := "a"
_, err := Duration(val)
So(err, ShouldNotBeNil)
})
Convey("Cast uint8 to duration", t, func() {
val := uint8(123)
castVal, _ := Duration(val)
dur := time.Duration(123)
So(castVal, ShouldEqual, dur)
})
Convey("Cast NotDuration to duration and return error", t, func() {
val := NotDuration{}
_, err := Duration(val)
So(err, ShouldNotBeNil)
})
Convey("Must cast without panic", t, func() {
So(func() {
val := 123
MustDuration(val)
}, ShouldNotPanic)
})
Convey("Must cast with panic", t, func() {
So(func() {
val := NotDuration{}
MustDuration(val)
}, ShouldPanic)
})
}