-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathweekly_test.go
98 lines (92 loc) · 3.69 KB
/
weekly_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
package recurrence
import (
. "github.com/smartystreets/goconvey/convey"
"testing"
"time"
)
func TestWeeklyPatternToInt(t *testing.T) {
Convey("With sunday as first day of week", t, func() {
startOfWeek := time.Sunday
Convey("sunday should encode to 1", func() {
So(WeeklyPatternToInt(startOfWeek, time.Sunday), ShouldEqual, 1)
})
Convey("monday should encode to 2", func() {
So(WeeklyPatternToInt(startOfWeek, time.Monday), ShouldEqual, 2)
})
Convey("sunday and mondy should encode to 3", func() {
So(WeeklyPatternToInt(startOfWeek, time.Sunday, time.Monday), ShouldEqual, 3)
})
Convey("passing the same day twice should not change the result", func() {
So(WeeklyPatternToInt(startOfWeek, time.Sunday, time.Sunday), ShouldEqual, 1)
})
Convey("passing all days should encode to 127", func() {
So(WeeklyPatternToInt(time.Sunday, time.Sunday, time.Monday, time.Tuesday, time.Wednesday, time.Thursday, time.Friday, time.Saturday), ShouldEqual, 127)
})
})
Convey("With monday as first day of week", t, func() {
Convey("sunday should encode to 257", func() {
So(WeeklyPatternToInt(time.Monday, time.Sunday), ShouldEqual, 257)
})
})
Convey("With tuesday as first day of week", t, func() {
Convey("sunday should encode to 513", func() {
So(WeeklyPatternToInt(time.Tuesday, time.Sunday), ShouldEqual, 513)
})
})
}
func TestWeeklyPattern(t *testing.T) {
Convey("In berlin", t, func() {
local, err := time.LoadLocation("Europe/Berlin")
So(err, ShouldBeNil)
Convey("With a weekly recurrence that happens every two weeks on monday and saturday", func() {
r := Recurrence{
Frequence: Weekly,
Location: local,
Interval: 2, // Every 2 weeks
Pattern: WeeklyPatternToInt(time.Monday, time.Monday, time.Saturday),
Start: time.Date(2016, 1, 1, 12, 0, 0, 0, local),
}
Convey("and an end in early 2017", func() {
r.End = time.Date(2017, 1, 12, 23, 59, 0, 0, time.UTC)
Convey("there should be no event after the end", func() {
nextEvent := r.GetNextDate(r.End)
So(nextEvent, ShouldNotHappen)
})
Convey("there should be no after 10th january 2017", func() {
nextEvent := r.GetNextDate(time.Date(2017, 1, 10, 0, 0, 0, 0, time.UTC))
So(nextEvent, ShouldNotHappen)
})
Convey("the first event should be on 2nd january", func() {
nextEvent := r.GetNextDate(time.Date(2015, 12, 1, 0, 0, 0, 0, time.UTC))
So(nextEvent, ShouldHappenOn, time.Date(2016, 1, 2, 12, 0, 0, 0, local))
})
Convey("the second event 11th january", func() {
nextEvent := r.GetNextDate(time.Date(2016, 1, 2, 12, 0, 0, 0, time.UTC))
So(nextEvent, ShouldHappenOn, time.Date(2016, 1, 11, 12, 0, 0, 0, local))
})
Convey("there should be another event on 12th feburary", func() {
nextEvent := r.GetNextDate(time.Date(2016, 2, 7, 0, 0, 0, 0, time.UTC))
So(nextEvent, ShouldHappenOn, time.Date(2016, 2, 8, 12, 0, 0, 0, local))
})
Convey("the time after the change to DST should stay the same", func() {
nextEvent := r.GetNextDate(time.Date(2016, 3, 26, 15, 0, 0, 0, time.UTC))
So(nextEvent, ShouldHappenOn, time.Date(2016, 4, 4, 12, 0, 0, 0, local))
})
})
Convey("without an enddate", func() {
r.End = time.Time{}
Convey("there should be an event in 2017", func() {
nextEvent := r.GetNextDate(time.Date(2017, 1, 1, 1, 0, 0, 0, time.UTC))
So(nextEvent, ShouldHappenOn, time.Date(2017, 1, 9, 12, 0, 0, 0, local))
})
})
Convey("without any day set", func() {
r.Pattern = WeeklyPatternToInt(time.Monday)
Convey("there should be no event", func() {
nextEvent := r.GetNextDate(time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC))
So(nextEvent, ShouldNotHappen)
})
})
})
})
}