-
Notifications
You must be signed in to change notification settings - Fork 2
/
range_test.go
255 lines (225 loc) · 6.26 KB
/
range_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
package date
import (
"encoding/json"
"testing"
)
var (
empty = Empty()
year2015 = EntireYear(2015)
nov = EntireMonth(2015, 11)
dec = EntireMonth(2015, 12)
novOnward = Range{Start: nov.Start}
decOnward = Range{Start: dec.Start}
untilDec = Range{End: nov.End}
jan = EntireMonth(2016, 1)
feb = EntireMonth(2016, 2)
)
func TestRange(t *testing.T) {
if !Infinity().IsInfinity() {
t.Error("Infinity should be infinity")
}
if !Empty().IsEmpty() {
t.Error("Empty should be empty")
}
if !Never().Equals(Empty()) {
t.Error("Never should equal Empty")
}
if !Forever().Equals(Infinity()) {
t.Error("Forever should equal Infinity")
}
if Forever().Equals(Empty()) {
t.Error("Forever should not equal Empty")
}
}
func TestRange_Contains(t *testing.T) {
if !year2015.Contains(dec) {
t.Error("year 2015 should contain December 2015")
}
if !dec.DoesNotContain(year2015) {
t.Error("December 2015 should not contain the year 2015")
}
if !novOnward.Contains(dec) {
t.Error("November 2015 onward should contain December 2015")
}
if year2015.Contains(novOnward) {
t.Error("Year 2015 should not contain November 2015 onward")
}
if !novOnward.Contains(novOnward) {
t.Error("November 2015 onward should contain itself")
}
if !novOnward.Contains(decOnward) {
t.Error("November 2015 onward should contain December 2015 onward")
}
if decOnward.Contains(novOnward) {
t.Error("December 2015 onward should not contain November 2015 onward")
}
}
var daysTests = []struct {
want, have int
}{
{0, Empty().Days()},
{1, OnlyToday().Days()},
{365, EntireYear(2015).Days()},
{366, EntireYear(2016).Days()},
{29, EntireMonth(2016, 2).Days()},
}
func TestRange_Days(t *testing.T) {
for _, test := range daysTests {
if test.want != test.have {
t.Errorf("Range Days() want=%d have=%d", test.want, test.have)
}
}
}
func TestRange_Error(t *testing.T) {
if Never().Error() != nil {
t.Error("Never should not error")
}
// Unbounded ranges are allowed
end := Range{End: New(2015, 3, 1)}
if end.Error() != nil {
t.Error("Unbounded start dates should not error")
}
start := Range{Start: New(2015, 3, 1)}
if start.Error() != nil {
t.Error("Unbounded end dates should not error")
}
var invalid Range
invalid.Start = New(2015, 3, 2)
invalid.End = New(2015, 3, 1)
if invalid.Error() == nil {
t.Error("Invalid ranges should error")
}
}
var intersectionTests = []struct {
want, have Range
}{
{dec, novOnward.Intersection(dec)},
{nov, year2015.Intersection(nov)},
{decOnward, decOnward.Intersection(novOnward)},
{decOnward, novOnward.Intersection(decOnward)},
{nov, novOnward.Intersection(untilDec)},
}
func TestRange_Intersection(t *testing.T) {
if !empty.Intersection(nov).IsZero() {
t.Error("An empty range should have a zero intersection")
}
for _, test := range intersectionTests {
if test.want != test.have {
t.Errorf(
"Range Intersection() want=%v have=%v", test.want, test.have,
)
}
}
}
func TestRange_Overlaps(t *testing.T) {
if nov.Overlaps(dec) {
t.Error("November should not overlap December")
}
if !dec.Overlaps(year2015) {
t.Error("December 2015 should overlap the year 2015")
}
if !nov.Overlaps(SingleDay(New(2015, 11, 30))) {
t.Error("November 2015 should overlap 2015-11-30")
}
if !novOnward.Overlaps(dec) {
t.Error("November 2015 onward should overlap December 2015")
}
}
func TestRange_Marshal(t *testing.T) {
// Empty ranges should render as null
b, err := json.Marshal(Never())
if err != nil {
t.Fatal("json.Marshal of Never() should not error")
}
if string(b) != "null" {
t.Error(`json.Marshal of Never() should be null`)
}
// Infinite ranges should render as null start and end dates
b, err = json.Marshal(Infinity())
if err != nil {
t.Fatal("json.Marshal of Infinity() should not error")
}
if string(b) != `{"start":null,"end":null}` {
t.Error(
`json.Marshal of Infinity() should be {"start":null,"end":null}`,
)
}
}
var stringTests = []struct {
want, have string
}{
{"never", Never().String()},
{"forever", Forever().String()},
{"2016-02-01 to 2016-02-29", EntireMonth(2016, 2).String()},
{"until 2016-02-29", Range{End: New(2016, 2, 29)}.String()},
{"2016-02-01 onward", Range{Start: New(2016, 2, 1)}.String()},
}
func TestRange_String(t *testing.T) {
for _, test := range stringTests {
if test.want != test.have {
t.Errorf("Range String() want=%s have=%s", test.want, test.have)
}
}
}
func TestRange_Union(t *testing.T) {
union := year2015.Union(jan)
if New(2015, 1, 1) != union.Start {
t.Error(
"The union of 2015 and January 2016 should start on 2015-01-01",
)
}
if New(2016, 1, 31) != union.End {
t.Error(
"The union of 2015 and January 2016 should end on 2016-01-31",
)
}
union = jan.Union(feb)
if jan.Start != union.Start {
t.Error(
"The union of January and February 2016 should start on 2016-01-01",
)
}
if feb.End != union.End {
t.Error(
"The union of January and February 2016 should end on 2016-02-29",
)
}
if decOnward.Union(novOnward) != novOnward {
t.Error("The union of November onward and December onward should be Novermber onward")
}
if untilDec.Union(decOnward) != Forever() {
t.Error("The union of until December and December onward should be forever")
}
if Empty().Union(Empty()) != Empty() {
t.Error("The union of two empty ranges should be empty")
}
if Empty().Union(Forever()) != Forever() {
t.Error("The union of a forever range should be forever")
}
if Forever().Union(Empty()) != Forever() {
t.Error("The union of a forever range should be forever")
}
}
func TestRange_Unmarshal(t *testing.T) {
// Unmarshaling should overwrite values
open := EntireMonth(2015, 2)
raw := `{"start":"2015-03-01","end":null}`
if json.Unmarshal([]byte(raw), &open) != nil {
t.Error("json.Unmarshal should not error")
}
if New(2015, 3, 1) != open.Start {
t.Error("The start date after json.Unmarshal should be 2015-03-01")
}
if !open.End.IsZero() {
t.Error("The end date after json.Unmarshal should be zero")
}
// TODO nulls should be unmarshaled as empty ranges
// raw = `null`
// var zero Range
// if json.Unmarshal([]byte(raw), &zero) != nil {
// t.Error("json.Unmarshal should not error for null ranges")
// }
// if !zero.IsEmpty() {
// t.Error("null ranges after json.Unmarshal should be empty")
// }
}