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
# 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)}
end

Instance Method Details

#actual_deltaObject



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

def actual_delta
  @after - @before
end

#by(amount) ⇒ Object



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

def by(amount)
  @amount = amount
  self
end

#by_at_least(minimum) ⇒ Object



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

def by_at_least(minimum)
  @minimum = minimum
  self
end

#by_at_most(maximum) ⇒ Object



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

def by_at_most(maximum)
  @maximum = maximum
  self
end

#descriptionObject



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

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

#evaluate_value_procObject



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

def evaluate_value_proc
  @value_proc.call
end

#failure_message_for_shouldObject



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

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



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

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

#from(from) ⇒ Object



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

def from (from)
  @from = from
  self
end

#matches?(event_proc) ⇒ Boolean



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

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:



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

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



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

def to(to)
  @to = to
  self
end