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
dchelimsky-rspec-1.1.99.13 lib/spec/matchers/respond_to.rb
rspec-instructure-1.3.3 lib/spec/matchers/respond_to.rb
radiant-1.0.0 ruby-debug/ruby/1.8/gems/rspec-1.3.2/lib/spec/matchers/respond_to.rb
rspec-1.3.2 lib/spec/matchers/respond_to.rb
rspec-1.3.1 lib/spec/matchers/respond_to.rb
rspec-1.3.1.rc lib/spec/matchers/respond_to.rb
rspec-1.3.0 lib/spec/matchers/respond_to.rb
hubbub-0.0.11 lib/vendor/plugins/rspec/lib/spec/matchers/respond_to.rb
hubbub-0.0.10 lib/vendor/plugins/rspec/lib/spec/matchers/respond_to.rb
hubbub-0.0.9 lib/vendor/plugins/rspec/lib/spec/matchers/respond_to.rb
hubbub-0.0.8 lib/vendor/plugins/rspec/lib/spec/matchers/respond_to.rb
hubbub-0.0.6 lib/vendor/plugins/rspec/lib/spec/matchers/respond_to.rb
simple-templater-0.0.1.4 gems/gems/rspec-1.2.9/lib/spec/matchers/respond_to.rb
media-path-0.1.2 vendor/rspec/lib/spec/matchers/respond_to.rb
simple-templater-0.0.1.3 vendor/rspec/lib/spec/matchers/respond_to.rb
pupu-0.0.2.pre vendor/rspec/lib/spec/matchers/respond_to.rb
media-path-0.1.1.pre vendor/rspec/lib/spec/matchers/respond_to.rb
simple-templater-0.0.1.2 vendor/rspec/lib/spec/matchers/respond_to.rb
media-path-0.1.1 vendor/rspec/lib/spec/matchers/respond_to.rb
simple-templater-0.0.1.1 vendor/rspec/lib/spec/matchers/respond_to.rb