Class: Spec::Matchers::Change
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 |
# File 'lib/spec/matchers/change.rb', line 6 def initialize(receiver=nil, =nil, &block) @message = || "result" @value_proc = block || lambda {receiver.__send__()} @to = @from = @minimum = @maximum = @amount = nil end |
Instance Method Details
#actual_delta ⇒ Object
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 |
#description ⇒ Object
87 88 89 |
# File 'lib/spec/matchers/change.rb', line 87 def description "change ##{@message}" end |
#evaluate_value_proc ⇒ Object
34 35 36 |
# File 'lib/spec/matchers/change.rb', line 34 def evaluate_value_proc @value_proc.call end |
#failure_message_for_should ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/spec/matchers/change.rb', line 38 def 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_not ⇒ Object
58 59 60 |
# File 'lib/spec/matchers/change.rb', line 58 def "#{@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
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_error ⇒ Object
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 |