ยปCore Development>Code coverage>Lib/idlelib/idle_test/test_delegator.py

Python code coverage for Lib/idlelib/idle_test/test_delegator.py

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