spec/validator_spec.rb in validates_timeliness-1.1.7 vs spec/validator_spec.rb in validates_timeliness-2.0.0

- old
+ new

@@ -64,21 +64,21 @@ evaluate_option_value([time1, time2], :datetime).should == [time2, time1] end it "should return array of Time objects when restriction is array of strings" do time1, time2 = "2000-01-02", "2000-01-01" - evaluate_option_value([time1, time2], :datetime).should == [Person.parse_date_time(time2, :datetime), Person.parse_date_time(time1, :datetime)] + evaluate_option_value([time1, time2], :datetime).should == [parse(time2, :datetime), parse(time1, :datetime)] end it "should return array of Time objects when restriction is Range of Time objects" do time1, time2 = Time.now, 1.day.ago evaluate_option_value(time1..time2, :datetime).should == [time2, time1] end it "should return array of Time objects when restriction is Range of time strings" do time1, time2 = "2000-01-02", "2000-01-01" - evaluate_option_value(time1..time2, :datetime).should == [Person.parse_date_time(time2, :datetime), Person.parse_date_time(time1, :datetime)] + evaluate_option_value(time1..time2, :datetime).should == [parse(time2, :datetime), parse(time1, :datetime)] end def evaluate_option_value(restriction, type) configure_validator(:type => type) validator.class.send(:evaluate_option_value, restriction, type, person) end @@ -471,10 +471,24 @@ validate_with(:birth_time, "12:00") should_have_no_error(:birth_time, :on_or_before) end end + describe "instance with format option" do + it "should validate attribute when value matches format" do + configure_validator(:type => :time, :format => 'hh:nn:ss') + validate_with(:birth_time, "12:00:00") + should_have_no_error(:birth_time, :invalid_time) + end + + it "should not validate attribute when value does not match format" do + configure_validator(:type => :time, :format => 'hh:nn:ss') + validate_with(:birth_time, "12:00") + should_have_error(:birth_time, :invalid_time) + end + end + describe "custom_error_messages" do it "should return hash of custom error messages from configuration with _message truncated from keys" do configure_validator(:type => :date, :invalid_date_message => 'thats no date') validator.send(:custom_error_messages)[:invalid_date].should == 'thats no date' end @@ -552,16 +566,22 @@ end describe "custom formats" do before :all do - @@formats = ValidatesTimeliness::Validator.error_value_formats - ValidatesTimeliness::Validator.error_value_formats = { + custom = { :time => '%H:%M %p', :date => '%d-%m-%Y', :datetime => '%d-%m-%Y %H:%M %p' } + + if defined?(I18n) + I18n.backend.store_translations 'en', :validates_timeliness => { :error_value_formats => custom } + else + @@formats = ValidatesTimeliness::Validator.error_value_formats + ValidatesTimeliness::Validator.error_value_formats = custom + end end it "should format datetime value of restriction" do configure_validator(:type => :datetime, :after => 1.day.from_now) validate_with(:birth_date_and_time, Time.now) @@ -579,13 +599,21 @@ validate_with(:birth_time, '11:59') person.errors.on(:birth_time).should match(/after \d{2}:\d{2} (AM|PM)\Z/) end after :all do - ValidatesTimeliness::Validator.error_value_formats = @@formats + if defined?(I18n) + I18n.reload! + else + ValidatesTimeliness::Validator.error_value_formats = @@formats + end end end + end + + def parse(*args) + ValidatesTimeliness::Parser.parse(*args) end def configure_validator(options={}) @validator = ValidatesTimeliness::Validator.new(options) end