-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser_test.go
130 lines (110 loc) · 3.98 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
package te
import (
"reflect"
"testing"
"time"
)
func TestParse(t *testing.T) {
tests := []struct {
in string
want Expression
}{
{"daily", Daily()},
{"midnight", Hour(0)},
{"noon", Hour(12)},
{"3pm", Hour(15)},
{"3 PM", Hour(15)},
{"15:00", Hour(15)},
{"15:04", Intersect(Hour(15), Minute(4))},
{"15:04:05", Intersect(Hour(15), Minute(4), Second(5))},
{"at midnight", Hour(0)},
{"at noon", Hour(12)},
{"at 3pm", Hour(15)},
{"daily at midnight", Hour(0)},
{"daily at noon", Hour(12)},
{"daily at 3pm", Hour(15)},
{"every day", Daily()},
{"every day at 3pm", Hour(15)},
{"weekly", Weekday(time.Sunday)},
{"weekly on Friday", Weekday(time.Friday)},
{"every week", Weekday(time.Sunday)},
{"every week on Friday", Weekday(time.Friday)},
{"every Friday", Weekday(time.Friday)},
{"Friday", Weekday(time.Friday)},
{"monthly", Day(1)},
{"every month", Day(1)},
{"every 1st", Day(1)},
{"every 4th", Day(4)},
{"4th", Day(4)},
{"quarterly", Intersect(Union(Month(time.January), Month(time.April), Month(time.July), Month(time.October)), Day(1))},
{"yearly", Intersect(Month(time.January), Day(1))},
{"annually", Intersect(Month(time.January), Day(1))},
{"annually at 4am", Intersect(Intersect(Month(time.January), Day(1)), Hour(4))},
{"every year", Intersect(Month(time.January), Day(1))},
{"every November", Month(time.November)},
{"November", Month(time.November)},
{"hourly", Minute(0)},
{"every hour", Minute(0)},
{"every minute", Second(0)},
{"every second", Secondly(1)},
{"every 2 hours", Hourly(2)},
{"every 15 minutes", Minutely(15)},
{"every 30 seconds", Secondly(30)},
{"3pm and 9pm", Union(Hour(15), Hour(21))},
{"15:00 and 21:00", Union(Hour(15), Hour(21))},
{"15:04:05 and 9pm", Union(Intersect(Hour(15), Minute(4), Second(5)), Hour(21))},
{"at noon and 9pm", Union(Hour(12), Hour(21))},
{"at noon and 6pm and 9pm", Union(Hour(12), Hour(18), Hour(21))},
{"at noon, 6pm, and 9pm", Union(Hour(12), Hour(18), Hour(21))},
{"at noon/6pm/9pm", Union(Hour(12), Hour(18), Hour(21))},
{"Tuesday and Thursday", Union(Weekday(time.Tuesday), Weekday(time.Thursday))},
{"Tue and Wed and Thu", Union(Weekday(time.Tuesday), Weekday(time.Wednesday), Weekday(time.Thursday))},
{"Tue, Wed, and Thu", Union(Weekday(time.Tuesday), Weekday(time.Wednesday), Weekday(time.Thursday))},
{"Tue/Wed/Thu", Union(Weekday(time.Tuesday), Weekday(time.Wednesday), Weekday(time.Thursday))},
{"every 1st and 3rd", Union(Day(1), Day(3))},
{"every 1st and 3rd and 9th", Union(Day(1), Day(3), Day(9))},
{"every 1st, 3rd, and 9th", Union(Day(1), Day(3), Day(9))},
{"every 1st/3rd/9th", Union(Day(1), Day(3), Day(9))},
{"July and August", Union(Month(time.July), Month(time.August))},
{"May and June and July", Union(Month(time.May), Month(time.June), Month(time.July))},
{"May, June, and July", Union(Month(time.May), Month(time.June), Month(time.July))},
{"May/Jun/Jul", Union(Month(time.May), Month(time.June), Month(time.July))},
{"April 19th", Intersect(Month(time.April), Day(19))},
{"every April 19th", Intersect(Month(time.April), Day(19))},
{"every April 19th at 3pm", Intersect(Month(time.April), Day(19), Hour(15))},
{"every 2 hours on Sunday", Intersect(Hourly(2), Weekday(time.Sunday))},
{"Tue/Thu at 4am", Intersect(Union(Weekday(time.Tuesday), Weekday(time.Thursday)), Hour(4))},
}
for _, tt := range tests {
have, err := Parse(tt.in, time.UTC)
if err != nil {
t.Fatalf("Parse(%q) %v", tt.in, err)
} else if !reflect.DeepEqual(have, tt.want) {
t.Errorf("Parse(%q)\nhave %#v\nwant %#v", tt.in, have, tt.want)
}
}
}
func TestParseError(t *testing.T) {
var tests = []string{
"",
" ",
"daily at",
"daily at daily",
"midnight at",
"midnight at daily",
"noon at",
"noon at daily",
"3:",
"3:4",
"15:04:",
"every",
"in noon",
"at noon and",
}
for _, tt := range tests {
have, err := Parse(tt, time.UTC)
if err == nil {
t.Errorf("Parse(%q)\nhave %v\nwant parse error", tt, have)
}
}
}