spec/timeliness/parser_spec.rb in timeliness-0.1.1 vs spec/timeliness/parser_spec.rb in timeliness-0.2.0
- old
+ new
@@ -69,15 +69,40 @@
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
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')
+ }.should raise_error(Timeliness::Parser::MissingTimezoneSupport)
+ end
+ end
end
describe "for time type" do
- it 'should use date from date_for_time_type' do
- parse('12:13:14', :time).should == Time.local(2000,1,1,12,13,14)
+ context "with date from date_for_time_type" do
+ before do
+ @original = Timeliness.date_for_time_type
+ end
+
+ it 'should return date array' do
+ Timeliness.date_for_time_type = [2010,1,1]
+ parse('12:13:14', :time).should == Time.local(2010,1,1,12,13,14)
+ end
+
+ it 'should return date array evaluated lambda' do
+ Timeliness.date_for_time_type = lambda { Time.local(2010,2,1) }
+ parse('12:13:14', :time).should == Time.local(2010,2,1,12,13,14)
+ end
+
+ after do
+ Timeliness.date_for_time_type = @original
+ end
end
context "with :now option" do
it 'should use date from :now' do
parse('12:13:14', :time, :now => Time.local(2010, 6, 1)).should == Time.local(2010,6,1,12,13,14)
@@ -105,58 +130,58 @@
context "_parse" do
context "with type" do
it "should return date array from date string" do
time_array = parser._parse('2000-02-01', :date)
- time_array.should == [2000,2,1,nil,nil,nil,nil]
+ 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]
+ 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.should == [2000,2,1,12,13,14,nil]
+ 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]
+ 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]
+ time_array.should == [2000,2,1,12,13,14,nil,nil]
end
end
context "with no type" do
it "should return date array from date string" do
time_array = parser._parse('2000-02-01')
- time_array.should == [2000,2,1,nil,nil,nil,nil]
+ 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]
+ 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')
- time_array.should == [2000,2,1,12,13,14,nil]
+ 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.should == [2000,2,1,nil,nil,nil,nil]
+ 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]
+ time_array.should == [2000,2,1,12,13,14,nil,nil]
end
end
context "with :strict => true" do
it "should return nil from date string when type is datetime" do
@@ -203,11 +228,11 @@
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]
+ time_array.should == [2000,2,1,12,13,14,nil,nil]
end
it "should return nil if string does not match specified format" do
time_array = parser._parse('2000-02-01 12:13', :datetime, :format => 'yyyy-mm-dd hh:nn:ss')
time_array.should be_nil
@@ -215,24 +240,24 @@
end
context "date with ambiguous year" do
it "should return year in current century if year below threshold" do
time_array = parser._parse('01-02-29', :date)
- time_array.should == [2029,2,1,nil,nil,nil,nil]
+ time_array.should == [2029,2,1,nil,nil,nil,nil,nil]
end
it "should return year in last century if year at or above threshold" do
time_array = parser._parse('01-02-30', :date)
- time_array.should == [1930,2,1,nil,nil,nil,nil]
+ time_array.should == [1930,2,1,nil,nil,nil,nil,nil]
end
it "should allow custom threshold" do
default = Timeliness.ambiguous_year_threshold
Timeliness.ambiguous_year_threshold = 40
time_array = parser._parse('01-02-39', :date)
- time_array.should == [2039,2,1,nil,nil,nil,nil]
+ time_array.should == [2039,2,1,nil,nil,nil,nil,nil]
time_array = parser._parse('01-02-40', :date)
- time_array.should == [1940,2,1,nil,nil,nil,nil]
+ time_array.should == [1940,2,1,nil,nil,nil,nil,nil]
Timeliness.ambiguous_year_threshold = default
end
end
end