Class: Spec::Matchers::Have

Inherits:
Object show all
Defined in:
lib/gems/rspec-1.1.12/lib/spec/matchers/have.rb

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initialize(expected, relativity = :exactly) ⇒ Have

Returns a new instance of Have.



4
5
6
7
# File 'lib/gems/rspec-1.1.12/lib/spec/matchers/have.rb', line 4

def initialize(expected, relativity=:exactly)
  @expected = (expected == :no ? 0 : expected)
  @relativity = relativity
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object (private)



75
76
77
78
79
80
81
82
83
# File 'lib/gems/rspec-1.1.12/lib/spec/matchers/have.rb', line 75

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

Instance Method Details

#descriptionObject



65
66
67
# File 'lib/gems/rspec-1.1.12/lib/spec/matchers/have.rb', line 65

def description
  "have #{relative_expectation} #{@collection_name}"
end

#failure_messageObject



39
40
41
# File 'lib/gems/rspec-1.1.12/lib/spec/matchers/have.rb', line 39

def failure_message
  "expected #{relative_expectation} #{@collection_name}, got #{@given}"
end

#matches?(collection_owner) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/gems/rspec-1.1.12/lib/spec/matchers/have.rb', line 17

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
  @given = collection.size if collection.respond_to?(:size)
  @given = collection.length if collection.respond_to?(:length)
  raise not_a_collection if @given.nil?
  return @given >= @expected if @relativity == :at_least
  return @given <= @expected if @relativity == :at_most
  return @given == @expected
end

#negative_failure_messageObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/gems/rspec-1.1.12/lib/spec/matchers/have.rb', line 43

def negative_failure_message
  if @relativity == :exactly
    return "expected target not to have #{@expected} #{@collection_name}, got #{@given}"
  elsif @relativity == :at_most
    return <<-EOF
Isn't life confusing enough?
Instead of having to figure out the meaning of this:
  should_not have_at_most(#{@expected}).#{@collection_name}
We recommend that you use this instead:
  should have_at_least(#{@expected + 1}).#{@collection_name}
EOF
  elsif @relativity == :at_least
    return <<-EOF
Isn't life confusing enough?
Instead of having to figure out the meaning of this:
  should_not have_at_least(#{@expected}).#{@collection_name}
We recommend that you use this instead:
  should have_at_most(#{@expected - 1}).#{@collection_name}
EOF
  end
end

#not_a_collectionObject



35
36
37
# File 'lib/gems/rspec-1.1.12/lib/spec/matchers/have.rb', line 35

def not_a_collection
  "expected #{@collection_name} to be a collection but it does not respond to #length or #size"
end

#relativitiesObject



9
10
11
12
13
14
15
# File 'lib/gems/rspec-1.1.12/lib/spec/matchers/have.rb', line 9

def relativities
  @relativities ||= {
    :exactly => "",
    :at_least => "at least ",
    :at_most => "at most "
  }
end

#respond_to?(sym) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/gems/rspec-1.1.12/lib/spec/matchers/have.rb', line 69

def respond_to?(sym)
  @expected.respond_to?(sym) || super
end