Sha256: 96d230d1a6d1f4bead5a9fe3fc42914980ea1e8447654284150d11522b5afeb8

Contents?: true

Size: 1.32 KB

Versions: 3

Compression:

Stored size: 1.32 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.
  #
  # @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

    # @return [BasicObject] the object to be compared with the expected one
    #   though the matcher.
    #
    attr_reader :actual

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

    # Evaluate to a negative assertion.
    #
    # @param (see #to)
    #
    # @return (see #to)
    def not_to(definition)
      !to(definition)
    end

    private

    # @api private
    #
    # Load the matcher.
    #
    # @param (see #to)
    #
    # @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

3 entries across 3 versions & 1 rubygems

Version Path
expect-0.0.13 lib/expect/expectation_target.rb
expect-0.0.12 lib/expect/expectation_target.rb
expect-0.0.11 lib/expect/expectation_target.rb