2008-06-18 02:57:57 -04:00
|
|
|
module Spec
|
|
|
|
|
module Matchers
|
|
|
|
|
|
|
|
|
|
class RespondTo #:nodoc:
|
|
|
|
|
def initialize(*names)
|
|
|
|
|
@names = names
|
|
|
|
|
@names_not_responded_to = []
|
|
|
|
|
end
|
|
|
|
|
|
2008-11-29 12:00:06 -05:00
|
|
|
def matches?(given)
|
|
|
|
|
@given = given
|
2008-06-18 02:57:57 -04:00
|
|
|
@names.each do |name|
|
2008-11-29 12:00:06 -05:00
|
|
|
unless given.respond_to?(name)
|
2008-06-18 02:57:57 -04:00
|
|
|
@names_not_responded_to << name
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
return @names_not_responded_to.empty?
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def failure_message
|
2008-11-29 12:00:06 -05:00
|
|
|
"expected #{@given.inspect} to respond to #{@names_not_responded_to.collect {|name| name.inspect }.join(', ')}"
|
2008-06-18 02:57:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def negative_failure_message
|
2008-11-29 12:00:06 -05:00
|
|
|
"expected #{@given.inspect} not to respond to #{@names.collect {|name| name.inspect }.join(', ')}"
|
2008-06-18 02:57:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def description
|
|
|
|
|
"respond to ##{@names.to_s}"
|
|
|
|
|
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
|