Sha256: d094ce040af630c1f09a8cb3356fccb56690b6ce86d71ff0bd4e0900b0e35b37

Contents?: true

Size: 1.86 KB

Versions: 42

Compression:

Stored size: 1.86 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_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

42 entries across 42 versions & 12 rubygems

Version Path
pupu-0.0.2 vendor/rspec/lib/spec/matchers/respond_to.rb
rango-0.0.6 vendor/rspec/lib/spec/matchers/respond_to.rb
rango-0.1.pre vendor/rspec/lib/spec/matchers/respond_to.rb
pupu-0.0.1 vendor/rspec/lib/spec/matchers/respond_to.rb
media-path-0.1 vendor/rspec/lib/spec/matchers/respond_to.rb
simple-templater-0.0.1 vendor/rspec/lib/spec/matchers/respond_to.rb
middleman-0.10.17 vendor/gems/gems/rspec-1.2.9/lib/spec/matchers/respond_to.rb
middleman-0.10.16 vendor/gems/gems/rspec-1.2.9/lib/spec/matchers/respond_to.rb
middleman-0.10.15 vendor/gems/gems/rspec-1.2.9/lib/spec/matchers/respond_to.rb
rackjour-0.1.8 vendor/gems/gems/rspec-1.2.9/lib/spec/matchers/respond_to.rb
middleman-0.10.14 vendor/gems/gems/rspec-1.2.9/lib/spec/matchers/respond_to.rb
ageweke-rspec-1.2.9 lib/spec/matchers/respond_to.rb
rspec-1.2.9 lib/spec/matchers/respond_to.rb
rspec-1.2.7 lib/spec/matchers/respond_to.rb
rspec-1.2.8 lib/spec/matchers/respond_to.rb
rspec-1.2.0 lib/spec/matchers/respond_to.rb
rspec-1.2.2 lib/spec/matchers/respond_to.rb
rspec-1.2.1 lib/spec/matchers/respond_to.rb
rspec-1.2.5 lib/spec/matchers/respond_to.rb
rspec-1.2.4 lib/spec/matchers/respond_to.rb