testsentinel.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import unittest
  2. import copy
  3. import pickle
  4. from unittest.mock import sentinel, DEFAULT
  5. class SentinelTest(unittest.TestCase):
  6. def testSentinels(self):
  7. self.assertEqual(sentinel.whatever, sentinel.whatever,
  8. 'sentinel not stored')
  9. self.assertNotEqual(sentinel.whatever, sentinel.whateverelse,
  10. 'sentinel should be unique')
  11. def testSentinelName(self):
  12. self.assertEqual(str(sentinel.whatever), 'sentinel.whatever',
  13. 'sentinel name incorrect')
  14. def testDEFAULT(self):
  15. self.assertIs(DEFAULT, sentinel.DEFAULT)
  16. def testBases(self):
  17. # If this doesn't raise an AttributeError then help(mock) is broken
  18. self.assertRaises(AttributeError, lambda: sentinel.__bases__)
  19. def testPickle(self):
  20. for proto in range(pickle.HIGHEST_PROTOCOL+1):
  21. with self.subTest(protocol=proto):
  22. pickled = pickle.dumps(sentinel.whatever, proto)
  23. unpickled = pickle.loads(pickled)
  24. self.assertIs(unpickled, sentinel.whatever)
  25. def testCopy(self):
  26. self.assertIs(copy.copy(sentinel.whatever), sentinel.whatever)
  27. self.assertIs(copy.deepcopy(sentinel.whatever), sentinel.whatever)
  28. if __name__ == '__main__':
  29. unittest.main()