Class: RSpec::Matchers::Change

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/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
11
# File 'lib/rspec/matchers/change.rb', line 6

def initialize(receiver=nil, message=nil, &block)
  @message = message
  @value_proc = block || lambda {receiver.__send__(message)}
  @to = @from = @minimum = @maximum = @amount = nil
  @given_from = @given_to = false
end

Instance Method Details

#actual_deltaObject



50
51
52
# File 'lib/rspec/matchers/change.rb', line 50

def actual_delta
  @after - @before
end

#by(amount) ⇒ Object



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

def by(amount)
  @amount = amount
  self
end

#by_at_least(minimum) ⇒ Object



63
64
65
66
# File 'lib/rspec/matchers/change.rb', line 63

def by_at_least(minimum)
  @minimum = minimum
  self
end

#by_at_most(maximum) ⇒ Object



68
69
70
71
# File 'lib/rspec/matchers/change.rb', line 68

def by_at_most(maximum)
  @maximum = maximum
  self
end

#descriptionObject



85
86
87
# File 'lib/rspec/matchers/change.rb', line 85

def description
  "change ##{message}"
end

#evaluate_value_procObject



30
31
32
# File 'lib/rspec/matchers/change.rb', line 30

def evaluate_value_proc
  @value_proc.call
end

#failure_message_for_shouldObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rspec/matchers/change.rb', line 34

def failure_message_for_should
  if @given_from && @before != @from
    "#{message} should have initially been #{@from.inspect}, but was #{@before.inspect}"
  elsif @given_to && @to != @after
    "#{message} should have been changed to #{@to.inspect}, but is now #{@after.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



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

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

#from(from) ⇒ Object



79
80
81
82
83
# File 'lib/rspec/matchers/change.rb', line 79

def from (from)
  @given_from = true
  @from = from
  self
end

#matches?(event_proc) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
16
17
18
19
20
21
# File 'lib/rspec/matchers/change.rb', line 13

def matches?(event_proc)
  raise_block_syntax_error if block_given?
  
  @before = evaluate_value_proc
  event_proc.call
  @after = evaluate_value_proc

  (!change_expected? || changed?) && matches_before? && matches_after? && matches_amount? && matches_min? && matches_max?
end

#raise_block_syntax_errorObject

Raises:



23
24
25
26
27
28
# File 'lib/rspec/matchers/change.rb', line 23

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



73
74
75
76
77
# File 'lib/rspec/matchers/change.rb', line 73

def to(to)
  @given_to = true
  @to = to
  self
end