Class: Minicrest::Includes

Inherits:
Matcher
  • Object
show all
Defined in:
lib/minicrest/includes.rb

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

Examples:

With strings

includes('hello', 'world').matches?('hello world')  # => true

With arrays

includes(1, 3).matches?([1, 2, 3])  # => true

With hashes

includes(a: 1).matches?({a: 1, b: 2})  # => true

Instance Method Summary collapse

Methods inherited from Matcher

#&, #|

Constructor Details

#initialize(*items) ⇒ Includes

Creates a new includes matcher.

Parameters:

  • items (Array)

    the items that must be included



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

#descriptionString

Returns a description of what this matcher expects.

Returns:

  • (String)

    description



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.

Parameters:

  • actual (Object)

    the value that was checked

Returns:

  • (String)

    failure message listing missing items



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/minicrest/includes.rb', line 59

def failure_message(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.

Parameters:

  • actual (Object)

    the value to check

Returns:

  • (Boolean)

    true if all items are included



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.

Parameters:

  • actual (Object)

    the value that was checked

Returns:

  • (String)

    message indicating unexpected inclusion



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/minicrest/includes.rb', line 78

def negated_failure_message(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