Example #1
0
 def testReturnValueForFollowingCallsIsCached(self):
   result = object()
   mock_fn = mock.Mock(side_effect=[result])
   mock_fn.__name__ = compatibility.NativeStr("MockFunction")
   fn = utils.RunOnce(mock_fn)
   self.assertIs(fn(), result)
   self.assertIs(fn(), result)
Example #2
0
 def testDecoratedFunctionIsCalledAtLeastOnce(self):
   mock_fn = mock.Mock()
   mock_fn.__name__ = compatibility.NativeStr("MockFunction")
   fn = utils.RunOnce(mock_fn)
   mock_fn.assert_not_called()
   fn()
   mock_fn.assert_called_once()
Example #3
0
 def testExceptionsArePassedThrough(self):
   mock_fn = mock.Mock(side_effect=ValueError())
   mock_fn.__name__ = compatibility.NativeStr("MockFunction")
   fn = utils.RunOnce(mock_fn)
   with self.assertRaises(ValueError):
     fn()
   with self.assertRaises(ValueError):
     fn()
Example #4
0
 def testDecoratedFunctionIsCalledAtMostOnce(self):
   mock_fn = mock.Mock(side_effect=[None, AssertionError()])
   mock_fn.__name__ = compatibility.NativeStr("MockFunction")
   fn = utils.RunOnce(mock_fn)
   fn()
   fn()
   fn()
   mock_fn.assert_called_once()
Example #5
0
 def testWrapsFunctionProperly(self):
   mock_fn = mock.Mock()
   mock_fn.__name__ = compatibility.NativeStr("MockFunction")
   fn = utils.RunOnce(mock_fn)
   self.assertEqual(fn.__name__, compatibility.NativeStr("MockFunction"))
Example #6
0
 def testReturnValueIsPassedThrough(self):
   mock_fn = mock.Mock(return_value="bar")
   mock_fn.__name__ = compatibility.NativeStr("MockFunction")
   fn = utils.RunOnce(mock_fn)
   self.assertEqual("bar", fn())
Example #7
0
 def testArgumentsArePassedThrough(self):
   mock_fn = mock.Mock()
   mock_fn.__name__ = compatibility.NativeStr("MockFunction")
   fn = utils.RunOnce(mock_fn)
   fn(1, 2, foo="bar")
   mock_fn.assert_called_once_with(1, 2, foo="bar")