Sha256: 18f65ea2b1fad9111c4d4e1d0f755a3cdee7cb94e8e809e8557b6159eafa662c

Contents?: true

Size: 1.86 KB

Versions: 18

Compression:

Stored size: 1.86 KB

Contents

module Rspec
  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_for_should
        "expected #{@actual.inspect} to respond to #{@names_not_responded_to.collect {|name| name.inspect }.join(', ')}#{with_arity}"
      end
      
      def failure_message_for_should_not
        "expected #{@actual.inspect} not to respond to #{@names.collect {|name| name.inspect }.join(', ')}"
      end
      
      def description
        "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
        # Ruby 1.9 returns the same thing for array.to_s as array.inspect, so just use array.inspect here
        @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

18 entries across 18 versions & 1 rubygems

Version Path
rspec-expectations-2.0.0.beta.8 lib/rspec/matchers/respond_to.rb
rspec-expectations-2.0.0.beta.7 lib/rspec/matchers/respond_to.rb
rspec-expectations-2.0.0.beta.6 lib/rspec/matchers/respond_to.rb
rspec-expectations-2.0.0.beta.5 lib/rspec/matchers/respond_to.rb
rspec-expectations-2.0.0.beta.4 lib/rspec/matchers/respond_to.rb
rspec-expectations-2.0.0.beta.3 lib/rspec/matchers/respond_to.rb
rspec-expectations-2.0.0.beta.2 lib/rspec/matchers/respond_to.rb
rspec-expectations-2.0.0.beta.1 lib/rspec/matchers/respond_to.rb
rspec-expectations-2.0.0.a10 lib/rspec/matchers/respond_to.rb
rspec-expectations-2.0.0.a9 lib/rspec/matchers/respond_to.rb
rspec-expectations-2.0.0.a8 lib/rspec/matchers/respond_to.rb
rspec-expectations-2.0.0.a7 lib/rspec/matchers/respond_to.rb
rspec-expectations-2.0.0.a6 lib/rspec/matchers/respond_to.rb
rspec-expectations-2.0.0.a5 lib/rspec/matchers/respond_to.rb
rspec-expectations-2.0.0.a4 lib/rspec/matchers/respond_to.rb
rspec-expectations-2.0.0.a3 lib/rspec/matchers/respond_to.rb
rspec-expectations-2.0.0.a2 lib/rspec/matchers/respond_to.rb
rspec-expectations-2.0.0.a1 lib/rspec/matchers/respond_to.rb