+publish = exports ? window
+
+
+publish.mock = (test_function) ->
+  try
+    install_expects_method()
+    convenience_mocks = build_convenience_mock_objects()
+    run_test_function(test_function, convenience_mocks)
+    verify_all_expectations()
+  finally
+    uninstall_expects_method()
+    uninstall_all_mocked_methods()
+    clear_all_expectations()
+
+install_expects_method = ->
+  Object.prototype.expects = expects
+
+uninstall_expects_method = ->
+  delete Object.prototype.expects
+
+uninstall_all_mocked_methods = ->
+  for expectation in all_expectations     # TODO: do this in reverse
+    object = if typeof expectation._object == 'function' then expectation._object.prototype else expectation._object
+    if expectation._original_method?
+      object[ expectation.method_name ] = expectation._original_method
+    else
+      delete object[ expectation.method_name ]
+
+build_convenience_mock_objects = ->
+  ( new Object() for i in [1..5] )
+
+run_test_function = (test_function, convenience_mocks) ->
+  test_function.apply(undefined, convenience_mocks)
+
+
 all_expectations = []
-  
+
 
 clear_all_expectations = ->
   all_expectations.length = 0


build_errors = ->
  (build_not_called_error(expectation) for expectation in all_expectations when not expectation.called).join("")
  
build_not_called_error = (expectation) ->
  "'#{expectation.method_name}(#{expectation._args})' was never called\n"



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