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