From 7fd251dddfadc2d786345a0e3ddd9eea1c0a0897 Mon Sep 17 00:00:00 2001 From: Adam Meehan Date: Sat, 26 Jul 2008 07:49:26 +1000 Subject: [PATCH] added rest of specs for matcher --- .../validate_timeliness_matcher.rb | 4 + spec/validate_timeliness_matcher_spec.rb | 148 +++++++++++++----- 2 files changed, 114 insertions(+), 38 deletions(-) diff --git a/lib/validates_timeliness/validate_timeliness_matcher.rb b/lib/validates_timeliness/validate_timeliness_matcher.rb index 7524b51..2f80c33 100644 --- a/lib/validates_timeliness/validate_timeliness_matcher.rb +++ b/lib/validates_timeliness/validate_timeliness_matcher.rb @@ -100,6 +100,10 @@ module Spec @last_failure = "value #{value} to not have error matching #{match.inspect}" unless pass pass end + + def format_value(value) + value.strftime(ActiveRecord::Errors.date_time_error_value_formats[options[:type]]) + end end def validate_date(attribute, options={}) diff --git a/spec/validate_timeliness_matcher_spec.rb b/spec/validate_timeliness_matcher_spec.rb index 8922d58..2c90d4d 100644 --- a/spec/validate_timeliness_matcher_spec.rb +++ b/spec/validate_timeliness_matcher_spec.rb @@ -1,32 +1,38 @@ require File.dirname(__FILE__) + '/spec_helper' -describe Spec::Rails::Matchers, "ValidateTimeliness matcher" do +describe "ValidateTimeliness matcher" do attr_accessor :no_validation, :with_validation before do - class Person < ActiveRecord::Base + class NoValidation < Person alias_attribute :birth_datetime, :birth_date_and_time end - class PersonWithValidations < Person - validates_date :birth_date, :before => '2000-01-10', :after => '2000-01-01' - validates_time :birth_time, :before => '23:00', :after => '09:00' - validates_datetime :birth_date_and_time, :before => '2000-01-10 23:00', :after => '2000-01-01 09:00' + class WithValidation < Person + validates_date :birth_date, + :before => '2000-01-10', :after => '2000-01-01', + :on_or_before => '2000-01-09', :on_or_after => '2000-01-02' + validates_time :birth_time, + :before => '23:00', :after => '09:00', + :on_or_before => '22:00', :on_or_after => '10:00' + validates_datetime :birth_date_and_time, + :before => '2000-01-10 23:00', :after => '2000-01-01 09:00', + :on_or_before => '2000-01-09 23:00', :on_or_after => '2000-01-02 09:00' alias_attribute :birth_datetime, :birth_date_and_time end - @no_validation = Person.new - @with_validation = PersonWithValidations.new + @no_validation = NoValidation.new + @with_validation = WithValidation.new end [:date, :time, :datetime].each do |type| attribute = type == :datetime ? :date_and_time : type - it "should correctly report that #{type} is validated" do + it "should report that #{type} is validated" do with_validation.should self.send("validate_#{type}", "birth_#{attribute}".to_sym) end - it "should correctly report that #{type} is not validated" do + it "should report that #{type} is not validated" do no_validation.should_not self.send("validate_#{type}", "birth_#{attribute}".to_sym) end end @@ -41,15 +47,15 @@ describe Spec::Rails::Matchers, "ValidateTimeliness matcher" do [:date, :time, :datetime].each do |type| attribute = type == :datetime ? :date_and_time : type - it "should correctly report that #{type} is validated" do + it "should report that #{type} is validated" do with_validation.should self.send("validate_#{type}", "birth_#{attribute}", :before => test_values[type][0]) end - it "should correctly report that #{type} is not validated when option value is incorrect" do + it "should report that #{type} is not validated when option value is incorrect" do with_validation.should_not self.send("validate_#{type}", "birth_#{attribute}", :before => test_values[type][1]) end - it "should correctly report that #{type} is not validated with option" do + it "should report that #{type} is not validated with option" do no_validation.should_not self.send("validate_#{type}", "birth_#{attribute}", :before => test_values[type][0]) end end @@ -65,41 +71,107 @@ describe Spec::Rails::Matchers, "ValidateTimeliness matcher" do [:date, :time, :datetime].each do |type| attribute = type == :datetime ? :date_and_time : type - it "should correctly report that #{type} is validated" do + it "should report that #{type} is validated" do with_validation.should self.send("validate_#{type}", "birth_#{attribute}", :after => test_values[type][0]) end - it "should correctly report that #{type} is not validated when option value is incorrect" do + it "should report that #{type} is not validated when option value is incorrect" do with_validation.should_not self.send("validate_#{type}", "birth_#{attribute}", :after => test_values[type][1]) end - it "should correctly report that #{type} is not validated with option" do + it "should report that #{type} is not validated with option" do no_validation.should_not self.send("validate_#{type}", "birth_#{attribute}", :after => test_values[type][0]) end end end -# describe "with on_or_before option" do -# test_values = { -# :date => ['2000-01-01', '2000-01-02'], -# :time => ['09:00', '09:01'], -# :datetime => ['2000-01-01 09:00', '2000-01-01 09:01'] -# } -# -# [:date, :time, :datetime].each do |type| -# attribute = type == :datetime ? :date_and_time : type + describe "with on_or_before option" do + test_values = { + :date => ['2000-01-09', '2000-01-08'], + :time => ['22:00', '21:59'], + :datetime => ['2000-01-09 23:00', '2000-01-09 22:59'] + } + + [:date, :time, :datetime].each do |type| + attribute = type == :datetime ? :date_and_time : type -# it "should correctly report that #{type} is validated" do -# with_validation.should self.send("validate_#{type}", "birth_#{attribute}", :after => test_values[type][0]) -# end -# -# it "should correctly report that #{type} is not validated when option value is incorrect" do -# with_validation.should_not self.send("validate_#{type}", "birth_#{attribute}", :after => test_values[type][1]) -# end -# -# it "should correctly report that #{type} is not validated with option" do -# no_validation.should_not self.send("validate_#{type}", "birth_#{attribute}", :after => test_values[type][0]) -# end -# end -# end + it "should report that #{type} is validated" do + with_validation.should self.send("validate_#{type}", "birth_#{attribute}", :on_or_before => test_values[type][0]) + end + + it "should report that #{type} is not validated when option value is incorrect" do + with_validation.should_not self.send("validate_#{type}", "birth_#{attribute}", :on_or_before => test_values[type][1]) + end + + it "should report that #{type} is not validated with option" do + no_validation.should_not self.send("validate_#{type}", "birth_#{attribute}", :on_or_before => test_values[type][0]) + end + end + end + + describe "with on_or_after option" do + test_values = { + :date => ['2000-01-02', '2000-01-03'], + :time => ['10:00', '10:01'], + :datetime => ['2000-01-02 09:00', '2000-01-02 09:01'] + } + + [:date, :time, :datetime].each do |type| + attribute = type == :datetime ? :date_and_time : type + + it "should report that #{type} is validated" do + with_validation.should self.send("validate_#{type}", "birth_#{attribute}", :on_or_after => test_values[type][0]) + end + + it "should report that #{type} is not validated when option value is incorrect" do + with_validation.should_not self.send("validate_#{type}", "birth_#{attribute}", :on_or_after => test_values[type][1]) + end + + it "should report that #{type} is not validated with option" do + no_validation.should_not self.send("validate_#{type}", "birth_#{attribute}", :on_or_after => test_values[type][0]) + end + end + end + + describe "custom messages" do + before do + class CustomMessages < Person + validates_date :birth_date, :invalid_date_message => 'is not really a date', + :before => '2000-01-10', :before_message => 'is too late', + :after => '2000-01-01', :after_message => 'is too early', + :on_or_before=> '2000-01-09', :on_or_before_message => 'is just too late', + :on_or_after => '2000-01-02', :on_or_after_message => 'is just too early' + end + @person = CustomMessages.new + end + + it "should match error message for invalid" do + @person.should validate_date(:birth_date, :invalid_date_message => 'is not really a date') + end + + it "should match error message for before option" do + @person.should validate_date(:birth_date, :before => '2000-01-10', + :invalid_date_message => 'is not really a date', + :before_message => 'is too late') + end + + it "should match error message for after option" do + @person.should validate_date(:birth_date, :after => '2000-01-01', + :invalid_date_message => 'is not really a date', + :after_message => 'is too early') + end + + it "should match error message for on_or_before option" do + @person.should validate_date(:birth_date, :on_or_before => '2000-01-09', + :invalid_date_message => 'is not really a date', + :on_or_before_message => 'is just too late') + end + + it "should match error message for on_or_after option" do + @person.should validate_date(:birth_date, :on_or_after => '2000-01-02', + :invalid_date_message => 'is not really a date', + :on_or_after_message => 'is just too early') + end + + end end