forked from gorhill/cronexpr
-
Notifications
You must be signed in to change notification settings - Fork 3
/
cronexpr_prev_test.go
184 lines (167 loc) · 4.58 KB
/
cronexpr_prev_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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package cronexpr
/******************************************************************************/
import (
"testing"
"time"
)
/******************************************************************************/
var cronprevtests = []crontest{
// Seconds
{
"* * * * * * *",
"2006-01-02 15:04:05",
[]crontimes{
{"2013-01-01 00:00:01", "2013-01-01 00:00:00" },
{"2013-01-01 00:01:00", "2013-01-01 00:00:59"},
{"2013-01-01 01:00:00", "2013-01-01 00:59:59" },
{"2013-01-02 00:00:00", "2013-01-01 23:59:59"},
{"2013-03-01 00:00:00", "2013-02-28 23:59:59"},
{"2016-02-29 00:00:00", "2016-02-28 23:59:59"},
{"2013-01-01 00:00:00", "2012-12-31 23:59:59"},
},
},
// every 5 Second
{
"*/5 * * * * * *",
"2006-01-02 15:04:05",
[]crontimes{
{"2013-01-01 00:00:05", "2013-01-01 00:00:00"},
{"2013-01-01 00:00:01", "2013-01-01 00:00:00"},
{"2013-01-01 00:59:59", "2013-01-01 00:59:55"},
},
},
// Minutes
{
"* * * * *",
"2006-01-02 15:04:05",
[]crontimes{
{"2013-01-01 00:00:00", "2012-12-31 23:59:00"},
{"2013-01-01 00:01:59", "2013-01-01 00:01:00"},
{"2013-01-01 00:00:03", "2013-01-01 00:00:00"},
{"2013-03-01 00:00:00", "2013-02-28 23:59:00"},
{"2013-02-28 00:00:00", "2013-02-27 23:59:00"},
{"2016-03-01 00:00:00", "2016-02-29 23:59:00"},
{"2012-01-01 17:00:00", "2012-01-01 16:59:00"},
},
},
// Minutes with interval
{
"17-43/5 * * * *",
"2006-01-02 15:04:05",
[]crontimes{
{"2013-01-01 00:00:00", "2012-12-31 23:42:00"},
{"2013-01-01 00:16:59", "2012-12-31 23:42:00"},
{"2013-01-01 00:30:00", "2013-01-01 00:27:00"},
{"2013-01-01 00:12:00", "2012-12-31 23:42:00"},
{"2013-01-01 23:10:00", "2013-01-01 22:42:00"},
{"2013-03-01 00:08:00", "2013-02-28 23:42:00"},
{"2016-03-01 00:05:00", "2016-02-29 23:42:00"},
},
},
// Minutes interval, list
{
"15-30/4,55 * * * *",
"2006-01-02 15:04:05",
[]crontimes{
{"2013-01-01 00:59:59", "2013-01-01 00:55:00"},
{"2013-01-01 00:54:00", "2013-01-01 00:27:00"},
{"2013-01-01 00:59:00", "2013-01-01 00:55:00"},
{"2013-01-01 00:55:00", "2013-01-01 00:27:00"},
{"2013-01-02 00:15:00", "2013-01-01 23:55:00"},
{"2013-03-01 00:00:00", "2013-02-28 23:55:00"},
{"2016-03-01 00:00:05", "2016-02-29 23:55:00"},
{"2013-01-01 00:00:09", "2012-12-31 23:55:00"},
{"2013-01-01 00:15:00", "2012-12-31 23:55:00"},
},
},
// Days of week
{
"0 0 * * MON",
"Mon 2006-01-02 15:04",
[]crontimes{
{"2013-01-01 00:00:00", "Mon 2012-12-31 00:00"},
{"2013-01-28 00:00:00", "Mon 2013-01-21 00:00"},
{"2013-12-30 00:30:00", "Mon 2013-12-30 00:00"},
},
},
{
"0 0 * * friday",
"Mon 2006-01-02 15:04",
[]crontimes{
{"2013-01-01 00:00:00", "Fri 2012-12-28 00:00"},
{"2013-02-01 00:00:00", "Fri 2013-01-25 00:00"},
{"2014-01-02 00:30:00", "Fri 2013-12-27 00:00"},
},
},
{
"0 0 * * 6,7",
"Mon 2006-01-02 15:04",
[]crontimes{
{"2013-01-01 00:00:00", "Sun 2012-12-30 00:00"},
},
},
// Specific days of week
{
"0 0 * * 6#5",
"Mon 2006-01-02 15:04",
[]crontimes{
{"2013-08-22 00:00:00", "Sat 2013-06-29 00:00"}, // july doesn't have a 5th saturday
},
},
// Work day of month
{
"0 0 6W * *",
"Mon 2006-01-02 15:04",
[]crontimes{
{"2013-08-05 00:00:00", "Fri 2013-07-05 00:00"},
{"2013-11-05 00:00:00", "Mon 2013-10-07 00:00"},
},
},
// Work day of month -- end of month
{
"0 0 30W * *",
"Mon 2006-01-02 15:04",
[]crontimes{
{"2013-03-02 00:00:00", "Wed 2013-01-30 00:00"},
{"2013-06-02 00:00:00", "Thu 2013-05-30 00:00"},
{"2013-03-02 00:00:00", "Wed 2013-01-30 00:00"},
},
},
// Last day of month
{
"0 0 L * *",
"Mon 2006-01-02 15:04",
[]crontimes{
{"2013-09-02 00:00:00", "Sat 2013-08-31 00:00"},
{"2014-01-01 00:00:00", "Tue 2013-12-31 00:00"},
{"2014-03-01 00:00:00", "Fri 2014-02-28 00:00"},
{"2016-03-15 00:00:00", "Mon 2016-02-29 00:00"},
},
},
// Last work day of month
{
"0 0 LW * *",
"Mon 2006-01-02 15:04",
[]crontimes{
{"2013-09-02 00:00:00", "Fri 2013-08-30 00:00"},
{"2013-11-02 00:00:00", "Thu 2013-10-31 00:00"},
{"2013-07-15 00:00:00", "Fri 2013-06-28 00:00"},
},
},
}
func TestPrevExpressions(t *testing.T) {
for _, test := range cronprevtests {
for _, times := range test.times {
from, _ := time.Parse("2006-01-02 15:04:05", times.from)
expr, err := Parse(test.expr)
if err != nil {
t.Errorf(`Parse("%s") returned "%s"`, test.expr, err.Error())
}
prev := expr.Prev(from)
prevstr := prev.Format(test.layout)
if prevstr != times.next {
t.Errorf(`("%s").Prev("%s") = "%s", got "%s"`, test.expr, times.from, times.next, prevstr)
}
}
}
}