Class: Micronaut::Matchers::Change

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

def initialize(receiver=nil, message=nil, &block)
  @message = message || "result"
  @value_proc = block || lambda {
    receiver.__send__(message)
  }
end

Instance Method Details

#actual_deltaObject



55
56
57
# File 'lib/micronaut/matchers/change.rb', line 55

def actual_delta
  @after - @before
end

#by(amount) ⇒ Object



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

def by(amount)
  @amount = amount
  self
end

#by_at_least(minimum) ⇒ Object



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

def by_at_least(minimum)
  @minimum = minimum
  self
end

#by_at_most(maximum) ⇒ Object



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

def by_at_most(maximum)
  @maximum = maximum
  self
end

#evaluate_value_procObject



35
36
37
# File 'lib/micronaut/matchers/change.rb', line 35

def evaluate_value_proc
  @value_proc.call
end

#failure_messageObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/micronaut/matchers/change.rb', line 39

def failure_message
  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

#from(from) ⇒ Object



83
84
85
86
# File 'lib/micronaut/matchers/change.rb', line 83

def from (from)
  @from = from
  self
end

#matches?(event_proc) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/micronaut/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
  
  return 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        
  @before != @after
end

#negative_failure_messageObject



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

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

#raise_block_syntax_errorObject

Raises:



28
29
30
31
32
33
# File 'lib/micronaut/matchers/change.rb', line 28

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



78
79
80
81
# File 'lib/micronaut/matchers/change.rb', line 78

def to(to)
  @to = to
  self
end