spec/formats_spec.rb in validates_timeliness-2.1.0 vs spec/formats_spec.rb in validates_timeliness-2.2.0

- old
+ new

@@ -1,13 +1,8 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper') describe ValidatesTimeliness::Formats do - attr_reader :formats - - before do - @formats = ValidatesTimeliness::Formats - end describe "format proc generator" do it "should generate proc which outputs date array with values in correct order" do generate_proc('yyyy-mm-dd').call('2000', '1', '2').should == [2000,1,2,0,0,0,0] end @@ -102,11 +97,11 @@ describe "parse" do it "should return time array from date string" do time_array = formats.parse('12:13:14', :time, :strict => true) - time_array.should == [0,0,0,12,13,14,0] + time_array.should == [2000,1,1,12,13,14,0] end it "should return date array from time string" do time_array = formats.parse('2000-02-01', :date, :strict => true) time_array.should == [2000,2,1,0,0,0,0] @@ -132,11 +127,11 @@ time_array.should == [2000,2,1,0,0,0,0] end it "should ignore date when extracting time and strict is false" do time_array = formats.parse('2000-02-01 12:13', :time, :strict => false) - time_array.should == [0,0,0,12,13,0,0] + time_array.should == [2000,1,1,12,13,0,0] end it "should return zone offset when :include_offset options is true" do time_array = formats.parse('2000-02-01T12:13:14-10:30', :datetime, :include_offset => true) time_array.should == [2000,2,1,12,13,14,0,-37800] @@ -175,10 +170,26 @@ time_array.should == [1940,2,1,0,0,0,0] ValidatesTimeliness::Formats.ambiguous_year_threshold = default end end + describe "parse with custom dummy date values" do + before(:all) do + @old_dummy_date = formats.dummy_date_for_time_type + formats.dummy_date_for_time_type = [2009,1,1] + end + + it "should return time array with custom dummy date" do + time_array = formats.parse('12:13:14', :time, :strict => true) + time_array.should == [2009,1,1,12,13,14,0] + end + + after(:all) do + formats.dummy_date_for_time_type = @old_dummy_date + end + end + describe "removing formats" do it "should remove format from format array" do formats.remove_formats(:time, 'h.nn_ampm') formats.time_formats.should_not include("h o'clock") end @@ -217,11 +228,11 @@ it "should add format before specified format and be higher precedence" do formats.add_formats(:time, "ss:hh:nn", :before => 'hh:nn:ss') validate("59:23:58", :time).should be_true time_array = formats.parse('59:23:58', :time) - time_array.should == [0,0,0,23,58,59,0] + time_array.should == [2000,1,1,23,58,59,0] end it "should raise error if format exists" do lambda { formats.add_formats(:time, "hh:nn:ss") }.should raise_error() end @@ -249,9 +260,14 @@ after do # reload class end end + + def formats + ValidatesTimeliness::Formats + end + def validate(time_string, type) valid = false formats.send("#{type}_expressions").each do |format, regexp, processor| valid = true and break if /\A#{regexp}\Z/ =~ time_string end