Sha256: f8dc7c5f3aad2b5e3cbb9dc37bd01051c6fe645435b55d2830de4f83f89ebe9d

Contents?: true

Size: 1.57 KB

Versions: 1

Compression:

Stored size: 1.57 KB

Contents

require 'set'
require 'spectus'
require_relative 'db'
require_relative 'subject'

module Fix
  class Expectation
    include Spectus::DSL

    attr_reader :priority

    def initialize object, positive, definition, *args
      @positive   = positive
      @definition = definition
      @args       = args
      @priority   = DB.instance.random.rand

      DB.instance.update(object => SortedSet[]) unless DB.instance.key? object
      DB.instance.fetch(object).add self
    end

    def <=> other
      self.priority <=> other.priority
    end

    def evaluate front_object
      Thread.new {
        subject = Subject.new front_object, *@args
        got     = nil

        report = begin
          expect { got = subject.actual }.__send__ target, @definition
        rescue => e
          e
        end

        data = presenter report, got, subject

        Hash[ data.to_a + meta(subject).to_a ].merge object: front_object
      }.value
    end

    private

    def exception result
      result unless [ true, false ].include? result
    end

    def negated?
      !@positive
    end

    def presenter result, got, subject
      {
        pass:       pass(result, subject),
        negated:    negated?,
        definition: @definition,
        exception:  exception(result),
        got:        got
      }
    end

    def meta subject
      {
        level:      level,
        params:     subject.params,
        challenge:  subject.challenge,
        last_arg:   subject.last_arg
      }
    end

    def target
      if negated?
        :not_to
      else
        :to
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fix-0.6.1 lib/fix/expectation.rb