Class: Crspec::Matchers::ChangeMatcher

Inherits:
BaseMatcher show all
Defined in:
lib/crspec/matchers.rb

Instance Attribute Summary

Attributes inherited from BaseMatcher

#actual, #expected

Instance Method Summary collapse

Methods inherited from BaseMatcher

#failure_message_when_negated

Constructor Details

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

Returns a new instance of ChangeMatcher.



172
173
174
175
176
177
178
179
180
# File 'lib/crspec/matchers.rb', line 172

def initialize(receiver = nil, message = nil, &block)
  super()
  @receiver = receiver
  @message = message
  @block = block || -> { receiver.send(message) }
  @expected_by = nil
  @expected_from = nil
  @expected_to = nil
end

Instance Method Details

#by(amount) ⇒ Object



182
183
184
185
# File 'lib/crspec/matchers.rb', line 182

def by(amount)
  @expected_by = amount
  self
end

#failure_messageObject



213
214
215
216
217
218
219
# File 'lib/crspec/matchers.rb', line 213

def failure_message
  if @expected_by
    "Expected result to change by #{@expected_by}, but changed by #{@after_val - @before_val} (from #{@before_val.inspect} to #{@after_val.inspect})"
  else
    "Expected result to change, but remained #{@before_val.inspect}"
  end
end

#from(val) ⇒ Object



187
188
189
190
# File 'lib/crspec/matchers.rb', line 187

def from(val)
  @expected_from = val
  self
end

#matches?(proc_to_run) ⇒ Boolean

Returns:

  • (Boolean)


197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/crspec/matchers.rb', line 197

def matches?(proc_to_run)
  @before_val = @block.call
  proc_to_run.call
  @after_val = @block.call

  if @expected_by
    (@after_val - @before_val) == @expected_by
  elsif !@expected_from.nil? && !@expected_to.nil?
    @before_val == @expected_from && @after_val == @expected_to
  elsif !@expected_to.nil?
    @after_val == @expected_to
  else
    @before_val != @after_val
  end
end

#to(val) ⇒ Object



192
193
194
195
# File 'lib/crspec/matchers.rb', line 192

def to(val)
  @expected_to = val
  self
end