added specs for matcher and fixed up bugs

This commit is contained in:
Adam Meehan
2008-07-26 00:16:17 +10:00
parent ed165d3f9d
commit 79460dc39b
3 changed files with 163 additions and 23 deletions

View File

@@ -2,40 +2,52 @@ module Spec
module Rails
module Matchers
class ValidateTimeliness
attr_reader :actual, :expected, :record, :options, :last_failure
def initialize(attribute, options)
@expected, @options = attribute, options
@options.reverse_merge!(error_messages)
end
def error_messages
messages = ActiveRecord::Base.send(:timeliness_default_error_messages)
messages = messages.inject({}) {|h , (k, v)| h[k] = v.sub('%s', '') }
@options.reverse_merge!(messages)
end
def matches?(record)
@record = record
type = options[:type]
conversion_method = case type
when :time then :to_dummy_time
when :date then :to_date
when :datetime then :to_time
end
test_values = {
:date => {:pass => '2000-01-01', :fail => '2000-01-32'},
:time => {:pass => '12:00', :fail => '25:00'},
:datetime => {:pass => '2000-01-01 00:00:00', :fail => '2000-01-32 00:00:00'}
}
valid = error_matching('2000-01-32 00:00:00', /#{options[:invalid_datetime_message]}/) &&
error_matching('2000-01-01 25:00:00', /#{options[:invalid_datetime_message]}/) &&
no_error_matching('2000-01-01 00:00:00', /#{options[:invalid_datetime_message]}/)
valid = error_matching(test_values[type][:fail], /#{options["invalid_#{type}_message".to_sym]}/) &&
no_error_matching(test_values[type][:pass], /#{options["invalid_#{type}_message".to_sym]}/)
if valid && after = options[:after]
if valid && options[:after]
after = parse(options[:after], type).send(conversion_method)
valid = error_matching(after, /#{options[:after_message]}/) &&
no_error_matching(after + 1, /#{options[:after_message]}/)
end
if valid && before = options[:before]
valid = error_matching(before, /#{options[:after_message]}/) &&
no_error_matching(before - 1, /#{options[:after_message]}/)
if valid && options[:before]
before = parse(options[:before], type).send(conversion_method)
valid = error_matching(before, /#{options[:before_message]}/) &&
no_error_matching(before - 1, /#{options[:before_message]}/)
end
if valid && on_or_after = options[:on_or_after]
if valid && options[:on_or_after]
on_or_after = parse(options[:on_or_after], type).send(conversion_method)
valid = error_matching(on_or_after -1, /#{options[:on_or_after_message]}/) &&
no_error_matching(on_or_after, /#{options[:on_or_after_message]}/)
end
if valid && on_or_before = options[:on_or_before]
if valid && options[:on_or_before]
on_or_before = parse(options[:on_or_before], type).send(conversion_method)
valid = error_matching(on_or_before + 1, /#{options[:on_or_before_message]}/) &&
no_error_matching(on_or_before, /#{options[:on_or_before_message]}/)
end
@@ -56,7 +68,16 @@ module Spec
end
private
attr_reader :actual, :expected, :record, :options, :last_failure
def parse(value, type)
ActiveRecord::Base.parse_date_time(value, type)
end
def error_messages
messages = ActiveRecord::Base.send(:timeliness_default_error_messages)
messages = messages.inject({}) {|h, (k, v)| h[k] = v.sub('%s', ''); h }
@options.reverse_merge!(messages)
end
def error_matching(value, match)
record.send("#{expected}=", value)
@@ -80,14 +101,26 @@ module Spec
pass
end
end
def validate_timeliness_of(attribute, options={})
ValidateTimeliness.new(attribute, options)
def validate_date(attribute, options={})
options[:type] = :date
validate_timeliness_of(attribute, options)
end
alias validate_date validate_timeliness_of
alias validate_time validate_timeliness_of
alias validate_datetime validate_timeliness_of
def validate_time(attribute, options={})
options[:type] = :time
validate_timeliness_of(attribute, options)
end
def validate_datetime(attribute, options={})
options[:type] = :datetime
validate_timeliness_of(attribute, options)
end
private
def validate_timeliness_of(attribute, options={})
ValidateTimeliness.new(attribute, options)
end
end
end
end