-
Notifications
You must be signed in to change notification settings - Fork 58
/
rruleset_test.go
383 lines (344 loc) · 12 KB
/
rruleset_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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
// 2017-2022, Teambition. All rights reserved.
package rrule
import (
"testing"
"time"
)
func TestSet(t *testing.T) {
set := Set{}
r, _ := NewRRule(ROption{Freq: YEARLY, Count: 2, Byweekday: []Weekday{TU},
Dtstart: time.Date(1997, 9, 2, 9, 0, 0, 0, time.UTC)})
set.RRule(r)
value := set.All()
want := []time.Time{time.Date(1997, 9, 2, 9, 0, 0, 0, time.UTC),
time.Date(1997, 9, 9, 9, 0, 0, 0, time.UTC)}
if !timesEqual(value, want) {
t.Errorf("get %v, want %v", value, want)
}
}
func TestSetOverlapping(t *testing.T) {
set := Set{}
r, _ := NewRRule(ROption{Freq: YEARLY,
Dtstart: time.Date(1997, 9, 2, 9, 0, 0, 0, time.UTC)})
set.RRule(r)
v1 := set.All()
if len(v1) > 300 || len(v1) < 200 {
t.Errorf("No default Util time")
}
}
func TestSetString(t *testing.T) {
moscow, _ := time.LoadLocation("Europe/Moscow")
newYork, _ := time.LoadLocation("America/New_York")
tehran, _ := time.LoadLocation("Asia/Tehran")
set := Set{}
r, _ := NewRRule(ROption{Freq: YEARLY, Count: 1, Byweekday: []Weekday{TU},
Dtstart: time.Date(1997, 9, 2, 8, 0, 0, 0, time.UTC)})
set.RRule(r)
set.ExDate(time.Date(1997, 9, 4, 9, 0, 0, 0, time.UTC))
set.ExDate(time.Date(1997, 9, 11, 9, 0, 0, 0, time.UTC).In(moscow))
set.ExDate(time.Date(1997, 9, 18, 9, 0, 0, 0, time.UTC).In(newYork))
set.RDate(time.Date(1997, 9, 4, 9, 0, 0, 0, time.UTC).In(tehran))
set.RDate(time.Date(1997, 9, 9, 9, 0, 0, 0, time.UTC))
want := `DTSTART:19970902T080000Z
RRULE:FREQ=YEARLY;COUNT=1;BYDAY=TU
RDATE;TZID=Asia/Tehran:19970904T133000
RDATE:19970909T090000Z
EXDATE:19970904T090000Z
EXDATE;TZID=Europe/Moscow:19970911T130000
EXDATE;TZID=America/New_York:19970918T050000`
value := set.String()
if want != value {
t.Errorf("get %v, want %v", value, want)
}
}
func TestSetDTStart(t *testing.T) {
set := Set{}
r, _ := NewRRule(ROption{Freq: YEARLY, Count: 1, Byweekday: []Weekday{TU},
Dtstart: time.Date(1997, 9, 2, 9, 0, 0, 0, time.UTC)})
set.RRule(r)
set.ExDate(time.Date(1997, 9, 4, 9, 0, 0, 0, time.UTC))
set.ExDate(time.Date(1997, 9, 11, 9, 0, 0, 0, time.UTC))
set.ExDate(time.Date(1997, 9, 18, 9, 0, 0, 0, time.UTC))
set.RDate(time.Date(1997, 9, 4, 9, 0, 0, 0, time.UTC))
set.RDate(time.Date(1997, 9, 9, 9, 0, 0, 0, time.UTC))
nyLoc, _ := time.LoadLocation("America/New_York")
set.DTStart(time.Date(1997, 9, 3, 9, 0, 0, 0, nyLoc))
want := `DTSTART;TZID=America/New_York:19970903T090000
RRULE:FREQ=YEARLY;COUNT=1;BYDAY=TU
RDATE:19970904T090000Z
RDATE:19970909T090000Z
EXDATE:19970904T090000Z
EXDATE:19970911T090000Z
EXDATE:19970918T090000Z`
value := set.String()
if want != value {
t.Errorf("get \n%v\n want \n%v\n", value, want)
}
sset, err := StrToRRuleSet(set.String())
if err != nil {
t.Errorf("Could not create RSET from set output")
}
if sset.String() != set.String() {
t.Errorf("RSET created from set output different than original set, %s", sset.String())
}
}
func TestSetRecurrence(t *testing.T) {
set := Set{}
r, _ := NewRRule(ROption{Freq: YEARLY, Count: 1, Byweekday: []Weekday{TU},
Dtstart: time.Date(1997, 9, 2, 9, 0, 0, 0, time.UTC)})
set.RRule(r)
value := set.Recurrence()
if len(value) != 2 {
t.Errorf("Wrong length for recurrence got=%v want=%v", len(value), 2)
}
want := "DTSTART:19970902T090000Z\nRRULE:FREQ=YEARLY;COUNT=1;BYDAY=TU"
if set.String() != want {
t.Errorf("get %s, want %v", set.String(), want)
}
}
func TestSetDate(t *testing.T) {
set := Set{}
r, _ := NewRRule(ROption{Freq: YEARLY, Count: 1, Byweekday: []Weekday{TU},
Dtstart: time.Date(1997, 9, 2, 9, 0, 0, 0, time.UTC)})
set.RRule(r)
set.RDate(time.Date(1997, 9, 4, 9, 0, 0, 0, time.UTC))
set.RDate(time.Date(1997, 9, 9, 9, 0, 0, 0, time.UTC))
value := set.All()
want := []time.Time{time.Date(1997, 9, 2, 9, 0, 0, 0, time.UTC),
time.Date(1997, 9, 4, 9, 0, 0, 0, time.UTC),
time.Date(1997, 9, 9, 9, 0, 0, 0, time.UTC)}
if !timesEqual(value, want) {
t.Errorf("get %v, want %v", value, want)
}
}
func TestSetRDates(t *testing.T) {
set := Set{}
r, _ := NewRRule(ROption{Freq: YEARLY, Count: 1, Byweekday: []Weekday{TU},
Dtstart: time.Date(1997, 9, 2, 9, 0, 0, 0, time.UTC)})
set.RRule(r)
set.SetRDates([]time.Time{
time.Date(1997, 9, 4, 9, 0, 0, 0, time.UTC),
time.Date(1997, 9, 9, 9, 0, 0, 0, time.UTC),
})
value := set.All()
want := []time.Time{
time.Date(1997, 9, 2, 9, 0, 0, 0, time.UTC),
time.Date(1997, 9, 4, 9, 0, 0, 0, time.UTC),
time.Date(1997, 9, 9, 9, 0, 0, 0, time.UTC),
}
if !timesEqual(value, want) {
t.Errorf("get %v, want %v", value, want)
}
}
func TestSetExDate(t *testing.T) {
set := Set{}
r, _ := NewRRule(ROption{Freq: YEARLY, Count: 6, Byweekday: []Weekday{TU, TH},
Dtstart: time.Date(1997, 9, 2, 9, 0, 0, 0, time.UTC)})
set.RRule(r)
set.ExDate(time.Date(1997, 9, 4, 9, 0, 0, 0, time.UTC))
set.ExDate(time.Date(1997, 9, 11, 9, 0, 0, 0, time.UTC))
set.ExDate(time.Date(1997, 9, 18, 9, 0, 0, 0, time.UTC))
value := set.All()
want := []time.Time{time.Date(1997, 9, 2, 9, 0, 0, 0, time.UTC),
time.Date(1997, 9, 9, 9, 0, 0, 0, time.UTC),
time.Date(1997, 9, 16, 9, 0, 0, 0, time.UTC)}
if !timesEqual(value, want) {
t.Errorf("get %v, want %v", value, want)
}
}
func TestSetExDates(t *testing.T) {
set := Set{}
r, _ := NewRRule(ROption{Freq: YEARLY, Count: 6, Byweekday: []Weekday{TU, TH},
Dtstart: time.Date(1997, 9, 2, 9, 0, 0, 0, time.UTC)})
set.RRule(r)
set.SetExDates([]time.Time{
time.Date(1997, 9, 4, 9, 0, 0, 0, time.UTC),
time.Date(1997, 9, 11, 9, 0, 0, 0, time.UTC),
time.Date(1997, 9, 18, 9, 0, 0, 0, time.UTC),
})
value := set.All()
want := []time.Time{time.Date(1997, 9, 2, 9, 0, 0, 0, time.UTC),
time.Date(1997, 9, 9, 9, 0, 0, 0, time.UTC),
time.Date(1997, 9, 16, 9, 0, 0, 0, time.UTC)}
if !timesEqual(value, want) {
t.Errorf("get %v, want %v", value, want)
}
}
func TestSetExDateRevOrder(t *testing.T) {
set := Set{}
r, _ := NewRRule(ROption{Freq: MONTHLY, Count: 5, Bymonthday: []int{10},
Dtstart: time.Date(2004, 1, 1, 9, 0, 0, 0, time.UTC)})
set.RRule(r)
set.ExDate(time.Date(2004, 4, 10, 9, 0, 0, 0, time.UTC))
set.ExDate(time.Date(2004, 2, 10, 9, 0, 0, 0, time.UTC))
value := set.All()
want := []time.Time{time.Date(2004, 1, 10, 9, 0, 0, 0, time.UTC),
time.Date(2004, 3, 10, 9, 0, 0, 0, time.UTC),
time.Date(2004, 5, 10, 9, 0, 0, 0, time.UTC)}
if !timesEqual(value, want) {
t.Errorf("get %v, want %v", value, want)
}
}
func TestSetDateAndExDate(t *testing.T) {
set := Set{}
set.RDate(time.Date(1997, 9, 2, 9, 0, 0, 0, time.UTC))
set.RDate(time.Date(1997, 9, 4, 9, 0, 0, 0, time.UTC))
set.RDate(time.Date(1997, 9, 9, 9, 0, 0, 0, time.UTC))
set.RDate(time.Date(1997, 9, 11, 9, 0, 0, 0, time.UTC))
set.RDate(time.Date(1997, 9, 16, 9, 0, 0, 0, time.UTC))
set.RDate(time.Date(1997, 9, 18, 9, 0, 0, 0, time.UTC))
set.ExDate(time.Date(1997, 9, 4, 9, 0, 0, 0, time.UTC))
set.ExDate(time.Date(1997, 9, 11, 9, 0, 0, 0, time.UTC))
set.ExDate(time.Date(1997, 9, 18, 9, 0, 0, 0, time.UTC))
value := set.All()
want := []time.Time{time.Date(1997, 9, 2, 9, 0, 0, 0, time.UTC),
time.Date(1997, 9, 9, 9, 0, 0, 0, time.UTC),
time.Date(1997, 9, 16, 9, 0, 0, 0, time.UTC)}
if !timesEqual(value, want) {
t.Errorf("get %v, want %v", value, want)
}
}
func TestSetBefore(t *testing.T) {
set := Set{}
r, _ := NewRRule(ROption{Freq: DAILY, Count: 7,
Dtstart: time.Date(1997, 9, 2, 9, 0, 0, 0, time.UTC)})
set.RRule(r)
want := time.Date(1997, 9, 4, 9, 0, 0, 0, time.UTC)
value := set.Before(time.Date(1997, 9, 5, 9, 0, 0, 0, time.UTC), false)
if value != want {
t.Errorf("get %v, want %v", value, want)
}
}
func TestSetBeforeInc(t *testing.T) {
set := Set{}
r, _ := NewRRule(ROption{Freq: DAILY, Count: 7,
Dtstart: time.Date(1997, 9, 2, 9, 0, 0, 0, time.UTC)})
set.RRule(r)
want := time.Date(1997, 9, 5, 9, 0, 0, 0, time.UTC)
value := set.Before(time.Date(1997, 9, 5, 9, 0, 0, 0, time.UTC), true)
if value != want {
t.Errorf("get %v, want %v", value, want)
}
}
func TestSetAfter(t *testing.T) {
set := Set{}
r, _ := NewRRule(ROption{Freq: DAILY, Count: 7,
Dtstart: time.Date(1997, 9, 2, 9, 0, 0, 0, time.UTC)})
set.RRule(r)
want := time.Date(1997, 9, 5, 9, 0, 0, 0, time.UTC)
value := set.After(time.Date(1997, 9, 4, 9, 0, 0, 0, time.UTC), false)
if value != want {
t.Errorf("get %v, want %v", value, want)
}
}
func TestSetAfterInc(t *testing.T) {
set := Set{}
r, _ := NewRRule(ROption{Freq: DAILY, Count: 7,
Dtstart: time.Date(1997, 9, 2, 9, 0, 0, 0, time.UTC)})
set.RRule(r)
want := time.Date(1997, 9, 4, 9, 0, 0, 0, time.UTC)
value := set.After(time.Date(1997, 9, 4, 9, 0, 0, 0, time.UTC), true)
if value != want {
t.Errorf("get %v, want %v", value, want)
}
}
func TestSetBetween(t *testing.T) {
set := Set{}
r, _ := NewRRule(ROption{Freq: DAILY, Count: 7,
Dtstart: time.Date(1997, 9, 2, 9, 0, 0, 0, time.UTC)})
set.RRule(r)
value := set.Between(time.Date(1997, 9, 3, 9, 0, 0, 0, time.UTC), time.Date(1997, 9, 6, 9, 0, 0, 0, time.UTC), false)
want := []time.Time{time.Date(1997, 9, 4, 9, 0, 0, 0, time.UTC),
time.Date(1997, 9, 5, 9, 0, 0, 0, time.UTC)}
if !timesEqual(value, want) {
t.Errorf("get %v, want %v", value, want)
}
}
func TestSetBetweenInc(t *testing.T) {
set := Set{}
r, _ := NewRRule(ROption{Freq: DAILY, Count: 7,
Dtstart: time.Date(1997, 9, 2, 9, 0, 0, 0, time.UTC)})
set.RRule(r)
value := set.Between(time.Date(1997, 9, 3, 9, 0, 0, 0, time.UTC), time.Date(1997, 9, 6, 9, 0, 0, 0, time.UTC), true)
want := []time.Time{time.Date(1997, 9, 3, 9, 0, 0, 0, time.UTC),
time.Date(1997, 9, 4, 9, 0, 0, 0, time.UTC),
time.Date(1997, 9, 5, 9, 0, 0, 0, time.UTC),
time.Date(1997, 9, 6, 9, 0, 0, 0, time.UTC)}
if !timesEqual(value, want) {
t.Errorf("get %v, want %v", value, want)
}
}
func TestSetTrickyTimeZones(t *testing.T) {
set := Set{}
moscow, _ := time.LoadLocation("Europe/Moscow")
newYork, _ := time.LoadLocation("America/New_York")
tehran, _ := time.LoadLocation("Asia/Tehran")
r, _ := NewRRule(ROption{
Freq: DAILY,
Count: 4,
Dtstart: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC).In(moscow),
})
set.RRule(r)
set.ExDate(time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC).In(newYork))
set.ExDate(time.Date(2000, 1, 2, 0, 0, 0, 0, time.UTC).In(tehran))
set.ExDate(time.Date(2000, 1, 3, 0, 0, 0, 0, time.UTC).In(moscow))
set.ExDate(time.Date(2000, 1, 4, 0, 0, 0, 0, time.UTC))
occurrences := set.All()
if len(occurrences) > 0 {
t.Errorf("No all occurrences excluded by ExDate: [%+v]", occurrences)
}
}
func TestSetDtStart(t *testing.T) {
ogr := []string{"DTSTART;TZID=America/Los_Angeles:20181115T000000", "RRULE:FREQ=DAILY;INTERVAL=1;WKST=SU;UNTIL=20181117T235959"}
set, _ := StrSliceToRRuleSet(ogr)
ogoc := set.All()
set.DTStart(set.GetDTStart().AddDate(0, 0, 1))
noc := set.All()
if len(noc) != len(ogoc)-1 {
t.Fatalf("As per the new DTStart the new occurences should exactly be one less that the original, new :%d original: %d", len(noc), len(ogoc))
}
for i := range noc {
if noc[i] != ogoc[i+1] {
t.Errorf("New occurences should just offset by one, mismatch at %d, expected: %+v, actual: %+v", i, ogoc[i+1], noc[i])
}
}
}
func TestRuleSetChangeDTStartTimezoneRespected(t *testing.T) {
/*
https://golang.org/pkg/time/#LoadLocation
"The time zone database needed by LoadLocation may not be present on all systems, especially non-Unix systems.
LoadLocation looks in the directory or uncompressed zip file named by the ZONEINFO environment variable,
if any, then looks in known installation locations on Unix systems, and finally looks in
$GOROOT/lib/time/zoneinfo.zip."
*/
loc, err := time.LoadLocation("CET")
if err != nil {
t.Fatal("expected", nil, "got", err)
}
ruleSet := &Set{}
rule, err := NewRRule(
ROption{
Freq: DAILY,
Count: 10,
Wkst: MO,
Byhour: []int{10},
Byminute: []int{0},
Bysecond: []int{0},
Dtstart: time.Date(2019, 3, 6, 0, 0, 0, 0, loc),
},
)
if err != nil {
t.Fatal("expected", nil, "got", err)
}
ruleSet.RRule(rule)
ruleSet.DTStart(time.Date(2019, 3, 6, 0, 0, 0, 0, time.UTC))
events := ruleSet.All()
if len(events) != 10 {
t.Fatal("expected", 10, "got", len(events))
}
for _, e := range events {
if e.Location().String() != "UTC" {
t.Fatal("expected", "UTC", "got", e.Location().String())
}
}
}