Class: RSpec::Matchers::Change
- Inherits:
-
Object
- Object
- RSpec::Matchers::Change
- Defined in:
- lib/rspec/matchers/change.rb
Overview
Based on patch from Wilson Bilkovich
Instance Method Summary collapse
- #actual_delta ⇒ Object
- #by(amount) ⇒ Object
- #by_at_least(minimum) ⇒ Object
- #by_at_most(maximum) ⇒ Object
- #description ⇒ Object
- #evaluate_value_proc ⇒ Object
- #failure_message_for_should ⇒ Object
- #failure_message_for_should_not ⇒ Object
- #from(from) ⇒ Object
-
#initialize(receiver = nil, message = nil, &block) ⇒ Change
constructor
:nodoc:.
- #matches?(event_proc) ⇒ Boolean
- #raise_block_syntax_error ⇒ Object
- #to(to) ⇒ Object
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, =nil, &block) @message = @value_proc = block || lambda {receiver.__send__()} @to = @from = @minimum = @maximum = @amount = nil @given_from = @given_to = false end |
Instance Method Details
#actual_delta ⇒ Object
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 |
#description ⇒ Object
85 86 87 |
# File 'lib/rspec/matchers/change.rb', line 85 def description "change ##{}" end |
#evaluate_value_proc ⇒ Object
30 31 32 |
# File 'lib/rspec/matchers/change.rb', line 30 def evaluate_value_proc @value_proc.call end |
#failure_message_for_should ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/rspec/matchers/change.rb', line 34 def if @given_from && @before != @from "#{} should have initially been #{@from.inspect}, but was #{@before.inspect}" elsif @given_to && @to != @after "#{} should have been changed to #{@to.inspect}, but is now #{@after.inspect}" elsif @amount "#{} should have been changed by #{@amount.inspect}, but was changed by #{actual_delta.inspect}" elsif @minimum "#{} should have been changed by at least #{@minimum.inspect}, but was changed by #{actual_delta.inspect}" elsif @maximum "#{} should have been changed by at most #{@maximum.inspect}, but was changed by #{actual_delta.inspect}" else "#{} should have changed, but is still #{@before.inspect}" end end |
#failure_message_for_should_not ⇒ Object
54 55 56 |
# File 'lib/rspec/matchers/change.rb', line 54 def "#{} 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
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_error ⇒ Object
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 |