class RSpec::Matchers::ChangeCollection::Change

Public Class Methods

new(receiver=nil, message=nil, &block) click to toggle source
Calls superclass method
# File lib/rspec/change_collection.rb, line 8
def initialize(receiver=nil, message=nil, &block)
  @expected_to_include = []
  @expected_to_exclude = []

  @missing_original_items = []
  @extra_original_items = []

  @missing_final_items = []
  @extra_final_items = []

  super
end

Public Instance Methods

failure_message() click to toggle source
Calls superclass method
# File lib/rspec/change_collection.rb, line 49
def failure_message
  if @parent_matches
    message =  ""
  else
    message = super
  end

  if expect_collection_to_change?
    array_messages = []
    append_array_message = lambda do |message, values|
      array_messages << "#{message}#{values.inspect}" if values.length > 0
    end
    append_array_message.call(
      "the original collection should have included:     ",
      @missing_original_items
    )
    append_array_message.call(
      "the original collection should not have included: ",
      @extra_original_items
    )
    append_array_message.call(
      "the final collection should have included:        ",
      @missing_final_items
    )
    append_array_message.call(
      "the final collection should not have included:    ",
      @extra_final_items
    )
    append_array_message.call(
      "the original collection was:                      ",
      @change_details.actual_before
    )
    append_array_message.call(
      "the final collection was:                         ",
      @change_details.actual_after
    )
    message << "\n#{array_messages.join "\n"}\n" unless array_messages.empty?
    message
  end

  message
end
failure_message_when_negated() click to toggle source
Calls superclass method
# File lib/rspec/change_collection.rb, line 92
def failure_message_when_negated
  return "Matcher does not support negation" if expect_collection_to_change?
  super
end
matches?(*) click to toggle source
Calls superclass method
# File lib/rspec/change_collection.rb, line 21
def matches?(*)
  @parent_matches = super

  if expect_collection_to_change?
    actual_before = @change_details.actual_before
    actual_after = @change_details.actual_after

    if actual_before.respond_to?(:to_ary)
      @missing_original_items = extract_items(@expected_to_exclude) - actual_before
      @missing_original_items += reject_procs(actual_before, @expected_to_exclude)
      @extra_original_items = select_items(actual_before, @expected_to_include) & actual_before
    end

    if actual_after.respond_to?(:to_ary)
      @missing_final_items = extract_items(@expected_to_include) - actual_after
      @missing_final_items += reject_procs(actual_after, @expected_to_include)
      @extra_final_items = select_items(actual_after, @expected_to_exclude) & actual_after
    end
  end

  @parent_matches && ![
    @missing_original_items,
    @extra_original_items,
    @missing_final_items,
    @extra_final_items
  ].map(&:empty?).include?(false)
end
to_exclude(*items, &block) click to toggle source
# File lib/rspec/change_collection.rb, line 104
def to_exclude(*items, &block)
  check_arguments(__method__, *items, &block)
  @expected_to_exclude << items
  @expected_to_exclude << block if block
  self
end
to_include(*items, &block) click to toggle source
# File lib/rspec/change_collection.rb, line 97
def to_include(*items, &block)
  check_arguments(__method__, *items, &block)
  @expected_to_include << items
  @expected_to_include << block if block
  self
end

Private Instance Methods

callable?(rule) click to toggle source
# File lib/rspec/change_collection.rb, line 113
def callable?(rule)
  rule.respond_to?(:call)
end
check_arguments(method, *items, &block) click to toggle source
# File lib/rspec/change_collection.rb, line 147
def check_arguments(method, *items, &block)
  if block && !items.empty?
    raise(
      ArgumentError,
      "`#{method}` requires either objects " \
      "(`to_include(obj1, obj2, ...)`) or a block (`to_include { }`) but not both. "
    )
  end
end
expect_collection_to_change?() click to toggle source
# File lib/rspec/change_collection.rb, line 139
def expect_collection_to_change?
  [@expected_to_include, @expected_to_exclude].map(&:empty?).include?(false)
end
extract_items(rules) click to toggle source
# File lib/rspec/change_collection.rb, line 117
def extract_items(rules)
  rules.reject { |rule| callable?(rule) }.flatten(1)
end
item_in_rules?(item, rules) click to toggle source
# File lib/rspec/change_collection.rb, line 129
def item_in_rules?(item, rules)
  rules.any? do |rule|
    if callable?(rule)
      rule.call(item)
    else
      rule.include?(item)
    end
  end
end
reject_procs(items, rules) click to toggle source
# File lib/rspec/change_collection.rb, line 121
def reject_procs(items, rules)
  rules.select { |rule| callable?(rule) }.reject { |proc| items.any? { |item| proc.call(item) }}
end
safe_sort(array) click to toggle source
# File lib/rspec/change_collection.rb, line 143
def safe_sort(array)
  array.sort rescue array
end
select_items(items, rules) click to toggle source
# File lib/rspec/change_collection.rb, line 125
def select_items(items, rules)
  items.select { |item| item_in_rules?(item, rules) }
end