thoughtbot--shoulda-matchers/lib/shoulda/matchers/active_model/allow_value_matcher.rb

139 lines
3.8 KiB
Ruby
Raw Normal View History

2010-12-15 22:34:19 +00:00
module Shoulda # :nodoc:
module Matchers
module ActiveModel # :nodoc:
2010-12-15 22:34:19 +00:00
# Ensures that the attribute can be set to the given value or values. If
# multiple values are given the match succeeds only if all given values
# are allowed. Otherwise, the matcher fails at the first bad value in the
# argument list (the remaining arguments are not processed then).
2010-12-15 22:34:19 +00:00
#
# Options:
# * <tt>with_message</tt> - value the test expects to find in
# <tt>errors.on(:attribute)</tt>. Regexp or string. If omitted,
# the test looks for any errors in <tt>errors.on(:attribute)</tt>.
#
# Example:
# it { should_not allow_value('bad').for(:isbn) }
# it { should allow_value("isbn 1 2345 6789 0").for(:isbn) }
#
def allow_value(*values)
2012-03-23 14:10:08 +00:00
if values.empty?
raise ArgumentError, "need at least one argument"
else
AllowValueMatcher.new(*values)
end
2010-12-15 22:34:19 +00:00
end
class AllowValueMatcher # :nodoc:
include Helpers
def initialize(*values)
@values_to_match = values
2012-05-07 14:25:46 +00:00
@options = {}
2010-12-15 22:34:19 +00:00
end
def for(attribute)
@attribute = attribute
self
end
def with_message(message)
2012-05-07 14:25:46 +00:00
@options[:expected_message] = message
2010-12-15 22:34:19 +00:00
self
end
def matches?(instance)
@instance = instance
2012-03-23 19:10:33 +00:00
@values_to_match.none? do |value|
@value = value
@instance.send("#{@attribute}=", @value)
2012-03-23 19:10:33 +00:00
errors_match?
end
2010-12-15 22:34:19 +00:00
end
def failure_message
"Did not expect #{expectation}, got error: #{@matched_error}"
end
def negative_failure_message
"Expected #{expectation}, got #{error_description}"
end
def description
2012-03-23 14:10:08 +00:00
"allow #{@attribute} to be set to #{allowed_values}"
2010-12-15 22:34:19 +00:00
end
private
def errors_match?
2012-03-23 14:10:08 +00:00
if @instance.valid?
2010-09-05 15:23:09 +00:00
false
2012-03-23 14:10:08 +00:00
else
if expected_message
2012-05-07 14:25:46 +00:00
@matched_error = errors_match_regexp? || errors_match_string?
2012-03-23 14:10:08 +00:00
else
errors_for_attribute.compact.any?
end
2010-09-05 15:23:09 +00:00
end
2010-12-15 22:34:19 +00:00
end
2012-03-23 14:10:08 +00:00
def errors_for_attribute
if @instance.errors.respond_to?(:[])
errors = @instance.errors[@attribute]
2010-12-15 22:34:19 +00:00
else
2012-03-23 14:10:08 +00:00
errors = @instance.errors.on(@attribute)
2010-12-15 22:34:19 +00:00
end
2012-03-23 14:10:08 +00:00
Array.wrap(errors)
2010-12-15 22:34:19 +00:00
end
def errors_match_regexp?
2012-03-23 14:10:08 +00:00
if Regexp === expected_message
2012-05-07 14:25:46 +00:00
errors_for_attribute.detect { |e| e =~ expected_message }
2010-12-15 22:34:19 +00:00
end
end
def errors_match_string?
2012-03-23 14:10:08 +00:00
if errors_for_attribute.include?(expected_message)
2012-05-07 14:25:46 +00:00
expected_message
2010-12-15 22:34:19 +00:00
end
end
def expectation
2012-03-23 14:10:08 +00:00
includes_expected_message = expected_message ? "to include #{expected_message.inspect}" : ''
["errors", includes_expected_message, "when #{@attribute} is set to #{@value.inspect}"].join(' ')
2010-12-15 22:34:19 +00:00
end
def error_description
if @instance.errors.empty?
"no errors"
else
"errors: #{pretty_error_messages(@instance)}"
end
end
2012-03-23 14:10:08 +00:00
def allowed_values
if @values_to_match.length > 1
"any of [#{@values_to_match.map(&:inspect).join(', ')}]"
else
@values_to_match.first.inspect
end
end
def expected_message
2012-05-07 14:25:46 +00:00
if @options.key?(:expected_message)
if Symbol === @options[:expected_message]
default_error_message(@options[:expected_message], :model_name => model_name, :attribute => @attribute)
2012-03-23 14:10:08 +00:00
else
2012-05-07 14:25:46 +00:00
@options[:expected_message]
2012-03-23 14:10:08 +00:00
end
end
end
def model_name
@instance.class.to_s.underscore
end
end
2010-12-15 22:34:19 +00:00
end
end
end