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