Class: Spec::Matchers::Change
Overview
Based on patch from Wilson Bilkovich
Instance Method Summary collapse
- #actual_delta ⇒ Object
- #by(amount) ⇒ Object
- #execute_change ⇒ Object
- #failure_message ⇒ Object
- #from(from) ⇒ Object
-
#initialize(receiver = nil, message = nil, &block) ⇒ Change
constructor
:nodoc:.
- #matches?(target, &block) ⇒ Boolean
- #negative_failure_message ⇒ Object
- #result ⇒ 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) @receiver = receiver @message = @block = block end |
Instance Method Details
#actual_delta ⇒ Object
49 50 51 |
# File 'lib/spec/matchers/change.rb', line 49 def actual_delta @after - @before end |
#by(amount) ⇒ Object
57 58 59 60 |
# File 'lib/spec/matchers/change.rb', line 57 def by(amount) @amount = amount self end |
#execute_change ⇒ Object
27 28 29 30 31 |
# File 'lib/spec/matchers/change.rb', line 27 def execute_change @before = @block.nil? ? @receiver.send(@message) : @block.call @target.call @after = @block.nil? ? @receiver.send(@message) : @block.call end |
#failure_message ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/spec/matchers/change.rb', line 33 def if @to "#{result} should have been changed to #{@to.inspect}, but is now #{@after.inspect}" elsif @from "#{result} should have initially been #{@from.inspect}, but was #{@before.inspect}" elsif @amount "#{result} should have been changed by #{@amount.inspect}, but was changed by #{actual_delta.inspect}" else "#{result} should have changed, but is still #{@before.inspect}" end end |
#from(from) ⇒ Object
67 68 69 70 |
# File 'lib/spec/matchers/change.rb', line 67 def from (from) @from = from self end |
#matches?(target, &block) ⇒ 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?(target, &block) if block raise MatcherError.new(<<-EOF block passed to should or should_not change must use {} instead of do/end EOF ) end @target = target execute_change return false if @from && (@from != @before) return false if @to && (@to != @after) return (@before + @amount == @after) if @amount return @before != @after end |
#negative_failure_message ⇒ Object
53 54 55 |
# File 'lib/spec/matchers/change.rb', line 53 def "#{result} should not have changed, but did change from #{@before.inspect} to #{@after.inspect}" end |
#result ⇒ Object
45 46 47 |
# File 'lib/spec/matchers/change.rb', line 45 def result @message || "result" end |
#to(to) ⇒ Object
62 63 64 65 |
# File 'lib/spec/matchers/change.rb', line 62 def to(to) @to = to self end |