date_range_covers 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +17 -0
- data/.rvmrc +52 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/date_range_covers.gemspec +25 -0
- data/lib/a_date.rb +26 -0
- data/lib/date_range_covers/version.rb +3 -0
- data/lib/date_range_covers.rb +274 -0
- data/spec/a_date_spec.rb +86 -0
- data/spec/date_range_covers_spec.rb +782 -0
- metadata +135 -0
@@ -0,0 +1,782 @@
|
|
1
|
+
require 'lib/date_range_covers'
|
2
|
+
|
3
|
+
describe DateRangeCovers do
|
4
|
+
describe "#is_single_date_range?" do
|
5
|
+
context 'when same start and end dates are passed' do
|
6
|
+
it 'should return true' do
|
7
|
+
date = Date.today
|
8
|
+
drange = DateRangeCovers::DateRange.new(date, date)
|
9
|
+
drange.is_single_date_range?.should == true
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
context 'when same start and end dates are passed and either are nil' do
|
14
|
+
it 'should return true' do
|
15
|
+
date = nil
|
16
|
+
drange = DateRangeCovers::DateRange.new(date, date)
|
17
|
+
drange.is_single_date_range?.should == false
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
context 'when different start and end dates are passed' do
|
22
|
+
it 'should return false' do
|
23
|
+
date = Date.today
|
24
|
+
drange = DateRangeCovers::DateRange.new(date, date + 1)
|
25
|
+
drange.is_single_date_range?.should == false
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe "#is_date_range?" do
|
31
|
+
context 'when same start and end dates are passed' do
|
32
|
+
it 'should return false' do
|
33
|
+
date = Date.today
|
34
|
+
drange = DateRangeCovers::DateRange.new(date, date)
|
35
|
+
drange.is_date_range?.should == false
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
context 'when diff start and end dates are passed and either are nil' do
|
40
|
+
it 'should return false' do
|
41
|
+
date = nil
|
42
|
+
drange = DateRangeCovers::DateRange.new(date, date)
|
43
|
+
drange.is_date_range?.should == false
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
context 'when different start and end dates are passed' do
|
48
|
+
it 'should return true' do
|
49
|
+
date = Date.today
|
50
|
+
drange = DateRangeCovers::DateRange.new(date, date + 1)
|
51
|
+
drange.is_date_range?.should == true
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe "#months_covered" do
|
57
|
+
context 'when dates are in the same month' do
|
58
|
+
let(:date) { Date.today }
|
59
|
+
let(:drange) { DateRangeCovers::DateRange.new(date, date, :sunday) }
|
60
|
+
|
61
|
+
context 'when include is nil' do
|
62
|
+
let(:months) { drange.months_covered }
|
63
|
+
it 'should return no months' do
|
64
|
+
months.should be_empty
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
context 'when include is start_month' do
|
69
|
+
let(:months) { drange.months_covered(:include => :start_month) }
|
70
|
+
it 'should return start months' do
|
71
|
+
months.should_not be_empty
|
72
|
+
months.should include date.beginning_of_month
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
context 'when include is end_month' do
|
77
|
+
let(:months) { drange.months_covered(:include => :end_month) }
|
78
|
+
it 'should return end months' do
|
79
|
+
months.should_not be_empty
|
80
|
+
months.should include date.beginning_of_month
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
context 'when include is both' do
|
85
|
+
let(:months) { drange.months_covered(:include => :both) }
|
86
|
+
it 'should return start months' do
|
87
|
+
months.should_not be_empty
|
88
|
+
months.should include date.beginning_of_month
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
context 'when dates are not the same' do
|
94
|
+
context '2013-03-01 to 2013-03-31' do
|
95
|
+
let(:start_date) { Date.parse('2013-03-01') }
|
96
|
+
let(:end_date) { Date.parse('2013-03-31') }
|
97
|
+
let(:drange) { DateRangeCovers::DateRange.new(start_date, end_date, :sunday) }
|
98
|
+
|
99
|
+
context 'when include is nil' do
|
100
|
+
let(:months) { drange.months_covered }
|
101
|
+
it 'should return month with current start date' do
|
102
|
+
months.should_not be_empty
|
103
|
+
months.should include start_date.beginning_of_month
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
context 'when include is start_month' do
|
108
|
+
let(:months) { drange.months_covered(:include => :start_month) }
|
109
|
+
it 'should return month with current start date' do
|
110
|
+
months.should_not be_empty
|
111
|
+
months.should include start_date.beginning_of_month
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
context 'when include is end_month' do
|
116
|
+
let(:months) { drange.months_covered(:include => :end_month) }
|
117
|
+
it 'should return month with current start date' do
|
118
|
+
months.should_not be_empty
|
119
|
+
months.should include start_date.beginning_of_month
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
context 'when include is both' do
|
124
|
+
let(:months) { drange.months_covered(:include => :both) }
|
125
|
+
it 'should return month with current start date' do
|
126
|
+
months.should_not be_empty
|
127
|
+
months.should include start_date.beginning_of_month
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
context '2013-03-01 to 2013-03-30' do
|
133
|
+
let(:start_date) { Date.parse('2013-03-01') }
|
134
|
+
let(:end_date) { Date.parse('2013-03-30') }
|
135
|
+
let(:drange) { DateRangeCovers::DateRange.new(start_date, end_date, :sunday) }
|
136
|
+
|
137
|
+
context 'when include is nil' do
|
138
|
+
let(:months) { drange.months_covered }
|
139
|
+
it 'should return no months' do
|
140
|
+
months.should be_empty
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
context 'when include is start_month' do
|
145
|
+
let(:months) { drange.months_covered(:include => :start_month) }
|
146
|
+
it 'should return month with current start date' do
|
147
|
+
months.should_not be_empty
|
148
|
+
months.should include start_date.beginning_of_month
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
context 'when include is end_month' do
|
153
|
+
let(:months) { drange.months_covered(:include => :end_month) }
|
154
|
+
it 'should return month with current start date' do
|
155
|
+
months.should_not be_empty
|
156
|
+
months.should include start_date.beginning_of_month
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
context 'when include is both' do
|
161
|
+
let(:months) { drange.months_covered(:include => :both) }
|
162
|
+
it 'should return month with current start date' do
|
163
|
+
months.should_not be_empty
|
164
|
+
months.should include start_date.beginning_of_month
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
context '2013-02-15 to 2013-03-15' do
|
170
|
+
let(:start_date) { Date.parse('2013-02-15') }
|
171
|
+
let(:end_date) { Date.parse('2013-03-15') }
|
172
|
+
let(:drange) { DateRangeCovers::DateRange.new(start_date, end_date, :sunday) }
|
173
|
+
|
174
|
+
context 'when include is nil' do
|
175
|
+
let(:months) { drange.months_covered }
|
176
|
+
it 'should return no months' do
|
177
|
+
months.should be_empty
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
context 'when include is start_month' do
|
182
|
+
let(:months) { drange.months_covered(:include => :start_month) }
|
183
|
+
it 'should return month with current start date' do
|
184
|
+
months.should_not be_empty
|
185
|
+
months.should include start_date.beginning_of_month
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
context 'when include is end_month' do
|
190
|
+
let(:months) { drange.months_covered(:include => :end_month) }
|
191
|
+
it 'should return month with current start date' do
|
192
|
+
months.should_not be_empty
|
193
|
+
months.should include end_date.beginning_of_month
|
194
|
+
end
|
195
|
+
end
|
196
|
+
|
197
|
+
context 'when include is both' do
|
198
|
+
let(:months) { drange.months_covered(:include => :both) }
|
199
|
+
it 'should return month with current start date' do
|
200
|
+
months.length.should == 2
|
201
|
+
months.should include start_date.beginning_of_month
|
202
|
+
months.should include end_date.beginning_of_month
|
203
|
+
end
|
204
|
+
end
|
205
|
+
end
|
206
|
+
|
207
|
+
context '2013-02-15 to 2013-04-15' do
|
208
|
+
let(:start_date) { Date.parse('2013-02-15') }
|
209
|
+
let(:end_date) { Date.parse('2013-04-15') }
|
210
|
+
let(:drange) { DateRangeCovers::DateRange.new(start_date, end_date, :sunday) }
|
211
|
+
|
212
|
+
context 'when include is nil' do
|
213
|
+
let(:months) { drange.months_covered }
|
214
|
+
it 'should return no months' do
|
215
|
+
months.length.should == 1
|
216
|
+
months.should include Date.parse('2013-03-01')
|
217
|
+
end
|
218
|
+
end
|
219
|
+
|
220
|
+
context 'when include is start_month' do
|
221
|
+
let(:months) { drange.months_covered(:include => :start_month) }
|
222
|
+
it 'should return month with current start date' do
|
223
|
+
months.length.should == 2
|
224
|
+
months.should include start_date.beginning_of_month
|
225
|
+
months.should include Date.parse('2013-03-01')
|
226
|
+
end
|
227
|
+
end
|
228
|
+
|
229
|
+
context 'when include is end_month' do
|
230
|
+
let(:months) { drange.months_covered(:include => :end_month) }
|
231
|
+
it 'should return month with current start date' do
|
232
|
+
months.length.should == 2
|
233
|
+
months.should include end_date.beginning_of_month
|
234
|
+
months.should include Date.parse('2013-03-01')
|
235
|
+
end
|
236
|
+
end
|
237
|
+
|
238
|
+
context 'when include is both' do
|
239
|
+
let(:months) { drange.months_covered(:include => :both) }
|
240
|
+
it 'should return month with current start date' do
|
241
|
+
months.length.should == 3
|
242
|
+
months.should include start_date.beginning_of_month
|
243
|
+
months.should include end_date.beginning_of_month
|
244
|
+
months.should include Date.parse('2013-03-01')
|
245
|
+
end
|
246
|
+
end
|
247
|
+
end
|
248
|
+
|
249
|
+
context '2013-02-01 to 2013-04-15' do
|
250
|
+
let(:start_date) { Date.parse('2013-02-01') }
|
251
|
+
let(:end_date) { Date.parse('2013-04-15') }
|
252
|
+
let(:drange) { DateRangeCovers::DateRange.new(start_date, end_date, :sunday) }
|
253
|
+
|
254
|
+
context 'when include is nil' do
|
255
|
+
let(:months) { drange.months_covered }
|
256
|
+
it 'should return no months' do
|
257
|
+
months.length.should == 2
|
258
|
+
months.should include Date.parse('2013-03-01')
|
259
|
+
months.should include start_date.beginning_of_month
|
260
|
+
end
|
261
|
+
end
|
262
|
+
|
263
|
+
context 'when include is start_month' do
|
264
|
+
let(:months) { drange.months_covered(:include => :start_month) }
|
265
|
+
it 'should return month with current start date' do
|
266
|
+
months.length.should == 2
|
267
|
+
months.should include start_date.beginning_of_month
|
268
|
+
months.should include Date.parse('2013-03-01')
|
269
|
+
end
|
270
|
+
end
|
271
|
+
|
272
|
+
context 'when include is end_month' do
|
273
|
+
let(:months) { drange.months_covered(:include => :end_month) }
|
274
|
+
it 'should return month with current start date' do
|
275
|
+
months.length.should == 3
|
276
|
+
months.should include end_date.beginning_of_month
|
277
|
+
months.should include start_date.beginning_of_month
|
278
|
+
months.should include Date.parse('2013-03-01')
|
279
|
+
end
|
280
|
+
end
|
281
|
+
|
282
|
+
context 'when include is both' do
|
283
|
+
let(:months) { drange.months_covered(:include => :both) }
|
284
|
+
it 'should return month with current start date' do
|
285
|
+
months.length.should == 3
|
286
|
+
months.should include start_date.beginning_of_month
|
287
|
+
months.should include end_date.beginning_of_month
|
288
|
+
months.should include Date.parse('2013-03-01')
|
289
|
+
end
|
290
|
+
end
|
291
|
+
end
|
292
|
+
end
|
293
|
+
end
|
294
|
+
|
295
|
+
describe "#covers" do
|
296
|
+
context 'date range split across four months' do
|
297
|
+
let(:start_date) { Date.parse('2013-03-06') }
|
298
|
+
let(:end_date) { Date.parse('2013-06-22') }
|
299
|
+
let(:drange) { DateRangeCovers::DateRange.new(start_date, end_date, :sunday) }
|
300
|
+
|
301
|
+
context 'when include is :all' do
|
302
|
+
let(:covers) { drange.covers }
|
303
|
+
it "should return with just the days between" do
|
304
|
+
covers[:months].length.should == 2
|
305
|
+
(4..5).each do |month|
|
306
|
+
covers[:months].should include Date.parse("2013-#{month}-01")
|
307
|
+
end
|
308
|
+
|
309
|
+
covers[:weeks].length.should == 6
|
310
|
+
%w(2013-03-10 2013-03-17 2013-03-24 2013-06-02 2013-06-09 2013-06-16).each do |date|
|
311
|
+
covers[:weeks].should include Date.parse(date)
|
312
|
+
end
|
313
|
+
|
314
|
+
covers[:dates].length.should == 6
|
315
|
+
%w(2013-03-06 2013-03-07 2013-03-08 2013-03-09 2013-03-31 2013-06-01).each do |date|
|
316
|
+
covers[:dates].should include Date.parse(date)
|
317
|
+
end
|
318
|
+
end
|
319
|
+
end
|
320
|
+
|
321
|
+
context 'when include is :months' do
|
322
|
+
let(:covers) { drange.covers([:months]) }
|
323
|
+
it "should return with just the days between" do
|
324
|
+
covers[:months].length.should == 2
|
325
|
+
(4..5).each do |month|
|
326
|
+
covers[:months].should include Date.parse("2013-#{month}-01")
|
327
|
+
end
|
328
|
+
|
329
|
+
covers[:weeks].should be_nil
|
330
|
+
|
331
|
+
covers[:dates].length.should == 48
|
332
|
+
(6..31).each do |day|
|
333
|
+
covers[:dates].should include Date.parse("2013-03-#{day}")
|
334
|
+
end
|
335
|
+
(1..22).each do |day|
|
336
|
+
covers[:dates].should include Date.parse("2013-06-#{day}")
|
337
|
+
end
|
338
|
+
end
|
339
|
+
end
|
340
|
+
|
341
|
+
context 'when include is :weeks' do
|
342
|
+
let(:covers) { drange.covers([:weeks]) }
|
343
|
+
it "should return with just the weeks and dates" do
|
344
|
+
covers[:months].should be_nil
|
345
|
+
covers[:weeks].length.should == 15
|
346
|
+
covers[:dates].length.should == 4
|
347
|
+
end
|
348
|
+
end
|
349
|
+
|
350
|
+
context 'when include is :dates' do
|
351
|
+
let(:covers) { drange.covers([:dates]) }
|
352
|
+
it "should return with just the days between" do
|
353
|
+
covers[:months].should be_nil
|
354
|
+
covers[:weeks].should be_nil
|
355
|
+
covers[:dates].length.should == (start_date..end_date).collect {|date| date}.length
|
356
|
+
(start_date..end_date).each do |date|
|
357
|
+
covers[:dates].should include date
|
358
|
+
end
|
359
|
+
end
|
360
|
+
end
|
361
|
+
end
|
362
|
+
|
363
|
+
context 'date range split across three weeks' do
|
364
|
+
let(:start_date) { Date.parse('2013-03-06') }
|
365
|
+
let(:end_date) { Date.parse('2013-03-22') }
|
366
|
+
let(:drange) { DateRangeCovers::DateRange.new(start_date, end_date, :sunday) }
|
367
|
+
|
368
|
+
context 'when include is :all' do
|
369
|
+
let(:covers) { drange.covers }
|
370
|
+
it "should return with just the days between" do
|
371
|
+
covers[:months].should be_empty
|
372
|
+
covers[:weeks].length.should == 1
|
373
|
+
covers[:weeks].should include Date.parse('2013-03-10')
|
374
|
+
covers[:dates].length.should == 10
|
375
|
+
(Date.parse('2013-03-06')..Date.parse('2013-03-09')).each do |date|
|
376
|
+
covers[:dates].should include date
|
377
|
+
end
|
378
|
+
(Date.parse('2013-03-17')..Date.parse('2013-03-22')).each do |date|
|
379
|
+
covers[:dates].should include date
|
380
|
+
end
|
381
|
+
end
|
382
|
+
end
|
383
|
+
|
384
|
+
context 'when include is :months' do
|
385
|
+
let(:covers) { drange.covers([:months]) }
|
386
|
+
it "should return with just the days between" do
|
387
|
+
covers[:months].should be_empty
|
388
|
+
covers[:weeks].should be_nil
|
389
|
+
covers[:dates].length.should == 17
|
390
|
+
(start_date..end_date).each do |date|
|
391
|
+
covers[:dates].should include date
|
392
|
+
end
|
393
|
+
end
|
394
|
+
end
|
395
|
+
|
396
|
+
context 'when include is :weeks' do
|
397
|
+
let(:covers) { drange.covers([:weeks]) }
|
398
|
+
it "should return with just the days between" do
|
399
|
+
covers[:months].should be_nil
|
400
|
+
covers[:weeks].length.should == 1
|
401
|
+
covers[:weeks].should include Date.parse('2013-03-10')
|
402
|
+
covers[:dates].length.should == 10
|
403
|
+
(Date.parse('2013-03-06')..Date.parse('2013-03-09')).each do |date|
|
404
|
+
covers[:dates].should include date
|
405
|
+
end
|
406
|
+
(Date.parse('2013-03-17')..Date.parse('2013-03-22')).each do |date|
|
407
|
+
covers[:dates].should include date
|
408
|
+
end
|
409
|
+
end
|
410
|
+
end
|
411
|
+
|
412
|
+
context 'when include is :dates' do
|
413
|
+
let(:covers) { drange.covers([:dates]) }
|
414
|
+
it "should return with just the days between" do
|
415
|
+
covers[:months].should be_nil
|
416
|
+
covers[:weeks].should be_nil
|
417
|
+
covers[:dates].length.should == 17
|
418
|
+
(start_date..end_date).each do |date|
|
419
|
+
covers[:dates].should include date
|
420
|
+
end
|
421
|
+
end
|
422
|
+
end
|
423
|
+
end
|
424
|
+
|
425
|
+
context 'date range split across two weeks' do
|
426
|
+
let(:start_date) { Date.parse('2013-03-06') }
|
427
|
+
let(:end_date) { Date.parse('2013-03-15') }
|
428
|
+
let(:drange) { DateRangeCovers::DateRange.new(start_date, end_date, :sunday) }
|
429
|
+
|
430
|
+
context 'when include is :all' do
|
431
|
+
let(:covers) { drange.covers }
|
432
|
+
it "should return with just the days between" do
|
433
|
+
covers[:months].should be_empty
|
434
|
+
covers[:weeks].should be_empty
|
435
|
+
covers[:dates].should_not be_empty
|
436
|
+
(start_date..end_date).each do |date|
|
437
|
+
covers[:dates].should include date
|
438
|
+
end
|
439
|
+
end
|
440
|
+
end
|
441
|
+
|
442
|
+
context 'when include is :months' do
|
443
|
+
let(:covers) { drange.covers([:months]) }
|
444
|
+
it "should return with just the days between" do
|
445
|
+
covers[:months].should be_empty
|
446
|
+
covers[:weeks].should be_nil
|
447
|
+
covers[:dates].should_not be_empty
|
448
|
+
(start_date..end_date).each do |date|
|
449
|
+
covers[:dates].should include date
|
450
|
+
end
|
451
|
+
end
|
452
|
+
end
|
453
|
+
|
454
|
+
context 'when include is :weeks' do
|
455
|
+
let(:covers) { drange.covers([:weeks]) }
|
456
|
+
it "should return with just the days between" do
|
457
|
+
covers[:months].should be_nil
|
458
|
+
covers[:weeks].should be_empty
|
459
|
+
covers[:dates].should_not be_empty
|
460
|
+
(start_date..end_date).each do |date|
|
461
|
+
covers[:dates].should include date
|
462
|
+
end
|
463
|
+
end
|
464
|
+
end
|
465
|
+
|
466
|
+
context 'when include is :dates' do
|
467
|
+
let(:covers) { drange.covers([:dates]) }
|
468
|
+
it "should return with just the days between" do
|
469
|
+
covers[:months].should be_nil
|
470
|
+
covers[:weeks].should be_nil
|
471
|
+
covers[:dates].should_not be_empty
|
472
|
+
(start_date..end_date).each do |date|
|
473
|
+
covers[:dates].should include date
|
474
|
+
end
|
475
|
+
end
|
476
|
+
end
|
477
|
+
end
|
478
|
+
end
|
479
|
+
|
480
|
+
describe "#weeks_covered" do
|
481
|
+
context "when dates in the same week" do
|
482
|
+
context 'when dates are the same' do
|
483
|
+
let(:date) { Date.today }
|
484
|
+
let(:drange) { DateRangeCovers::DateRange.new(date, date, :sunday) }
|
485
|
+
|
486
|
+
context 'when include is nil' do
|
487
|
+
let(:weeks) { drange.weeks_covered }
|
488
|
+
it 'should return no weeks' do
|
489
|
+
weeks.should be_empty
|
490
|
+
end
|
491
|
+
end
|
492
|
+
|
493
|
+
context 'when include is :start_week' do
|
494
|
+
let(:weeks) { drange.weeks_covered(:include => :start_week) }
|
495
|
+
it 'should return week containing the start date' do
|
496
|
+
weeks.should_not be_empty
|
497
|
+
weeks.should include ADate.beginning_of_week(date)
|
498
|
+
end
|
499
|
+
end
|
500
|
+
|
501
|
+
context 'when include is :end_week' do
|
502
|
+
let(:weeks) { drange.weeks_covered(:include => :end_week) }
|
503
|
+
it 'should return week containing the end date' do
|
504
|
+
weeks.should_not be_empty
|
505
|
+
weeks.should include ADate.beginning_of_week(date)
|
506
|
+
end
|
507
|
+
end
|
508
|
+
|
509
|
+
context 'when include is :both' do
|
510
|
+
let(:weeks) { drange.weeks_covered(:include => :both) }
|
511
|
+
it 'should return weeks containing the start date and the end date' do
|
512
|
+
weeks.should_not be_empty
|
513
|
+
weeks.should include ADate.beginning_of_week(date)
|
514
|
+
end
|
515
|
+
end
|
516
|
+
end
|
517
|
+
end
|
518
|
+
|
519
|
+
context "when dates are in different weeks" do
|
520
|
+
context '2013-03-02(Saturday) to 2013-03-03(Sunday)' do
|
521
|
+
let(:start_date) { Date.parse('2013-03-02') }
|
522
|
+
let(:end_date) { Date.parse('2013-03-03') }
|
523
|
+
let(:drange) { DateRangeCovers::DateRange.new(start_date, end_date, :sunday) }
|
524
|
+
|
525
|
+
context 'when include is nil' do
|
526
|
+
let(:weeks) { drange.weeks_covered }
|
527
|
+
it 'should return no weeks' do
|
528
|
+
weeks.should be_empty
|
529
|
+
end
|
530
|
+
end
|
531
|
+
|
532
|
+
context 'when include is :start_week' do
|
533
|
+
let(:weeks) { drange.weeks_covered(:include => :start_week) }
|
534
|
+
it 'should return beginning of start week' do
|
535
|
+
weeks.should_not be_empty
|
536
|
+
weeks.should include ADate.beginning_of_week(start_date)
|
537
|
+
end
|
538
|
+
end
|
539
|
+
|
540
|
+
context 'when include is :end_week' do
|
541
|
+
let(:weeks) { drange.weeks_covered(:include => :end_week) }
|
542
|
+
it 'should return beginning of end week' do
|
543
|
+
weeks.should_not be_empty
|
544
|
+
weeks.should include ADate.beginning_of_week(end_date)
|
545
|
+
end
|
546
|
+
end
|
547
|
+
|
548
|
+
context 'when include is :both' do
|
549
|
+
let(:weeks) { drange.weeks_covered(:include => :both) }
|
550
|
+
it 'should return beginning of start week and end week' do
|
551
|
+
weeks.should_not be_empty
|
552
|
+
weeks.should include ADate.beginning_of_week(end_date)
|
553
|
+
weeks.should include ADate.beginning_of_week(start_date)
|
554
|
+
end
|
555
|
+
end
|
556
|
+
end
|
557
|
+
|
558
|
+
context '2013-03-03(Sunday) to 2013-03-09(Saturday)' do
|
559
|
+
let(:start_date) { Date.parse('2013-03-03') }
|
560
|
+
let(:end_date) { Date.parse('2013-03-09') }
|
561
|
+
let(:drange) { DateRangeCovers::DateRange.new(start_date, end_date, :sunday) }
|
562
|
+
context 'when include is nil' do
|
563
|
+
let(:weeks) { drange.weeks_covered }
|
564
|
+
it 'should return week with 2013-03-03 as the date' do
|
565
|
+
weeks.should_not be_empty
|
566
|
+
weeks.should include ADate.beginning_of_week(start_date)
|
567
|
+
end
|
568
|
+
end
|
569
|
+
|
570
|
+
context 'when include is :start_week' do
|
571
|
+
let(:weeks) { drange.weeks_covered(:include => :start_week) }
|
572
|
+
it 'should return beginning of start week' do
|
573
|
+
weeks.should_not be_empty
|
574
|
+
weeks.should include ADate.beginning_of_week(start_date)
|
575
|
+
end
|
576
|
+
end
|
577
|
+
|
578
|
+
context 'when include is :end_week' do
|
579
|
+
let(:weeks) { drange.weeks_covered(:include => :end_week) }
|
580
|
+
it 'should return beginning of end week' do
|
581
|
+
weeks.should_not be_empty
|
582
|
+
weeks.should include ADate.beginning_of_week(end_date)
|
583
|
+
end
|
584
|
+
end
|
585
|
+
|
586
|
+
context 'when include is :both' do
|
587
|
+
let(:weeks) { drange.weeks_covered(:include => :both) }
|
588
|
+
it 'should return beginning of start week and end week' do
|
589
|
+
weeks.should_not be_empty
|
590
|
+
weeks.should include ADate.beginning_of_week(end_date)
|
591
|
+
weeks.should include ADate.beginning_of_week(start_date)
|
592
|
+
end
|
593
|
+
end
|
594
|
+
end
|
595
|
+
|
596
|
+
context '2013-03-03(Sunday) to 2013-03-10(Sunday)' do
|
597
|
+
let(:start_date) { Date.parse('2013-03-03') }
|
598
|
+
let(:end_date) { Date.parse('2013-03-10') }
|
599
|
+
let(:drange) { DateRangeCovers::DateRange.new(start_date, end_date, :sunday) }
|
600
|
+
context 'when include is nil' do
|
601
|
+
let(:weeks) { drange.weeks_covered }
|
602
|
+
it 'should return week with 2013-03-03 as the date' do
|
603
|
+
weeks.should_not be_empty
|
604
|
+
weeks.should include ADate.beginning_of_week(start_date)
|
605
|
+
weeks.should_not include ADate.beginning_of_week(end_date)
|
606
|
+
end
|
607
|
+
end
|
608
|
+
|
609
|
+
context 'when include is :start_week' do
|
610
|
+
let(:weeks) { drange.weeks_covered(:include => :start_week) }
|
611
|
+
it 'should return beginning of start week' do
|
612
|
+
weeks.should_not be_empty
|
613
|
+
weeks.should include ADate.beginning_of_week(start_date)
|
614
|
+
weeks.should_not include ADate.beginning_of_week(end_date)
|
615
|
+
end
|
616
|
+
end
|
617
|
+
|
618
|
+
context 'when include is :end_week' do
|
619
|
+
let(:weeks) { drange.weeks_covered(:include => :end_week) }
|
620
|
+
it 'should return beginning of end week' do
|
621
|
+
weeks.should_not be_empty
|
622
|
+
weeks.should include ADate.beginning_of_week(end_date)
|
623
|
+
weeks.should include ADate.beginning_of_week(start_date)
|
624
|
+
end
|
625
|
+
end
|
626
|
+
|
627
|
+
context 'when include is :both' do
|
628
|
+
let(:weeks) { drange.weeks_covered(:include => :both) }
|
629
|
+
it 'should return beginning of start week and end week' do
|
630
|
+
weeks.should_not be_empty
|
631
|
+
weeks.should include ADate.beginning_of_week(end_date)
|
632
|
+
weeks.should include ADate.beginning_of_week(start_date)
|
633
|
+
end
|
634
|
+
end
|
635
|
+
end
|
636
|
+
|
637
|
+
context '2013-03-05(Tuesday) to 2013-03-31(Sunday)' do
|
638
|
+
let(:start_date) { Date.parse('2013-03-05') }
|
639
|
+
let(:end_date) { Date.parse('2013-03-31') }
|
640
|
+
let(:drange) { DateRangeCovers::DateRange.new(start_date, end_date, :sunday) }
|
641
|
+
context 'when include is nil' do
|
642
|
+
let(:weeks) { drange.weeks_covered }
|
643
|
+
it 'should return weeks of 10, 17 and 24' do
|
644
|
+
weeks.length.should == 3
|
645
|
+
weeks.should include Date.parse('2013-03-10')
|
646
|
+
weeks.should include Date.parse('2013-03-17')
|
647
|
+
weeks.should include Date.parse('2013-03-24')
|
648
|
+
end
|
649
|
+
end
|
650
|
+
|
651
|
+
context 'when include is :start_week' do
|
652
|
+
let(:weeks) { drange.weeks_covered(:include => :start_week) }
|
653
|
+
it 'should return beginning of start week' do
|
654
|
+
weeks.length.should == 4
|
655
|
+
weeks.should include ADate.beginning_of_week(start_date)
|
656
|
+
weeks.should include Date.parse('2013-03-10')
|
657
|
+
weeks.should include Date.parse('2013-03-17')
|
658
|
+
weeks.should include Date.parse('2013-03-24')
|
659
|
+
weeks.should_not include ADate.beginning_of_week(end_date)
|
660
|
+
end
|
661
|
+
end
|
662
|
+
|
663
|
+
context 'when include is :end_week' do
|
664
|
+
let(:weeks) { drange.weeks_covered(:include => :end_week) }
|
665
|
+
it 'should return beginning of end week' do
|
666
|
+
weeks.length.should == 4
|
667
|
+
weeks.should_not include ADate.beginning_of_week(start_date)
|
668
|
+
weeks.should include Date.parse('2013-03-10')
|
669
|
+
weeks.should include Date.parse('2013-03-17')
|
670
|
+
weeks.should include Date.parse('2013-03-24')
|
671
|
+
weeks.should include ADate.beginning_of_week(end_date)
|
672
|
+
end
|
673
|
+
end
|
674
|
+
|
675
|
+
context 'when include is :both' do
|
676
|
+
let(:weeks) { drange.weeks_covered(:include => :both) }
|
677
|
+
it 'should return beginning of start week and end week' do
|
678
|
+
weeks.length.should == 5
|
679
|
+
weeks.should include ADate.beginning_of_week(start_date)
|
680
|
+
weeks.should include Date.parse('2013-03-10')
|
681
|
+
weeks.should include Date.parse('2013-03-17')
|
682
|
+
weeks.should include Date.parse('2013-03-24')
|
683
|
+
weeks.should include ADate.beginning_of_week(end_date)
|
684
|
+
end
|
685
|
+
end
|
686
|
+
end
|
687
|
+
end
|
688
|
+
end
|
689
|
+
|
690
|
+
describe "#dates_covered" do
|
691
|
+
context "when start_date and end_date are the same" do
|
692
|
+
let(:date) { Date.today }
|
693
|
+
let(:drange) { DateRangeCovers::DateRange.new(date, date, :sunday) }
|
694
|
+
|
695
|
+
context 'when include is nil' do
|
696
|
+
let(:dates) { drange.dates_covered }
|
697
|
+
it { dates.should be_empty }
|
698
|
+
end
|
699
|
+
|
700
|
+
context 'when include is set to :both' do
|
701
|
+
let(:dates) { drange.dates_covered(:include => :both) }
|
702
|
+
it { dates.should include date }
|
703
|
+
end
|
704
|
+
|
705
|
+
context 'when include is set to :start_date' do
|
706
|
+
let(:dates) { drange.dates_covered(:include => :start_date) }
|
707
|
+
it { dates.should include date }
|
708
|
+
end
|
709
|
+
|
710
|
+
context 'when include is set to :end_date' do
|
711
|
+
let(:dates) { drange.dates_covered(:include => :end_date) }
|
712
|
+
it { dates.should include date }
|
713
|
+
end
|
714
|
+
end
|
715
|
+
|
716
|
+
context "when start_date and end_date are different" do
|
717
|
+
let(:start_date) { Date.today - 1 }
|
718
|
+
let(:end_date) { Date.today }
|
719
|
+
let(:drange) { DateRangeCovers::DateRange.new(start_date, end_date, :sunday) }
|
720
|
+
|
721
|
+
context 'when include is nil' do
|
722
|
+
let(:dates) { drange.dates_covered }
|
723
|
+
it { dates.should be_empty }
|
724
|
+
end
|
725
|
+
|
726
|
+
context 'when include is set to :start_date' do
|
727
|
+
let(:dates) { drange.dates_covered(:include => :start_date) }
|
728
|
+
it { dates.length.should == 1 }
|
729
|
+
it { dates.should include start_date }
|
730
|
+
it { dates.should_not include end_date }
|
731
|
+
end
|
732
|
+
|
733
|
+
context 'when include is set to :end_date' do
|
734
|
+
let(:dates) { drange.dates_covered(:include => :end_date) }
|
735
|
+
it { dates.length.should == 1 }
|
736
|
+
it { dates.should include end_date }
|
737
|
+
it { dates.should_not include start_date }
|
738
|
+
end
|
739
|
+
|
740
|
+
context 'when include is set to :both' do
|
741
|
+
let(:dates) { drange.dates_covered(:include => :both) }
|
742
|
+
it { dates.length.should == 2 }
|
743
|
+
it { dates.should include end_date }
|
744
|
+
it { dates.should include start_date }
|
745
|
+
end
|
746
|
+
end
|
747
|
+
|
748
|
+
context "when start_date and end_date are more than a day apart" do
|
749
|
+
let(:start_date) { Date.today - 2 }
|
750
|
+
let(:end_date) { Date.today }
|
751
|
+
let(:drange) { DateRangeCovers::DateRange.new(start_date, end_date, :sunday) }
|
752
|
+
|
753
|
+
context 'when include is nil' do
|
754
|
+
let(:dates) { drange.dates_covered }
|
755
|
+
it { dates.length.should == 1 }
|
756
|
+
it { dates.should_not include start_date }
|
757
|
+
it { dates.should_not include end_date }
|
758
|
+
end
|
759
|
+
|
760
|
+
context 'when include is set to :start_date' do
|
761
|
+
let(:dates) { drange.dates_covered(:include => :start_date) }
|
762
|
+
it { dates.length.should == 2 }
|
763
|
+
it { dates.should include start_date }
|
764
|
+
it { dates.should_not include end_date }
|
765
|
+
end
|
766
|
+
|
767
|
+
context 'when include is set to :end_date' do
|
768
|
+
let(:dates) { drange.dates_covered(:include => :end_date) }
|
769
|
+
it { dates.length.should == 2 }
|
770
|
+
it { dates.should include end_date }
|
771
|
+
it { dates.should_not include start_date }
|
772
|
+
end
|
773
|
+
|
774
|
+
context 'when include is set to :both' do
|
775
|
+
let(:dates) { drange.dates_covered(:include => :both) }
|
776
|
+
it { dates.length.should == 3 }
|
777
|
+
it { dates.should include end_date }
|
778
|
+
it { dates.should include start_date }
|
779
|
+
end
|
780
|
+
end
|
781
|
+
end
|
782
|
+
end
|