-
Notifications
You must be signed in to change notification settings - Fork 0
/
avail_test.go
228 lines (209 loc) · 4.5 KB
/
avail_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
package avail
import (
"fmt"
"testing"
"time"
"github.com/google/go-cmp/cmp"
)
func TestParseable(t *testing.T) {
tests := map[string]struct {
expression string
}{
"wildcard": {
expression: "* * * * * *",
},
"ranges": {
expression: "* * 25-30 12 * 2020",
},
"single values": {
expression: "* * 1 1 * *",
},
"range + single value": {
expression: "* * * 6,7,8 * 2020",
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
_, err := New(tc.expression)
if err != nil {
t.Errorf("expression %s should be parsed successfully", tc.expression)
}
})
}
}
func TestUnparseable(t *testing.T) {
tests := map[string]struct {
expression string
}{
"too many arguments": {
expression: "* * * * * * *",
},
"too few arguments": {
expression: "* * * *",
},
"too few arguments w/ value": {
expression: "* 14 * * *",
},
"out of bounds single value": {
expression: "* * * * 22222 *",
},
"out of bounds range": {
expression: "10-500 * * * * *",
},
"out of bounds list": {
expression: "* 1,40,100 * * * *",
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
_, err := New(tc.expression)
if err == nil {
t.Errorf("expression %s should not be parsed successfully", tc.expression)
}
})
}
}
func TestNew(t *testing.T) {
tests := map[string]struct {
expression string
want Timeframe
}{
"wildcard": {"* * * * * *", Timeframe{
Expression: "* * * * * *",
ParsedExpression: ParsedExpression{
Minutes: Field{
Kind: minute,
Term: "*",
Min: 0,
Max: 59,
Values: generateSequentialSet(0, 59),
},
Hours: Field{
Kind: hour,
Term: "*",
Min: 0,
Max: 23,
Values: generateSequentialSet(0, 23),
},
Days: Field{
Kind: day,
Term: "*",
Min: 1,
Max: 31,
Values: generateSequentialSet(1, 31),
},
Months: Field{
Kind: month,
Term: "*",
Min: 1,
Max: 12,
Values: generateSequentialSet(1, 12),
},
Weekdays: Field{
Kind: weekday,
Term: "*",
Min: 0,
Max: 6,
Values: generateSequentialSet(0, 6),
},
Years: Field{
Kind: year,
Term: "*",
Min: 1970,
Max: 2100,
Values: generateSequentialSet(1970, 2100),
},
},
}},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
got, err := New(tc.expression)
if err != nil {
t.Error(err)
}
diff := cmp.Diff(tc.want, got)
if diff != "" {
t.Errorf("result is different than expected(-want +got):\n%s", diff)
}
})
}
}
func TestParseWildcard(t *testing.T) {
want := Field{
Kind: minute,
Term: "*",
Min: 0,
Max: 59,
Values: generateSequentialSet(0, 59),
}
got, err := newField(minute, "*", 0, 59)
if err != nil {
t.Error(err)
}
diff := cmp.Diff(want, got)
if diff != "" {
t.Errorf("result is different than expected(-want +got):\n%s", diff)
}
}
func TestParseSpan(t *testing.T) {
want := Field{
Kind: hour,
Term: "4-14",
Min: 0,
Max: 23,
Values: generateSequentialSet(4, 14),
}
got, err := newField(hour, "4-14", 0, 23)
if err != nil {
t.Error(err)
}
diff := cmp.Diff(want, got)
if diff != "" {
t.Errorf("result is different than expected(-want +got):\n%s", diff)
}
}
func TestAble(t *testing.T) {
tests := map[string]struct {
expression string
time time.Time
want bool
}{
"wildcard": {"* * * * * *", time.Now(), true},
"year out of range": {"* * * * * 2019", time.Now(), false},
"year list in range": {"* * * * * 2019,2020,2021", time.Date(2020, 1, 1, 1, 1, 1, 1, time.UTC), true},
"specific day of week": {
"* * * 6 2 2020",
time.Date(2020, 6, 2, 1, 1, 1, 1, time.UTC), true,
},
"specific day of week; out of range": {
"* * * 6 2 2020",
time.Date(2020, 8, 4, 1, 1, 1, 1, time.UTC), false,
},
"every day at noon in January only": {
"0 12 * 1 * *",
time.Date(2020, 1, 24, 12, 0, 0, 0, time.UTC), true,
},
"every day from 6am to 2pm": {
"* 6-14 * * * *",
time.Date(2020, 1, 24, 12, 0, 0, 0, time.UTC), true,
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
avail, err := New(tc.expression)
if err != nil {
t.Error(err)
}
if avail.Able(tc.time) != tc.want {
t.Errorf("want %t, got %t", tc.want, !tc.want)
}
})
}
}
func ExampleTimeframe_Able() {
avail, _ := New("* * * * * *")
now := time.Now()
fmt.Println(avail.Able(now))
// Output: true
}