class RSpec::Matchers::BuiltIn::BePredicate
@api private Provides the implementation of `be_<predicate>`. Not intended to be instantiated directly.
Public Class Methods
new(*args, &block)
click to toggle source
# File lib/rspec/matchers/built_in/be.rb, line 181 def initialize(*args, &block) @expected = parse_expected(args.shift) @args = args @block = block end
Public Instance Methods
description()
click to toggle source
@api private @return [String]
# File lib/rspec/matchers/built_in/be.rb, line 213 def description "#{prefix_to_sentence}#{expected_to_sentence}#{args_to_sentence}" end
does_not_match?(actual, &block)
click to toggle source
# File lib/rspec/matchers/built_in/be.rb, line 193 def does_not_match?(actual, &block) @actual = actual @block ||= block predicate_accessible? && !predicate_matches? end
failure_message()
click to toggle source
@api private @return [String]
# File lib/rspec/matchers/built_in/be.rb, line 201 def failure_message failure_message_expecting(true) end
failure_message_when_negated()
click to toggle source
@api private @return [String]
# File lib/rspec/matchers/built_in/be.rb, line 207 def failure_message_when_negated failure_message_expecting(false) end
matches?(actual, &block)
click to toggle source
# File lib/rspec/matchers/built_in/be.rb, line 187 def matches?(actual, &block) @actual = actual @block ||= block predicate_accessible? && predicate_matches? end
Private Instance Methods
failure_message_expecting(value)
click to toggle source
# File lib/rspec/matchers/built_in/be.rb, line 262 def failure_message_expecting(value) validity_message || "expected `#{actual_formatted}.#{predicate}#{args_to_s}` to return #{value}, got #{description_of @predicate_matches}" end
parse_expected(expected)
click to toggle source
# File lib/rspec/matchers/built_in/be.rb, line 249 def parse_expected(expected) @prefix, expected = prefix_and_expected(expected) expected end
predicate()
click to toggle source
# File lib/rspec/matchers/built_in/be.rb, line 241 def predicate :"#{@expected}?" end
predicate_accessible?()
click to toggle source
# File lib/rspec/matchers/built_in/be.rb, line 219 def predicate_accessible? actual.respond_to?(predicate) || actual.respond_to?(present_tense_predicate) end
predicate_matches?()
click to toggle source
# File lib/rspec/matchers/built_in/be.rb, line 236 def predicate_matches? method_name = actual.respond_to?(predicate) ? predicate : present_tense_predicate @predicate_matches = actual.__send__(method_name, *@args, &@block) end
prefix_and_expected(symbol)
click to toggle source
# File lib/rspec/matchers/built_in/be.rb, line 254 def prefix_and_expected(symbol) Matchers::BE_PREDICATE_REGEX.match(symbol.to_s).captures.compact end
prefix_to_sentence()
click to toggle source
# File lib/rspec/matchers/built_in/be.rb, line 258 def prefix_to_sentence EnglishPhrasing.split_words(@prefix) end
present_tense_predicate()
click to toggle source
# File lib/rspec/matchers/built_in/be.rb, line 245 def present_tense_predicate :"#{@expected}s?" end
private_predicate?()
click to toggle source
:nocov:
# File lib/rspec/matchers/built_in/be.rb, line 226 def private_predicate? @actual.private_methods.include? predicate.to_s end
validity_message()
click to toggle source
# File lib/rspec/matchers/built_in/be.rb, line 267 def validity_message return nil if predicate_accessible? msg = "expected #{@actual} to respond to `#{predicate}`" if private_predicate? msg << " but `#{predicate}` is a private method" elsif predicate == :true? msg << " or perhaps you meant `be true` or `be_truthy`" elsif predicate == :false? msg << " or perhaps you meant `be false` or `be_falsey`" end msg end