spec/timeliness/parser_spec.rb in timeliness-0.2.0 vs spec/timeliness/parser_spec.rb in timeliness-0.3.0
- old
+ new
@@ -1,40 +1,130 @@
require 'spec_helper'
describe Timeliness::Parser do
- context "parse" do
+ 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
+ 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
+ end
+
+ it "should return nil for nil value" do
+ parse(nil).should be_nil
+ end
+
+ it "should return return same value if value not a string" do
+ value = Time.now
+ parse(value).should == value
+ end
+
+ it "should return time object for valid date string" do
+ parse("2000-01-01").should be_kind_of(Time)
+ end
+
+ it "should return nil for invalid date string" do
+ should_not_parse("2000-02-30")
+ end
+
it "should return time object for valid time string" do
- parse("2000-01-01 12:13:14", :datetime).should be_kind_of(Time)
+ parse("12:13:14").should be_kind_of(Time)
end
+ it "should return nil for invalid time string" do
+ should_not_parse("25:00:00")
+ end
+
it "should return nil for datetime string with invalid date part" do
- should_not_parse("2000-02-30 12:13:14", :datetime)
+ should_not_parse("2000-02-30 12:13:14")
end
it "should return nil for datetime string with invalid time part" do
- should_not_parse("2000-02-01 25:13:14", :datetime)
+ should_not_parse("2000-02-01 25:13:14")
end
- it "should return nil for invalid date string" do
- should_not_parse("2000-02-30", :date)
+ context "string with zone offset value" do
+ context "when current timezone is earler than string zone" do
+ it 'should return value shifted by positive offset in default timezone' do
+ value = parse("2000-06-01T12:00:00+02:00")
+ value.should == Time.local(2000,6,1,20,0,0)
+ value.utc_offset.should == 10.hours
+ end
+
+ it 'should return value shifted by negative offset in default timezone' do
+ value = parse("2000-06-01T12:00:00-01:00")
+ value.should == Time.local(2000,6,1,23,0,0)
+ value.utc_offset.should == 10.hours
+ end
+ end
+
+ context "when current timezone is later than string zone" do
+ before(:all) do
+ Timeliness.default_timezone = :current
+ Time.zone = 'America/Phoenix'
+ end
+
+ it 'should return value shifted by positive offset in default timezone' do
+ value = parse("2000-06-01T12:00:00+02:00")
+ value.should == Time.zone.local(2000,6,1,3,0,0)
+ value.utc_offset.should == -7.hours
+ end
+
+ it 'should return value shifted by negative offset in default timezone' do
+ value = parse("2000-06-01T12:00:00-01:00")
+ value.should == Time.zone.local(2000,6,1,6,0,0)
+ value.utc_offset.should == -7.hours
+ end
+
+ after(:all) do
+ Time.zone = nil
+ Timeliness.default_timezone = :local
+ end
+ end
end
- it "should return nil for invalid time string" do
- should_not_parse("25:00:00", :time)
+ context "string with zone abbreviation" do
+ it 'should return value using string zone in default timezone' do
+ 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
end
- it "should ignore time in datetime string for date type" do
- parse('2000-02-01 12:13', :date).should == Time.local(2000,2,1)
+ 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)
+ end
end
- it "should ignore date in datetime string for time type" do
- parse('2010-02-01 12:13', :time).should == Time.local(2000,1,1,12,13)
+ context "with :date type" do
+ it "should return time object for valid date string" do
+ parse("2000-01-01", :date).should == Time.local(2000,1,1)
+ end
+
+ it "should ignore time in datetime string" do
+ parse('2000-02-01 12:13', :date).should == Time.local(2000,2,1)
+ end
end
- it "should return return same value if value not a string" do
- value = Time.now
- parse(value, :datetime).should == value
+ context "with :time type" do
+ it "should return time object with a dummy date values" do
+ parse('12:13', :time).should == Time.local(2010,1,1,12,13)
+ end
+
+ it "should ignore date in datetime string" do
+ parse('2010-02-01 12:13', :time).should == Time.local(2010,1,1,12,13)
+ end
+
+ it "should raise error if time hour is out of range for AM meridian" do
+ parse('13:14 am', :time).should be_nil
+ end
end
context "with :now option" do
it 'should use date parts if string does not specify' do
time = parse("12:13:14", :now => Time.local(2010,1,1))
@@ -73,18 +163,18 @@
end
context "without ActiveSupport loaded" do
it 'should output message' do
lambda {
- Time.should_receive(:use_zone).and_raise(NoMethodError.new("undefined method `zone' for Time:Class"))
- time = parse("2000-06-01 12:13:14", :datetime, :zone => 'London')
+ Time.should_receive(:zone).and_raise(NoMethodError.new("undefined method `zone' for Time:Class"))
+ time = parse("2000-06-01 12:13:14", :datetime, :zone => :current)
}.should raise_error(Timeliness::Parser::MissingTimezoneSupport)
end
end
end
- describe "for time type" do
+ context "for time type" do
context "with date from date_for_time_type" do
before do
@original = Timeliness.date_for_time_type
end
@@ -108,80 +198,73 @@
parse('12:13:14', :time, :now => Time.local(2010, 6, 1)).should == Time.local(2010,6,1,12,13,14)
end
end
context "with :zone option" do
- before(:all) do
- Timecop.freeze(2010,1,1,0,0,0)
- end
-
it "should use date from the specified zone" do
time = parse("12:13:14", :time, :zone => :utc)
time.year.should == 2009
time.month.should == 12
time.day.should == 31
end
-
- after(:all) do
- Timecop.return
- end
end
+
end
end
- context "_parse" do
- context "with type" do
+ describe "_parse" do
+ context "with no type" do
it "should return date array from date string" do
- time_array = parser._parse('2000-02-01', :date)
+ time_array = parser._parse('2000-02-01')
time_array.should == [2000,2,1,nil,nil,nil,nil,nil]
end
it "should return time array from time string" do
time_array = parser._parse('12:13:14', :time)
time_array.should == [nil,nil,nil,12,13,14,nil,nil]
end
it "should return datetime array from datetime string" do
- time_array = parser._parse('2000-02-01 12:13:14', :datetime)
+ time_array = parser._parse('2000-02-01 12:13:14')
time_array.should == [2000,2,1,12,13,14,nil,nil]
end
-
- it "should return date array from date string when type is datetime" do
- time_array = parser._parse('2000-02-01', :datetime)
- time_array.should == [2000,2,1,nil,nil,nil,nil,nil]
- end
-
- it "should return datetime array from datetime string when type is date" do
- time_array = parser._parse('2000-02-01 12:13:14', :date)
- time_array.should == [2000,2,1,12,13,14,nil,nil]
- end
end
- context "with no type" do
+ context "with type" do
it "should return date array from date string" do
- time_array = parser._parse('2000-02-01')
+ time_array = parser._parse('2000-02-01', :date)
time_array.should == [2000,2,1,nil,nil,nil,nil,nil]
end
+ it "should not return time array from time string for :date type" do
+ time_array = parser._parse('12:13:14', :date)
+ time_array.should == nil
+ end
+
it "should return time array from time string" do
time_array = parser._parse('12:13:14', :time)
time_array.should == [nil,nil,nil,12,13,14,nil,nil]
end
- it "should return datetime array from datetime string" do
- time_array = parser._parse('2000-02-01 12:13:14')
+ it "should not return date array from date string for :time type" do
+ time_array = parser._parse('2000-02-01', :time)
+ time_array.should == nil
+ end
+
+ it "should return datetime array from datetime string when type is date" do
+ time_array = parser._parse('2000-02-01 12:13:14', :date)
time_array.should == [2000,2,1,12,13,14,nil,nil]
end
it "should return date array from date string when type is datetime" do
- time_array = parser._parse('2000-02-01')
+ time_array = parser._parse('2000-02-01', :datetime)
time_array.should == [2000,2,1,nil,nil,nil,nil,nil]
end
- it "should return datetime array from datetime string when type is date" do
- time_array = parser._parse('2000-02-01 12:13:14')
- time_array.should == [2000,2,1,12,13,14,nil,nil]
+ it "should not return time array from time string when type is datetime" do
+ time_array = parser._parse('12:13:14', :datetime)
+ time_array.should == nil
end
end
context "with :strict => true" do
it "should return nil from date string when type is datetime" do
@@ -218,17 +301,10 @@
time_array = parser._parse('2000-02-01', :strict => true)
time_array.should_not be_nil
end
end
- it "should return nil if time hour is out of range for AM meridian" do
- time_array = parser._parse('13:14 am', :time)
- time_array.should == nil
- time_array = parser._parse('00:14 am', :time)
- time_array.should == nil
- end
-
context "with :format option" do
it "should return values if string matches specified format" do
time_array = parser._parse('2000-02-01 12:13:14', :datetime, :format => 'yyyy-mm-dd hh:nn:ss')
time_array.should == [2000,2,1,12,13,14,nil,nil]
end
@@ -325,13 +401,10 @@
end
end
end
describe "current_date" do
- before(:all) do
- Timecop.freeze(2010,1,1,0,0,0)
- end
context "with no options" do
it 'should return date_for_time_type values with no options' do
current_date.should == Timeliness.date_for_time_type
end
@@ -369,11 +442,11 @@
time = Time.use_zone('London') { Time.current }
date_array = [time.year, time.month, time.day]
current_date(:zone => 'London').should == date_array
end
end
+ end
- after(:all) do
- Timecop.return
- end
+ after(:all) do
+ Timecop.return
end
end