Class: RSpec::Matchers::Change
- Inherits:
-
Object
- Object
- RSpec::Matchers::Change
- Defined in:
- lib/rspec/matchers/change.rb
Instance Method Summary collapse
- #actual_delta ⇒ Object
- #by(expected_delta) ⇒ 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(before) ⇒ Object
-
#initialize(receiver = nil, message = nil, &block) ⇒ Change
constructor
A new instance of Change.
- #matches?(event_proc) ⇒ Boolean
- #raise_block_syntax_error ⇒ Object
- #to(to) ⇒ Object
Constructor Details
#initialize(receiver = nil, message = nil, &block) ⇒ Change
Returns a new instance of Change.
4 5 6 7 8 9 |
# File 'lib/rspec/matchers/change.rb', line 4 def initialize(receiver=nil, =nil, &block) @message = @value_proc = block || lambda {receiver.__send__()} @expected_after = @expected_before = @minimum = @maximum = @expected_delta = nil @eval_before = @eval_after = false end |
Instance Method Details
#actual_delta ⇒ Object
53 54 55 |
# File 'lib/rspec/matchers/change.rb', line 53 def actual_delta @actual_after - @actual_before end |
#by(expected_delta) ⇒ Object
61 62 63 64 |
# File 'lib/rspec/matchers/change.rb', line 61 def by(expected_delta) @expected_delta = expected_delta self end |
#by_at_least(minimum) ⇒ Object
66 67 68 69 |
# File 'lib/rspec/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/rspec/matchers/change.rb', line 71 def by_at_most(maximum) @maximum = maximum self end |
#description ⇒ Object
88 89 90 |
# File 'lib/rspec/matchers/change.rb', line 88 def description "change ##{}" end |
#evaluate_value_proc ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/rspec/matchers/change.rb', line 28 def evaluate_value_proc case val = @value_proc.call when Enumerable val.dup else val end end |
#failure_message_for_should ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/rspec/matchers/change.rb', line 37 def if @eval_before && !expected_matches_actual?(@expected_before, @actual_before) "#{} should have initially been #{@expected_before.inspect}, but was #{@actual_before.inspect}" elsif @eval_after && !expected_matches_actual?(@expected_after, @actual_after) "#{} should have been changed to #{@expected_after.inspect}, but is now #{@actual_after.inspect}" elsif @expected_delta "#{} should have been changed by #{@expected_delta.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 #{@actual_before.inspect}" end end |
#failure_message_for_should_not ⇒ Object
57 58 59 |
# File 'lib/rspec/matchers/change.rb', line 57 def "#{} should not have changed, but did change from #{@actual_before.inspect} to #{@actual_after.inspect}" end |
#from(before) ⇒ Object
82 83 84 85 86 |
# File 'lib/rspec/matchers/change.rb', line 82 def from (before) @eval_before = true @expected_before = before self end |
#matches?(event_proc) ⇒ Boolean
11 12 13 14 15 16 17 18 19 |
# File 'lib/rspec/matchers/change.rb', line 11 def matches?(event_proc) raise_block_syntax_error if block_given? @actual_before = evaluate_value_proc event_proc.call @actual_after = evaluate_value_proc (!change_expected? || changed?) && matches_before? && matches_after? && matches_expected_delta? && matches_min? && matches_max? end |
#raise_block_syntax_error ⇒ Object
21 22 23 24 25 26 |
# File 'lib/rspec/matchers/change.rb', line 21 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 80 |
# File 'lib/rspec/matchers/change.rb', line 76 def to(to) @eval_after = true @expected_after = to self end |