-
Notifications
You must be signed in to change notification settings - Fork 0
/
times_test.go
100 lines (93 loc) · 1.24 KB
/
times_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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package times
import (
"github.com/google/go-cmp/cmp"
"testing"
"time"
)
func TestToDuration(t *testing.T) {
type Test struct {
Value string
Result time.Duration
}
tests := []Test{
{
"1s",
time.Second,
},
{
"5m",
5 * time.Minute,
},
{
"3h",
3 * time.Hour,
},
{
"3h24m",
3*time.Hour + 24*time.Minute,
},
{
"1us",
time.Microsecond,
},
{
"3.5m",
3*time.Minute + 30*time.Second,
},
}
for _, te := range tests {
d, err := ToDuration(te.Value)
if err != nil {
t.Error(err)
}
if diff := cmp.Diff(te.Result, d); diff != "" {
t.Error(diff)
}
}
}
func TestToTime(t *testing.T) {
type Test struct {
Value string
Result time.Time
}
date20180102 := time.Date(2018, 1, 2, 0, 0, 0, 0, time.Local)
tests := []Test{
{
"2018-1-2",
date20180102,
},
{
"2018-01-02",
date20180102,
},
{
"2018/1/2",
date20180102,
},
{
"2018/01/02",
date20180102,
},
{
"20180102",
date20180102,
},
{
"02/01/2018",
date20180102,
},
{
"2/1/2018",
date20180102,
},
}
for _, te := range tests {
d, err := ToTime(te.Value)
if err != nil {
t.Error(err)
}
if diff := cmp.Diff(te.Result, d); diff != "" {
t.Error(diff)
}
}
}