fat_core 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/fat_core/period.rb +9 -0
- data/lib/fat_core/version.rb +1 -1
- data/spec/lib/date_spec.rb +158 -167
- data/spec/lib/numeric_spec.rb +9 -9
- data/spec/lib/period_spec.rb +139 -137
- data/spec/lib/range_spec.rb +53 -53
- data/spec/lib/string_spec.rb +27 -27
- data/spec/spec_helper.rb +2 -1
- metadata +2 -2
data/spec/lib/numeric_spec.rb
CHANGED
@@ -2,27 +2,27 @@ require File.dirname(File.absolute_path(__FILE__)) + '/../spec_helper.rb'
|
|
2
2
|
|
3
3
|
describe Numeric do
|
4
4
|
it "should properly round up" do
|
5
|
-
236.5555.commas(2).
|
6
|
-
-236.5555.commas(2).
|
5
|
+
expect(236.5555.commas(2)).to eq '236.56'
|
6
|
+
expect(-236.5555.commas(2)).to eq '-236.56'
|
7
7
|
end
|
8
8
|
|
9
9
|
it "should properly round down" do
|
10
|
-
236.5512.commas(2).
|
11
|
-
-236.5512.commas(2).
|
10
|
+
expect(236.5512.commas(2)).to eq '236.55'
|
11
|
+
expect(-236.5512.commas(2)).to eq '-236.55'
|
12
12
|
end
|
13
13
|
|
14
14
|
it "should place commas properly" do
|
15
|
-
123456789.0123.commas(2).
|
16
|
-
-123456789.0123.commas(2).
|
15
|
+
expect(123456789.0123.commas(2)).to eq '123,456,789.01'
|
16
|
+
expect(-123456789.0123.commas(2)).to eq '-123,456,789.01'
|
17
17
|
end
|
18
18
|
|
19
19
|
it "should place commas properly with no fraction" do
|
20
|
-
123456789.commas.
|
21
|
-
-123456789.commas.
|
20
|
+
expect(123456789.commas).to eq '123,456,789'
|
21
|
+
expect(-123456789.commas).to eq '-123,456,789'
|
22
22
|
end
|
23
23
|
|
24
24
|
it "should not place commas in a number with exponent" do
|
25
|
-
123456.789e100.commas.
|
25
|
+
expect(123456.789e100.commas).to eq '1.23456789e+105'
|
26
26
|
end
|
27
27
|
|
28
28
|
it "should be able to convert itself into an H:M:S string" do
|
data/spec/lib/period_spec.rb
CHANGED
@@ -4,19 +4,19 @@ 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
|
-
Date.
|
8
|
-
|
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
11
|
describe "initialization" do
|
12
12
|
|
13
13
|
it "should be initializable with date strings" do
|
14
|
-
Period.new('2013-01-01', '2013-12-13').
|
14
|
+
expect(Period.new('2013-01-01', '2013-12-13')).to be_instance_of Period
|
15
15
|
end
|
16
16
|
|
17
17
|
it "should be initializable with Dates" do
|
18
|
-
Period.new(Date.parse('2013-01-01'), Date.parse('2013-12-13')).
|
19
|
-
|
18
|
+
expect(Period.new(Date.parse('2013-01-01'), Date.parse('2013-12-13'))).
|
19
|
+
to be_instance_of Period
|
20
20
|
end
|
21
21
|
|
22
22
|
it "should raise a ArgumentError if last > first" do
|
@@ -43,27 +43,33 @@ describe Period do
|
|
43
43
|
|
44
44
|
describe "class methods" do
|
45
45
|
|
46
|
+
it "should know how to parse a pair of date specs" do
|
47
|
+
expect(Period.parse_spec.first).to eq Date.current
|
48
|
+
expect(Period.parse_spec('2014-3Q').first).to eq Date.parse('2014-07-01')
|
49
|
+
expect(Period.parse_spec('2014-3Q').last).to eq Date.parse('2014-09-30')
|
50
|
+
end
|
51
|
+
|
46
52
|
it "should know the days in a chunk sym" do
|
47
|
-
Period.chunk_sym_to_days(:year).
|
48
|
-
Period.chunk_sym_to_days(:quarter).
|
49
|
-
Period.chunk_sym_to_days(:bimonth).
|
50
|
-
Period.chunk_sym_to_days(:month).
|
51
|
-
Period.chunk_sym_to_days(:semimonth).
|
52
|
-
Period.chunk_sym_to_days(:biweek).
|
53
|
-
Period.chunk_sym_to_days(:week).
|
54
|
-
Period.chunk_sym_to_days(:day).
|
55
|
-
Period.chunk_sym_to_days(:irregular).
|
53
|
+
expect(Period.chunk_sym_to_days(:year)).to eq(365)
|
54
|
+
expect(Period.chunk_sym_to_days(:quarter)).to eq(90)
|
55
|
+
expect(Period.chunk_sym_to_days(:bimonth)).to eq(60)
|
56
|
+
expect(Period.chunk_sym_to_days(:month)).to eq(30)
|
57
|
+
expect(Period.chunk_sym_to_days(:semimonth)).to eq(15)
|
58
|
+
expect(Period.chunk_sym_to_days(:biweek)).to eq(14)
|
59
|
+
expect(Period.chunk_sym_to_days(:week)).to eq(7)
|
60
|
+
expect(Period.chunk_sym_to_days(:day)).to eq(1)
|
61
|
+
expect(Period.chunk_sym_to_days(:irregular)).to eq(30)
|
56
62
|
expect {
|
57
63
|
Period.chunk_sym_to_days(:eon)
|
58
64
|
}.to raise_error ArgumentError
|
59
65
|
end
|
60
66
|
|
61
67
|
it "should know the chunk sym for given days but only :year, :quarter, :month" do
|
62
|
-
(356..376).each { |d| Period.days_to_chunk_sym(d).
|
63
|
-
(86..96).each { |d| Period.days_to_chunk_sym(d).
|
64
|
-
(28..31).each { |d| Period.days_to_chunk_sym(d).
|
65
|
-
Period.days_to_chunk_sym(7).
|
66
|
-
Period.days_to_chunk_sym(1).
|
68
|
+
(356..376).each { |d| expect(Period.days_to_chunk_sym(d)).to eq(:year) }
|
69
|
+
(86..96).each { |d| expect(Period.days_to_chunk_sym(d)).to eq(:quarter) }
|
70
|
+
(28..31).each { |d| expect(Period.days_to_chunk_sym(d)).to eq(:month) }
|
71
|
+
expect(Period.days_to_chunk_sym(7)).to eq(:week)
|
72
|
+
expect(Period.days_to_chunk_sym(1)).to eq(:day)
|
67
73
|
end
|
68
74
|
|
69
75
|
it "should know what to call a chunk based on its size" do
|
@@ -85,210 +91,206 @@ describe Period do
|
|
85
91
|
pp1 = Period.new('2013-01-01', '2013-12-31')
|
86
92
|
pp2 = Period.new('2013-01-01', '2013-12-31')
|
87
93
|
pp3 = Period.new('2013-01-01', '2013-12-30')
|
88
|
-
(pp1 == pp2).
|
89
|
-
(pp1 == pp3).
|
94
|
+
expect((pp1 == pp2)).to be true
|
95
|
+
expect((pp1 == pp3)).to_not be true
|
90
96
|
end
|
91
97
|
|
92
98
|
it "should be able to convert into a Range" do
|
93
99
|
pp = Period.new('2013-01-01', '2013-12-31')
|
94
100
|
rr = Period.new('2013-01-01', '2013-12-31').to_range
|
95
|
-
rr.
|
96
|
-
rr.first.
|
97
|
-
rr.first.
|
101
|
+
expect(rr).to be_instance_of Range
|
102
|
+
expect(rr.first).to eq(pp.first)
|
103
|
+
expect(rr.first).to eq(pp.first)
|
98
104
|
end
|
99
105
|
|
100
106
|
it "should be able to tell if it contains a date" do
|
101
107
|
pp = Period.new('2013-01-01', '2013-12-31')
|
102
|
-
pp.contains?(Date.parse('2013-01-01')).
|
103
|
-
pp.contains?(Date.parse('2013-07-04')).
|
104
|
-
pp.contains?(Date.parse('2013-12-31')).
|
105
|
-
pp.contains?(Date.parse('2012-07-04')).
|
108
|
+
expect(pp.contains?(Date.parse('2013-01-01'))).to be true
|
109
|
+
expect(pp.contains?(Date.parse('2013-07-04'))).to be true
|
110
|
+
expect(pp.contains?(Date.parse('2013-12-31'))).to be true
|
111
|
+
expect(pp.contains?(Date.parse('2012-07-04'))).to be false
|
106
112
|
end
|
107
113
|
|
108
114
|
it "should be able to make a concise period string" do
|
109
|
-
Period.new('2013-01-01', '2013-12-31').to_s.
|
110
|
-
|
111
|
-
Period.new('2013-
|
112
|
-
|
113
|
-
Period.new('2013-03-01', '2013-03-31').to_s.
|
114
|
-
should eq('2013-03')
|
115
|
-
Period.new('2013-03-11', '2013-10-31').to_s.
|
116
|
-
should eq('2013-03-11 to 2013-10-31')
|
115
|
+
expect(Period.new('2013-01-01', '2013-12-31').to_s).to eq('2013')
|
116
|
+
expect(Period.new('2013-04-01', '2013-06-30').to_s).to eq('2013-2Q')
|
117
|
+
expect(Period.new('2013-03-01', '2013-03-31').to_s).to eq('2013-03')
|
118
|
+
expect(Period.new('2013-03-11', '2013-10-31').to_s).to eq('2013-03-11 to 2013-10-31')
|
117
119
|
end
|
118
120
|
|
119
121
|
# Note in the following that first period must begin within self.
|
120
122
|
it "should be able to chunk into years" do
|
121
123
|
chunks = Period.new('2009-12-15', '2013-01-10').chunks(size: :year)
|
122
|
-
chunks.size.
|
123
|
-
chunks[0].first.iso.
|
124
|
-
chunks[0].last.iso.
|
125
|
-
chunks[1].first.iso.
|
126
|
-
chunks[1].last.iso.
|
127
|
-
chunks[2].first.iso.
|
128
|
-
chunks[2].last.iso.
|
124
|
+
expect(chunks.size).to eq(3)
|
125
|
+
expect(chunks[0].first.iso).to eq('2010-01-01')
|
126
|
+
expect(chunks[0].last.iso).to eq('2010-12-31')
|
127
|
+
expect(chunks[1].first.iso).to eq('2011-01-01')
|
128
|
+
expect(chunks[1].last.iso).to eq('2011-12-31')
|
129
|
+
expect(chunks[2].first.iso).to eq('2012-01-01')
|
130
|
+
expect(chunks[2].last.iso).to eq('2012-12-31')
|
129
131
|
end
|
130
132
|
|
131
133
|
it "should be able to chunk into quarters" do
|
132
134
|
chunks = Period.new('2009-12-15', '2013-01-10').chunks(size: :quarter)
|
133
|
-
chunks.size.
|
134
|
-
chunks[0].first.iso.
|
135
|
-
chunks[0].last.iso.
|
136
|
-
chunks[1].first.iso.
|
137
|
-
chunks[1].last.iso.
|
138
|
-
chunks[2].first.iso.
|
139
|
-
chunks[2].last.iso.
|
140
|
-
chunks.last.first.iso.
|
141
|
-
chunks.last.last.iso.
|
135
|
+
expect(chunks.size).to eq(12)
|
136
|
+
expect(chunks[0].first.iso).to eq('2010-01-01')
|
137
|
+
expect(chunks[0].last.iso).to eq('2010-03-31')
|
138
|
+
expect(chunks[1].first.iso).to eq('2010-04-01')
|
139
|
+
expect(chunks[1].last.iso).to eq('2010-06-30')
|
140
|
+
expect(chunks[2].first.iso).to eq('2010-07-01')
|
141
|
+
expect(chunks[2].last.iso).to eq('2010-09-30')
|
142
|
+
expect(chunks.last.first.iso).to eq('2012-10-01')
|
143
|
+
expect(chunks.last.last.iso).to eq('2012-12-31')
|
142
144
|
end
|
143
145
|
|
144
146
|
it "should be able to chunk into bimonths" do
|
145
147
|
chunks = Period.new('2009-12-15', '2013-01-10').chunks(size: :bimonth)
|
146
|
-
chunks.size.
|
147
|
-
chunks[0].first.iso.
|
148
|
-
chunks[0].last.iso.
|
149
|
-
chunks[1].first.iso.
|
150
|
-
chunks[1].last.iso.
|
151
|
-
chunks[2].first.iso.
|
152
|
-
chunks[2].last.iso.
|
153
|
-
chunks.last.first.iso.
|
154
|
-
chunks.last.last.iso.
|
148
|
+
expect(chunks.size).to eq(18)
|
149
|
+
expect(chunks[0].first.iso).to eq('2010-01-01')
|
150
|
+
expect(chunks[0].last.iso).to eq('2010-02-28')
|
151
|
+
expect(chunks[1].first.iso).to eq('2010-03-01')
|
152
|
+
expect(chunks[1].last.iso).to eq('2010-04-30')
|
153
|
+
expect(chunks[2].first.iso).to eq('2010-05-01')
|
154
|
+
expect(chunks[2].last.iso).to eq('2010-06-30')
|
155
|
+
expect(chunks.last.first.iso).to eq('2012-11-01')
|
156
|
+
expect(chunks.last.last.iso).to eq('2012-12-31')
|
155
157
|
end
|
156
158
|
|
157
159
|
it "should be able to chunk into months" do
|
158
160
|
chunks = Period.new('2009-12-15', '2013-01-10').chunks(size: :month)
|
159
|
-
chunks.size.
|
160
|
-
chunks[0].first.iso.
|
161
|
-
chunks[0].last.iso.
|
162
|
-
chunks[1].first.iso.
|
163
|
-
chunks[1].last.iso.
|
164
|
-
chunks[2].first.iso.
|
165
|
-
chunks[2].last.iso.
|
166
|
-
chunks.last.first.iso.
|
167
|
-
chunks.last.last.iso.
|
161
|
+
expect(chunks.size).to eq(36)
|
162
|
+
expect(chunks[0].first.iso).to eq('2010-01-01')
|
163
|
+
expect(chunks[0].last.iso).to eq('2010-01-31')
|
164
|
+
expect(chunks[1].first.iso).to eq('2010-02-01')
|
165
|
+
expect(chunks[1].last.iso).to eq('2010-02-28')
|
166
|
+
expect(chunks[2].first.iso).to eq('2010-03-01')
|
167
|
+
expect(chunks[2].last.iso).to eq('2010-03-31')
|
168
|
+
expect(chunks.last.first.iso).to eq('2012-12-01')
|
169
|
+
expect(chunks.last.last.iso).to eq('2012-12-31')
|
168
170
|
end
|
169
171
|
|
170
172
|
it "should be able to chunk into semimonths" do
|
171
173
|
chunks = Period.new('2009-12-25', '2013-01-10').chunks(size: :semimonth)
|
172
|
-
chunks.size.
|
173
|
-
chunks[0].first.iso.
|
174
|
-
chunks[0].last.iso.
|
175
|
-
chunks[1].first.iso.
|
176
|
-
chunks[1].last.iso.
|
177
|
-
chunks[2].first.iso.
|
178
|
-
chunks[2].last.iso.
|
179
|
-
chunks.last.first.iso.
|
180
|
-
chunks.last.last.iso.
|
174
|
+
expect(chunks.size).to eq(72)
|
175
|
+
expect(chunks[0].first.iso).to eq('2010-01-01')
|
176
|
+
expect(chunks[0].last.iso).to eq('2010-01-15')
|
177
|
+
expect(chunks[1].first.iso).to eq('2010-01-16')
|
178
|
+
expect(chunks[1].last.iso).to eq('2010-01-31')
|
179
|
+
expect(chunks[2].first.iso).to eq('2010-02-01')
|
180
|
+
expect(chunks[2].last.iso).to eq('2010-02-15')
|
181
|
+
expect(chunks.last.first.iso).to eq('2012-12-16')
|
182
|
+
expect(chunks.last.last.iso).to eq('2012-12-31')
|
181
183
|
end
|
182
184
|
|
183
185
|
it "should be able to chunk into biweeks" do
|
184
186
|
chunks = Period.new('2009-12-29', '2013-01-10').chunks(size: :biweek)
|
185
187
|
expect(chunks.size).to be >=(26*3)
|
186
|
-
chunks[0].first.iso.
|
187
|
-
chunks[0].last.iso.
|
188
|
-
chunks[1].first.iso.
|
189
|
-
chunks[1].last.iso.
|
190
|
-
chunks[2].first.iso.
|
191
|
-
chunks[2].last.iso.
|
192
|
-
chunks.last.first.iso.
|
193
|
-
chunks.last.last.iso.
|
188
|
+
expect(chunks[0].first.iso).to eq('2010-01-04')
|
189
|
+
expect(chunks[0].last.iso).to eq('2010-01-17')
|
190
|
+
expect(chunks[1].first.iso).to eq('2010-01-18')
|
191
|
+
expect(chunks[1].last.iso).to eq('2010-01-31')
|
192
|
+
expect(chunks[2].first.iso).to eq('2010-02-01')
|
193
|
+
expect(chunks[2].last.iso).to eq('2010-02-14')
|
194
|
+
expect(chunks.last.first.iso).to eq('2012-12-17')
|
195
|
+
expect(chunks.last.last.iso).to eq('2012-12-30')
|
194
196
|
end
|
195
197
|
|
196
198
|
it "should be able to chunk into weeks" do
|
197
199
|
chunks = Period.new('2010-01-01', '2012-12-31').chunks(size: :week)
|
198
200
|
expect(chunks.size).to be >=(52*3)
|
199
|
-
chunks[0].first.iso.
|
200
|
-
chunks[0].last.iso.
|
201
|
-
chunks[1].first.iso.
|
202
|
-
chunks[1].last.iso.
|
203
|
-
chunks[2].first.iso.
|
204
|
-
chunks[2].last.iso.
|
205
|
-
chunks.last.first.iso.
|
206
|
-
chunks.last.last.iso.
|
201
|
+
expect(chunks[0].first.iso).to eq('2010-01-04')
|
202
|
+
expect(chunks[0].last.iso).to eq('2010-01-10')
|
203
|
+
expect(chunks[1].first.iso).to eq('2010-01-11')
|
204
|
+
expect(chunks[1].last.iso).to eq('2010-01-17')
|
205
|
+
expect(chunks[2].first.iso).to eq('2010-01-18')
|
206
|
+
expect(chunks[2].last.iso).to eq('2010-01-24')
|
207
|
+
expect(chunks.last.first.iso).to eq('2012-12-24')
|
208
|
+
expect(chunks.last.last.iso).to eq('2012-12-30')
|
207
209
|
end
|
208
210
|
|
209
211
|
it "should be able to chunk into days" do
|
210
212
|
chunks = Period.new('2012-12-28', '2012-12-31').chunks(size: :day)
|
211
|
-
chunks.size.
|
212
|
-
chunks[0].first.iso.
|
213
|
-
chunks[0].last.iso.
|
214
|
-
chunks[1].first.iso.
|
215
|
-
chunks[1].last.iso.
|
216
|
-
chunks[2].first.iso.
|
217
|
-
chunks[2].last.iso.
|
218
|
-
chunks.last.first.iso.
|
219
|
-
chunks.last.last.iso.
|
213
|
+
expect(chunks.size).to eq(4)
|
214
|
+
expect(chunks[0].first.iso).to eq('2012-12-28')
|
215
|
+
expect(chunks[0].last.iso).to eq('2012-12-28')
|
216
|
+
expect(chunks[1].first.iso).to eq('2012-12-29')
|
217
|
+
expect(chunks[1].last.iso).to eq('2012-12-29')
|
218
|
+
expect(chunks[2].first.iso).to eq('2012-12-30')
|
219
|
+
expect(chunks[2].last.iso).to eq('2012-12-30')
|
220
|
+
expect(chunks.last.first.iso).to eq('2012-12-31')
|
221
|
+
expect(chunks.last.last.iso).to eq('2012-12-31')
|
220
222
|
end
|
221
223
|
|
222
224
|
it "should not include a partial final chunk by default" do
|
223
225
|
chunks = Period.new('2012-01-01', '2012-03-30').chunks(size: :month)
|
224
|
-
chunks.size.
|
226
|
+
expect(chunks.size).to eq(2)
|
225
227
|
end
|
226
228
|
|
227
229
|
it "should include a partial final chunk if partial_last" do
|
228
230
|
chunks = Period.new('2012-01-01', '2012-03-30').
|
229
231
|
chunks(size: :month, partial_last: true)
|
230
|
-
chunks.size.
|
231
|
-
chunks.last.first.
|
232
|
-
chunks.last.last.
|
232
|
+
expect(chunks.size).to eq(3)
|
233
|
+
expect(chunks.last.first).to eq(Date.parse('2012-03-01'))
|
234
|
+
expect(chunks.last.last).to eq(Date.parse('2012-03-30'))
|
233
235
|
end
|
234
236
|
|
235
237
|
it "should include a final chunk beyond end_date if round_up" do
|
236
238
|
chunks = Period.new('2012-01-01', '2012-03-30').
|
237
239
|
chunks(size: :month, round_up_last: true)
|
238
|
-
chunks.size.
|
239
|
-
chunks.last.first.
|
240
|
-
chunks.last.last.
|
240
|
+
expect(chunks.size).to eq(3)
|
241
|
+
expect(chunks.last.first).to eq(Date.parse('2012-03-01'))
|
242
|
+
expect(chunks.last.last).to eq(Date.parse('2012-03-31'))
|
241
243
|
end
|
242
244
|
|
243
245
|
it "should not include a partial initial chunk by default" do
|
244
246
|
chunks = Period.new('2012-01-13', '2012-03-31').chunks(size: :month)
|
245
|
-
chunks.size.
|
246
|
-
chunks[0].first.
|
247
|
-
chunks[0].last.
|
247
|
+
expect(chunks.size).to eq(2)
|
248
|
+
expect(chunks[0].first).to eq(Date.parse('2012-02-01'))
|
249
|
+
expect(chunks[0].last).to eq(Date.parse('2012-02-29'))
|
248
250
|
end
|
249
251
|
|
250
252
|
it "should include a partial initial chunk by if partial_first" do
|
251
253
|
chunks = Period.new('2012-01-13', '2012-03-31').
|
252
254
|
chunks(size: :month, partial_first: true)
|
253
|
-
chunks.size.
|
254
|
-
chunks[0].first.
|
255
|
-
chunks[0].last.
|
255
|
+
expect(chunks.size).to eq(3)
|
256
|
+
expect(chunks[0].first).to eq(Date.parse('2012-01-13'))
|
257
|
+
expect(chunks[0].last).to eq(Date.parse('2012-01-31'))
|
256
258
|
end
|
257
259
|
|
258
260
|
it "should include a final chunk beyond end_date if round_up" do
|
259
261
|
chunks = Period.new('2012-01-01', '2012-03-30').
|
260
262
|
chunks(size: :month, round_up_last: true)
|
261
|
-
chunks.size.
|
262
|
-
chunks.last.first.
|
263
|
-
chunks.last.last.
|
263
|
+
expect(chunks.size).to eq(3)
|
264
|
+
expect(chunks.last.first).to eq(Date.parse('2012-03-01'))
|
265
|
+
expect(chunks.last.last).to eq(Date.parse('2012-03-31'))
|
264
266
|
end
|
265
267
|
|
266
268
|
it "should be able to its chunk_sym" do
|
267
|
-
Period.new('2013-01-01', '2013-12-31').chunk_sym.
|
268
|
-
Period.new('2012-01-01', '2013-12-31').chunk_sym.
|
269
|
+
expect(Period.new('2013-01-01', '2013-12-31').chunk_sym).to eq(:year)
|
270
|
+
expect(Period.new('2012-01-01', '2013-12-31').chunk_sym).to_not eq(:year)
|
269
271
|
|
270
|
-
Period.new('2013-04-01', '2013-06-30').chunk_sym.
|
271
|
-
Period.new('2013-04-01', '2013-09-30').chunk_sym.
|
272
|
+
expect(Period.new('2013-04-01', '2013-06-30').chunk_sym).to eq(:quarter)
|
273
|
+
expect(Period.new('2013-04-01', '2013-09-30').chunk_sym).to_not eq(:quarter)
|
272
274
|
|
273
|
-
Period.new('2013-03-01', '2013-04-30').chunk_sym.
|
274
|
-
Period.new('2013-03-01', '2013-06-30').chunk_sym.
|
275
|
+
expect(Period.new('2013-03-01', '2013-04-30').chunk_sym).to eq(:bimonth)
|
276
|
+
expect(Period.new('2013-03-01', '2013-06-30').chunk_sym).to_not eq(:bimonth)
|
275
277
|
|
276
|
-
Period.new('2013-04-01', '2013-04-30').chunk_sym.
|
277
|
-
Period.new('2013-04-01', '2013-05-30').chunk_sym.
|
278
|
+
expect(Period.new('2013-04-01', '2013-04-30').chunk_sym).to eq(:month)
|
279
|
+
expect(Period.new('2013-04-01', '2013-05-30').chunk_sym).to_not eq(:month)
|
278
280
|
|
279
|
-
Period.new('2013-05-16', '2013-05-31').chunk_sym.
|
280
|
-
Period.new('2013-05-16', '2013-06-30').chunk_sym.
|
281
|
+
expect(Period.new('2013-05-16', '2013-05-31').chunk_sym).to eq(:semimonth)
|
282
|
+
expect(Period.new('2013-05-16', '2013-06-30').chunk_sym).to_not eq(:semimonth)
|
281
283
|
|
282
|
-
Period.new('2013-11-04', '2013-11-17').chunk_sym.
|
283
|
-
Period.new('2013-11-04', '2013-11-24').chunk_sym.
|
284
|
+
expect(Period.new('2013-11-04', '2013-11-17').chunk_sym).to eq(:biweek)
|
285
|
+
expect(Period.new('2013-11-04', '2013-11-24').chunk_sym).to_not eq(:biweek)
|
284
286
|
|
285
|
-
Period.new('2013-11-11', '2013-11-17').chunk_sym.
|
286
|
-
Period.new('2013-11-11', '2013-11-24').chunk_sym.
|
287
|
+
expect(Period.new('2013-11-11', '2013-11-17').chunk_sym).to eq(:week)
|
288
|
+
expect(Period.new('2013-11-11', '2013-11-24').chunk_sym).to_not eq(:week)
|
287
289
|
|
288
|
-
Period.new('2013-11-10', '2013-11-10').chunk_sym.
|
289
|
-
Period.new('2013-11-10', '2013-11-11').chunk_sym.
|
290
|
+
expect(Period.new('2013-11-10', '2013-11-10').chunk_sym).to eq(:day)
|
291
|
+
expect(Period.new('2013-11-10', '2013-11-11').chunk_sym).to_not eq(:day)
|
290
292
|
|
291
|
-
Period.new('2013-11-02', '2013-12-16').chunk_sym.
|
293
|
+
expect(Period.new('2013-11-02', '2013-12-16').chunk_sym).to eq(:irregular)
|
292
294
|
end
|
293
295
|
end
|
294
296
|
end
|
data/spec/lib/range_spec.rb
CHANGED
@@ -32,7 +32,7 @@ describe Range do
|
|
32
32
|
expect((4..8)).not_to be_superset_of((9..20))
|
33
33
|
end
|
34
34
|
|
35
|
-
it "should know if it is a superset of another range" do
|
35
|
+
it "should know if it is a proper superset of another range" do
|
36
36
|
expect((4..8)).to be_proper_superset_of((5..7))
|
37
37
|
expect((4..8)).not_to be_proper_superset_of((6..8))
|
38
38
|
expect((4..8)).not_to be_proper_superset_of((4..7))
|
@@ -47,23 +47,23 @@ describe Range do
|
|
47
47
|
end
|
48
48
|
|
49
49
|
it "should know its intersection with another range" do
|
50
|
-
((0..10) & (5..20)).
|
51
|
-
((0..10) & (5..20)).
|
52
|
-
((0..10) & (10..20)).
|
50
|
+
expect(((0..10) & (5..20))).to eq((5..10))
|
51
|
+
expect(((0..10) & (5..20))).to eq((5..20) & (0..10))
|
52
|
+
expect(((0..10) & (10..20))).to eq((10..10))
|
53
53
|
end
|
54
54
|
|
55
55
|
it "intersection should return nil if there is no overlap" do
|
56
|
-
((0..10) & (15..20)).
|
56
|
+
expect(((0..10) & (15..20))).to be_nil
|
57
57
|
end
|
58
58
|
|
59
59
|
it "should know its union with another range" do
|
60
|
-
((0..10) + (5..20)).
|
61
|
-
((0..10) + (5..20)).
|
62
|
-
((0..10) + (10..20)).
|
60
|
+
expect(((0..10) + (5..20))).to eq((0..20))
|
61
|
+
expect(((0..10) + (5..20))).to eq((5..20) + (0..10))
|
62
|
+
expect(((0..10) + (10..20))).to eq((0..20))
|
63
63
|
end
|
64
64
|
|
65
65
|
it "union should return nil if there is no overlap" do
|
66
|
-
((0..10) & (15..20)).
|
66
|
+
expect(((0..10) & (15..20))).to be_nil
|
67
67
|
end
|
68
68
|
|
69
69
|
it "should know the difference with another range" do
|
@@ -99,148 +99,148 @@ describe Range do
|
|
99
99
|
|
100
100
|
describe "joining" do
|
101
101
|
it "should be able to join contiguous ranges" do
|
102
|
-
(0..3).join(4..8).
|
102
|
+
expect((0..3).join(4..8)).to eq (0..8)
|
103
103
|
end
|
104
104
|
|
105
105
|
it "should return nil on join of non-contiguous ranges" do
|
106
|
-
(0..3).join(5..8).
|
107
|
-
(0...3).join(4..8).
|
106
|
+
expect((0..3).join(5..8)).to be_nil
|
107
|
+
expect((0...3).join(4..8)).to be_nil
|
108
108
|
end
|
109
109
|
|
110
110
|
it "should work with Floats, allowing single-point overlap" do
|
111
|
-
(0.0..3.0).join(3.0..8.2).
|
111
|
+
expect((0.0..3.0).join(3.0..8.2)).to eq (0.0..8.2)
|
112
112
|
end
|
113
113
|
end
|
114
114
|
|
115
115
|
describe "spanning" do
|
116
116
|
it "should be able to determine whether it is spanned by a set of ranges" do
|
117
|
-
(0..10).
|
117
|
+
expect((0..10)).to be_spanned_by([(0..3), (4..6), (7..10)])
|
118
118
|
end
|
119
119
|
|
120
120
|
it "should be determine that overlapping ranges do not span" do
|
121
|
-
(0..10).
|
121
|
+
expect((0..10)).to_not be_spanned_by([(0..3), (3..6), (7..10)])
|
122
122
|
end
|
123
123
|
|
124
124
|
it "should allow spanning ranges to be any Enumerable" do
|
125
125
|
require 'set'
|
126
126
|
set = [(0..3), (4..6), (7..10)].to_set
|
127
|
-
(0..10).
|
127
|
+
expect((0..10)).to be_spanned_by(set)
|
128
128
|
set = [(0...3), (4..6), (7..10)].to_set
|
129
|
-
(0..10).
|
129
|
+
expect((0..10)).to_not be_spanned_by(set)
|
130
130
|
end
|
131
131
|
|
132
132
|
it "should allow the spanning set to be wider than itself" do
|
133
133
|
set = [(0..3), (4..6), (7..10)].to_set
|
134
|
-
(2..8).
|
135
|
-
(5..6).
|
134
|
+
expect((2..8)).to be_spanned_by(set)
|
135
|
+
expect((5..6)).to be_spanned_by(set)
|
136
136
|
end
|
137
137
|
end
|
138
138
|
|
139
139
|
describe "overlapping a single range" do
|
140
140
|
it "should know if another range overlaps it" do
|
141
|
-
(0..10).overlaps?(-3..5).
|
142
|
-
(0..10).overlaps?(3..5).
|
143
|
-
(0..10).overlaps?(8..15).
|
144
|
-
(0..10).overlaps?(0..10).
|
145
|
-
(0..10).overlaps?(11..12).
|
146
|
-
(0..10).overlaps?(-11..-1).
|
141
|
+
expect((0..10).overlaps?(-3..5)).to be_truthy
|
142
|
+
expect((0..10).overlaps?(3..5)).to be_truthy
|
143
|
+
expect((0..10).overlaps?(8..15)).to be_truthy
|
144
|
+
expect((0..10).overlaps?(0..10)).to be_truthy
|
145
|
+
expect((0..10).overlaps?(11..12)).to be_falsy
|
146
|
+
expect((0..10).overlaps?(-11..-1)).to be_falsy
|
147
147
|
end
|
148
148
|
|
149
149
|
it "should be able to determine whether a set contains covered overlaps" do
|
150
|
-
(0..10).
|
150
|
+
expect((0..10)).to have_overlaps_within([(0..3), (2..6), (7..10)])
|
151
151
|
end
|
152
152
|
|
153
153
|
it "should not care about overlaps outside range" do
|
154
|
-
(11..15).
|
154
|
+
expect((11..15)).to_not have_overlaps_within([(0..3), (2..6), (7..10)])
|
155
155
|
end
|
156
156
|
|
157
157
|
it "should not count contiguous ranges as overlapping" do
|
158
|
-
(0..10).
|
158
|
+
expect((0..10)).to_not have_overlaps_within([(0..3), (4..6), (7..10)])
|
159
159
|
end
|
160
160
|
|
161
161
|
it "should not count non-contiguous ranges as overlapping" do
|
162
|
-
(0..10).
|
162
|
+
expect((0..10)).to_not have_overlaps_within([(0..3), (4..6), (8..10)])
|
163
163
|
end
|
164
164
|
|
165
165
|
it "should not count an empty set as overlapping" do
|
166
|
-
(0..10).
|
166
|
+
expect((0..10)).to_not have_overlaps_within([])
|
167
167
|
end
|
168
168
|
end
|
169
169
|
|
170
170
|
describe "coverage gaps" do
|
171
171
|
it "should return an empty array if ranges completely cover" do
|
172
|
-
(0..10).gaps([(-1..3), (4..8), (9..11)]).
|
172
|
+
expect((0..10).gaps([(-1..3), (4..8), (9..11)])).to be_empty
|
173
173
|
end
|
174
174
|
|
175
175
|
it "should return array for itself if ranges are empty" do
|
176
|
-
(0..10).gaps([]).
|
176
|
+
expect((0..10).gaps([])).to eq([(0..10)])
|
177
177
|
end
|
178
178
|
|
179
179
|
it "should return an array of gaps" do
|
180
180
|
gaps = (0..10).gaps([(0..3), (5..6), (9..10)])
|
181
|
-
gaps[0].
|
182
|
-
gaps[1].
|
181
|
+
expect(gaps[0]).to eq((4..4))
|
182
|
+
expect(gaps[1]).to eq((7..8))
|
183
183
|
end
|
184
184
|
|
185
185
|
it "should allow ranges to extend before and after self" do
|
186
186
|
gaps = (0..10).gaps([(-3..3), (4..6), (7..13)])
|
187
|
-
gaps.
|
187
|
+
expect(gaps).to be_empty
|
188
188
|
end
|
189
189
|
|
190
190
|
it "should return an gaps at beginning and end" do
|
191
191
|
gaps = (0..10).gaps([(2..3), (4..6), (7..8)])
|
192
|
-
gaps[0].
|
193
|
-
gaps[1].
|
192
|
+
expect(gaps[0]).to eq((0..1))
|
193
|
+
expect(gaps[1]).to eq((9..10))
|
194
194
|
end
|
195
195
|
|
196
196
|
it "should work even if ranges are out of order" do
|
197
197
|
gaps = (0..10).gaps([(2..3), (7..8), (4..6)])
|
198
|
-
gaps[0].
|
199
|
-
gaps[1].
|
198
|
+
expect(gaps[0]).to eq((0..1))
|
199
|
+
expect(gaps[1]).to eq((9..10))
|
200
200
|
end
|
201
201
|
|
202
202
|
it "should work even if ranges overlap" do
|
203
203
|
gaps = (0..10).gaps([(-2..3), (2..8), (4..10)])
|
204
|
-
gaps.
|
204
|
+
expect(gaps).to be_empty
|
205
205
|
end
|
206
206
|
end
|
207
207
|
|
208
208
|
describe "coverage overlaps" do
|
209
209
|
it "should return an empty array if ranges are empty" do
|
210
|
-
(0..10).overlaps([]).
|
210
|
+
expect((0..10).overlaps([])).to be_empty
|
211
211
|
end
|
212
212
|
|
213
213
|
it "should return an empty array if ranges is same as self" do
|
214
|
-
(0..10).overlaps([(0..10)]).
|
214
|
+
expect((0..10).overlaps([(0..10)])).to be_empty
|
215
215
|
end
|
216
216
|
|
217
217
|
it "should return an empty array if ranges is wider than self" do
|
218
|
-
(0..10).overlaps([(-5..15)]).
|
218
|
+
expect((0..10).overlaps([(-5..15)])).to be_empty
|
219
219
|
end
|
220
220
|
|
221
221
|
it "should return an empty array if ranges is narrower than self" do
|
222
|
-
(0..10).overlaps([(5..8)]).
|
222
|
+
expect((0..10).overlaps([(5..8)])).to be_empty
|
223
223
|
end
|
224
224
|
|
225
225
|
it "should return an array of overlaps" do
|
226
226
|
overlaps = (0..10).overlaps([(0..3), (2..6), (4..10)])
|
227
|
-
overlaps.size.
|
228
|
-
overlaps[0].
|
229
|
-
overlaps[1].
|
227
|
+
expect(overlaps.size).to eq(2)
|
228
|
+
expect(overlaps[0]).to eq((2..3))
|
229
|
+
expect(overlaps[1]).to eq((4..6))
|
230
230
|
end
|
231
231
|
|
232
232
|
it "should not return any overlaps before self" do
|
233
233
|
overlaps = (0..10).overlaps([(-5..-3), (-4..-1), (0..3), (2..6), (4..10)])
|
234
|
-
overlaps.size.
|
235
|
-
overlaps[0].
|
236
|
-
overlaps[1].
|
234
|
+
expect(overlaps.size).to eq(2)
|
235
|
+
expect(overlaps[0]).to eq((2..3))
|
236
|
+
expect(overlaps[1]).to eq((4..6))
|
237
237
|
end
|
238
238
|
|
239
239
|
it "should not return any overlaps after self" do
|
240
240
|
overlaps = (0..10).overlaps([(0..3), (2..6), (4..15), (11..20)])
|
241
|
-
overlaps.size.
|
242
|
-
overlaps[0].
|
243
|
-
overlaps[1].
|
241
|
+
expect(overlaps.size).to eq(2)
|
242
|
+
expect(overlaps[0]).to eq((2..3))
|
243
|
+
expect(overlaps[1]).to eq((4..6))
|
244
244
|
end
|
245
245
|
end
|
246
246
|
end
|