spec/timeframe_spec.rb in timeframe-0.1.0 vs spec/timeframe_spec.rb in timeframe-0.1.1

- old
+ new

@@ -57,11 +57,11 @@ tf = Timeframe.new(start, finish) tf.inspect.must_match %r{<Timeframe\(-?\d+\) 86 days starting 2008-02-14 ending 2008-05-10>} end end - describe '.constrained_new' do + describe :constrained_new do let(:start) { Date.parse('2008-02-14') } let(:finish) { Date.parse('2008-05-10') } let(:constraint_start) { Date.parse('2008-01-01') } let(:constraint_finish) { Date.parse('2008-12-01') } let(:constraint) { Timeframe.new(constraint_start, constraint_finish) } @@ -106,11 +106,11 @@ Date.new(2009,1,1), Date.new(2010,1,1), constraint) timeframe.days.must_equal 0 end end - describe '.this_year' do + describe :this_year do it "should return the current year" do Timeframe.this_year.must_equal Timeframe.new(:year => Time.now.year) end end @@ -188,11 +188,11 @@ end end describe '#/' do it "should return a fraction of another timeframe" do - (Timeframe.new(:month => 4, :year => 2009) / Timeframe.new(:year => 2009)).must_equal (30.0 / 365.0) + (Timeframe.new(:month => 4, :year => 2009) / Timeframe.new(:year => 2009)).must_equal(30.0 / 365.0) end end describe '#gaps_left_by' do it "should be able to ascertain gaps left by a list of other Timeframes" do @@ -222,14 +222,54 @@ it "should return its predecessor in a previous year" do Timeframe.this_year.last_year.must_equal Timeframe.new(Date.new(Date.today.year - 1, 1, 1), Date.new(Date.today.year, 1, 1)) end end - describe '.parse' do - it 'understands ISO8601' do - Timeframe.parse('2009-01-01/2010-01-01').must_equal Timeframe.new(:year => 2009) + describe :parse do + describe 'ISO 8601 <start>/<end>' do + it 'works without time' do + Timeframe.parse('2007-03-01/2008-05-11').must_equal Timeframe.new(Date.new(2007, 3, 1), Date.new(2008, 5, 11)) + Timeframe.parse('2007-03-01--2008-05-11').must_equal Timeframe.new(Date.new(2007, 3, 1), Date.new(2008, 5, 11)) + end + it 'works with time' do + Timeframe.parse('2007-03-01T13:00:00Z/2008-05-11T15:30:00Z').must_equal Timeframe.new(Date.new(2007, 3, 1), Date.new(2008, 5, 11)) + Timeframe.parse('2007-03-01T13:00:00Z--2008-05-11T15:30:00Z').must_equal Timeframe.new(Date.new(2007, 3, 1), Date.new(2008, 5, 11)) + end + it 'takes shorthand' do + Timeframe.parse('2007-11-13/15').must_equal Timeframe.new(Date.new(2007, 11, 13), Date.new(2007, 11, 15)) # "2007-11-13/15", i.e. from any time on 2007-11-13 to any time on 2007-11-15 + Timeframe.parse("2008-02-15/03-14").must_equal Timeframe.new(Date.new(2008, 2, 15), Date.new(2008, 3, 14)) # "2008-02-15/2008-03-14" + Timeframe.parse("2007-12-14T13:30/15:30").must_equal Timeframe.new(Date.new(2007, 12, 14), Date.new(2007, 12, 14)) # "2007-12-14T13:30/2007-12-14T15:30".. imprecise! + end end + + describe 'ISO 8601 <start>/<duration>' do + it 'works without time' do + Timeframe.parse('2007-03-01/P1Y2M10DT2H30M').must_equal Timeframe.new(Date.new(2007, 3, 1), Date.new(2008, 5, 11)) + Timeframe.parse('2007-03-01--P1Y2M10DT2H30M').must_equal Timeframe.new(Date.new(2007, 3, 1), Date.new(2008, 5, 11)) + end + it 'works with time' do + Timeframe.parse('2007-03-01T13:00:00Z/P1Y2M10DT2H30M').must_equal Timeframe.new(Date.new(2007, 3, 1), Date.new(2008, 5, 11)) + Timeframe.parse('2007-03-01T13:00:00Z--P1Y2M10DT2H30M').must_equal Timeframe.new(Date.new(2007, 3, 1), Date.new(2008, 5, 11)) + end + end + + # note that 2008 was a leap year + describe 'ISO 8601 <duration>/<end>' do + it 'works with leap years' do + Timeframe.parse('2007-02-28--P1Y').must_equal Timeframe.new(Date.new(2007, 2, 28), Date.new(2008, 2, 29)) + Timeframe.parse('P1Y--2008-02-29').must_equal Timeframe.new(Date.new(2007, 3, 1), Date.new(2008, 2, 29)) + end + it 'works without time' do + Timeframe.parse('P1Y2M10DT2H30M/2008-05-11').must_equal Timeframe.new(Date.new(2007, 3, 2), Date.new(2008, 5, 11)) + Timeframe.parse('P1Y2M10DT2H30M--2008-05-11').must_equal Timeframe.new(Date.new(2007, 3, 2), Date.new(2008, 5, 11)) + end + it 'works with time' do + Timeframe.parse('P1Y2M10DT2H30M/2008-05-11T15:30:00Z').must_equal Timeframe.new(Date.new(2007, 3, 3), Date.new(2008, 5, 11)) + Timeframe.parse('P1Y2M10DT2H30M--2008-05-11T15:30:00Z').must_equal Timeframe.new(Date.new(2007, 3, 3), Date.new(2008, 5, 11)) + end + end + it 'understands plain year' do plain_year = 2009 Timeframe.parse(plain_year).must_equal Timeframe.new(:year => plain_year) Timeframe.parse(plain_year.to_s).must_equal Timeframe.new(:year => plain_year) end @@ -267,9 +307,26 @@ end describe '#to_s' do it 'should not only look at month numbers when describing multi-year timeframes' do Timeframe.new(Date.parse('2008-01-01'), Date.parse('2010-01-01')).to_s.must_equal "2008-01-01/2010-01-01" + end + end + + describe '#dates' do + it "should enumerate all dates between start and end" do + dates = Timeframe.new(:year => 2008).dates + dates.min.must_equal Date.new(2008,1,1) + dates.max.must_equal Date.new(2008,12,31) + dates.uniq.length.must_equal 366 + dates.select { |d| d.month == 2 }.length.must_equal 29 + end + end + + describe '#first_days_of_months' do + it "should enumerate all the first days of included months" do + dates = Timeframe.parse('2011-05-01/2012-02-01').first_days_of_months + dates.must_equal [Date.new(2011,5,1), Date.new(2011,6,1), Date.new(2011,7,1), Date.new(2011,8,1), Date.new(2011,9,1), Date.new(2011,10,1), Date.new(2011,11,1), Date.new(2011,12,1), Date.new(2012,1,1)] end end describe "Array#multiple_timeframes_gaps_left_by" do it "should raise error if not a Timeframes are going to be merged" do