Sha256: 6d875969483fb7fb0b6c4a1c20d3dc2dbc69b6dcf25fde3455067f00049dc288

Contents?: true

Size: 1.98 KB

Versions: 1

Compression:

Stored size: 1.98 KB

Contents

# frozen_string_literal: true

module Matchi
  class Change
    class From
      # *Change from to* matcher.
      class To
        # Initialize the matcher with two objects and a block.
        #
        # @example
        #   require "matchi/change/from/to"
        #
        #   object = "foo"
        #
        #   Matchi::Change::From::To.new("foo", "FOO") { object.to_s }
        #
        # @param expected_init      [#object_id] An expected initial value.
        # @param expected_new_value [#object_id] An expected new value.
        # @param state              [Proc]       A block of code to execute to
        #   get the state of the object.
        def initialize(expected_init, expected_new_value, &state)
          @expected_init  = expected_init
          @expected       = expected_new_value
          @state          = state
        end

        # Boolean comparison on the expected change by comparing the value
        # before and after the code execution.
        #
        # @example
        #   require "matchi/change/from/to"
        #
        #   object = "foo"
        #
        #   matcher = Matchi::Change::From::To.new("foo", "FOO") { object.to_s }
        #   matcher.matches? { object.upcase! } # => true
        #
        # @yieldreturn [#object_id] The block of code to execute.
        #
        # @return [Boolean] Comparison between the value before and after the
        #   code execution.
        def matches?(*, **)
          value_before = @state.call
          return false unless @expected_init == value_before

          yield
          value_after = @state.call

          @expected == value_after
        end

        # A string containing a human-readable representation of the matcher.
        def inspect
          "#{self.class}(#{@expected_init.inspect}, #{@expected.inspect})"
        end

        # Returns a string representing the matcher.
        def to_s
          "change from #{@expected_init.inspect} to #{@expected.inspect}"
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
matchi-3.0.0 lib/matchi/change/from/to.rb