spec/timeliness/parser_spec.rb in timeliness-0.3.3 vs spec/timeliness/parser_spec.rb in timeliness-0.3.4

- old
+ new

@@ -4,11 +4,11 @@ before(:all) do Timecop.freeze(2010,1,1,0,0,0) end describe "parse" do - it "should return time object for valid datetime string" do + it "should return Time object for valid datetime string" do parse("2000-01-01 12:13:14").should be_kind_of(Time) end it "should return nil for empty string" do parse("").should be_nil @@ -86,15 +86,39 @@ end end end context "string with zone abbreviation" do - it 'should return value using string zone in default timezone' do + before do + Time.zone = 'Melbourne' + end + + it 'should return value using string zone adjusted to default :local timezone' do + Timeliness.default_timezone = :local value = parse("Thu, 01 Jun 2000 03:00:00 MST") value.should == Time.local(2000,6,1,20,0,0) value.utc_offset.should == 10.hours end + + it 'should return value using string zone adjusted to default :current timezone' do + Timeliness.default_timezone = :current + Time.zone = 'Adelaide' + value = parse("Thu, 01 Jun 2000 03:00:00 MST") + value.should == Time.zone.local(2000,6,1,19,30,0) + value.utc_offset.should == 9.5.hours + end + + it 'should return value using string zone adjusted to :zone option string timezone' do + Timeliness.default_timezone = :local + value = parse("Thu, 01 Jun 2000 03:00:00 MST", :zone => 'Perth') + value.should == Time.use_zone('Perth') { Time.zone.local(2000,6,1,18,0,0) } + value.utc_offset.should == 8.hours + end + + after do + Time.zone = nil + end end context "with :datetime type" do it "should return time object for valid datetime string" do parse("2000-01-01 12:13:14", :datetime).should == Time.local(2000,1,1,12,13,14) @@ -130,36 +154,59 @@ time = parse("12:13:14", :now => Time.local(2010,1,1)) time.should == Time.local(2010,1,1,12,13,14) end end + context "with time value argument" do + it 'should use argument as :now option value' do + time = parse("12:13:14", Time.local(2010,1,1)) + time.should == Time.local(2010,1,1,12,13,14) + end + end + context "with :zone option" do context ":utc" do it "should return time object in utc timezone" do time = parse("2000-06-01 12:13:14", :datetime, :zone => :utc) time.utc_offset.should == 0 end + + it 'should return nil for partial invalid time component' do + parse("2000-06-01 12:60", :datetime, :zone => :utc).should be_nil + end end context ":local" do it "should return time object in local system timezone" do time = parse("2000-06-01 12:13:14", :datetime, :zone => :local) time.utc_offset.should == 10.hours end + + it 'should return nil for partial invalid time component' do + parse("2000-06-01 12:60", :datetime, :zone => :local).should be_nil + end end context ":current" do it "should return time object in current timezone" do Time.zone = 'Adelaide' time = parse("2000-06-01 12:13:14", :datetime, :zone => :current) time.utc_offset.should == 9.5.hours end + + it 'should return nil for partial invalid time component' do + parse("2000-06-01 12:60", :datetime, :zone => :current).should be_nil + end end context "named zone" do it "should return time object in the timezone" do time = parse("2000-06-01 12:13:14", :datetime, :zone => 'London') time.utc_offset.should == 1.hour + end + + it 'should return nil for partial invalid time component' do + parse("2000-06-01 12:60", :datetime, :zone => 'London').should be_nil end end context "without ActiveSupport loaded" do it 'should output message' do