Class: Minicrest::ContainsExactly
- Defined in:
- lib/minicrest/contains_exactly.rb
Overview
Matcher that checks if an array contains exactly the specified items in the specified order.
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) ⇒ ContainsExactly
constructor
Creates a new contains_exactly matcher.
-
#matches?(actual) ⇒ Boolean
Checks if actual contains exactly the expected items in order.
-
#negated_failure_message(actual) ⇒ String
Returns the failure message when a negated match fails.
Methods inherited from Matcher
Constructor Details
#initialize(*items) ⇒ ContainsExactly
Creates a new contains_exactly matcher.
14 15 16 17 |
# File 'lib/minicrest/contains_exactly.rb', line 14 def initialize(*items) super() @items = items end |
Instance Method Details
#description ⇒ String
Returns a description of what this matcher expects.
30 31 32 |
# File 'lib/minicrest/contains_exactly.rb', line 30 def description "contains exactly #{@items.inspect} (in order)" end |
#failure_message(actual) ⇒ String
Returns the failure message when the match fails.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/minicrest/contains_exactly.rb', line 38 def (actual) actual_array = actual.to_a lines = ["expected #{actual.inspect} to contain exactly #{@items.inspect} (in order)"] if actual_array.length == @items.length @items.each_with_index do |expected_item, index| actual_item = actual_array[index] if expected_item != actual_item lines << "at index #{index}: expected #{expected_item.inspect}, got #{actual_item.inspect}" end end else lines << "expected size #{@items.length}, got #{actual_array.length}" end lines.join("\n") end |
#matches?(actual) ⇒ Boolean
Checks if actual contains exactly the expected items in order.
23 24 25 |
# File 'lib/minicrest/contains_exactly.rb', line 23 def matches?(actual) actual.to_a == @items end |
#negated_failure_message(actual) ⇒ String
Returns the failure message when a negated match fails.
60 61 62 63 64 |
# File 'lib/minicrest/contains_exactly.rb', line 60 def (actual) <<~MSG.chomp expected #{actual.inspect} not to contain exactly #{@items.inspect} (in order), but it did MSG end |