-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathparser_test.go
282 lines (270 loc) · 7.92 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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package cron
import (
"errors"
"reflect"
"testing"
)
func TestCronParser_Parse(t *testing.T) {
type testCase struct {
name string
inTestDOWStartsAtOne bool
inExpr string
outExprs []string
outErr error
}
tcs := []testCase{
// extractExprParts
{
name: "should failed on empty",
inExpr: " ",
outExprs: nil,
outErr: InvalidExprError,
}, {
name: "should parse cron with multiple spaces between parts",
inExpr: "30 2 * * *",
outExprs: []string{"", "30", "2", "*", "*", "*", ""},
outErr: nil,
}, {
name: "should parse 5 part cron",
inExpr: "* * * * *",
outExprs: []string{"", "*", "*", "*", "*", "*", ""},
outErr: nil,
}, {
name: "should parse 6 part cron with year",
inExpr: "* * * * * 2020",
outExprs: []string{"", "*", "*", "*", "*", "*", "2020"},
outErr: nil,
}, {
name: "should parse 6 part cron with second",
inExpr: "5 * * * * *",
outExprs: []string{"5", "*", "*", "*", "*", "*", ""},
outErr: nil,
}, {
name: "should parse 7 part cron",
inExpr: " 5 * * * * * 2020 ",
outExprs: []string{"5", "*", "*", "*", "*", "*", "2020"},
outErr: nil,
}, {
name: "should error if more than 7 part",
inExpr: "5 * * * * * 2020 *",
outExprs: nil,
outErr: InvalidExprError,
}, {
name: "should error if expression is not a cron schedule",
inExpr: "sdlksCRAPdlkskl- dds",
outExprs: nil,
outErr: InvalidExprError,
},
// normalize
{
name: "should parse cron in uppercase",
inExpr: "* * L JAN-NOV MON-FRI",
outExprs: []string{"", "*", "*", "l", "1-11", "1-5", ""},
outErr: nil,
}, {
name: "should parse cron in lowercase",
inExpr: "* * l jan-NOV MON-fri",
outExprs: []string{"", "*", "*", "l", "1-11", "1-5", ""},
outErr: nil,
}, {
name: "should convert 0 second to empty",
inExpr: "0 * * * * * *",
outExprs: []string{"", "*", "*", "*", "*", "*", "*"},
outErr: nil,
}, {
name: "should convert ? to *",
inExpr: "* ? ? * ?",
outExprs: []string{"", "*", "*", "*", "*", "*", ""},
outErr: nil,
}, {
name: "should convert 0/ to */",
inExpr: "0/5 0/5 0/5 * * *",
outExprs: []string{"*/5", "*/5", "*/5", "*", "*", "*", ""},
outErr: nil,
}, {
name: "should convert 1/ to */",
inExpr: "3 * * 1/5 1/5 1/2 1/10",
outExprs: []string{"3", "*", "*", "*/5", "*/5", "*/2", "*/10"},
outErr: nil,
}, {
name: "should use 7 as Sunday when DOWStartsAtZero is true",
inExpr: "* * * * 1-7",
outExprs: []string{"", "*", "*", "*", "*", "1-0", ""},
outErr: nil,
}, {
name: "should failed when DOWStartsAtZero is false and have 0",
inExpr: "* * * * 0-7",
inTestDOWStartsAtOne: true,
outExprs: nil,
outErr: InvalidExprDayOfWeekError,
}, {
name: "should parse when DOWStartsAtZero is false",
inExpr: "* * * * 1-7",
inTestDOWStartsAtOne: true,
outExprs: []string{"", "*", "*", "*", "*", "0-6", ""},
outErr: nil,
}, {
name: "should convert L DOW to 6",
inExpr: "* * * * L",
outExprs: []string{"", "*", "*", "*", "*", "6", ""},
outErr: nil,
}, {
name: "should failed if DOM 'W' comes with list of dates 1",
inExpr: "* * 1-3W * *",
outExprs: nil,
outErr: InvalidExprDayOfMonthError,
}, {
name: "should failed if DOM 'W' comes with list of dates 2",
inExpr: "* * 1,2,5W * *",
outExprs: nil,
outErr: InvalidExprDayOfMonthError,
}, {
name: "should convert hour to range 1",
inExpr: "0-20/3 9 * * *",
outExprs: []string{"", "0-20/3", "9-9", "*", "*", "*", ""},
outErr: nil,
}, {
name: "should convert hour to range 2",
inExpr: "*/5 3 * * *",
outExprs: []string{"", "*/5", "3-3", "*", "*", "*", ""},
outErr: nil,
}, {
name: "should normalize */1 to *",
inExpr: "*/1 3 * * *",
outExprs: []string{"", "*", "3-3", "*", "*", "*", ""},
outErr: nil,
}, {
name: "should normalize to range",
inExpr: "5 * * 3/5 2/5 2/2 2000/10",
outExprs: []string{"5", "*", "*", "3/5", "2-12/5", "2-6/2", "2000-2099/10"},
outErr: nil,
},
// validate
{
name: "should error if second is invalid 2",
inExpr: "60 * * * * * *",
outExprs: nil,
outErr: InvalidExprSecondError,
}, {
name: "should error if second is invalid 3",
inExpr: "9223372036854775808 * * * * * *",
outExprs: nil,
outErr: InvalidExprSecondError,
}, {
name: "should error if minute is invalid 2",
inExpr: "* 60 * * * * *",
outExprs: nil,
outErr: InvalidExprMinuteError,
}, {
name: "should error if hour is invalid 2",
inExpr: "* * 24 * * * *",
outExprs: nil,
outErr: InvalidExprHourError,
}, {
name: "should error if DOM is invalid 2",
inExpr: "* * * 32 * * *",
outExprs: nil,
outErr: InvalidExprDayOfMonthError,
}, {
name: "should error if DOM contains invalid characters",
inExpr: "* * LX * *",
outExprs: nil,
outErr: InvalidExprDayOfMonthError,
}, {
name: "should error if month is invalid 2",
inExpr: "* * * * 13 * *",
outExprs: nil,
outErr: InvalidExprMonthError,
}, {
name: "should error if DOW is invalid 2",
inExpr: "* * * * * 8 *",
outExprs: nil,
outErr: InvalidExprDayOfWeekError,
}, {
name: "should error if DOW contains invalid characters",
inExpr: "* * * * MO",
outExprs: nil,
outErr: InvalidExprDayOfWeekError,
}, {
name: "should error if year is invalid 1",
inExpr: "* * * * * * 0",
outExprs: nil,
outErr: InvalidExprYearError,
}, {
name: "should error if year is invalid 2",
inExpr: "* * * * * * 2100",
outExprs: nil,
outErr: InvalidExprYearError,
},
// Cannot test due to no reliable way to detect negative number in cron expression
// {
// name: "should error if second is invalid 1",
// inExpr: "-1 * * * * * *",
// outExprs: nil,
// outErr: InvalidExprSecondError,
// }, {
// name: "should error if minute is invalid 1",
// inExpr: "* -1 * * * * *",
// outExprs: nil,
// outErr: InvalidExprMinuteError,
// },{
// name: "should error if hour is invalid 1",
// inExpr: "* * -1 * * * *",
// outExprs: nil,
// outErr: InvalidExprHourError,
// },{
// name: "should error if DOM is invalid 1",
// inExpr: "* * * -1 * * *",
// outExprs: nil,
// outErr: InvalidExprDayOfMonthError,
// },{
// name: "should error if month is invalid 1",
// inExpr: "* * * * 0 * *",
// outExprs: nil,
// outErr: InvalidExprMonthError,
// }, {
// name: "should error if DOW is invalid 1",
// inExpr: "* * * * * -1 *",
// outExprs: nil,
// outErr: InvalidExprDayOfWeekError,
// },
}
parser := cronParser{}
for i, tc := range tcs {
parser.isDOWStartsAtOne = tc.inTestDOWStartsAtOne
parsed, err := parser.Parse(tc.inExpr)
if tc.outErr != nil {
if err == nil {
t.Errorf("%d. %s: expected error, got nil", i, tc.name)
return
}
if !errors.Is(err, tc.outErr) {
t.Errorf("%d. %s: expected '%v' error, got '%v'", i, tc.name, tc.outErr, err)
return
}
if !reflect.DeepEqual(parsed, tc.outExprs) {
t.Errorf("%d. %s: expected return nil when error, got '%v'", i, tc.name, parsed)
return
}
continue
}
if !reflect.DeepEqual(parsed, tc.outExprs) {
t.Errorf("%d. %s: expected '%v', got '%v'", i, tc.name, tc.outExprs, parsed)
return
}
}
}
var _parsed []string
func BenchmarkCronParser_Parse(b *testing.B) {
b.StopTimer()
parser := &cronParser{}
expr := "0/5 1,5,10,15 */2 L JAN-OCT 1-5/2 2000-2050/10"
var err error
b.StartTimer()
for i := 0; i < b.N; i++ {
_parsed, err = parser.Parse(expr)
if err != nil {
b.Fatalf("expected nil, got error: %s", err)
}
}
}