forked from danott/recurrence
/
intersection_test.go
88 lines (73 loc) · 2.52 KB
/
intersection_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
package recurrence
import (
"encoding/json"
"reflect"
"testing"
"time"
)
func TestIntersection(t *testing.T) {
i := Intersection{
January,
Sunday,
}
r := YearRange(2006)
assertIsOnlyOccurring(t, r, i, "2006-01-01", "2006-01-08", "2006-01-15",
"2006-01-22", "2006-01-29")
}
func TestIntersectionOccurrences(t *testing.T) {
tr := TimeRange{time.Time(NewDate("2006-01-01")), time.Time(NewDate("2009-12-31"))}
expectations := map[int]Schedule{
8: Intersection{Friday, Day(13)},
4: Intersection{November, Thursday, Week(4)},
3: Intersection{Day(Last), Day(28)},
}
assertOccurrenceGeneration2(t, tr, expectations)
}
func TestIntersectionMarshalJSON(t *testing.T) {
tests := map[string]Intersection{
`{"intersection":[{"day":1},{"day":"Last"},{"month":"January"}]}`: Intersection{Day(First), Day(Last), January},
`{"intersection":[{"weekday":"Thursday"},{"week":"Last"},{"year":2012}]}`: Intersection{Thursday, Week(Last), Year(2012)},
}
for expected, input := range tests {
output, err := json.Marshal(input)
if string(output) != expected || err != nil {
t.Errorf("\nInput: %v\nExpected: %v\nActual: %v\nError: %v", input, expected, string(output), err)
}
}
}
func TestIntersectionUnmarshalJSON(t *testing.T) {
tests := map[string]Intersection{
`[{"day":"Last"},{"month":"January"}]`: Intersection{Day(Last), January},
`[{"weekday":"Thursday"},{"year":2014}]`: Intersection{Thursday, Year(2014)},
}
for input, expected := range tests {
var output Intersection
err := json.Unmarshal([]byte(input), &output)
if !reflect.DeepEqual(output, expected) || err != nil {
t.Errorf("\nInput: %v\nExpected: %v\nActual: %v\nError: %v", input, expected, output, err)
}
}
}
func TestIntersectionDayAndHourMinuteSecondOccuring(t *testing.T) {
dhms := Intersection{Union{Saturday, Sunday}, NewHourMinuteSeconds(14, 30, 0)}
// Today is Sunday 14/02/2016
today := time.Date(2016, time.February, 14, 14, 30, 0, 0, time.UTC)
if !dhms.IsOccurring(today) {
t.Errorf("Event should be scheduled today: %v", today)
}
nextWeek := today.AddDate(0, 0, 6)
if !dhms.IsOccurring(nextWeek) {
t.Errorf("Event should be scheduled next Saturday too: %v", nextWeek)
}
nextWeek = nextWeek.AddDate(0, 0, 1)
if !dhms.IsOccurring(nextWeek) {
t.Errorf("Event should be scheduled next Sunday too: %v", nextWeek)
}
}
func BenchmarkIntersectionOccurrences(b *testing.B) {
d := Intersection{November, Thursday, Week(4)}
tr := TimeRange{time.Now(), time.Now().AddDate(1000, 0, 0)}
for n := 0; n < b.N; n++ {
d.Occurrences(tr)
}
}