Skip to content

Commit

Permalink
add equal_to option to matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
adzap committed Apr 10, 2009
1 parent e1d23d0 commit f041524
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
Expand Up @@ -10,6 +10,7 @@ class ValidateTimeliness
}

OPTION_TEST_SETTINGS = {
:equal_to => { :method => :+, :modify_on => :invalid },
:before => { :method => :-, :modify_on => :valid },
:after => { :method => :+, :modify_on => :valid },
:on_or_before => { :method => :+, :modify_on => :invalid },
Expand All @@ -27,9 +28,9 @@ def matches?(record)

valid = test_validity

valid = test_option(:equal_to) if @options[:equal_to] && valid
valid = test_option(:before) if @options[:before] && valid
valid = test_option(:after) if @options[:after] && valid

valid = test_option(:on_or_before) if @options[:on_or_before] && valid
valid = test_option(:on_or_after) if @options[:on_or_after] && valid

Expand Down
26 changes: 26 additions & 0 deletions spec/spec/rails/matchers/validate_timeliness_spec.rb
Expand Up @@ -5,20 +5,23 @@ class NoValidation < Person

class WithValidation < Person
validates_date :birth_date,
:equal_to => '2000-01-01',
:before => '2000-01-10',
:after => '2000-01-01',
:on_or_before => '2000-01-09',
:on_or_after => '2000-01-02',
:between => ['2000-01-01', '2000-01-03']

validates_time :birth_time,
:equal_to => '09:00',
:before => '23:00',
:after => '09:00',
:on_or_before => '22:00',
:on_or_after => '10:00',
:between => ['09:00', '17:00']

validates_datetime :birth_date_and_time,
:equal_to => '2000-01-01 09:00',
:before => '2000-01-10 23:00',
:after => '2000-01-01 09:00',
:on_or_before => '2000-01-09 23:00',
Expand Down Expand Up @@ -61,6 +64,29 @@ class CustomMessages < Person
end
end

describe "with equal_to 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|

it "should report that #{type} is validated" do
with_validation.should self.send("validate_#{type}", attribute_for_type(type), :equal_to => 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}", attribute_for_type(type), :equal_to => test_values[type][1])
end

it "should report that #{type} is not validated with option" do
no_validation.should_not self.send("validate_#{type}", attribute_for_type(type), :equal_to => test_values[type][0])
end
end
end

describe "with before option" do
test_values = {
:date => ['2000-01-10', '2000-01-11'],
Expand Down

0 comments on commit f041524

Please sign in to comment.