Class: Spec::Expectations::Should::CollectionHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/spec/expectations/should/have.rb

Instance Method Summary collapse

Constructor Details

#initialize(target, relativity = :exactly, expected = nil) ⇒ CollectionHandler

Returns a new instance of CollectionHandler.



37
38
39
40
41
42
# File 'lib/spec/expectations/should/have.rb', line 37

def initialize(target, relativity=:exactly, expected=nil)
  @target = target
  @expected = expected == :no ? 0 : expected
  @at_least = (relativity == :at_least)
  @at_most = (relativity == :at_most)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object



58
59
60
61
62
# File 'lib/spec/expectations/should/have.rb', line 58

def method_missing(sym, *args)
  if @target.respond_to?(sym)
    handle_message(sym, *args)
  end
end

Instance Method Details

#actual_size_of(collection) ⇒ Object



91
92
93
94
# File 'lib/spec/expectations/should/have.rb', line 91

def actual_size_of(collection)
  return collection.length if collection.respond_to? :length
  return collection.size if collection.respond_to? :size
end

#as_specified?(sym, args) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
84
85
# File 'lib/spec/expectations/should/have.rb', line 81

def as_specified?(sym, args)
  return actual_size_of(collection(sym, args)) >= @expected if @at_least
  return actual_size_of(collection(sym, args)) <= @expected if @at_most
  return actual_size_of(collection(sym, args)) == @expected
end

#at_least(expected_number = nil) ⇒ Object



44
45
46
47
48
49
# File 'lib/spec/expectations/should/have.rb', line 44

def at_least(expected_number=nil)
  @at_least = true
  @at_most = false
  @expected = expected_number == :no ? 0 : expected_number
  self
end

#at_most(expected_number = nil) ⇒ Object



51
52
53
54
55
56
# File 'lib/spec/expectations/should/have.rb', line 51

def at_most(expected_number=nil)
  @at_least = false
  @at_most = true
  @expected = expected_number == :no ? 0 : expected_number
  self
end

#build_message(sym, args) ⇒ Object



74
75
76
77
78
79
# File 'lib/spec/expectations/should/have.rb', line 74

def build_message(sym, args)
  message = "expected"
  message += " at least" if @at_least
  message += " at most" if @at_most
  message += " #{@expected} #{sym}, got #{actual_size_of(collection(sym, args))}"
end

#collection(sym, args) ⇒ Object



87
88
89
# File 'lib/spec/expectations/should/have.rb', line 87

def collection(sym, args)
  @target.send(sym, *args)
end

#handle_message(sym, *args) ⇒ Object



68
69
70
71
72
# File 'lib/spec/expectations/should/have.rb', line 68

def handle_message(sym, *args)
  return at_least(args[0]) if sym == :at_least
  return at_most(args[0]) if sym == :at_most
  Spec::Expectations.fail_with(build_message(sym, args)) unless as_specified?(sym, args)
end

#wants_to_handle(sym) ⇒ Object



64
65
66
# File 'lib/spec/expectations/should/have.rb', line 64

def wants_to_handle(sym)
  respond_to?(sym) || @target.respond_to?(sym)
end