2014-01-23 18:07:36 +00:00
|
|
|
module Shoulda
|
2010-12-15 22:34:19 +00:00
|
|
|
module Matchers
|
2014-01-23 18:07:36 +00:00
|
|
|
module ActiveModel
|
|
|
|
# @private
|
|
|
|
class ValidationMatcher
|
2010-12-15 22:34:19 +00:00
|
|
|
def initialize(attribute)
|
|
|
|
@attribute = attribute
|
2015-12-14 00:01:14 +00:00
|
|
|
@expects_strict = false
|
|
|
|
@subject = nil
|
|
|
|
@last_submatcher_run = nil
|
|
|
|
@expected_message = nil
|
|
|
|
@expects_custom_validation_message = false
|
2012-09-12 00:53:21 +00:00
|
|
|
end
|
|
|
|
|
2013-03-04 03:34:38 +00:00
|
|
|
def on(context)
|
|
|
|
@context = context
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2012-09-12 00:53:21 +00:00
|
|
|
def strict
|
2015-12-14 00:01:14 +00:00
|
|
|
@expects_strict = true
|
2012-09-12 00:53:21 +00:00
|
|
|
self
|
2010-12-15 22:34:19 +00:00
|
|
|
end
|
|
|
|
|
2015-12-14 00:01:14 +00:00
|
|
|
def expects_strict?
|
|
|
|
@expects_strict
|
2010-12-15 22:34:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def matches?(subject)
|
|
|
|
@subject = subject
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2015-12-14 00:01:14 +00:00
|
|
|
def with_message(expected_message)
|
|
|
|
if expected_message
|
|
|
|
@expects_custom_validation_message = true
|
|
|
|
@expected_message = expected_message
|
2010-12-15 22:34:19 +00:00
|
|
|
end
|
2015-12-14 00:01:14 +00:00
|
|
|
|
|
|
|
self
|
2010-12-15 22:34:19 +00:00
|
|
|
end
|
|
|
|
|
2015-12-14 00:01:14 +00:00
|
|
|
def expects_custom_validation_message?
|
|
|
|
@expects_custom_validation_message
|
2010-12-15 22:34:19 +00:00
|
|
|
end
|
2012-09-12 00:53:21 +00:00
|
|
|
|
2015-12-14 00:01:14 +00:00
|
|
|
def description
|
|
|
|
ValidationMatcher::BuildDescription.call(self, simple_description)
|
|
|
|
end
|
2012-09-12 13:47:20 +00:00
|
|
|
|
2015-12-14 00:01:14 +00:00
|
|
|
def failure_message
|
|
|
|
overall_failure_message.dup.tap do |message|
|
|
|
|
if failure_reason.present?
|
|
|
|
message << "\n"
|
|
|
|
message << Shoulda::Matchers.word_wrap(
|
|
|
|
failure_reason,
|
|
|
|
indent: 2
|
|
|
|
)
|
|
|
|
end
|
2014-06-27 22:39:39 +00:00
|
|
|
end
|
2015-12-14 00:01:14 +00:00
|
|
|
end
|
2014-06-27 22:39:39 +00:00
|
|
|
|
2015-12-14 00:01:14 +00:00
|
|
|
def failure_message_when_negated
|
|
|
|
overall_failure_message_when_negated.dup.tap do |message|
|
|
|
|
if failure_reason_when_negated.present?
|
|
|
|
message << "\n"
|
|
|
|
message << Shoulda::Matchers.word_wrap(
|
|
|
|
failure_reason_when_negated,
|
|
|
|
indent: 2
|
|
|
|
)
|
|
|
|
end
|
2012-09-12 13:47:20 +00:00
|
|
|
end
|
2015-12-14 00:01:14 +00:00
|
|
|
end
|
2014-06-27 22:39:39 +00:00
|
|
|
|
2015-12-14 00:01:14 +00:00
|
|
|
protected
|
|
|
|
|
|
|
|
attr_reader :attribute, :context, :subject, :last_submatcher_run
|
|
|
|
|
|
|
|
def model
|
|
|
|
subject.class
|
2012-09-12 13:47:20 +00:00
|
|
|
end
|
|
|
|
|
2015-12-14 00:01:14 +00:00
|
|
|
def allows_value_of(value, message = nil, &block)
|
|
|
|
matcher = allow_value_matcher(value, message, &block)
|
|
|
|
run_allow_or_disallow_matcher(matcher)
|
|
|
|
end
|
2013-03-27 18:13:46 +00:00
|
|
|
|
2015-12-14 00:01:14 +00:00
|
|
|
def disallows_value_of(value, message = nil, &block)
|
|
|
|
matcher = disallow_value_matcher(value, message, &block)
|
|
|
|
run_allow_or_disallow_matcher(matcher)
|
|
|
|
end
|
2014-06-27 22:39:39 +00:00
|
|
|
|
2015-12-14 00:01:14 +00:00
|
|
|
def allow_value_matcher(value, message = nil, &block)
|
|
|
|
build_allow_or_disallow_value_matcher(
|
|
|
|
matcher_class: AllowValueMatcher,
|
|
|
|
value: value,
|
|
|
|
message: message,
|
|
|
|
&block
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def disallow_value_matcher(value, message = nil, &block)
|
|
|
|
build_allow_or_disallow_value_matcher(
|
|
|
|
matcher_class: DisallowValueMatcher,
|
|
|
|
value: value,
|
|
|
|
message: message,
|
|
|
|
&block
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def overall_failure_message
|
|
|
|
Shoulda::Matchers.word_wrap(
|
|
|
|
"#{model.name} did not properly #{description}."
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def overall_failure_message_when_negated
|
|
|
|
Shoulda::Matchers.word_wrap(
|
|
|
|
"Expected #{model.name} not to #{description}, but it did."
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def failure_reason
|
|
|
|
last_submatcher_run.try(:failure_message)
|
|
|
|
end
|
|
|
|
|
|
|
|
def failure_reason_when_negated
|
|
|
|
last_submatcher_run.try(:failure_message_when_negated)
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_allow_or_disallow_value_matcher(args)
|
|
|
|
matcher_class = args.fetch(:matcher_class)
|
|
|
|
value = args.fetch(:value)
|
|
|
|
message = args[:message]
|
|
|
|
|
|
|
|
matcher = matcher_class.new(value).
|
|
|
|
for(attribute).
|
|
|
|
with_message(message).
|
|
|
|
on(context).
|
|
|
|
strict(expects_strict?)
|
|
|
|
|
|
|
|
yield matcher if block_given?
|
2014-06-27 22:39:39 +00:00
|
|
|
|
|
|
|
matcher
|
2013-03-27 18:13:46 +00:00
|
|
|
end
|
|
|
|
|
2015-12-14 00:01:14 +00:00
|
|
|
def run_allow_or_disallow_matcher(matcher)
|
|
|
|
@last_submatcher_run = matcher
|
|
|
|
matcher.matches?(subject)
|
2012-09-12 00:53:21 +00:00
|
|
|
end
|
2010-12-15 22:34:19 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-03-23 14:50:45 +00:00
|
|
|
end
|