Class: Spec::Matchers::Change

Inherits:
Object
  • Object
show all
Defined in:
lib/spec/matchers/change.rb

Overview

Based on patch from Wilson Bilkovich

Instance Method Summary collapse

Constructor Details

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

:nodoc:



6
7
8
9
10
# File 'lib/spec/matchers/change.rb', line 6

def initialize(receiver=nil, message=nil, &block)
  @receiver = receiver
  @message = message
  @block = block
end

Instance Method Details

#actual_deltaObject



49
50
51
# File 'lib/spec/matchers/change.rb', line 49

def actual_delta
  @after - @before
end

#by(amount) ⇒ Object



57
58
59
60
# File 'lib/spec/matchers/change.rb', line 57

def by(amount)
  @amount = amount
  self
end

#execute_changeObject



27
28
29
30
31
# File 'lib/spec/matchers/change.rb', line 27

def execute_change
  @before = @block.nil? ? @receiver.send(@message) : @block.call
  @target.call
  @after = @block.nil? ? @receiver.send(@message) : @block.call
end

#failure_messageObject



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/spec/matchers/change.rb', line 33

def failure_message
  if @to
    "#{result} should have been changed to #{@to.inspect}, but is now #{@after.inspect}"
  elsif @from
    "#{result} should have initially been #{@from.inspect}, but was #{@before.inspect}"
  elsif @amount
    "#{result} should have been changed by #{@amount.inspect}, but was changed by #{actual_delta.inspect}"
  else
    "#{result} should have changed, but is still #{@before.inspect}"
  end
end

#from(from) ⇒ Object



67
68
69
70
# File 'lib/spec/matchers/change.rb', line 67

def from (from)
  @from = from
  self
end

#matches?(target, &block) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/spec/matchers/change.rb', line 12

def matches?(target, &block)
  if block
    raise MatcherError.new(<<-EOF
block passed to should or should_not change must use {} instead of do/end
EOF
)
  end
  @target = target
  execute_change
  return false if @from && (@from != @before)
  return false if @to && (@to != @after)
  return (@before + @amount == @after) if @amount
  return @before != @after
end

#negative_failure_messageObject



53
54
55
# File 'lib/spec/matchers/change.rb', line 53

def negative_failure_message
  "#{result} should not have changed, but did change from #{@before.inspect} to #{@after.inspect}"
end

#resultObject



45
46
47
# File 'lib/spec/matchers/change.rb', line 45

def result
  @message || "result"
end

#to(to) ⇒ Object



62
63
64
65
# File 'lib/spec/matchers/change.rb', line 62

def to(to)
  @to = to
  self
end