-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime_test.go
80 lines (75 loc) · 1.65 KB
/
time_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
package frain
import (
"testing"
"time"
)
func TestTimeDiff(t *testing.T) {
tests := []struct {
u time.Time
v time.Time
want Clock
}{
{
time.Date(2019, 8, 17, 0, 5, 23, 0, time.UTC),
time.Date(2019, 8, 18, 12, 19, 47, 0, time.UTC),
Clock{24, 14, 12, 1, 0, 0},
},
{
time.Date(2020, 2, 28, 11, 30, 55, 0, time.UTC),
time.Date(2019, 4, 7, 9, 46, 2, 0, time.UTC),
Clock{53, 44, 1, 21, 10, 0},
},
{
time.Date(2019, 4, 7, 9, 46, 2, 0, time.UTC),
time.Date(2019, 2, 28, 11, 30, 55, 0, time.UTC),
Clock{7, 15, 22, 9, 1, 0},
},
}
for _, tt := range tests {
elapsed := TimeDiff(tt.u, tt.v)
if elapsed != tt.want {
t.Errorf("expected %v got %v", tt.want, elapsed)
}
}
}
func TestTimeAgo(t *testing.T) {
tests := []struct {
createdAt time.Time
updatedAt time.Time
wantElapsed string
wantErr error
}{
{
time.Date(2019, 8, 17, 0, 5, 23, 0, time.UTC),
time.Date(2019, 8, 18, 12, 19, 47, 0, time.UTC),
"1 day ago",
nil,
},
{
time.Date(2019, 8, 18, 12, 19, 47, 0, time.UTC),
time.Date(2019, 8, 17, 0, 5, 23, 0, time.UTC),
"",
ErrFutureTime,
},
{
time.Date(2002, 11, 9, 0, 5, 53, 0, time.UTC),
time.Date(2019, 11, 9, 0, 1, 24, 0, time.UTC),
"16 years ago",
nil,
},
{
time.Date(2020, 7, 23, 6, 5, 53, 0, time.UTC),
time.Date(2020, 7, 23, 6, 7, 23, 0, time.UTC),
"1 minute ago",
nil,
},
}
for _, tt := range tests {
elapsed, err := TimeAgo(tt.createdAt, tt.updatedAt)
if elapsed != tt.wantElapsed {
t.Errorf("expected %v got %v", tt.wantElapsed, elapsed)
} else if err != tt.wantErr {
t.Errorf("expected %v got %v", tt.wantErr, err)
}
}
}