-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecurrence_test.go
More file actions
205 lines (186 loc) · 6.83 KB
/
Copy pathrecurrence_test.go
File metadata and controls
205 lines (186 loc) · 6.83 KB
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
package icalendar
import (
"testing"
"time"
)
func mustRRule(t *testing.T, s string) *RRule {
t.Helper()
r, err := ParseRRule(s)
if err != nil {
t.Fatalf("ParseRRule(%q): %v", s, err)
}
return r
}
func datesEqual(t *testing.T, got, want []time.Time) {
t.Helper()
if len(got) != len(want) {
t.Fatalf("got %d times %v, want %d %v", len(got), got, len(want), want)
}
for i := range got {
if !got[i].Equal(want[i]) {
t.Errorf("times[%d] = %v, want %v", i, got[i], want[i])
}
}
}
func TestParseRRuleRoundTrip(t *testing.T) {
tests := []string{
"FREQ=DAILY",
"FREQ=WEEKLY;INTERVAL=2;COUNT=10;BYDAY=MO,WE,FR",
"FREQ=MONTHLY;BYDAY=-1FR",
"FREQ=YEARLY;BYMONTH=3;BYMONTHDAY=15",
"FREQ=MONTHLY;BYDAY=MO,TU,WE,TH,FR;BYSETPOS=-1",
}
for _, s := range tests {
r := mustRRule(t, s)
if got := r.String(); got != s {
t.Errorf("round-trip %q -> %q", s, got)
}
}
}
func TestParseRRuleErrors(t *testing.T) {
for _, s := range []string{"INTERVAL=2", "FREQ=DAILY;INTERVAL=0", "FREQ=DAILY;BYMONTH=13", "FREQ=DAILY;BYDAY=XX"} {
if _, err := ParseRRule(s); err == nil {
t.Errorf("ParseRRule(%q): want error", s)
}
}
}
func TestDailyCount(t *testing.T) {
start := time.Date(2026, 1, 1, 9, 0, 0, 0, time.UTC)
ev := &Event{UID: "d@x", Start: start, End: start.Add(time.Hour), Recurrence: mustRRule(t, "FREQ=DAILY;COUNT=3")}
got := ev.Occurrences(start, start.AddDate(0, 1, 0), 0)
datesEqual(t, got, []time.Time{
start,
start.AddDate(0, 0, 1),
start.AddDate(0, 0, 2),
})
}
func TestDailyInterval(t *testing.T) {
start := time.Date(2026, 1, 1, 9, 0, 0, 0, time.UTC)
ev := &Event{Start: start, Recurrence: mustRRule(t, "FREQ=DAILY;INTERVAL=2;COUNT=3")}
got := ev.Occurrences(start, start.AddDate(0, 1, 0), 0)
datesEqual(t, got, []time.Time{start, start.AddDate(0, 0, 2), start.AddDate(0, 0, 4)})
}
func TestWeeklyByDay(t *testing.T) {
// Thursday 2026-01-01. Want MO & WE, 4 occurrences.
start := time.Date(2026, 1, 1, 9, 0, 0, 0, time.UTC)
ev := &Event{Start: start, Recurrence: mustRRule(t, "FREQ=WEEKLY;BYDAY=MO,WE;COUNT=4")}
got := ev.Occurrences(start, start.AddDate(0, 2, 0), 0)
want := []time.Time{
time.Date(2026, 1, 5, 9, 0, 0, 0, time.UTC), // Mon
time.Date(2026, 1, 7, 9, 0, 0, 0, time.UTC), // Wed
time.Date(2026, 1, 12, 9, 0, 0, 0, time.UTC), // Mon
time.Date(2026, 1, 14, 9, 0, 0, 0, time.UTC), // Wed
}
datesEqual(t, got, want)
}
func TestUntilBound(t *testing.T) {
start := time.Date(2026, 1, 1, 9, 0, 0, 0, time.UTC)
ev := &Event{Start: start, Recurrence: mustRRule(t, "FREQ=DAILY;UNTIL=20260103T090000Z")}
got := ev.Occurrences(start, start.AddDate(0, 1, 0), 0)
datesEqual(t, got, []time.Time{start, start.AddDate(0, 0, 1), start.AddDate(0, 0, 2)})
}
func TestMonthlyByMonthDay(t *testing.T) {
start := time.Date(2026, 1, 15, 10, 0, 0, 0, time.UTC)
ev := &Event{Start: start, Recurrence: mustRRule(t, "FREQ=MONTHLY;COUNT=3")}
got := ev.Occurrences(start, start.AddDate(1, 0, 0), 0)
datesEqual(t, got, []time.Time{
time.Date(2026, 1, 15, 10, 0, 0, 0, time.UTC),
time.Date(2026, 2, 15, 10, 0, 0, 0, time.UTC),
time.Date(2026, 3, 15, 10, 0, 0, 0, time.UTC),
})
}
func TestMonthlySkipsMissingDay(t *testing.T) {
// 31st: Feb and Apr have no 31st, so they're skipped (RFC 5545).
start := time.Date(2026, 1, 31, 8, 0, 0, 0, time.UTC)
ev := &Event{Start: start, Recurrence: mustRRule(t, "FREQ=MONTHLY;BYMONTHDAY=31;COUNT=3")}
got := ev.Occurrences(start, start.AddDate(1, 0, 0), 0)
datesEqual(t, got, []time.Time{
time.Date(2026, 1, 31, 8, 0, 0, 0, time.UTC),
time.Date(2026, 3, 31, 8, 0, 0, 0, time.UTC),
time.Date(2026, 5, 31, 8, 0, 0, 0, time.UTC),
})
}
func TestMonthlyLastFriday(t *testing.T) {
start := time.Date(2026, 1, 1, 12, 0, 0, 0, time.UTC)
ev := &Event{Start: start, Recurrence: mustRRule(t, "FREQ=MONTHLY;BYDAY=-1FR;COUNT=3")}
got := ev.Occurrences(start, start.AddDate(1, 0, 0), 0)
want := []time.Time{
time.Date(2026, 1, 30, 12, 0, 0, 0, time.UTC),
time.Date(2026, 2, 27, 12, 0, 0, 0, time.UTC),
time.Date(2026, 3, 27, 12, 0, 0, 0, time.UTC),
}
datesEqual(t, got, want)
}
func TestMonthlySecondMonday(t *testing.T) {
start := time.Date(2026, 1, 1, 12, 0, 0, 0, time.UTC)
ev := &Event{Start: start, Recurrence: mustRRule(t, "FREQ=MONTHLY;BYDAY=2MO;COUNT=2")}
got := ev.Occurrences(start, start.AddDate(1, 0, 0), 0)
want := []time.Time{
time.Date(2026, 1, 12, 12, 0, 0, 0, time.UTC),
time.Date(2026, 2, 9, 12, 0, 0, 0, time.UTC),
}
datesEqual(t, got, want)
}
func TestYearly(t *testing.T) {
start := time.Date(2026, 3, 15, 9, 0, 0, 0, time.UTC)
ev := &Event{Start: start, Recurrence: mustRRule(t, "FREQ=YEARLY;COUNT=3")}
got := ev.Occurrences(start, start.AddDate(5, 0, 0), 0)
datesEqual(t, got, []time.Time{
time.Date(2026, 3, 15, 9, 0, 0, 0, time.UTC),
time.Date(2027, 3, 15, 9, 0, 0, 0, time.UTC),
time.Date(2028, 3, 15, 9, 0, 0, 0, time.UTC),
})
}
func TestBySetPosLastWeekday(t *testing.T) {
// Last weekday (MO-FR) of the month.
start := time.Date(2026, 1, 1, 9, 0, 0, 0, time.UTC)
ev := &Event{Start: start, Recurrence: mustRRule(t, "FREQ=MONTHLY;BYDAY=MO,TU,WE,TH,FR;BYSETPOS=-1;COUNT=2")}
got := ev.Occurrences(start, start.AddDate(1, 0, 0), 0)
want := []time.Time{
time.Date(2026, 1, 30, 9, 0, 0, 0, time.UTC), // Fri Jan 30
time.Date(2026, 2, 27, 9, 0, 0, 0, time.UTC), // Fri Feb 27
}
datesEqual(t, got, want)
}
func TestWindowFiltersAndExDate(t *testing.T) {
start := time.Date(2026, 1, 1, 9, 0, 0, 0, time.UTC)
ev := &Event{
Start: start,
Recurrence: mustRRule(t, "FREQ=DAILY;COUNT=10"),
ExDates: []time.Time{start.AddDate(0, 0, 1)},
}
// Window covering days 0..4 (5 occurrences), minus the excluded day 1.
got := ev.Occurrences(start, start.AddDate(0, 0, 5), 0)
datesEqual(t, got, []time.Time{
start,
start.AddDate(0, 0, 2),
start.AddDate(0, 0, 3),
start.AddDate(0, 0, 4),
})
}
func TestRDateMerge(t *testing.T) {
start := time.Date(2026, 1, 1, 9, 0, 0, 0, time.UTC)
extra := time.Date(2026, 1, 1, 15, 0, 0, 0, time.UTC)
ev := &Event{Start: start, Recurrence: mustRRule(t, "FREQ=DAILY;COUNT=1"), RDates: []time.Time{extra}}
got := ev.Occurrences(start, start.AddDate(0, 0, 1), 0)
datesEqual(t, got, []time.Time{start, extra})
}
func TestNonRecurringOccurrences(t *testing.T) {
start := time.Date(2026, 1, 1, 9, 0, 0, 0, time.UTC)
ev := &Event{Start: start}
if got := ev.Occurrences(start, start.AddDate(0, 0, 1), 0); len(got) != 1 || !got[0].Equal(start) {
t.Errorf("non-recurring occurrences = %v", got)
}
// Outside window -> none.
if got := ev.Occurrences(start.AddDate(0, 0, 1), start.AddDate(0, 0, 2), 0); len(got) != 0 {
t.Errorf("out-of-window = %v, want none", got)
}
}
func TestLimit(t *testing.T) {
start := time.Date(2026, 1, 1, 9, 0, 0, 0, time.UTC)
ev := &Event{Start: start, Recurrence: mustRRule(t, "FREQ=DAILY")}
got := ev.Occurrences(start, start.AddDate(1, 0, 0), 5)
if len(got) != 5 {
t.Errorf("limit not honored: got %d", len(got))
}
}