Sha256: 1e917fe3008429b7a6deed0c1c6ef95c23bdd1248389e75aa62fce8dbeee40fa

Contents?: true

Size: 1.87 KB

Versions: 1

Compression:

Stored size: 1.87 KB

Contents

require 'matchi'

module Spectus
  # This class evaluate the expectation with the passed block.
  #
  # @api private
  #
  class Sandbox
    # Execute the untested code from the passed block against the definition.
    #
    # @param definition [Hash, Symbol]  Definition.
    # @param negate     [Boolean]       Negate the expectation result.
    # @param object     [#object_id]    The front object which is challenged.
    # @param challenges [Array]         The list of challenges.
    def initialize(definition, negate, object, *challenges)
      @got = negate ^ matcher(definition).matches? do
        @actual = challenges.inject(object) do |subject, challenge|
          @last_challenge = challenge
          @last_challenge.to(subject)
        end
      end
    rescue => e
      @exception = e
    end

    # @!attribute [r] last_challenge
    #
    # @return [Challenge] The last evaluated challenge.
    attr_reader :last_challenge

    # @!attribute [r] actual
    #
    # @return [#object_id] The value that the subject return through its
    #   challenge.
    attr_reader :actual

    # @!attribute [r] exception
    #
    # @return [#exception, nil] Any possible raised exception.
    attr_reader :exception

    # @!attribute [r] got
    #
    # @return [#object_id] The result of the boolean comparison between the
    #   actual value and the expected value.
    attr_reader :got

    # Report to the spec's requirement level if the test is true or false.
    #
    # @return [Boolean] Report if the test was true or false.
    def valid?
      if defined?(@exception)
        false
      else
        @got
      end
    end

    private

    # Load the matcher.
    #
    # @param definition [Array, Hash, Symbol]
    #
    # @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
spectus-2.4.0 lib/spectus/sandbox.rb