     throw expectation._throws if expectation._throws?
     expectation._returns
 
-_build_errors = (expectation) ->
-  # TODO: use a mapping function?
-  errors = ""
-  for expectation in expectation._all_expectations when expectation.called == false
-    errors += "'#{expectation.method_name}(#{expectation._args})' was never called\n"
-  errors
-
-_throw_args_usage = ->
-  throw "you need to supply at least one argument to args(), e.g. my_mock.expects('my_method').args(42)"
-
-_throw_args_called_more_than_once = ->
-  throw new Error("you called args() more than once, e.g. my_mock.expects('my_method').args(1).args(2); call it just once")
-
-_throw_returns_usage = ->
-  throw "you need to supply an argument to returns(), e.g. my_mock.expects('my_method').returns(123)"
-
-_throw_returns_called_more_than_once = ->
-  throw new Error("you called returns() more than once, e.g. my_mock.expects('my_method').returns(1).returns(2); call it just once")
-
-_throw_throws_usage = ->
-  throw "you need to supply an argument to throws(), e.g. my_mock.expects('my_method').throws('an error')"
-
-_throw_throws_called_more_than_once = ->
-  throw new Error("you called throws() more than once, e.g. my_mock.expects('my_method').throws('something').throws('something else'); call it just once")
-
-_throw_returns_and_throws_both_called = ->
-  throw new Error("you called returns() and throws() on the same expectation; use one or the other but not both")
-
-
-
-#
-# Mock represents the mock of some object. @expectations is a list of
-# mocked methods, which are added with the expects(), args(),
-# returns(), and throws() functions.  These must be called in a
-# specific order, expects then args then returns or throws, so:
-#
-#   my_mock.expects("my_method").args(123).returns(456)
-#
-# is legal, whereas:
-#
-#   my_mock.returns(456).args(123).expects("my_method")
-#
-# is not.  Note that args, returns and throws are optional.  Also
-# note that either returns or throws can be used, but not both on
-# the same expectation.
-#
-# @expectations is used as a stack only in so far as the method expectation
-# at the front of the list is the most recently defined expectation and is
-# the one to which args and returns would be applied.  For example:
-#
-#   my_mock = new Mock()    # @expectations = []
-#   my_mock.expects("m1")   # [ { "m1" } ]
-#   my_mock.args(1,2,3)     # [ { "m1", [1,2,3] } ]
-#   my_mock.returns(42)     # [ { "m1", [1,2,3], 42 } ]
-#   my_mock.expects("m2")   # [ { "m2" }, { "m1", [1,2,3], 42 } ]
-#   my_mock.args(4,5,6)     # [ { "m2", [4,5,6] }, { "m1", [1,2,3], 42 } ]
-#   my_mock.returns(43)     # [ { "m2", [4,5,6], 43 }, { "m1", [1,2,3], 42 } ]
-#
-# @expectations is just an array, not a hash.  It will not grow very large
-# so a linear search for a particular expectation is fine.
-#
-# @expectations is added to the mock object by the various mock methods when
-# needed (i.e. lazily); if we mock an existing class then we only need to
-# worry about adding the mock methods to that class, not monkeying with its
-# contructor to also add @expectations.
-#
-#class Mock
-
-#
-# my_mock = new Mock()
-# my_mock.expects("my_method")
-# my_mock.my_method()
-#
-expects = (method_name) ->
-  _check_expects_usage(method_name)
-  if @[ method_name ]? and typeof @[ method_name ] != 'function'
-    throw new Error("'#{method_name}' is an existing property; you can only mock functions")
-  if typeof @ == 'function' and not @.prototype[ method_name ]?
-    throw new Error("'#{method_name}' is not an existing method; you can only mock existing methods on classes")
-  _start_new_expectation(@, method_name)
-
-
-
-#
-# private
-#
-
-_is_reserved_word = (word) ->
-  word in [ "expects", "args", "returns", "check" ]
-
-_check_expects_usage = (method_name) ->
-  _throw_expects_usage() unless method_name?
-  _throw_reserved_word(method_name) if _is_reserved_word(method_name)
+_find_expectation = (object, method_name, args...) ->
+  for expectation in Expectation._all_expectations when expectation.matches(object, method_name, args...)
+    return expectation
+  undefined
 
 _check_for_duplicate_expectations = (mock) ->
   # TODO: use each with index and slice to avoid last element

      if expectations[outer].equals( expectations[inner] )
        _throw_duplicate_expectation("#{expectations[outer].method_name}(#{expectations[outer]._args})") 

_find_expectation = (object, method_name, args...) ->
  for expectation in Expectation._all_expectations when expectation.matches(object, method_name, args...)
    return expectation
  undefined

_start_new_expectation = (object, method_name) ->
  new Expectation(object, method_name)

_throw_expects_usage = ->
  throw "you need to supply a method name to expects(), e.g. my_mock.expects('my_method')"

_throw_reserved_word = (reserved) ->
  throw "you cannot do my_mock.expects('#{reserved}'); '#{reserved}' is a reserved method name"

_throw_duplicate_expectation = (expectation) ->
  throw "#{expectation} is a duplicate expectation"

_throw_unknown_expectation = (expectation) ->
  throw "#{expectation} does not match any expectations"




class Expectation
  
  @_all_expectations: []
