Sha256: 3e1944017997e9066809296c06727c2cf4cae4f1f470bc022991c2befe1a9334

Contents?: true

Size: 1.39 KB

Versions: 2

Compression:

Stored size: 1.39 KB

Contents

require 'quarry/assertion'

module Quarry

  # = Expectation
  #
  # Expectation is an Assertion Functor.
  #
  class Expectation

    hide = instance_methods.select{ |m| m.to_s !~ /^__/ }
    hide.each{ |m| protected m  }

    private

    # New Expectation.
    #
    def initialize(delegate, ioc={}) #, backtrace)
      @delegate  = delegate
      @negate    = ioc[:negate]
      @message   = ioc[:message]
      @backtrace = ioc[:backtrace] || caller #[1..-1]
    end

    # Converts missing method into an Assertion.
    #
    def method_missing(sym, *a, &b)
      test = @delegate.__send__(sym, *a, &b)

      if (@negate ? test : !test)
        msg   = @message || __msg__(sym, *a, &b)
        error = Assertion.new(msg)
        error.set_backtrace(@backtrace)
        raise error
      end
    end

    # Puts together a suitable error message.
    #
    def __msg__(m, *a, &b)
      if @negate
        "! #{@delegate.inspect} #{m} #{a.collect{|x| x.inspect}.join(',')}"
      else
        "#{@delegate.inspect} #{m} #{a.collect{|x| x.inspect}.join(',')}"
      end
      #self.class.message(m)[@delegate, *a] )
    end

    # TODO: Ultimately better messages would be nice ?
    #
    #def self.message(op,&block)
    #  @message ||= {}
    #  block ? @message[op.to_sym] = block : @message[op.to_sym]
    #end
    #
    #message(:==){ |*a| "Expected #{a[0].inspect} to be equal to #{a[1].inspect}" }
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
quarry-0.5.2 lib/quarry/expectation.rb
quarry-0.5.0 lib/quarry/expectation.rb