Class: Minicrest::HasValue
- Defined in:
- lib/minicrest/has_value.rb
Overview
Matcher that checks if a hash contains specified values.
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(*values) ⇒ HasValue
constructor
Creates a new has_value matcher.
-
#matches?(actual) ⇒ Boolean
Checks if actual hash contains all expected values.
-
#negated_failure_message(actual) ⇒ String
Returns the failure message when a negated match fails.
Methods inherited from Matcher
Constructor Details
#initialize(*values) ⇒ HasValue
Creates a new has_value matcher.
15 16 17 18 |
# File 'lib/minicrest/has_value.rb', line 15 def initialize(*values) super() @values = values end |
Instance Method Details
#description ⇒ String
Returns a description of what this matcher expects.
35 36 37 38 39 40 41 |
# File 'lib/minicrest/has_value.rb', line 35 def description if @values.length == 1 "has value #{@values.first.inspect}" else "has values #{@values.map(&:inspect).join(', ')}" end end |
#failure_message(actual) ⇒ String
Returns the failure message when the match fails.
47 48 49 50 51 52 53 54 55 |
# File 'lib/minicrest/has_value.rb', line 47 def (actual) return "expected a Hash, but got #{actual.inspect}" unless actual.respond_to?(:value?) missing = @values.reject { |value| actual.value?(value) } <<~MSG.chomp expected #{format_hash(actual)} to have values #{@values.map(&:inspect).join(', ')} missing: #{missing.map(&:inspect).join(', ')} MSG end |
#matches?(actual) ⇒ Boolean
Checks if actual hash contains all expected values.
24 25 26 27 28 29 30 |
# File 'lib/minicrest/has_value.rb', line 24 def matches?(actual) return false unless actual.respond_to?(:value?) @values.all? { |value| actual.value?(value) } rescue NoMethodError false end |
#negated_failure_message(actual) ⇒ String
Returns the failure message when a negated match fails.
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/minicrest/has_value.rb', line 61 def (actual) if @values.length == 1 <<~MSG.chomp expected #{format_hash(actual)} not to have value #{@values.first.inspect}, but it did MSG else <<~MSG.chomp expected #{format_hash(actual)} not to have values #{@values.map(&:inspect).join(', ')}, but it did MSG end end |