2018-06-15 19:38:35 -03:00
|
|
|
"Test delegator, coverage 100%."
|
|
|
|
|
2016-05-28 14:22:31 -03:00
|
|
|
from idlelib.delegator import Delegator
|
2018-06-15 19:38:35 -03:00
|
|
|
import unittest
|
|
|
|
|
2013-06-30 19:37:05 -03:00
|
|
|
|
|
|
|
class DelegatorTest(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_mydel(self):
|
2016-05-15 23:06:49 -03:00
|
|
|
# Test a simple use scenario.
|
2013-06-30 19:37:05 -03:00
|
|
|
|
2016-05-15 23:06:49 -03:00
|
|
|
# Initialize an int delegator.
|
2013-06-30 19:37:05 -03:00
|
|
|
mydel = Delegator(int)
|
|
|
|
self.assertIs(mydel.delegate, int)
|
|
|
|
self.assertEqual(mydel._Delegator__cache, set())
|
2016-05-15 23:06:49 -03:00
|
|
|
# Trying to access a non-attribute of int fails.
|
2013-06-30 19:37:05 -03:00
|
|
|
self.assertRaises(AttributeError, mydel.__getattr__, 'xyz')
|
2016-05-15 23:06:49 -03:00
|
|
|
|
|
|
|
# Add real int attribute 'bit_length' by accessing it.
|
2013-06-30 19:37:05 -03:00
|
|
|
bl = mydel.bit_length
|
|
|
|
self.assertIs(bl, int.bit_length)
|
|
|
|
self.assertIs(mydel.__dict__['bit_length'], int.bit_length)
|
|
|
|
self.assertEqual(mydel._Delegator__cache, {'bit_length'})
|
|
|
|
|
2016-05-15 23:06:49 -03:00
|
|
|
# Add attribute 'numerator'.
|
2013-06-30 19:37:05 -03:00
|
|
|
mydel.numerator
|
|
|
|
self.assertEqual(mydel._Delegator__cache, {'bit_length', 'numerator'})
|
|
|
|
|
2016-05-15 23:06:49 -03:00
|
|
|
# Delete 'numerator'.
|
2013-06-30 19:37:05 -03:00
|
|
|
del mydel.numerator
|
|
|
|
self.assertNotIn('numerator', mydel.__dict__)
|
2016-05-15 23:06:49 -03:00
|
|
|
# The current implementation leaves it in the name cache.
|
|
|
|
# self.assertIn('numerator', mydel._Delegator__cache)
|
|
|
|
# However, this is not required and not part of the specification
|
2013-06-30 19:37:05 -03:00
|
|
|
|
2016-05-15 23:06:49 -03:00
|
|
|
# Change delegate to float, first resetting the attributes.
|
|
|
|
mydel.setdelegate(float) # calls resetcache
|
2013-06-30 19:37:05 -03:00
|
|
|
self.assertNotIn('bit_length', mydel.__dict__)
|
|
|
|
self.assertEqual(mydel._Delegator__cache, set())
|
2016-05-15 23:06:49 -03:00
|
|
|
self.assertIs(mydel.delegate, float)
|
2013-06-30 19:37:05 -03:00
|
|
|
|
2018-06-15 19:38:35 -03:00
|
|
|
|
2013-06-30 19:37:05 -03:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main(verbosity=2, exit=2)
|