mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-23 06:16:44 +00:00
added rest of specs for matcher
This commit is contained in:
parent
766e0f3bac
commit
7fd251dddf
@ -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={})
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user