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

#and, #failure_message_when_negated, #or

Constructor Details

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

Returns a new instance of ChangeMatcher.



252
253
254
255
256
257
258
259
260
# File 'lib/crspec/matchers.rb', line 252

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



262
263
264
265
# File 'lib/crspec/matchers.rb', line 262

def by(amount)
  @expected_by = amount
  self
end

#failure_messageObject



293
294
295
296
297
298
299
# File 'lib/crspec/matchers.rb', line 293

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



267
268
269
270
# File 'lib/crspec/matchers.rb', line 267

def from(val)
  @expected_from = val
  self
end

#matches?(proc_to_run) ⇒ Boolean

Returns:

  • (Boolean)


277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/crspec/matchers.rb', line 277

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



272
273
274
275
# File 'lib/crspec/matchers.rb', line 272

def to(val)
  @expected_to = val
  self
end