| 1 |
"Test delegator, coverage 100%." |
| 2 |
|
| 3 |
from idlelib.delegator import Delegator |
| 4 |
import unittest |
| 5 |
|
| 6 |
|
| 7 |
class DelegatorTest(unittest.TestCase): |
| 8 |
|
| 9 |
def test_mydel(self): |
| 10 |
# Test a simple use scenario. |
| 11 |
|
| 12 |
# Initialize an int delegator. |
| 13 |
mydel = Delegator(int) |
| 14 |
self.assertIs(mydel.delegate, int) |
| 15 |
self.assertEqual(mydel._Delegator__cache, set()) |
| 16 |
# Trying to access a non-attribute of int fails. |
| 17 |
self.assertRaises(AttributeError, mydel.__getattr__, 'xyz') |
| 18 |
|
| 19 |
# Add real int attribute 'bit_length' by accessing it. |
| 20 |
bl = mydel.bit_length |
| 21 |
self.assertIs(bl, int.bit_length) |
| 22 |
self.assertIs(mydel.__dict__['bit_length'], int.bit_length) |
| 23 |
self.assertEqual(mydel._Delegator__cache, {'bit_length'}) |
| 24 |
|
| 25 |
# Add attribute 'numerator'. |
| 26 |
mydel.numerator |
| 27 |
self.assertEqual(mydel._Delegator__cache, {'bit_length', 'numerator'}) |
| 28 |
|
| 29 |
# Delete 'numerator'. |
| 30 |
del mydel.numerator |
| 31 |
self.assertNotIn('numerator', mydel.__dict__) |
| 32 |
# The current implementation leaves it in the name cache. |
| 33 |
# self.assertIn('numerator', mydel._Delegator__cache) |
| 34 |
# However, this is not required and not part of the specification |
| 35 |
|
| 36 |
# Change delegate to float, first resetting the attributes. |
| 37 |
mydel.setdelegate(float) # calls resetcache |
| 38 |
self.assertNotIn('bit_length', mydel.__dict__) |
| 39 |
self.assertEqual(mydel._Delegator__cache, set()) |
| 40 |
self.assertIs(mydel.delegate, float) |
| 41 |
|
| 42 |
|
| 43 |
if __name__ == '__main__': |
| 44 |
unittest.main(verbosity=2, exit=2) |