Sha256: 6e3e42491829f6352c0d6e30b3ba05dd394b253247ede79462d207ff4142da2f

Contents?: true

Size: 1.91 KB

Versions: 2

Compression:

Stored size: 1.91 KB

Contents

module Quarry

  # = Mock
  #
  class Mock < Module
    attr :object

    def initialize
      super()
      @table = {}
    end

    #
    def __table__ ; @table ; end

    # TODO: Ruby has retry, but I need continue!
    def method_missing(meth, *args, &block)
      table     = @table
      interface = [meth, args, block_given?]

      table[interface] = nil

      define_method(meth) do |*args|
        result = super
        result.assert == table[interface]
        return result
      end

      Setter.new(table, interface)
    end

    #
    class Setter
      def initialize(table, interface)
        @table     = table
        @interface = interface
      end

      def ==(result)
        @table[@interface] = result
      end
    end

    # = Mock::Delegator
    #
    class Delegator
      instance_methods(true).each{ |m| protected m unless m.to_s =~ /^__/ }

      def initialize(object, mock_module)
        @instance_delegate = object
        extend(mock_module)
      end

      def method_missing(s, *a, &b)
        @instance_delegate.__send__(s, *a, &b)
      end
    end#class Delegator

  end#class Mock

  class ::Object
    # Create mock object.
    def mock(mock_module=nil)
      if mock_module
        Mock::Delegator.new(self, mock_module)
      else
        @_mock ||= Mock.new
        extend(@_mock)
        @_mock
      end
    end

    # We can't remove the module per-say.  So we have to 
    # just neuter it. This is a very weak solution, but
    # it will suffice for the moment.
    #-- 
    # TODO: Use Carats for #unmix.
    #++
    def remove_mock(mock_module=nil)
      mock_module ||= @_mock
      obj = self
      mod = Module.new
      mock_module.__table__.each do |interface, result|
        meth = interface[0]
        mod.module_eval do
          define_method(meth, &obj.class.instance_method(meth).bind(obj))
        end
      end    
      extend(mod)
    end
  end#class ::Object

end#module Quarry

Version data entries

2 entries across 2 versions & 1 rubygems

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