-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser_test.go
143 lines (120 loc) · 3.57 KB
/
parser_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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package cron
import (
"testing"
"time"
)
func TestActivation(t *testing.T) {
tests := []struct {
time string
spec string
expected bool
}{
// Every 15 seconds.
{"2012-07-09T15:00:00+08:00", "* * * * * * 0/15", true},
{"2012-07-09T15:00:45+08:00", "* * * * * * 0/15", true},
{"2012-07-09T15:00:40+08:00", "* * * * * * 0/15", false},
// Every 15 seconds, starting at 5 seconds.
{"2012-07-09T15:00:05+08:00", "* * * * * * 5/15", true},
{"2012-07-09T15:00:20+08:00", "* * * * * * 5/15", true},
{"2012-07-09T15:00:50+08:00", "* * * * * * 5/15", true},
// Test interaction of DOW and DOM.
// If both are restricted, then only one needs to match.
// {"2012-07-15T00:00:00+08:00", "* * 1,15 0 * * *", true},
// {"2012-06-15T00:00:00+08:00", "* * 1,15 0 * * *", true},
// {"2012-08-01T00:00:00+08:00", "* * 1,15 0 * * *", true},
// {"2012-07-15T00:00:00+08:00", "* * */10 0 * * *", true},
// However, if one has a star, then both need to match.
{"2012-07-15T00:00:00+08:00", "* * * 0 * * *", true},
{"2012-07-09T00:00:00+08:00", "* * 1,15 * * * *", false},
{"2012-07-15T00:00:00+08:00", "* * 1,15 * * * *", true},
{"2012-07-15T00:00:00+08:00", "* * */2 0 * * *", true},
}
for _, test := range tests {
sched, err := defaultParser.Parse(test.spec)
if err != nil {
t.Error(err)
continue
}
actual := sched.Next(parseTime(test.time).Add(-1 * time.Second))
// expected := getTime(test.time)
expected := parseTime(test.time)
if test.expected && expected != actual || !test.expected && expected == actual {
t.Errorf("Fail evaluating %s on %s: (expected) %s != %s (actual)",
test.spec, test.time, expected, actual)
}
}
}
func parseTime(value string) time.Time {
if value == "" {
return time.Time{}
}
t, err := time.Parse(time.RFC3339, value)
if err != nil {
panic(err)
}
return t
}
func TestDomAndDow(t *testing.T) {
start := "2024-11-06T00:00:00+08:00"
expr1 := "* * */2 3 0 0 0"
expectSet1 := []string{
"2024-11-13T00:00:00+08:00",
"2024-11-27T00:00:00+08:00",
"2024-12-11T00:00:00+08:00",
"2024-12-25T00:00:00+08:00",
"2025-01-01T00:00:00+08:00",
}
sched1, err := defaultParser.Parse(expr1)
if err != nil {
panic(err)
}
next := parseTime(start)
for _, item := range expectSet1 {
actual := sched1.Next(next)
expected := parseTime(item)
if actual != expected {
t.Errorf("Fail evaluating %s on %s: (expected) %s != %s (actual)",
expr1, next, expected, actual)
}
next = actual
}
expr2 := "* * * 3 0 0 0"
expectSet2 := []string{
"2024-11-13T00:00:00+08:00",
"2024-11-20T00:00:00+08:00",
"2024-11-27T00:00:00+08:00",
"2024-12-04T00:00:00+08:00",
"2024-12-11T00:00:00+08:00",
}
sched2, err := defaultParser.Parse(expr2)
if err != nil {
panic(err)
}
next = parseTime(start)
for _, item := range expectSet2 {
actual := sched2.Next(next)
expected := parseTime(item)
if actual != expected {
t.Errorf("Fail evaluating %s on %s: (expected) %s != %s (actual)",
expr2, next, expected, actual)
}
next = actual
}
}
func TestParserTimeZone(t *testing.T) {
// TODO: implement
// now := time.Now().Truncate(time.Second)
// tm := now.Add(2 * time.Second)
// fmt.Println("now ", now)
// fmt.Println("expected", tm)
// expr := fmt.Sprintf("%d %d %d %d %d %d %d", tm.Year(), tm.Month(), tm.Day(), tm.Weekday(), tm.Hour(), tm.Minute(), tm.Second())
// sched, err := defaultParser.Parse(expr)
// if err != nil {
// panic(err)
// }
// actual := sched.Next(now)
// if tm != actual {
// t.Errorf("Fail evaluating %s: (expected) %s != %s (actual)",
// expr, tm, actual)
// }
}