Sha256: 97909c42387c82c31f71ab22d2a7b3ae2e9c5d3f7735bc4d56bf7487a2a97e87

Contents?: true

Size: 1.29 KB

Versions: 1

Compression:

Stored size: 1.29 KB

Contents

require 'matchi'

module Expect
  # Wraps the target of an expectation.  This class is responsible for reporting
  # if the expectation is true or false.
  #
  # @api private
  #
  # @example
  #   ExpectationTarget.new { 42 }.to Equal: 42 # => true
  class ExpectationTarget
    # Create a new expection target
    #
    # @example
    #   new { 42 }.to Equal: 42 # => true
    #
    # @param actual [Proc] the value which is compared with the expected value.
    def initialize(&actual)
      @actual = actual
    end

    attr_reader :actual

    # Evaluate to a positive assertion.
    #
    # @api public
    #
    # @param [Hash, Symbol] definition
    #
    # @return [Boolean] report if the expectation is true or false
    def to(definition)
      matcher(definition).matches?(&actual)
    end

    # Evaluate to a negative assertion.
    #
    # @api public
    #
    # @param [Hash, Symbol] definition
    #
    # @return [Boolean] report if the expectation is not true or not false
    def not_to(definition)
      !to(definition)
    end

    private

    # Load the matcher.
    #
    # @param [Array, Hash, Symbol] definition
    #
    # @return [#matches?] the matcher
    def matcher(definition)
      params = Array(definition).flatten(1)
      Matchi.fetch(params.first, *params[1..-1])
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
expect-0.0.10 lib/expect/expectation_target.rb