Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

tabs!

  • Loading branch information...
commit ffc1b5f8eb846e63c2a3aa1c6766dedde8c0d60c 1 parent 846fc89
@gfxmonk authored
Showing with 165 additions and 165 deletions.
  1. +165 −165 test/mock/mock_test.py
View
330 test/mock/mock_test.py
@@ -8,7 +8,7 @@
import sys
this_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..','..'))
if not this_dir in sys.path:
- sys.path.insert(0, this_dir)
+ sys.path.insert(0, this_dir)
from testcase import TestCase
@@ -17,174 +17,174 @@
class MockTest(TestCase):
- def testConstructor(self):
- mock = Mock()
-
- self.assertFalse(mock.called, "called not initialised correctly")
- self.assertEquals(mock.call_count, 0, "call_count not initialised correctly")
- self.assertTrue(isinstance(mock.return_value, Mock), "return_value not initialised correctly")
-
- self.assertEquals(mock.call_args, None, "call_args not initialised correctly")
- self.assertEquals(mock.call_args_list, [], "call_args_list not initialised correctly")
- self.assertEquals(mock.method_calls, [],
- "method_calls not initialised correctly")
-
- self.assertNone(mock._parent, "parent not initialised correctly")
- self.assertNone(mock._methods, "methods not initialised correctly")
- self.assertEquals(mock._children, {}, "children not initialised incorrectly")
-
-
- def testReturnValueInConstructor(self):
- mock = Mock(return_value=None)
- self.assertNone(mock.return_value, "return value in constructor not honoured")
-
-
- def testSideEffect(self):
- mock = Mock()
- def effect():
- raise SystemError('kablooie')
- mock._side_effect = effect
-
- self.assertRaises(SystemError, mock)
- self.assertTrue(mock.called, "call not recorded")
-
- results = [1, 2, 3]
- def effect():
- mock.return_value = results.pop()
- mock._side_effect = effect
-
- self.assertEquals([mock(), mock(), mock()], [3, 2, 1],
- "side effect not used correctly")
+ def testConstructor(self):
+ mock = Mock()
+
+ self.assertFalse(mock.called, "called not initialised correctly")
+ self.assertEquals(mock.call_count, 0, "call_count not initialised correctly")
+ self.assertTrue(isinstance(mock.return_value, Mock), "return_value not initialised correctly")
+
+ self.assertEquals(mock.call_args, None, "call_args not initialised correctly")
+ self.assertEquals(mock.call_args_list, [], "call_args_list not initialised correctly")
+ self.assertEquals(mock.method_calls, [],
+ "method_calls not initialised correctly")
+
+ self.assertNone(mock._parent, "parent not initialised correctly")
+ self.assertNone(mock._methods, "methods not initialised correctly")
+ self.assertEquals(mock._children, {}, "children not initialised incorrectly")
+
+
+ def testReturnValueInConstructor(self):
+ mock = Mock(return_value=None)
+ self.assertNone(mock.return_value, "return value in constructor not honoured")
+
+
+ def testSideEffect(self):
+ mock = Mock()
+ def effect():
+ raise SystemError('kablooie')
+ mock._side_effect = effect
+
+ self.assertRaises(SystemError, mock)
+ self.assertTrue(mock.called, "call not recorded")
+
+ results = [1, 2, 3]
+ def effect():
+ mock.return_value = results.pop()
+ mock._side_effect = effect
+
+ self.assertEquals([mock(), mock(), mock()], [3, 2, 1],
+ "side effect not used correctly")
- mock = Mock(action=sentinel.SideEffect)
- self.assertEquals(mock._side_effect, sentinel.SideEffect,
- "side effect in constructor not used")
-
-
- def testReset(self):
- parent = Mock()
- methods = ["something"]
- mock = Mock(name="child", parent=parent, methods=methods)
- mock(sentinel.Something, something=sentinel.SomethingElse)
- something = mock.something
- mock.something()
- mock.side_effect = sentinel.SideEffect
- return_value = mock.return_value
- return_value()
-
- mock.reset()
-
- self.assertEquals(mock._name, "child", "name incorrectly reset")
- self.assertEquals(mock._parent, parent, "parent incorrectly reset")
- self.assertEquals(mock._methods, methods, "methods incorrectly reset")
-
- self.assertFalse(mock.called, "called not reset")
- self.assertEquals(mock.call_count, 0, "call_count not reset")
- self.assertEquals(mock.call_args, None, "call_args not reset")
- self.assertEquals(mock.call_args_list, [], "call_args_list not reset")
- self.assertEquals(mock.method_calls, [],
- "method_calls not initialised correctly")
-
- self.assertEquals(mock.side_effect, sentinel.SideEffect,
- "side_effect incorrectly reset")
- self.assertEquals(mock.return_value, return_value,
- "return_value incorrectly reset")
- self.assertFalse(return_value.called, "return value mock not reset")
- self.assertEquals(mock._children, {'something': something},
- "children reset incorrectly")
- self.assertEquals(mock.something, something,
- "children incorrectly cleared")
- self.assertFalse(mock.something.called, "child not reset")
-
-
- def testCall(self):
- mock = Mock()
- self.assertTrue(isinstance(mock.return_value, Mock), "Default return_value should be a Mock")
-
- result = mock()
- self.assertEquals(mock(), result, "different result from consecutive calls")
- mock.reset()
-
- ret_val = mock(sentinel.Arg)
- self.assertTrue(mock.called, "called not set")
- self.assertEquals(mock.call_count, 1, "call_count incoreect")
- self.assertEquals(mock.call_args, ((sentinel.Arg,), {}), "call_args not set")
- self.assertEquals(mock.call_args_list, [((sentinel.Arg,), {})], "call_args_list not initialised correctly")
+ mock = Mock(action=sentinel.SideEffect)
+ self.assertEquals(mock._side_effect, sentinel.SideEffect,
+ "side effect in constructor not used")
+
+
+ def testReset(self):
+ parent = Mock()
+ methods = ["something"]
+ mock = Mock(name="child", parent=parent, methods=methods)
+ mock(sentinel.Something, something=sentinel.SomethingElse)
+ something = mock.something
+ mock.something()
+ mock.side_effect = sentinel.SideEffect
+ return_value = mock.return_value
+ return_value()
+
+ mock.reset()
+
+ self.assertEquals(mock._name, "child", "name incorrectly reset")
+ self.assertEquals(mock._parent, parent, "parent incorrectly reset")
+ self.assertEquals(mock._methods, methods, "methods incorrectly reset")
+
+ self.assertFalse(mock.called, "called not reset")
+ self.assertEquals(mock.call_count, 0, "call_count not reset")
+ self.assertEquals(mock.call_args, None, "call_args not reset")
+ self.assertEquals(mock.call_args_list, [], "call_args_list not reset")
+ self.assertEquals(mock.method_calls, [],
+ "method_calls not initialised correctly")
+
+ self.assertEquals(mock.side_effect, sentinel.SideEffect,
+ "side_effect incorrectly reset")
+ self.assertEquals(mock.return_value, return_value,
+ "return_value incorrectly reset")
+ self.assertFalse(return_value.called, "return value mock not reset")
+ self.assertEquals(mock._children, {'something': something},
+ "children reset incorrectly")
+ self.assertEquals(mock.something, something,
+ "children incorrectly cleared")
+ self.assertFalse(mock.something.called, "child not reset")
+
+
+ def testCall(self):
+ mock = Mock()
+ self.assertTrue(isinstance(mock.return_value, Mock), "Default return_value should be a Mock")
+
+ result = mock()
+ self.assertEquals(mock(), result, "different result from consecutive calls")
+ mock.reset()
+
+ ret_val = mock(sentinel.Arg)
+ self.assertTrue(mock.called, "called not set")
+ self.assertEquals(mock.call_count, 1, "call_count incoreect")
+ self.assertEquals(mock.call_args, ((sentinel.Arg,), {}), "call_args not set")
+ self.assertEquals(mock.call_args_list, [((sentinel.Arg,), {})], "call_args_list not initialised correctly")
- mock.return_value = sentinel.ReturnValue
- ret_val = mock(sentinel.Arg, key=sentinel.KeyArg)
- self.assertEquals(ret_val, sentinel.ReturnValue, "incorrect return value")
-
- self.assertEquals(mock.call_count, 2, "call_count incorrect")
- self.assertEquals(mock.call_args, ((sentinel.Arg,), {'key': sentinel.KeyArg}), "call_args not set")
- self.assertEquals(mock.call_args_list, [((sentinel.Arg,), {}), ((sentinel.Arg,), {'key': sentinel.KeyArg})], "call_args_list not set")
-
+ mock.return_value = sentinel.ReturnValue
+ ret_val = mock(sentinel.Arg, key=sentinel.KeyArg)
+ self.assertEquals(ret_val, sentinel.ReturnValue, "incorrect return value")
+
+ self.assertEquals(mock.call_count, 2, "call_count incorrect")
+ self.assertEquals(mock.call_args, ((sentinel.Arg,), {'key': sentinel.KeyArg}), "call_args not set")
+ self.assertEquals(mock.call_args_list, [((sentinel.Arg,), {}), ((sentinel.Arg,), {'key': sentinel.KeyArg})], "call_args_list not set")
+
- def testAttributeAccessReturnsMocks(self):
- mock = Mock()
- something = mock.something
- self.assertTrue(isinstance(something, Mock), "attribute isn't a mock")
- self.assertEquals(mock.something, something, "different attributes returned for same name")
-
- # Usage example
- mock = Mock()
- mock.something.return_value = 3
-
- self.assertEquals(mock.something(), 3, "method returned wrong value")
- self.assertTrue(mock.something.called, "method didn't record being called")
-
+ def testAttributeAccessReturnsMocks(self):
+ mock = Mock()
+ something = mock.something
+ self.assertTrue(isinstance(something, Mock), "attribute isn't a mock")
+ self.assertEquals(mock.something, something, "different attributes returned for same name")
+
+ # Usage example
+ mock = Mock()
+ mock.something.return_value = 3
+
+ self.assertEquals(mock.something(), 3, "method returned wrong value")
+ self.assertTrue(mock.something.called, "method didn't record being called")
+
- def testAttributesHaveNameAndParentSet(self):
- mock = Mock()
- something = mock.something
-
- self.assertEquals(something._name, "something", "attribute name not set correctly")
- self.assertEquals(something._parent, mock, "attribute parent not set correctly")
+ def testAttributesHaveNameAndParentSet(self):
+ mock = Mock()
+ something = mock.something
+
+ self.assertEquals(something._name, "something", "attribute name not set correctly")
+ self.assertEquals(something._parent, mock, "attribute parent not set correctly")
- def testMethodCallsRecorded(self):
- mock = Mock()
- mock.something(3, fish=None)
- mock.something_else.something(6, cake=sentinel.Cake)
-
- self.assertEquals(mock.something_else.method_calls,
- [("something", (6,), {'cake': sentinel.Cake})],
- "method calls not recorded correctly")
- self.assertEquals(mock.method_calls,
- [("something", (3,), {'fish': None}),
- ("something_else.something", (6,), {'cake': sentinel.Cake})],
- "method calls not recorded correctly")
-
-
- def testOnlyAllowedMethodsExist(self):
- methods = ["something"]
- mock = Mock(methods=methods)
-
- # this should be allowed
- mock.something
- self.assertRaisesWithMessage(AttributeError,
- "object has no attribute 'something_else'",
- lambda: mock.something_else)
+ def testMethodCallsRecorded(self):
+ mock = Mock()
+ mock.something(3, fish=None)
+ mock.something_else.something(6, cake=sentinel.Cake)
+
+ self.assertEquals(mock.something_else.method_calls,
+ [("something", (6,), {'cake': sentinel.Cake})],
+ "method calls not recorded correctly")
+ self.assertEquals(mock.method_calls,
+ [("something", (3,), {'fish': None}),
+ ("something_else.something", (6,), {'cake': sentinel.Cake})],
+ "method calls not recorded correctly")
+
+
+ def testOnlyAllowedMethodsExist(self):
+ methods = ["something"]
+ mock = Mock(methods=methods)
+
+ # this should be allowed
+ mock.something
+ self.assertRaisesWithMessage(AttributeError,
+ "object has no attribute 'something_else'",
+ lambda: mock.something_else)
-
- def testFromSpec(self):
- class Something(object):
- x = 3
- __something__ = None
- def y(self):
- pass
-
- def testAttributes(mock):
- # should work
- mock.x
- mock.y
- self.assertRaisesWithMessage(AttributeError,
- "object has no attribute 'z'",
- lambda: mock.z)
- self.assertRaisesWithMessage(AttributeError,
- "object has no attribute '__something__'",
- lambda: mock.__something__)
-
- testAttributes(Mock(spec=Something))
- testAttributes(Mock(spec=Something()))
-
+
+ def testFromSpec(self):
+ class Something(object):
+ x = 3
+ __something__ = None
+ def y(self):
+ pass
+
+ def testAttributes(mock):
+ # should work
+ mock.x
+ mock.y
+ self.assertRaisesWithMessage(AttributeError,
+ "object has no attribute 'z'",
+ lambda: mock.z)
+ self.assertRaisesWithMessage(AttributeError,
+ "object has no attribute '__something__'",
+ lambda: mock.__something__)
+
+ testAttributes(Mock(spec=Something))
+ testAttributes(Mock(spec=Something()))
+
Please sign in to comment.
Something went wrong with that request. Please try again.