class RSpec::Matchers::BuiltIn::Include

Experimental RBS support (using type sampling data from the type_fusion project).

# sig/rspec/matchers/built_in/include.rbs

class RSpec::Matchers::BuiltIn::Include < RSpec::Matchers::BuiltIn::BaseMatcher
  def actual_collection_includes?: (Class expected_item) -> true
  def check_actual?: (Array[Class] actual) -> true
  def check_expected_count?: () -> false
  def comparing_hash_keys?: (Class expected_item) -> false
  def comparing_hash_to_a_subset?: (Class expected_item) -> false
  def convert_to_hash?: (Array[Class] obj) -> false
  def excluded_from_actual: () -> untyped
  def initialize: (*Array[Class] expecteds) -> void
  def matches?: (Array[Class] actual) -> true
  def perform_match: () -> true
end

rubocop:disable Metrics/ClassLength
Not intended to be instantiated directly.
Provides the implementation for ‘include`.
@api private

def actual_collection_includes?(expected_item)

Experimental RBS support (using type sampling data from the type_fusion project).

def actual_collection_includes?: (Class expected_item) -> true

This signature was generated using 6 samples from 1 application.

def actual_collection_includes?(expected_item)
  return true if actual.include?(expected_item)
  # String lacks an `any?` method...
  return false unless actual.respond_to?(:any?)
  actual.any? { |value| values_match?(expected_item, value) }
end

def actual_hash_has_key?(expected_key)

def actual_hash_has_key?(expected_key)
  # We check `key?` first for perf:
  # `key?` is O(1), but `any?` is O(N).
  has_exact_key =
    begin
      actual.key?(expected_key)
    rescue
      false
    end
  has_exact_key || actual.keys.any? { |key| values_match?(expected_key, key) }
end

def actual_hash_includes?(expected_key, expected_value)

def actual_hash_includes?(expected_key, expected_value)
  actual_value =
    actual.fetch(expected_key) do
      actual.find(Proc.new { return false }) { |actual_key, _| values_match?(expected_key, actual_key) }[1]
    end
  values_match?(expected_value, actual_value)
end

def check_actual?(actual)

Experimental RBS support (using type sampling data from the type_fusion project).

def check_actual?: (Class actual) -> true

This signature was generated using 7 samples from 1 application.

def check_actual?(actual)
  actual = actual.to_hash if convert_to_hash?(actual)
  @actual = actual
  @actual.respond_to?(:include?)
end

def check_expected_count?

Experimental RBS support (using type sampling data from the type_fusion project).

def check_expected_count?: () -> false

This signature was generated using 4 samples from 1 application.

def check_expected_count?
  case
  when !has_expected_count?
    return false
  when expecteds.size != 1
    raise NotImplementedError, 'Count constraint supported only when testing for a single value being included'
  when actual.is_a?(Hash)
    raise NotImplementedError, 'Count constraint on hash keys not implemented'
  end
  true
end

def comparing_hash_keys?(expected_item)

Experimental RBS support (using type sampling data from the type_fusion project).

def comparing_hash_keys?: (Class expected_item) -> false

This signature was generated using 7 samples from 1 application.

def comparing_hash_keys?(expected_item)
  actual.is_a?(Hash) && !expected_item.is_a?(Hash)
end

def comparing_hash_to_a_subset?(expected_item)

Experimental RBS support (using type sampling data from the type_fusion project).

def comparing_hash_to_a_subset?: (Class expected_item) -> false

This signature was generated using 9 samples from 1 application.

def comparing_hash_to_a_subset?(expected_item)
  actual.is_a?(Hash) && expected_item.is_a?(Hash)
end

def convert_to_hash?(obj)

Experimental RBS support (using type sampling data from the type_fusion project).

def convert_to_hash?: (Class obj) -> false

This signature was generated using 6 samples from 1 application.

def convert_to_hash?(obj)
  !obj.respond_to?(:include?) && obj.respond_to?(:to_hash)
end

def count_enumerable(expected_item)

def count_enumerable(expected_item)
  actual.select { |value| values_match?(expected_item, value) }.size
end

def count_enumerable(expected_item)

def count_enumerable(expected_item)
  actual.count { |value| values_match?(expected_item, value) }
end

def count_inclusions

def count_inclusions
  @divergent_items = expected
  case actual
  when String
    actual.scan(expected.first).length
  when Enumerable
    count_enumerable(Hash === expected ? expected : expected.first)
  else
    raise NotImplementedError, 'Count constraints are implemented for Enumerable and String values only'
  end
end

def description

Returns:
  • (String) -

Other tags:
    Api: - private
def description
  improve_hash_formatting("include#{readable_list_of(expecteds)}#{count_expectation_description}")
end

def diff_would_wrongly_highlight_matched_item?

def diff_would_wrongly_highlight_matched_item?
  return false unless actual.is_a?(String) && expected.is_a?(Array)
  lines = actual.split("\n")
  expected.any? do |str|
    actual.include?(str) && lines.none? { |line| line == str }
  end
end

def diffable?

Returns:
  • (Boolean) -

Other tags:
    Api: - private
def diffable?
  !diff_would_wrongly_highlight_matched_item?
end

def does_not_match?(actual)

Returns:
  • (Boolean) -

Other tags:
    Api: - private
def does_not_match?(actual)
  check_actual?(actual) &&
    if check_expected_count?
      !expected_count_matches?(count_inclusions)
    else
      perform_match { |v| !v }
    end
end

def excluded_from_actual

Experimental RBS support (using type sampling data from the type_fusion project).

def excluded_from_actual: () -> untyped

This signature was generated using 3 samples from 1 application.

def excluded_from_actual
  return [] unless @actual.respond_to?(:include?)
  expecteds.inject([]) do |memo, expected_item|
    if comparing_hash_to_a_subset?(expected_item)
      expected_item.each do |(key, value)|
        memo << { key => value } unless yield actual_hash_includes?(key, value)
      end
    elsif comparing_hash_keys?(expected_item)
      memo << expected_item unless yield actual_hash_has_key?(expected_item)
    else
      memo << expected_item unless yield actual_collection_includes?(expected_item)
    end
    memo
  end
end

def expected

Returns:
  • (Array, Hash) -

Other tags:
    Api: - private
def expected
  if expecteds.one? && Hash === expecteds.first
    expecteds.first
  else
    expecteds
  end
end

def failure_message

Returns:
  • (String) -

Other tags:
    Api: - private
def failure_message
  format_failure_message("to") { super }
end

def failure_message_when_negated

Returns:
  • (String) -

Other tags:
    Api: - private
def failure_message_when_negated
  format_failure_message("not to") { super }
end

def format_failure_message(preposition)

def format_failure_message(preposition)
  msg = if actual.respond_to?(:include?)
          "expected #{description_of @actual} #{preposition}" \
          " include#{readable_list_of @divergent_items}" \
          "#{count_failure_reason('it is included') if has_expected_count?}"
        else
          "#{yield}, but it does not respond to `include?`"
        end
  improve_hash_formatting(msg)
end

def initialize(*expecteds)

Experimental RBS support (using type sampling data from the type_fusion project).

def initialize: (* expecteds) -> void

This signature was generated using 3 samples from 1 application.

Other tags:
    Api: - private
def initialize(*expecteds)
  @expecteds = expecteds
end

def matches?(actual)

Experimental RBS support (using type sampling data from the type_fusion project).

def matches?: (Class actual) -> true

This signature was generated using 4 samples from 1 application.

Returns:
  • (Boolean) -

Other tags:
    Api: - private
def matches?(actual)
  check_actual?(actual) &&
    if check_expected_count?
      expected_count_matches?(count_inclusions)
    else
      perform_match { |v| v }
    end
end

def perform_match(&block)

Experimental RBS support (using type sampling data from the type_fusion project).

def perform_match: () -> true

This signature was generated using 4 samples from 1 application.

def perform_match(&block)
  @divergent_items = excluded_from_actual(&block)
  @divergent_items.empty?
end

def readable_list_of(items)

def readable_list_of(items)
  described_items = surface_descriptions_in(items)
  if described_items.all? { |item| item.is_a?(Hash) }
    " #{described_items.inject(:merge).inspect}"
  else
    EnglishPhrasing.list(described_items)
  end
end