fat_core 1.0.1 → 1.0.2
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.
- checksums.yaml +4 -4
- data/lib/fat_core/date.rb +2 -2
- data/lib/fat_core/period.rb +18 -21
- data/lib/fat_core/version.rb +1 -1
- data/spec/lib/date_spec.rb +215 -161
- data/spec/lib/period_spec.rb +120 -103
- metadata +2 -2
data/spec/lib/period_spec.rb
CHANGED
@@ -4,28 +4,27 @@ describe Period do
|
|
4
4
|
before :each do
|
5
5
|
# Pretend it is this date. Not at beg or end of year, quarter,
|
6
6
|
# month, or week. It is a Wednesday
|
7
|
-
allow(Date).to receive_messages(:
|
8
|
-
allow(Date).to receive_messages(:
|
7
|
+
allow(Date).to receive_messages(today: Date.parse('2012-07-18'))
|
8
|
+
allow(Date).to receive_messages(current: Date.parse('2012-07-18'))
|
9
9
|
end
|
10
10
|
|
11
|
-
describe
|
12
|
-
|
13
|
-
it "should be initializable with date strings" do
|
11
|
+
describe 'initialization' do
|
12
|
+
it 'should be initializable with date strings' do
|
14
13
|
expect(Period.new('2013-01-01', '2013-12-13')).to be_instance_of Period
|
15
14
|
end
|
16
15
|
|
17
|
-
it
|
18
|
-
expect(Period.new('2013-01-01', '2013-12-13'))
|
19
|
-
to be_instance_of Period
|
16
|
+
it 'should be initializable with Dates' do
|
17
|
+
expect(Period.new('2013-01-01', '2013-12-13'))
|
18
|
+
.to be_instance_of Period
|
20
19
|
end
|
21
20
|
|
22
|
-
it
|
21
|
+
it 'should raise a ArgumentError if last > first' do
|
23
22
|
expect {
|
24
23
|
Period.new('2013-01-01', '2012-12-31')
|
25
24
|
}.to raise_error ArgumentError
|
26
25
|
end
|
27
26
|
|
28
|
-
it
|
27
|
+
it 'should raise a ArgumentError if initialized with invalid date string' do
|
29
28
|
expect {
|
30
29
|
Period.new('2013-01-01', '2013-12-32')
|
31
30
|
}.to raise_error ArgumentError
|
@@ -34,7 +33,7 @@ describe Period do
|
|
34
33
|
}.to raise_error ArgumentError
|
35
34
|
end
|
36
35
|
|
37
|
-
it
|
36
|
+
it 'should raise a ArgumentError if initialized otherwise' do
|
38
37
|
expect {
|
39
38
|
Period.new(2013-01-01, 2013-12-31)
|
40
39
|
}.to raise_error ArgumentError
|
@@ -122,7 +121,7 @@ describe Period do
|
|
122
121
|
expect(Period.chunk_syms.size).to eq 9
|
123
122
|
end
|
124
123
|
|
125
|
-
it
|
124
|
+
it 'should know the days in a chunk sym' do
|
126
125
|
expect(Period.chunk_sym_to_days(:year)).to eq(365)
|
127
126
|
expect(Period.chunk_sym_to_days(:quarter)).to eq(90)
|
128
127
|
expect(Period.chunk_sym_to_days(:bimonth)).to eq(60)
|
@@ -137,7 +136,7 @@ describe Period do
|
|
137
136
|
}.to raise_error ArgumentError
|
138
137
|
end
|
139
138
|
|
140
|
-
it
|
139
|
+
it 'should know the maximum days in a chunk sym' do
|
141
140
|
expect(Period.chunk_sym_to_max_days(:year)).to eq(366)
|
142
141
|
expect(Period.chunk_sym_to_max_days(:quarter)).to eq(92)
|
143
142
|
expect(Period.chunk_sym_to_max_days(:bimonth)).to eq(62)
|
@@ -146,14 +145,14 @@ describe Period do
|
|
146
145
|
expect(Period.chunk_sym_to_max_days(:biweek)).to eq(14)
|
147
146
|
expect(Period.chunk_sym_to_max_days(:week)).to eq(7)
|
148
147
|
expect(Period.chunk_sym_to_max_days(:day)).to eq(1)
|
149
|
-
expect{ Period.chunk_sym_to_max_days(:irregular) }
|
148
|
+
expect { Period.chunk_sym_to_max_days(:irregular) }
|
150
149
|
.to raise_error ArgumentError
|
151
150
|
expect {
|
152
151
|
Period.chunk_sym_to_days(:eon)
|
153
152
|
}.to raise_error ArgumentError
|
154
153
|
end
|
155
154
|
|
156
|
-
it
|
155
|
+
it 'should know the chunk sym for given days but only :year, :quarter, :month' do
|
157
156
|
(356..376).each { |d| expect(Period.days_to_chunk_sym(d)).to eq(:year) }
|
158
157
|
(86..96).each { |d| expect(Period.days_to_chunk_sym(d)).to eq(:quarter) }
|
159
158
|
(28..31).each { |d| expect(Period.days_to_chunk_sym(d)).to eq(:month) }
|
@@ -161,12 +160,14 @@ describe Period do
|
|
161
160
|
expect(Period.days_to_chunk_sym(1)).to eq(:day)
|
162
161
|
end
|
163
162
|
|
164
|
-
it
|
163
|
+
it 'should know what to call a chunk based on its size' do
|
165
164
|
expect(Period.new('2011-01-01', '2011-12-31').chunk_name).to eq('Year')
|
166
165
|
expect(Period.new('2011-01-01', '2011-03-31').chunk_name).to eq('Quarter')
|
167
|
-
expect(Period.new('2011-01-01', '2011-02-28').chunk_name)
|
166
|
+
expect(Period.new('2011-01-01', '2011-02-28').chunk_name)
|
167
|
+
.to eq('Bi-month')
|
168
168
|
expect(Period.new('2011-01-01', '2011-01-31').chunk_name).to eq('Month')
|
169
|
-
expect(Period.new('2011-01-01', '2011-01-15').chunk_name)
|
169
|
+
expect(Period.new('2011-01-01', '2011-01-15').chunk_name)
|
170
|
+
.to eq('Semi-month')
|
170
171
|
expect(Period.new('2011-01-09', '2011-01-22').chunk_name).to eq('Bi-week')
|
171
172
|
expect(Period.new('2011-01-01', '2011-01-07').chunk_name).to eq('Week')
|
172
173
|
expect(Period.new('2011-01-01', '2011-01-01').chunk_name).to eq('Day')
|
@@ -177,8 +178,8 @@ describe Period do
|
|
177
178
|
end
|
178
179
|
end
|
179
180
|
|
180
|
-
describe
|
181
|
-
it
|
181
|
+
describe 'sorting' do
|
182
|
+
it 'should sort by first, then size' do
|
182
183
|
periods = []
|
183
184
|
periods << Period.new('2012-07-01', '2012-07-31')
|
184
185
|
periods << Period.new('2012-06-01', '2012-06-30')
|
@@ -193,42 +194,41 @@ describe Period do
|
|
193
194
|
expect(periods[2].last).to eq(Date.parse('2012-08-31'))
|
194
195
|
end
|
195
196
|
|
196
|
-
it
|
197
|
+
it 'should return nil if comparing incomparables' do
|
197
198
|
pd = Period.new('2012-08-01', '2012-08-31')
|
198
199
|
rg = (Date.parse('2012-08-01')..Date.parse('2012-08-31'))
|
199
200
|
expect(pd <=> rg).to be_nil
|
200
201
|
end
|
201
202
|
end
|
202
203
|
|
203
|
-
describe
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
expect(pp.first).to eq Date.parse('2014-12-01')
|
204
|
+
describe 'instance methods' do
|
205
|
+
it 'should be able to set first' do
|
206
|
+
pp = Period.new('2014-12-07', '2014-12-17')
|
207
|
+
pp.first = Date.parse('2014-12-01')
|
208
|
+
expect(pp.first).to eq Date.parse('2014-12-01')
|
209
209
|
end
|
210
210
|
|
211
|
-
it
|
212
|
-
|
213
|
-
|
214
|
-
|
211
|
+
it 'should be able to set last' do
|
212
|
+
pp = Period.new('2014-12-07', '2014-12-17')
|
213
|
+
pp.last = Date.parse('2014-12-31')
|
214
|
+
expect(pp.last).to eq Date.parse('2014-12-31')
|
215
215
|
end
|
216
216
|
|
217
|
-
it
|
217
|
+
it 'should not be able to set first > last' do
|
218
218
|
pp = Period.new('2014-12-07', '2014-12-17')
|
219
219
|
expect {
|
220
220
|
pp.first = Date.parse('2014-12-31')
|
221
221
|
}.to raise_error ArgumentError
|
222
222
|
end
|
223
223
|
|
224
|
-
it
|
224
|
+
it 'should not be able to set last < first' do
|
225
225
|
pp = Period.new('2014-12-07', '2014-12-17')
|
226
226
|
expect {
|
227
227
|
pp.last = Date.parse('2014-12-01')
|
228
228
|
}.to raise_error ArgumentError
|
229
229
|
end
|
230
230
|
|
231
|
-
it
|
231
|
+
it 'should be able to compare for equality' do
|
232
232
|
pp1 = Period.new('2013-01-01', '2013-12-31')
|
233
233
|
pp2 = Period.new('2013-01-01', '2013-12-31')
|
234
234
|
pp3 = Period.new('2013-01-01', '2013-12-30')
|
@@ -237,7 +237,7 @@ describe Period do
|
|
237
237
|
expect((pp1 != pp3)).to be true
|
238
238
|
end
|
239
239
|
|
240
|
-
it
|
240
|
+
it 'should be able to convert into a Range' do
|
241
241
|
pp = Period.new('2013-01-01', '2013-12-31')
|
242
242
|
rr = Period.new('2013-01-01', '2013-12-31').to_range
|
243
243
|
expect(rr).to be_instance_of Range
|
@@ -245,7 +245,7 @@ describe Period do
|
|
245
245
|
expect(rr.last).to eq(pp.last)
|
246
246
|
end
|
247
247
|
|
248
|
-
it
|
248
|
+
it 'should be able to tell if it contains a date' do
|
249
249
|
pp = Period.new('2013-01-01', '2013-12-31')
|
250
250
|
expect(pp.contains?(Date.parse('2013-01-01'))).to be true
|
251
251
|
expect(pp.contains?(Date.parse('2013-07-04'))).to be true
|
@@ -253,7 +253,7 @@ describe Period do
|
|
253
253
|
expect(pp.contains?(Date.parse('2012-07-04'))).to be false
|
254
254
|
end
|
255
255
|
|
256
|
-
it
|
256
|
+
it 'should raise an error if contains? arg is not a date' do
|
257
257
|
pp = Period.new('2013-01-01', '2013-12-31')
|
258
258
|
expect {
|
259
259
|
pp.contains?(Period.new('2013-06-01', '2013-06-30'))
|
@@ -265,7 +265,7 @@ describe Period do
|
|
265
265
|
}.not_to raise_error
|
266
266
|
end
|
267
267
|
|
268
|
-
it
|
268
|
+
it 'should be able to tell if it contains a date with ===' do
|
269
269
|
pp = Period.new('2013-01-01', '2013-12-31')
|
270
270
|
expect(pp === Date.parse('2013-01-01')).to be true
|
271
271
|
expect(pp === Date.parse('2013-07-04')).to be true
|
@@ -273,48 +273,48 @@ describe Period do
|
|
273
273
|
expect(pp === Date.parse('2012-07-04')).to be false
|
274
274
|
end
|
275
275
|
|
276
|
-
it
|
276
|
+
it 'should be able to convert itself to days' do
|
277
277
|
expect(Period.new('2013-01-01', '2013-01-01').days).to eq(1)
|
278
278
|
expect(Period.new('2013-01-01', '2013-12-31').days).to eq(365)
|
279
279
|
end
|
280
280
|
|
281
|
-
it
|
281
|
+
it 'should be able to convert itself to fractional months' do
|
282
282
|
expect(Period.new('2013-01-01', '2013-01-01').months).to eq(1/30.436875)
|
283
283
|
expect(Period.new('2013-01-01', '2013-12-31').months(30)).
|
284
284
|
to eq(365 / 30.0)
|
285
285
|
expect(Period.new('2013-01-01', '2013-06-30').months.round(0)).to eq(6.0)
|
286
286
|
end
|
287
287
|
|
288
|
-
it
|
288
|
+
it 'should be able to convert itself to fractional years' do
|
289
289
|
expect(Period.new('2013-01-01', '2013-01-01').years).to eq(1/365.2425)
|
290
290
|
expect(Period.new('2013-01-01', '2013-12-31').years(365)).to eq(1.0)
|
291
291
|
expect(Period.new('2013-01-01', '2013-06-30').years.round(1)).to eq(0.5)
|
292
292
|
end
|
293
293
|
|
294
|
-
it
|
294
|
+
it 'should be able to enumerate its days' do
|
295
295
|
Period.parse('2014-12').each do |dy|
|
296
296
|
expect(dy.class).to eq Date
|
297
297
|
end
|
298
298
|
end
|
299
299
|
|
300
|
-
it
|
300
|
+
it 'should be able to return the trading days within period' do
|
301
301
|
tds = Period.parse('2014-12').trading_days
|
302
302
|
expect(tds.count).to eq(22)
|
303
303
|
end
|
304
304
|
|
305
|
-
it
|
305
|
+
it 'should know its size' do
|
306
306
|
pp = Period.new('2013-01-01', '2013-12-31')
|
307
307
|
expect(pp.size).to eq 365
|
308
308
|
expect(pp.length).to eq 365
|
309
309
|
end
|
310
310
|
|
311
|
-
it
|
311
|
+
it 'should implement the each method' do
|
312
312
|
pp = Period.new('2013-12-01', '2013-12-31')
|
313
|
-
pp.map
|
313
|
+
pp.map(&:iso)
|
314
314
|
.each { |s| expect(s).to match(/\d{4}-\d\d-\d\d/) }
|
315
315
|
end
|
316
316
|
|
317
|
-
it
|
317
|
+
it 'should be able to make a concise period string' do
|
318
318
|
expect(Period.new('2013-01-01', '2013-12-31').to_s).to eq('2013')
|
319
319
|
expect(Period.new('2013-04-01', '2013-06-30').to_s).to eq('2013-2Q')
|
320
320
|
expect(Period.new('2013-03-01', '2013-03-31').to_s).to eq('2013-03')
|
@@ -322,13 +322,13 @@ describe Period do
|
|
322
322
|
.to eq('2013-03-11 to 2013-10-31')
|
323
323
|
end
|
324
324
|
|
325
|
-
it
|
325
|
+
it 'should be able to make a TeX string' do
|
326
326
|
expect(Period.new('2013-01-01', '2013-12-31').tex_quote)
|
327
327
|
.to eq('2013-01-01--2013-12-31')
|
328
328
|
end
|
329
329
|
|
330
330
|
# Note in the following that first period must begin within self.
|
331
|
-
it
|
331
|
+
it 'should be able to chunk into years' do
|
332
332
|
chunks = Period.new('2009-12-15', '2013-01-10').chunks(size: :year)
|
333
333
|
expect(chunks.size).to eq(3)
|
334
334
|
expect(chunks[0].first.iso).to eq('2010-01-01')
|
@@ -339,7 +339,20 @@ describe Period do
|
|
339
339
|
expect(chunks[2].last.iso).to eq('2012-12-31')
|
340
340
|
end
|
341
341
|
|
342
|
-
it
|
342
|
+
it 'should be able to chunk into halves' do
|
343
|
+
chunks = Period.new('2009-12-15', '2013-01-10').chunks(size: :half)
|
344
|
+
expect(chunks.size).to eq(6)
|
345
|
+
expect(chunks[0].first.iso).to eq('2010-01-01')
|
346
|
+
expect(chunks[0].last.iso).to eq('2010-06-30')
|
347
|
+
expect(chunks[1].first.iso).to eq('2010-07-01')
|
348
|
+
expect(chunks[1].last.iso).to eq('2010-12-31')
|
349
|
+
expect(chunks[2].first.iso).to eq('2011-01-01')
|
350
|
+
expect(chunks[2].last.iso).to eq('2011-06-30')
|
351
|
+
expect(chunks.last.first.iso).to eq('2012-07-01')
|
352
|
+
expect(chunks.last.last.iso).to eq('2012-12-31')
|
353
|
+
end
|
354
|
+
|
355
|
+
it 'should be able to chunk into quarters' do
|
343
356
|
chunks = Period.new('2009-12-15', '2013-01-10').chunks(size: :quarter)
|
344
357
|
expect(chunks.size).to eq(12)
|
345
358
|
expect(chunks[0].first.iso).to eq('2010-01-01')
|
@@ -352,7 +365,7 @@ describe Period do
|
|
352
365
|
expect(chunks.last.last.iso).to eq('2012-12-31')
|
353
366
|
end
|
354
367
|
|
355
|
-
it
|
368
|
+
it 'should be able to chunk into bimonths' do
|
356
369
|
chunks = Period.new('2009-12-15', '2013-01-10').chunks(size: :bimonth)
|
357
370
|
expect(chunks.size).to eq(18)
|
358
371
|
expect(chunks[0].first.iso).to eq('2010-01-01')
|
@@ -365,7 +378,7 @@ describe Period do
|
|
365
378
|
expect(chunks.last.last.iso).to eq('2012-12-31')
|
366
379
|
end
|
367
380
|
|
368
|
-
it
|
381
|
+
it 'should be able to chunk into months' do
|
369
382
|
chunks = Period.new('2009-12-15', '2013-01-10').chunks(size: :month)
|
370
383
|
expect(chunks.size).to eq(36)
|
371
384
|
expect(chunks[0].first.iso).to eq('2010-01-01')
|
@@ -378,7 +391,7 @@ describe Period do
|
|
378
391
|
expect(chunks.last.last.iso).to eq('2012-12-31')
|
379
392
|
end
|
380
393
|
|
381
|
-
it
|
394
|
+
it 'should be able to chunk into semimonths' do
|
382
395
|
chunks = Period.new('2009-12-25', '2013-01-10').chunks(size: :semimonth)
|
383
396
|
expect(chunks.size).to eq(72)
|
384
397
|
expect(chunks[0].first.iso).to eq('2010-01-01')
|
@@ -391,7 +404,7 @@ describe Period do
|
|
391
404
|
expect(chunks.last.last.iso).to eq('2012-12-31')
|
392
405
|
end
|
393
406
|
|
394
|
-
it
|
407
|
+
it 'should be able to chunk into biweeks' do
|
395
408
|
chunks = Period.new('2009-12-29', '2013-01-10').chunks(size: :biweek)
|
396
409
|
expect(chunks.size).to be >=(26*3)
|
397
410
|
expect(chunks[0].first.iso).to eq('2010-01-04')
|
@@ -404,9 +417,9 @@ describe Period do
|
|
404
417
|
expect(chunks.last.last.iso).to eq('2012-12-30')
|
405
418
|
end
|
406
419
|
|
407
|
-
it
|
420
|
+
it 'should be able to chunk into weeks' do
|
408
421
|
chunks = Period.new('2010-01-01', '2012-12-31').chunks(size: :week)
|
409
|
-
expect(chunks.size).to be >=(52*3)
|
422
|
+
expect(chunks.size).to be >= (52 * 3)
|
410
423
|
expect(chunks[0].first.iso).to eq('2010-01-04')
|
411
424
|
expect(chunks[0].last.iso).to eq('2010-01-10')
|
412
425
|
expect(chunks[1].first.iso).to eq('2010-01-11')
|
@@ -417,7 +430,7 @@ describe Period do
|
|
417
430
|
expect(chunks.last.last.iso).to eq('2012-12-30')
|
418
431
|
end
|
419
432
|
|
420
|
-
it
|
433
|
+
it 'should be able to chunk into days' do
|
421
434
|
chunks = Period.new('2012-12-28', '2012-12-31').chunks(size: :day)
|
422
435
|
expect(chunks.size).to eq(4)
|
423
436
|
expect(chunks[0].first.iso).to eq('2012-12-28')
|
@@ -430,68 +443,72 @@ describe Period do
|
|
430
443
|
expect(chunks.last.last.iso).to eq('2012-12-31')
|
431
444
|
end
|
432
445
|
|
433
|
-
it
|
446
|
+
it 'should not include a partial final chunk by default' do
|
434
447
|
chunks = Period.new('2012-01-01', '2012-03-30').chunks(size: :month)
|
435
448
|
expect(chunks.size).to eq(2)
|
436
449
|
end
|
437
450
|
|
438
|
-
it
|
439
|
-
chunks = Period.new('2012-01-01', '2012-03-30')
|
440
|
-
|
451
|
+
it 'should include a partial final chunk if partial_last' do
|
452
|
+
chunks = Period.new('2012-01-01', '2012-03-30')
|
453
|
+
.chunks(size: :month, partial_last: true)
|
441
454
|
expect(chunks.size).to eq(3)
|
442
455
|
expect(chunks.last.first).to eq(Date.parse('2012-03-01'))
|
443
456
|
expect(chunks.last.last).to eq(Date.parse('2012-03-30'))
|
444
457
|
end
|
445
458
|
|
446
|
-
it
|
447
|
-
chunks = Period.new('2012-01-01', '2012-03-30')
|
448
|
-
|
459
|
+
it 'should include a final chunk beyond end_date if round_up' do
|
460
|
+
chunks = Period.new('2012-01-01', '2012-03-30')
|
461
|
+
.chunks(size: :month, round_up_last: true)
|
449
462
|
expect(chunks.size).to eq(3)
|
450
463
|
expect(chunks.last.first).to eq(Date.parse('2012-03-01'))
|
451
464
|
expect(chunks.last.last).to eq(Date.parse('2012-03-31'))
|
452
465
|
end
|
453
466
|
|
454
|
-
it
|
467
|
+
it 'should not include a partial initial chunk by default' do
|
455
468
|
chunks = Period.new('2012-01-13', '2012-03-31').chunks(size: :month)
|
456
469
|
expect(chunks.size).to eq(2)
|
457
470
|
expect(chunks[0].first).to eq(Date.parse('2012-02-01'))
|
458
471
|
expect(chunks[0].last).to eq(Date.parse('2012-02-29'))
|
459
472
|
end
|
460
473
|
|
461
|
-
it
|
462
|
-
chunks = Period.new('2012-01-13', '2012-03-31')
|
463
|
-
|
474
|
+
it 'should include a partial initial chunk by if partial_first' do
|
475
|
+
chunks = Period.new('2012-01-13', '2012-03-31')
|
476
|
+
.chunks(size: :month, partial_first: true)
|
464
477
|
expect(chunks.size).to eq(3)
|
465
478
|
expect(chunks[0].first).to eq(Date.parse('2012-01-13'))
|
466
479
|
expect(chunks[0].last).to eq(Date.parse('2012-01-31'))
|
467
480
|
end
|
468
481
|
|
469
|
-
it
|
470
|
-
chunks = Period.new('2012-01-01', '2012-03-30')
|
471
|
-
|
482
|
+
it 'should include a final chunk beyond end_date if round_up' do
|
483
|
+
chunks = Period.new('2012-01-01', '2012-03-30')
|
484
|
+
.chunks(size: :month, round_up_last: true)
|
472
485
|
expect(chunks.size).to eq(3)
|
473
486
|
expect(chunks.last.first).to eq(Date.parse('2012-03-01'))
|
474
487
|
expect(chunks.last.last).to eq(Date.parse('2012-03-31'))
|
475
488
|
end
|
476
489
|
|
477
|
-
it
|
490
|
+
it 'should be able to its chunk_sym' do
|
478
491
|
expect(Period.new('2013-01-01', '2013-12-31').chunk_sym).to eq(:year)
|
479
492
|
expect(Period.new('2012-01-01', '2013-12-31').chunk_sym).to_not eq(:year)
|
480
493
|
|
481
494
|
expect(Period.new('2013-04-01', '2013-06-30').chunk_sym).to eq(:quarter)
|
482
|
-
expect(Period.new('2013-04-01', '2013-09-30').chunk_sym)
|
495
|
+
expect(Period.new('2013-04-01', '2013-09-30').chunk_sym)
|
496
|
+
.to_not eq(:quarter)
|
483
497
|
|
484
498
|
expect(Period.new('2013-03-01', '2013-04-30').chunk_sym).to eq(:bimonth)
|
485
|
-
expect(Period.new('2013-03-01', '2013-06-30').chunk_sym)
|
499
|
+
expect(Period.new('2013-03-01', '2013-06-30').chunk_sym)
|
500
|
+
.to_not eq(:bimonth)
|
486
501
|
|
487
502
|
expect(Period.new('2013-04-01', '2013-04-30').chunk_sym).to eq(:month)
|
488
503
|
expect(Period.new('2013-04-01', '2013-05-30').chunk_sym).to_not eq(:month)
|
489
504
|
|
490
505
|
expect(Period.new('2013-05-16', '2013-05-31').chunk_sym).to eq(:semimonth)
|
491
|
-
expect(Period.new('2013-05-16', '2013-06-30').chunk_sym)
|
506
|
+
expect(Period.new('2013-05-16', '2013-06-30').chunk_sym)
|
507
|
+
.to_not eq(:semimonth)
|
492
508
|
|
493
509
|
expect(Period.new('2013-11-04', '2013-11-17').chunk_sym).to eq(:biweek)
|
494
|
-
expect(Period.new('2013-11-04', '2013-11-24').chunk_sym)
|
510
|
+
expect(Period.new('2013-11-04', '2013-11-24').chunk_sym)
|
511
|
+
.to_not eq(:biweek)
|
495
512
|
|
496
513
|
expect(Period.new('2013-11-11', '2013-11-17').chunk_sym).to eq(:week)
|
497
514
|
expect(Period.new('2013-11-11', '2013-11-24').chunk_sym).to_not eq(:week)
|
@@ -502,35 +519,35 @@ describe Period do
|
|
502
519
|
expect(Period.new('2013-11-02', '2013-12-16').chunk_sym).to eq(:irregular)
|
503
520
|
end
|
504
521
|
|
505
|
-
it
|
522
|
+
it 'should know if it\'s a subset of another period' do
|
506
523
|
year = Period.parse('this_year')
|
507
524
|
month = Period.parse('this_month')
|
508
525
|
expect(month.subset_of?(year)).to be true
|
509
526
|
expect(year.subset_of?(year)).to be true
|
510
527
|
end
|
511
528
|
|
512
|
-
it
|
529
|
+
it 'should know if it\'s a proper subset of another period' do
|
513
530
|
year = Period.parse('this_year')
|
514
531
|
month = Period.parse('this_month')
|
515
532
|
expect(month.proper_subset_of?(year)).to be true
|
516
533
|
expect(year.proper_subset_of?(year)).to be false
|
517
534
|
end
|
518
535
|
|
519
|
-
it
|
536
|
+
it 'should know if it\'s a superset of another period' do
|
520
537
|
year = Period.parse('this_year')
|
521
538
|
month = Period.parse('this_month')
|
522
539
|
expect(year.superset_of?(month)).to be true
|
523
540
|
expect(year.superset_of?(year)).to be true
|
524
541
|
end
|
525
542
|
|
526
|
-
it
|
543
|
+
it 'should know if it\'s a proper superset of another period' do
|
527
544
|
year = Period.parse('this_year')
|
528
545
|
month = Period.parse('this_month')
|
529
546
|
expect(year.proper_superset_of?(month)).to be true
|
530
547
|
expect(year.proper_superset_of?(year)).to be false
|
531
548
|
end
|
532
549
|
|
533
|
-
it
|
550
|
+
it 'should know if it overlaps another period' do
|
534
551
|
period1 = Period.parse('2013')
|
535
552
|
period2 = Period.parse('2012-10', '2013-03')
|
536
553
|
period3 = Period.parse('2014')
|
@@ -539,24 +556,24 @@ describe Period do
|
|
539
556
|
expect(period1.overlaps?(period3)).to be false
|
540
557
|
end
|
541
558
|
|
542
|
-
it
|
543
|
-
months = (1..12).to_a.map{ |k| Period.parse("2013-#{k}") }
|
544
|
-
year = Period.parse(
|
559
|
+
it 'should know whether an array of periods have overlaps within it' do
|
560
|
+
months = (1..12).to_a.map { |k| Period.parse("2013-#{k}") }
|
561
|
+
year = Period.parse('2013')
|
545
562
|
expect(year.has_overlaps_within?(months)).to be false
|
546
|
-
months << Period.parse(
|
563
|
+
months << Period.parse('2013-09-15', '2013-10-02')
|
547
564
|
expect(year.has_overlaps_within?(months)).to be true
|
548
565
|
end
|
549
566
|
|
550
|
-
it
|
551
|
-
months = (1..12).to_a.map{ |k| Period.parse("2013-#{k}") }
|
552
|
-
year = Period.parse(
|
567
|
+
it 'should know whether an array of periods span it' do
|
568
|
+
months = (1..12).to_a.map { |k| Period.parse("2013-#{k}") }
|
569
|
+
year = Period.parse('2013')
|
553
570
|
expect(year.spanned_by?(months)).to be true
|
554
571
|
|
555
|
-
months = (2..12).to_a.map{ |k| Period.parse("2013-#{k}") }
|
572
|
+
months = (2..12).to_a.map { |k| Period.parse("2013-#{k}") }
|
556
573
|
expect(year.spanned_by?(months)).to be false
|
557
574
|
end
|
558
575
|
|
559
|
-
it
|
576
|
+
it 'should know its intersection with other period' do
|
560
577
|
year = Period.parse('this_year')
|
561
578
|
month = Period.parse('this_month')
|
562
579
|
expect(year & month).to eq(month)
|
@@ -565,7 +582,7 @@ describe Period do
|
|
565
582
|
expect((month & year).class).to eq(Period)
|
566
583
|
end
|
567
584
|
|
568
|
-
it
|
585
|
+
it 'should alias narrow_to to intersection' do
|
569
586
|
period1 = Period.parse('2014')
|
570
587
|
period2 = Period.new('2014-06-01', '2015-02-28')
|
571
588
|
period3 = period1.narrow_to(period2)
|
@@ -573,13 +590,13 @@ describe Period do
|
|
573
590
|
expect(period3.last).to eq(period1.last)
|
574
591
|
end
|
575
592
|
|
576
|
-
it
|
593
|
+
it 'should return nil if no intersection' do
|
577
594
|
year = Period.parse('2014')
|
578
595
|
month = Period.parse('2013-05')
|
579
596
|
expect(year & month).to be_nil
|
580
597
|
end
|
581
598
|
|
582
|
-
it
|
599
|
+
it 'should know its union with other period' do
|
583
600
|
last_month = Period.parse('last_month')
|
584
601
|
month = Period.parse('this_month')
|
585
602
|
expect((last_month + month).first).to eq(last_month.first)
|
@@ -588,7 +605,7 @@ describe Period do
|
|
588
605
|
expect((last_month + month).class).to eq(Period)
|
589
606
|
end
|
590
607
|
|
591
|
-
it
|
608
|
+
it 'should know its differences with other period' do
|
592
609
|
year = Period.parse('this_year')
|
593
610
|
month = Period.parse('this_month')
|
594
611
|
# Note: the difference operator returns an Array of Periods resulting
|
@@ -607,17 +624,17 @@ describe Period do
|
|
607
624
|
expect(last_year - month).to eq([last_year])
|
608
625
|
end
|
609
626
|
|
610
|
-
it
|
627
|
+
it 'should be able to find gaps from an array of periods' do
|
611
628
|
pp = Period.parse('2014-2Q')
|
612
629
|
periods = [
|
613
|
-
|
614
|
-
|
615
|
-
|
616
|
-
|
617
|
-
|
618
|
-
|
619
|
-
|
620
|
-
|
630
|
+
Period.parse('2013-11', '2013-12-20'),
|
631
|
+
Period.parse('2014-01', '2014-04-20'),
|
632
|
+
# Gap 2014-04-21 to 2014-04-30
|
633
|
+
Period.parse('2014-05', '2014-05-11'),
|
634
|
+
# Gap 2014-05-12 to 2014-05-24
|
635
|
+
Period.parse('2014-05-25', '2014-07-11'),
|
636
|
+
Period.parse('2014-09')
|
637
|
+
]
|
621
638
|
gaps = pp.gaps(periods)
|
622
639
|
expect(gaps.size).to eq(2)
|
623
640
|
expect(gaps.first.first).to eq(Date.parse('2014-04-21'))
|