-
Notifications
You must be signed in to change notification settings - Fork 0
/
time_test.go
68 lines (64 loc) · 1.87 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
package main
import (
"fmt"
"testing"
"time"
)
func TestParseTimeSpecial(t *testing.T) {
t.Run("all", func(t *testing.T) {
for _, all := range []string{"all", "ALL"} {
p, err := parseTime(all)
if err != nil {
t.Error(err)
}
if p.UTC().Format(time.RFC3339) != "1970-01-01T00:00:00Z" {
t.Error(fmt.Errorf("not expected: %s", p.UTC().Format(time.RFC3339)))
}
}
})
t.Run("empty", func(t *testing.T) {
for _, now := range []string{"", "now"} {
p, err := parseTime(now)
if err != nil {
t.Error(err)
}
if p.UTC().Format(time.RFC3339) == "1970-01-01T00:00:00Z" {
t.Error(fmt.Errorf("not expected: %s", p.UTC().Format(time.RFC3339)))
}
}
})
t.Run("error", func(t *testing.T) {
p, err := parseTime("HOGEHOGE")
if err == nil {
t.Error(fmt.Errorf("err should happen: %s", p))
}
})
}
func TestParseRelativeTime(t *testing.T) {
now, _ := time.Parse(time.RFC3339, "2018-10-30T10:10:00Z")
var tests = []struct {
in []string
expected string
}{
{[]string{"1m", "1", "m"}, "2018-10-30T10:09:00Z"},
{[]string{"1min", "1", "min"}, "2018-10-30T10:09:00Z"},
{[]string{"1h", "1", "h"}, "2018-10-30T09:10:00Z"},
{[]string{"1hour", "1", "hour"}, "2018-10-30T09:10:00Z"},
{[]string{"1hours", "1", "hours"}, "2018-10-30T09:10:00Z"},
{[]string{"1d", "1", "d"}, "2018-10-29T10:10:00Z"},
{[]string{"1day", "1", "day"}, "2018-10-29T10:10:00Z"},
{[]string{"1days", "1", "days"}, "2018-10-29T10:10:00Z"},
{[]string{"1w", "1", "w"}, "2018-10-23T10:10:00Z"},
{[]string{"1week", "1", "week"}, "2018-10-23T10:10:00Z"},
{[]string{"1weeks", "1", "weeks"}, "2018-10-23T10:10:00Z"},
}
for _, test := range tests {
p, err := parseRelativeTime(test.in, now)
if err != nil {
t.Error(err)
}
if p.Format(time.RFC3339) != test.expected {
t.Error(fmt.Errorf("%s is not expected: %s", p.Format(time.RFC3339), test.expected))
}
}
}