spec/date_range_spec.rb in timely-0.0.2 vs spec/date_range_spec.rb in timely-0.1.0
- old
+ new
@@ -1,40 +1,78 @@
require 'spec_helper'
describe Timely::DateRange do
it "should allow initialization with two dates" do
- lambda { @date_range = Timely::DateRange.new(Date.today, Date.today + 3) }.should_not raise_error
- @date_range.start_date.should eql Date.today
- @date_range.end_date.should eql Date.today + 3
+ expect { @date_range = Timely::DateRange.new(Date.today, Date.today + 3) }.to_not raise_error
+ expect(@date_range.start_date).to eq Date.today
+ expect(@date_range.end_date).to eq Date.today + 3
+ expect(@date_range.number_of_nights).to eq 4
end
it "should allow initialization with one date" do
- lambda { @date_range = Timely::DateRange.new(Date.today) }.should_not raise_error
- @date_range.start_date.should eql Date.today
- @date_range.end_date.should eql Date.today
+ expect { @date_range = Timely::DateRange.new(Date.today) }.to_not raise_error
+ expect(@date_range.start_date).to eq Date.today
+ expect(@date_range.end_date).to eq Date.today
+ expect(@date_range.number_of_nights).to eq 1
end
it "should allow initialization with a range" do
- lambda { @date_range = Timely::DateRange.new(Date.today..Date.today + 3) }.should_not raise_error
- @date_range.start_date.should eql Date.today
- @date_range.end_date.should eql Date.today + 3
+ expect { @date_range = Timely::DateRange.new(Date.today..Date.today + 3) }.to_not raise_error
+ expect(@date_range.start_date).to eq Date.today
+ expect(@date_range.end_date).to eq Date.today + 3
end
+ it 'should print a readable version of time between two dates' do
+ expect(Timely::DateRange.new('2000-01-04'.to_date, '2000-01-04'.to_date).to_s).to eq '2000-01-04'
+ expect(Timely::DateRange.new('2000-01-04'.to_date, '2000-01-06'.to_date).to_s).to eq '2000-01-04 to 2000-01-06 (inclusive)'
+ expect(Timely::DateRange.new('2000-01-01'.to_date, '2000-05-31'.to_date).to_s).to eq 'Jan 2000 to May 2000'
+ expect(Timely::DateRange.new('2000-01-01'.to_date, '2000-01-31'.to_date).to_s).to eq 'Jan 2000'
+ Date::DATE_FORMATS[:short] = '%Y-%m-%d'
+ expect(Timely::DateRange.to_s('2000-01-01'.to_date, nil)).to eq 'on or after 2000-01-01'
+ expect(Timely::DateRange.to_s(nil, '2000-01-31'.to_date)).to eq 'on or before 2000-01-31'
+ expect(Timely::DateRange.to_s(nil, nil)).to eq 'no date range'
+ end
+
+ let(:fourth) { '2000-01-04'.to_date }
+ let(:fifth) { '2000-01-05'.to_date }
+ let(:sixth) { '2000-01-06'.to_date }
+
+ it 'should print a readable version of time between two times' do
+ expect(Timely::DateRange.to_s(fourth.at(9, 30), sixth.at(4, 20)))
+ .to eq '2000-01-04 09:30 to 2000-01-06 04:20'
+ expect(Timely::DateRange.to_s(fourth.at(9, 30)))
+ .to eq 'on or after 2000-01-04 09:30'
+ end
+
+ it 'should handle params' do
+ today = Timely::DateRange.from_params('2000-01-04')
+ expect(today.first).to eq '2000-01-04'.to_date
+ expect(today.last).to eq '2000-01-04'.to_date
+
+ today = Timely::DateRange.from_params('2000-01-04', '2')
+ expect(today.first).to eq '2000-01-04'.to_date
+ expect(today.last).to eq '2000-01-05'.to_date
+
+ today = Timely::DateRange.from_params('2000-01-04', 2)
+ expect(today.first).to eq '2000-01-04'.to_date
+ expect(today.last).to eq '2000-01-05'.to_date
+ end
+
it "should correctly find the interesection between two date ranges" do
@date_range = Timely::DateRange.new("2000-01-03".to_date, "2000-01-06".to_date)
- @date_range.intersecting_dates(Timely::DateRange.new("2000-01-04".to_date, "2000-01-07".to_date)).should == ("2000-01-04".to_date.."2000-01-06".to_date)
- @date_range.intersecting_dates(Timely::DateRange.new("2000-01-01".to_date, "2000-01-02".to_date)).should == []
- @date_range.intersecting_dates(Timely::DateRange.new("2000-01-01".to_date, "2000-01-03".to_date)).should == ("2000-01-03".to_date.."2000-01-03".to_date)
- @date_range.intersecting_dates(Timely::DateRange.new("2000-01-06".to_date, "2000-01-07".to_date)).should == ("2000-01-06".to_date.."2000-01-06".to_date)
- @date_range.intersecting_dates(Timely::DateRange.new("2000-01-06".to_date, "2000-01-07".to_date)).should == ("2000-01-06".to_date.."2000-01-06".to_date)
- @date_range.intersecting_dates(Timely::DateRange.new("2000-01-04".to_date, "2000-01-05".to_date)).should == ("2000-01-04".to_date.."2000-01-05".to_date)
- @date_range.intersecting_dates(Timely::DateRange.new("2000-01-05".to_date, "2000-01-05".to_date)).should == ("2000-01-05".to_date.."2000-01-05".to_date)
+ expect(@date_range.intersecting_dates(Timely::DateRange.new("2000-01-04".to_date, "2000-01-07".to_date))).to eq ("2000-01-04".to_date.."2000-01-06".to_date)
+ expect(@date_range.intersecting_dates(Timely::DateRange.new("2000-01-01".to_date, "2000-01-02".to_date))).to eq []
+ expect(@date_range.intersecting_dates(Timely::DateRange.new("2000-01-01".to_date, "2000-01-03".to_date))).to eq ("2000-01-03".to_date.."2000-01-03".to_date)
+ expect(@date_range.intersecting_dates(Timely::DateRange.new("2000-01-06".to_date, "2000-01-07".to_date))).to eq ("2000-01-06".to_date.."2000-01-06".to_date)
+ expect(@date_range.intersecting_dates(Timely::DateRange.new("2000-01-06".to_date, "2000-01-07".to_date))).to eq ("2000-01-06".to_date.."2000-01-06".to_date)
+ expect(@date_range.intersecting_dates(Timely::DateRange.new("2000-01-04".to_date, "2000-01-05".to_date))).to eq ("2000-01-04".to_date.."2000-01-05".to_date)
+ expect(@date_range.intersecting_dates(Timely::DateRange.new("2000-01-05".to_date, "2000-01-05".to_date))).to eq ("2000-01-05".to_date.."2000-01-05".to_date)
@date_range = Timely::DateRange.new("2000-01-03".to_date, "2000-01-03".to_date)
- @date_range.intersecting_dates(Timely::DateRange.new("2000-01-04".to_date, "2000-01-07".to_date)).should == []
- @date_range.intersecting_dates(Timely::DateRange.new("2000-01-01".to_date, "2000-01-03".to_date)).should == ("2000-01-03".to_date.."2000-01-03".to_date)
- @date_range.intersecting_dates(Timely::DateRange.new("2000-01-03".to_date, "2000-01-05".to_date)).should == ("2000-01-03".to_date.."2000-01-03".to_date)
- @date_range.intersecting_dates(Timely::DateRange.new("2000-01-02".to_date, "2000-01-04".to_date)).should == ("2000-01-03".to_date.."2000-01-03".to_date)
+ expect(@date_range.intersecting_dates(Timely::DateRange.new("2000-01-04".to_date, "2000-01-07".to_date))).to eq []
+ expect(@date_range.intersecting_dates(Timely::DateRange.new("2000-01-01".to_date, "2000-01-03".to_date))).to eq ("2000-01-03".to_date.."2000-01-03".to_date)
+ expect(@date_range.intersecting_dates(Timely::DateRange.new("2000-01-03".to_date, "2000-01-05".to_date))).to eq ("2000-01-03".to_date.."2000-01-03".to_date)
+ expect(@date_range.intersecting_dates(Timely::DateRange.new("2000-01-02".to_date, "2000-01-04".to_date))).to eq ("2000-01-03".to_date.."2000-01-03".to_date)
end
end