-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatemath_test.go
More file actions
62 lines (55 loc) · 1.47 KB
/
datemath_test.go
File metadata and controls
62 lines (55 loc) · 1.47 KB
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
package datemath
import (
"testing"
"time"
)
type testCase struct {
Exp string
Res time.Time
Ok bool
}
func (tc testCase) test(t *testing.T) {
r, e := EvalAnchor(anchor, tc.Exp)
if tc.Ok {
// No error
if e != nil {
t.Errorf("`%s` was supposed to work but did not: %v", tc.Exp, e)
} else if !r.Equal(tc.Res) && tc.Res.Unix() != r.Unix() {
t.Errorf("`%s` != `%s` for expression: `%s`", tc.Res, r, tc.Exp)
}
} else {
// Expect an error
if e == nil {
t.Errorf("`%s` was supposed to error but did not", tc.Exp)
}
}
}
var anchor = time.Date(2014, 12, 31, 23, 59, 59, 0, time.UTC)
var tests []testCase = []testCase{
// Ok
{"+1s", anchor.Add(1 * time.Second), true},
{"now+1s", anchor.Add(1 * time.Second), true},
{"-1s", anchor.Add(-1 * time.Second), true},
{"+5m", anchor.Add(5 * time.Minute), true},
{"-5m", anchor.Add(-5 * time.Minute), true},
{"-6w", anchor.AddDate(0, 0, -6*7), true},
{"now-6w", anchor.AddDate(0, 0, -6*7), true},
{"+5M", anchor.AddDate(0, 5, 0), true},
{"-5M", anchor.AddDate(0, -5, 0), true},
{"-100y", time.Date(1914, 12, 31, 23, 59, 59, 0, time.UTC), true},
{"+1000y", time.Date(3014, 12, 31, 23, 59, 59, 0, time.UTC), true},
{"now", time.Now(), true},
// Bad
{"1s", zero, false},
{"-0.5s", zero, false},
{"now-", zero, false},
{"1S", zero, false},
{"++1s.", zero, false},
{"+5ms", zero, false},
{"+5m/m", zero, false}, // Rounding isn't supported
}
func TestEval(t *testing.T) {
for _, tc := range tests {
tc.test(t)
}
}