class Spec::Matchers::Have
:nodoc:
def description
def description "have #{relative_expectation} #{@collection_name}" end
def failure_message_for_should
def failure_message_for_should "expected #{relative_expectation} #{@collection_name}, got #{@actual}" end
def failure_message_for_should_not
def failure_message_for_should_not if @relativity == :exactly return "expected target not to have #{@expected} #{@collection_name}, got #{@actual}" elsif @relativity == :at_most return <<-EOF life confusing enough? d of having to figure out the meaning of this: ld_not have_at_most(#{@expected}).#{@collection_name} ommend that you use this instead: ld have_at_least(#{@expected + 1}).#{@collection_name} elsif @relativity == :at_least return <<-EOF life confusing enough? d of having to figure out the meaning of this: ld_not have_at_least(#{@expected}).#{@collection_name} ommend that you use this instead: ld have_at_most(#{@expected - 1}).#{@collection_name} end end
def initialize(expected, relativity=:exactly)
def initialize(expected, relativity=:exactly) @expected = (expected == :no ? 0 : expected) @relativity = relativity @actual = nil end
def matches?(collection_owner)
def matches?(collection_owner) if collection_owner.respond_to?(@collection_name) collection = collection_owner.__send__(@collection_name, *@args, &@block) elsif (@plural_collection_name && collection_owner.respond_to?(@plural_collection_name)) collection = collection_owner.__send__(@plural_collection_name, *@args, &@block) elsif (collection_owner.respond_to?(:length) || collection_owner.respond_to?(:size)) collection = collection_owner else collection_owner.__send__(@collection_name, *@args, &@block) end @actual = collection.size if collection.respond_to?(:size) @actual = collection.length if collection.respond_to?(:length) raise not_a_collection if @actual.nil? return @actual >= @expected if @relativity == :at_least return @actual <= @expected if @relativity == :at_most return @actual == @expected end
def method_missing(sym, *args, &block)
def method_missing(sym, *args, &block) @collection_name = sym if inflector = (defined?(ActiveSupport::Inflector) ? ActiveSupport::Inflector : (defined?(Inflector) ? Inflector : nil)) @plural_collection_name = inflector.pluralize(sym.to_s) end @args = args @block = block self end
def not_a_collection
def not_a_collection "expected #{@collection_name} to be a collection but it does not respond to #length or #size" end
def relative_expectation
def relative_expectation "#{relativities[@relativity]}#{@expected}" end
def relativities
def relativities @relativities ||= { :exactly => "", :at_least => "at least ", :at_most => "at most " } end
def respond_to?(sym)
def respond_to?(sym) @expected.respond_to?(sym) || super end