-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils_test.go
63 lines (56 loc) · 1.62 KB
/
utils_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
package utils
import (
"testing"
"time"
)
func TestFuzzyAgo(t *testing.T) {
cases := map[string]string{
"1s": "less than a minute ago",
"30s": "less than a minute ago",
"1m08s": "about 1 minute ago",
"15m0s": "about 15 minutes ago",
"59m10s": "about 59 minutes ago",
"1h10m02s": "about 1 hour ago",
"15h0m01s": "about 15 hours ago",
"30h10m": "about 1 day ago",
"50h": "about 2 days ago",
"720h05m": "about 1 month ago",
"3000h10m": "about 4 months ago",
"8760h59m": "about 1 year ago",
"17601h59m": "about 2 years ago",
"262800h19m": "about 30 years ago",
}
for duration, expected := range cases {
d, e := time.ParseDuration(duration)
if e != nil {
t.Errorf("failed to create a duration: %s", e)
}
fuzzy := FuzzyAgo(d)
if fuzzy != expected {
t.Errorf("unexpected fuzzy duration value: %s for %s", fuzzy, duration)
}
}
}
func TestFuzzyAgoAbbr(t *testing.T) {
const form = "2006-Jan-02 15:04:05"
now, _ := time.Parse(form, "2020-Nov-22 14:00:00")
cases := map[string]string{
"2020-Nov-22 14:00:00": "0m",
"2020-Nov-22 13:59:00": "1m",
"2020-Nov-22 13:30:00": "30m",
"2020-Nov-22 13:00:00": "1h",
"2020-Nov-22 02:00:00": "12h",
"2020-Nov-21 14:00:00": "1d",
"2020-Nov-07 14:00:00": "15d",
"2020-Oct-24 14:00:00": "29d",
"2020-Oct-23 14:00:00": "Oct 23, 2020",
"2019-Nov-22 14:00:00": "Nov 22, 2019",
}
for createdAt, expected := range cases {
d, _ := time.Parse(form, createdAt)
fuzzy := FuzzyAgoAbbr(now, d)
if fuzzy != expected {
t.Errorf("unexpected fuzzy duration abbr value: %s for %s", fuzzy, createdAt)
}
}
}