-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdate_range_spec.rb
97 lines (80 loc) · 3.17 KB
/
date_range_spec.rb
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
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
describe "DateRange" do
let(:firstOfYear) { DateTime.new(2001,1,1) }
let(:endOfYear) { DateTime.new(2001,12,31) }
let(:firstOfMarch) { DateTime.new(2001,3,1) }
let(:endOfMarch) { DateTime.new(2001,3,30) }
let(:firstOfJuly) { DateTime.new(2001,7,1) }
let(:endOfJuly) { DateTime.new(2001,7,31) }
let(:range) { SimpleRange.new(DateTime.new(2001,1,1), DateTime.new(2001,7,5))}
let(:year) { SimpleRange.new(firstOfYear, endOfYear)}
let(:march) { SimpleRange.new(firstOfMarch, endOfMarch)}
let(:july) { SimpleRange.new(firstOfJuly, endOfJuly)}
it "should be equal if start and end dates are the same" do
year.same_range_as(year).should == true
end
it "should have the quarters in a range" do
year.quarters.first.number.should == 1
year.quarters.to_a.length.should == 4
end
it "should count the days in the range" do
range.days.to_a.length.should == 31 + 28 + 31 + 30 + 31 + 30 + 5
end
it "should be able to get the number of months in a range" do
range.months.to_a.length.should == 7
end
describe DateRange, "intersecting dates" do
it "should tell if one date range encompasses another" do
year.encompasses(march).should == true
march.encompasses(year).should == false
march.encompasses(july).should == false
end
it "should be able to tell if one date range intersects another" do
year.intersects(march).should == true
year.intersects(year).should == true
march.intersects(year).should == true
march.intersects(july).should == false
range.intersects(july).should == true
july.intersects(range).should == true
end
it "can return the intersection" do
(year & march).should == march
(march & year).should == march
(march & march).should == march
left = (range & july)
left.start_date.should == firstOfJuly
left.end_date.should == range.end_date
left = (july & range)
left.start_date.should == firstOfJuly
left.end_date.should == range.end_date
(march & july).should == []
end
it "can return the union" do
(year | march).should == year
(march | year).should == year
(march | march).should == march
union = (range | july)
union.start_date.should == range.start_date
union.end_date.should == endOfJuly
union = (july | range)
union.start_date.should == range.start_date
union.end_date.should == endOfJuly
(march | july).should == []
end
it "can figure out the number of days in" do
year.number_of_days_in(march).should == 30
march.number_of_days_in(july).should == 0
end
it "returns null collections when start or end date is not set" do
r = SimpleRange.new(nil, DateTime.new)
r.months.length.should == 0
r = SimpleRange.new(DateTime.new, nil)
r.months.length.should == 0
end
it "can return the weeks" do
range = SimpleRange.new(Date.new(2011, 10, 1), DateTime.new(2011, 10, 29))
range.weeks.to_a.length.should == 5
range.weeks.first.should == Week.new(Date.new(2011, 10, 1))
end
end
end