Class: Minicrest::Includes
Overview
Matcher that checks if a value includes specified items.
Works with: - Strings: checks for substrings - Arrays: checks for elements - Hashes: checks for key-value pairs
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(*items) ⇒ Includes
constructor
Creates a new includes matcher.
-
#matches?(actual) ⇒ Boolean
Checks if actual includes all expected items.
-
#negated_failure_message(actual) ⇒ String
Returns the failure message when a negated match fails.
Methods inherited from Matcher
Constructor Details
#initialize(*items) ⇒ Includes
Creates a new includes matcher.
23 24 25 26 27 28 |
# File 'lib/minicrest/includes.rb', line 23 def initialize(*items) super() @items = items @is_hash = items.length == 1 && items.first.is_a?(Hash) @expected_hash = @is_hash ? items.first : nil end |
Instance Method Details
#description ⇒ String
Returns a description of what this matcher expects.
47 48 49 50 51 52 53 |
# File 'lib/minicrest/includes.rb', line 47 def description if @is_hash "includes #{format_hash(@expected_hash)}" else "includes #{@items.map(&:inspect).join(', ')}" end end |
#failure_message(actual) ⇒ String
Returns the failure message when the match fails.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/minicrest/includes.rb', line 59 def (actual) missing = find_missing(actual) if @is_hash <<~MSG.chomp expected #{format_value(actual)} to include #{format_hash(@expected_hash)} missing: #{format_hash(missing)} MSG else <<~MSG.chomp expected #{format_value(actual)} to include #{@items.map(&:inspect).join(', ')} missing: #{missing.map(&:inspect).join(', ')} MSG end end |
#matches?(actual) ⇒ Boolean
Checks if actual includes all expected items.
34 35 36 37 38 39 40 41 42 |
# File 'lib/minicrest/includes.rb', line 34 def matches?(actual) if @is_hash && actual.is_a?(Hash) @expected_hash.all? { |k, v| actual[k] == v } elsif actual.is_a?(Hash) @items.all? { |item| item.is_a?(Hash) ? item.all? { |k, v| actual[k] == v } : actual.include?(item) } else @items.all? { |item| actual.include?(item) } end end |
#negated_failure_message(actual) ⇒ String
Returns the failure message when a negated match fails.
78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/minicrest/includes.rb', line 78 def (actual) if @is_hash <<~MSG.chomp expected #{format_value(actual)} not to include #{format_hash(@expected_hash)}, but it did MSG else <<~MSG.chomp expected #{format_value(actual)} not to include #{@items.map(&:inspect).join(', ')}, but it did MSG end end |