Class: Minicrest::RespondsTo
- Defined in:
- lib/minicrest/responds_to.rb
Overview
Matcher that checks if a value responds to specified methods.
Uses Ruby’s respond_to? method to check method availability.
Instance Method Summary collapse
-
#description ⇒ String
Returns a description of what this matcher expects.
-
#failure_message(actual) ⇒ String
Returns the failure message when the match fails.
-
#initialize(*methods) ⇒ RespondsTo
constructor
Creates a new responds_to matcher.
-
#matches?(actual) ⇒ Boolean
Checks if actual responds to all specified methods.
-
#negated_failure_message(actual) ⇒ String
Returns the failure message when a negated match fails.
Methods inherited from Matcher
Constructor Details
#initialize(*methods) ⇒ RespondsTo
Creates a new responds_to matcher.
17 18 19 20 |
# File 'lib/minicrest/responds_to.rb', line 17 def initialize(*methods) super() @methods = methods.flatten end |
Instance Method Details
#description ⇒ String
Returns a description of what this matcher expects.
33 34 35 |
# File 'lib/minicrest/responds_to.rb', line 33 def description "respond to #{@methods.map(&:inspect).join(', ')}" end |
#failure_message(actual) ⇒ String
Returns the failure message when the match fails.
41 42 43 44 45 46 47 48 |
# File 'lib/minicrest/responds_to.rb', line 41 def (actual) missing = @methods.reject { |method| actual.respond_to?(method) } <<~MSG.chomp expected #{actual.inspect} to respond to #{@methods.map(&:inspect).join(', ')} missing: #{missing.map(&:inspect).join(', ')} MSG end |
#matches?(actual) ⇒ Boolean
Checks if actual responds to all specified methods.
26 27 28 |
# File 'lib/minicrest/responds_to.rb', line 26 def matches?(actual) @methods.all? { |method| actual.respond_to?(method) } end |
#negated_failure_message(actual) ⇒ String
Returns the failure message when a negated match fails.
54 55 56 57 58 59 60 |
# File 'lib/minicrest/responds_to.rb', line 54 def (actual) <<~MSG.chomp expected #{actual.inspect} not to respond to #{@methods.map(&:inspect).join(', ')} but it does MSG end |