Class: Matchi::Change
- Inherits:
-
Object
- Object
- Matchi::Change
- Defined in:
- lib/matchi/change.rb,
lib/matchi/change/to.rb,
lib/matchi/change/from.rb,
lib/matchi/change/delta.rb,
lib/matchi/change/from_to.rb
Overview
State change matcher: passes when evaluating the block changes the value returned by a public method of an object.
The observed method is called with public_send before and after the
block, and the two values are compared with eql?. The observed method
must return a value, not the mutated object itself: when it returns the
very same unfrozen object twice, the comparison cannot tell whether that
object changed, and match? raises instead of answering false.
attr_reader :items with items << x is that case; observe
items.length or use #to, which compares with the expected value. A
frozen object returned twice counts as an unchanged value.
The refinements #by, #by_at_least, #by_at_most, #from and #to
build more specific matchers. Change, by, by_at_least and
by_at_most compare values produced by the observed object, so they
depend on its honesty, like Predicate. to and from … to
compare with the expected value, like Eq. Whatever the
observed method raises propagates.
Defined Under Namespace
Classes: Delta, From, FromTo, To
Instance Method Summary collapse
- #by(delta) ⇒ Matchi::Change::Delta
- #by_at_least(delta) ⇒ Matchi::Change::Delta
- #by_at_most(delta) ⇒ Matchi::Change::Delta
-
#from(old_value) ⇒ Matchi::Change::From
A builder; call
toon it. -
#initialize(object, method, *args, **kwargs, &block) ⇒ Change
constructor
A new instance of Change.
- #match? ⇒ Boolean
- #to(new_value) ⇒ Matchi::Change::To
-
#to_s ⇒ String
The observed class and method, such as
change Array#length.
Constructor Details
#initialize(object, method, *args, **kwargs, &block) ⇒ Change
Returns a new instance of Change.
39 40 41 42 43 44 45 |
# File 'lib/matchi/change.rb', line 39 def initialize(object, method, *args, **kwargs, &block) raise ::ArgumentError, "method must be a Symbol or a String (got: #{method.inspect})" unless name?(method) @object = object @method = method.to_sym @state = -> { ::Kernel.instance_method(:public_send).bind_call(object, @method, *args, **kwargs, &block) } end |
Instance Method Details
#by(delta) ⇒ Matchi::Change::Delta
73 74 75 |
# File 'lib/matchi/change.rb', line 73 def by(delta) Delta.new(:by, delta, &@state) end |
#by_at_least(delta) ⇒ Matchi::Change::Delta
80 81 82 |
# File 'lib/matchi/change.rb', line 80 def by_at_least(delta) Delta.new(:by_at_least, delta, &@state) end |
#by_at_most(delta) ⇒ Matchi::Change::Delta
87 88 89 |
# File 'lib/matchi/change.rb', line 87 def by_at_most(delta) Delta.new(:by_at_most, delta, &@state) end |
#from(old_value) ⇒ Matchi::Change::From
Returns a builder; call to on it.
93 94 95 |
# File 'lib/matchi/change.rb', line 93 def from(old_value) From.new(old_value, &@state) end |
#match? ⇒ Boolean
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/matchi/change.rb', line 52 def match? raise ::ArgumentError, "a block must be provided" unless block_given? value_before = @state.call yield value_after = @state.call return !value_before.eql?(value_after) unless value_before.equal?(value_after) return false if ::Kernel.instance_method(:frozen?).bind_call(value_before) raise ::TypeError, "#{self} returned the same unfrozen object twice: observe a value, not the object" end |
#to(new_value) ⇒ Matchi::Change::To
99 100 101 |
# File 'lib/matchi/change.rb', line 99 def to(new_value) To.new(new_value, &@state) end |