Class: RSpec::Matchers::ChangeCollection::Change

Inherits:
BuiltIn::Change
  • Object
show all
Defined in:
lib/rspec/change_collection.rb

Instance Method Summary collapse

Constructor Details

#initialize(receiver = nil, message = nil, &block) ⇒ Change

Returns a new instance of Change.



8
9
10
11
12
13
14
15
16
17
18
19
# 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

Instance Method Details

#failure_messageObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# 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_negatedObject



92
93
94
95
# 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?Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 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) ⇒ Object



104
105
106
107
108
109
# 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) ⇒ Object



97
98
99
100
101
102
# 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