@api private Provides the implementation for `has_<predicate>`. Not intended to be instantiated directly.
# File lib/rspec/matchers/built_in/has.rb, line 8 def initialize(method_name, *args, &block) @method_name, @args, @block = method_name, args, block end
@api private @return [String]
# File lib/rspec/matchers/built_in/has.rb, line 40 def description [method_description, args_description].compact.join(' ') end
@private
# File lib/rspec/matchers/built_in/has.rb, line 20 def does_not_match?(actual, &block) @actual = actual @block ||= block predicate_accessible? && !predicate_matches? end
@api private @return [String]
# File lib/rspec/matchers/built_in/has.rb, line 28 def failure_message validity_message || "expected ##{predicate}#{failure_message_args_description} to return true, got false" end
@api private @return [String]
# File lib/rspec/matchers/built_in/has.rb, line 34 def failure_message_when_negated validity_message || "expected ##{predicate}#{failure_message_args_description} to return false, got true" end
@private
# File lib/rspec/matchers/built_in/has.rb, line 13 def matches?(actual, &block) @actual = actual @block ||= block predicate_accessible? && predicate_matches? end
# File lib/rspec/matchers/built_in/has.rb, line 83 def args_description return nil if @args.empty? @args.map { |arg| RSpec::Support::ObjectFormatter.format(arg) }.join(', ') end
# File lib/rspec/matchers/built_in/has.rb, line 88 def failure_message_args_description desc = args_description "(#{desc})" if desc end
# File lib/rspec/matchers/built_in/has.rb, line 79 def method_description @method_name.to_s.gsub('_', ' ') end
# File lib/rspec/matchers/built_in/has.rb, line 71 def predicate # On 1.9, there appears to be a bug where String#match can return `false` # rather than the match data object. Changing to Regex#match appears to # work around this bug. For an example of this bug, see: # https://travis-ci.org/rspec/rspec-expectations/jobs/27549635 @predicate ||= :"has_#{Matchers::HAS_REGEX.match(@method_name.to_s).captures.first}?" end
# File lib/rspec/matchers/built_in/has.rb, line 46 def predicate_accessible? !private_predicate? && predicate_exists? end
# File lib/rspec/matchers/built_in/has.rb, line 63 def predicate_exists? @actual.respond_to? predicate end
# File lib/rspec/matchers/built_in/has.rb, line 67 def predicate_matches? @actual.__send__(predicate, *@args, &@block) end
:nocov:
# File lib/rspec/matchers/built_in/has.rb, line 53 def private_predicate? @actual.private_methods.include? predicate.to_s end
# File lib/rspec/matchers/built_in/has.rb, line 93 def validity_message if private_predicate? "expected #{@actual} to respond to `#{predicate}` but `#{predicate}` is a private method" elsif !predicate_exists? "expected #{@actual} to respond to `#{predicate}`" end end