Class: RSpec::Matchers::BuiltIn::Change

Inherits:
Object
  • Object
show all
Includes:
MatchAliases
Defined in:
lib/rspec/matchers/built_in/change.rb

Instance Method Summary (collapse)

Methods included from MatchAliases

#==, #===

Constructor Details

- (Change) initialize(receiver = nil, message = nil)

A new instance of Change

7
8
9
10
11
12
# File 'lib/rspec/matchers/built_in/change.rb', line 7
def initialize(receiver=nil, message=nil, &block)
  @message = message
  @value_proc = block || lambda {receiver.__send__(message)}
  @expected_after = @expected_before = @minimum = @maximum = @expected_delta = nil
  @eval_before = @eval_after = false
end

Instance Method Details

- (Object) actual_delta

88
89
90
# File 'lib/rspec/matchers/built_in/change.rb', line 88
def actual_delta
  @actual_after - @actual_before
end

- (Object) by(expected_delta)

96
97
98
99
# File 'lib/rspec/matchers/built_in/change.rb', line 96
def by(expected_delta)
  @expected_delta = expected_delta
  self
end

- (Object) by_at_least(minimum)

101
102
103
104
# File 'lib/rspec/matchers/built_in/change.rb', line 101
def by_at_least(minimum)
  @minimum = minimum
  self
end

- (Object) by_at_most(maximum)

106
107
108
109
# File 'lib/rspec/matchers/built_in/change.rb', line 106
def by_at_most(maximum)
  @maximum = maximum
  self
end

- (Object) description

123
124
125
# File 'lib/rspec/matchers/built_in/change.rb', line 123
def description
  "change ##{message}"
end

- (Boolean) does_not_match?(event_proc)

Returns:

  • (Boolean)
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rspec/matchers/built_in/change.rb', line 24
def does_not_match?(event_proc, &block)
  expression = if @expected_delta
    "by()"
  elsif @minimum
    "by_at_least()"
  elsif @maximum
    "by_at_most()"
  elsif @eval_after
    "to()"
  end
  if expression
    RSpec.deprecate("`expect { }.not_to change { }.#{expression}`")
  end
  matched_positively = matches?(event_proc, &block)
  unless matches_before?
    RSpec.warn_deprecation(<<-EOS.gsub(/^\s+\|/, ''))
      |The semantics of `expect { }.not_to change { }.from()` are changing
      |in RSpec 3. In RSpec 2.x, this would pass if the value changed but
      |the starting value was not what you specified with `from()`. In
      |RSpec 3, this will only pass if the starting value matches your
      |`from()` value _and_ it has not changed.
      |
      |You have an expectation that relies upon the old RSpec 2.x semantics
      |at: #{CallerFilter.first_non_rspec_line}"
    EOS
  end
  !matched_positively
end

- (Object) evaluate_value_proc

63
64
65
66
67
68
69
70
# File 'lib/rspec/matchers/built_in/change.rb', line 63
def evaluate_value_proc
  case val = @value_proc.call
  when Enumerable, String
    val.dup
  else
    val
  end
end

- (Object) failure_message_for_should

72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/rspec/matchers/built_in/change.rb', line 72
def failure_message_for_should
  if @eval_before && !expected_matches_actual?(@expected_before, @actual_before)
    "#{message} should have initially been #{@expected_before.inspect}, but was #{@actual_before.inspect}"
  elsif @eval_after && !expected_matches_actual?(@expected_after, @actual_after)
    "#{message} should have been changed to #{failure_message_for_expected_after}, but is now #{@actual_after.inspect}"
  elsif @expected_delta
    "#{message} should have been changed by #{@expected_delta.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 #{@actual_before.inspect}"
  end
end

- (Object) failure_message_for_should_not

92
93
94
# File 'lib/rspec/matchers/built_in/change.rb', line 92
def failure_message_for_should_not
  "#{message} should not have changed, but did change from #{@actual_before.inspect} to #{@actual_after.inspect}"
end

- (Object) from(before)

117
118
119
120
121
# File 'lib/rspec/matchers/built_in/change.rb', line 117
def from (before)
  @eval_before = true
  @expected_before = before
  self
end

- (Boolean) matches?(event_proc)

Returns:

  • (Boolean)
14
15
16
17
18
19
20
21
22
# File 'lib/rspec/matchers/built_in/change.rb', line 14
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

- (Object) raise_block_syntax_error

Raises:

  • (SyntaxError)
57
58
59
60
61
# File 'lib/rspec/matchers/built_in/change.rb', line 57
def raise_block_syntax_error
  raise SyntaxError.new(<<-MESSAGE)
block passed to should or should_not change must use {} instead of do/end
MESSAGE
end

- (Boolean) supports_block_expectations?

Returns:

  • (Boolean)
127
128
129
# File 'lib/rspec/matchers/built_in/change.rb', line 127
def supports_block_expectations?
  true
end

- (Object) to(to)

111
112
113
114
115
# File 'lib/rspec/matchers/built_in/change.rb', line 111
def to(to)
  @eval_after = true
  @expected_after = to
  self
end