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 10 mydel = Delegator(int) 11 self.assertIs(mydel.delegate, int) 12 self.assertEqual(mydel._Delegator__cache, set()) 13 14 # add an attribute: 15 self.assertRaises(AttributeError, mydel.__getattr__, 'xyz') 16 bl = mydel.bit_length 17 self.assertIs(bl, int.bit_length) 18 self.assertIs(mydel.__dict__['bit_length'], int.bit_length) 19 self.assertEqual(mydel._Delegator__cache, {'bit_length'}) 20 21 # add a second attribute 22 mydel.numerator 23 self.assertEqual(mydel._Delegator__cache, {'bit_length', 'numerator'}) 24 25 # delete the second (which, however, leaves it in the name cache) 26 del mydel.numerator 27 self.assertNotIn('numerator', mydel.__dict__) 28 self.assertIn('numerator', mydel._Delegator__cache) 29 30 # reset by calling .setdelegate, which calls .resetcache 31 mydel.setdelegate(float) 32 self.assertIs(mydel.delegate, float) 33 self.assertNotIn('bit_length', mydel.__dict__) 34 self.assertEqual(mydel._Delegator__cache, set()) 35 36if __name__ == '__main__': 37 unittest.main(verbosity=2, exit=2) 38