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)
  @message = message || "result"
  @value_proc = block || lambda {receiver.__send__(message)}
  @to = @from = @minimum = @maximum = @amount = nil
end

Instance Method Details

#actual_deltaObject



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

def actual_delta
  @after - @before
end

#by(amount) ⇒ Object



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

def by(amount)
  @amount = amount
  self
end

#by_at_least(minimum) ⇒ Object



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

def by_at_least(minimum)
  @minimum = minimum
  self
end

#by_at_most(maximum) ⇒ Object



72
73
74
75
# File 'lib/spec/matchers/change.rb', line 72

def by_at_most(maximum)
  @maximum = maximum
  self
end

#descriptionObject



87
88
89
# File 'lib/spec/matchers/change.rb', line 87

def description
  "change ##{@message}"
end

#evaluate_value_procObject



34
35
36
# File 'lib/spec/matchers/change.rb', line 34

def evaluate_value_proc
  @value_proc.call
end

#failure_message_for_shouldObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/spec/matchers/change.rb', line 38

def failure_message_for_should
  if @to
    "#{@message} should have been changed to #{@to.inspect}, but is now #{@after.inspect}"
  elsif @from
    "#{@message} should have initially been #{@from.inspect}, but was #{@before.inspect}"
  elsif @amount
    "#{@message} should have been changed by #{@amount.inspect}, but was changed by #{actual_delta.inspect}"
  elsif @minimum
    "#{@message} should have been changed by at least #{@minimum.inspect}, but was changed by #{actual_delta.inspect}"
  elsif @maximum
    "#{@message} should have been changed by at most #{@maximum.inspect}, but was changed by #{actual_delta.inspect}"
  else
    "#{@message} should have changed, but is still #{@before.inspect}"
  end
end

#failure_message_for_should_notObject



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

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

#from(from) ⇒ Object



82
83
84
85
# File 'lib/spec/matchers/change.rb', line 82

def from (from)
  @from = from
  self
end

#matches?(event_proc) ⇒ 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?(event_proc)
  raise_block_syntax_error if block_given?
  
  @before = evaluate_value_proc
  event_proc.call
  @after = evaluate_value_proc
  
  return (@to = false) if @from unless @from == @before
  return false if @to unless @to == @after
  return (@before + @amount == @after) if @amount
  return ((@after - @before) >= @minimum) if @minimum
  return ((@after - @before) <= @maximum) if @maximum        
  return @before != @after
end

#raise_block_syntax_errorObject

Raises:



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

def raise_block_syntax_error
  raise MatcherError.new(<<-MESSAGE
block passed to should or should_not change must use {} instead of do/end
MESSAGE
  )
end

#to(to) ⇒ Object



77
78
79
80
# File 'lib/spec/matchers/change.rb', line 77

def to(to)
  @to = to
  self
end