Sha256: 050494e7e5b5eec47fc3e63565365ec62a6f75ecd7e10d8c2b8f20f7a2e7adc5

Contents?: true

Size: 1.5 KB

Versions: 1

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true

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

      # Boolean comparison on the expected change by comparing the value
      # before and after the code execution.
      #
      # @example
      #   require "matchi/change/to"
      #
      #   object = "foo"
      #
      #   matcher = Matchi::Change::To.new("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?(*, **)
        yield
        value_after = @state.call

        @expected == value_after
      end

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

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

Version data entries

1 entries across 1 versions & 1 rubygems

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