Sha256: 4e1e3f386edb27841624436637e8d87c62d6972b1dc532ad1ac4381c0f775ad4

Contents?: true

Size: 1.84 KB

Versions: 14

Compression:

Stored size: 1.84 KB

Contents

module Spec
  module Matchers
    
    class RespondTo #:nodoc:
      def initialize(*names)
        @names = names
        @expected_arity = nil
        @names_not_responded_to = []
      end
      
      def matches?(actual)
        @actual = actual
        @names.each do |name|
          @names_not_responded_to << name unless actual.respond_to?(name) && matches_arity?(actual, name)
        end
        return @names_not_responded_to.empty?
      end
      
      def failure_message
        "expected #{@actual.inspect} to respond to #{@names_not_responded_to.collect {|name| name.inspect }.join(', ')}#{with_arity}"
      end
      
      def negative_failure_message
        "expected #{@actual.inspect} not to respond to #{@names.collect {|name| name.inspect }.join(', ')}"
      end
      
      def description
        # Ruby 1.9 returns the same thing for array.to_s as array.inspect, so just use array.inspect here
        "respond to #{pp_names}#{with_arity}"
      end
      
      def with(n)
        @expected_arity = n
        self
      end
      
      def argument
        self
      end
      alias :arguments :argument
      
    private
      
      def matches_arity?(actual, name)
        @expected_arity.nil?? true : @expected_arity == actual.method(name).arity 
      end
      
      def with_arity
        @expected_arity.nil?? "" :
          " with #{@expected_arity} argument#{@expected_arity == 1 ? '' : 's'}"
      end
      
      def pp_names
        @names.length == 1 ? "##{@names.first}" : @names.inspect
      end
    end
    
    # :call-seq:
    #   should respond_to(*names)
    #   should_not respond_to(*names)
    #
    # Matches if the target object responds to all of the names
    # provided. Names can be Strings or Symbols.
    #
    # == Examples
    # 
    def respond_to(*names)
      Matchers::RespondTo.new(*names)
    end
  end
end

Version data entries

14 entries across 14 versions & 6 rubygems

Version Path
dchelimsky-rspec-1.1.11.5 lib/spec/matchers/respond_to.rb
dchelimsky-rspec-1.1.11.6 lib/spec/matchers/respond_to.rb
dchelimsky-rspec-1.1.11.7 lib/spec/matchers/respond_to.rb
dchelimsky-rspec-1.1.12 lib/spec/matchers/respond_to.rb
newbamboo-evented-rspec-1.1.12 lib/spec/matchers/respond_to.rb
newbamboo-rspec-1.1.12 lib/spec/matchers/respond_to.rb
mack-0.8.3.1 lib/gems/rspec-1.1.12/lib/spec/matchers/respond_to.rb
mack-0.8.3 lib/gems/rspec-1.1.12/lib/spec/matchers/respond_to.rb
rspec-1.1.12 lib/spec/matchers/respond_to.rb
spree-0.6.0 vendor/plugins/rspec/lib/spec/matchers/respond_to.rb
spree-0.7.1 vendor/plugins/rspec/lib/spec/matchers/respond_to.rb
spree-0.7.0 vendor/plugins/rspec/lib/spec/matchers/respond_to.rb
spree-0.8.0 vendor/plugins/rspec/lib/spec/matchers/respond_to.rb
spree-0.8.1 vendor/plugins/rspec/lib/spec/matchers/respond_to.rb