17db96d56Sopenharmony_ci"Test delegator, coverage 100%." 27db96d56Sopenharmony_ci 37db96d56Sopenharmony_cifrom idlelib.delegator import Delegator 47db96d56Sopenharmony_ciimport unittest 57db96d56Sopenharmony_ci 67db96d56Sopenharmony_ci 77db96d56Sopenharmony_ciclass DelegatorTest(unittest.TestCase): 87db96d56Sopenharmony_ci 97db96d56Sopenharmony_ci def test_mydel(self): 107db96d56Sopenharmony_ci # Test a simple use scenario. 117db96d56Sopenharmony_ci 127db96d56Sopenharmony_ci # Initialize an int delegator. 137db96d56Sopenharmony_ci mydel = Delegator(int) 147db96d56Sopenharmony_ci self.assertIs(mydel.delegate, int) 157db96d56Sopenharmony_ci self.assertEqual(mydel._Delegator__cache, set()) 167db96d56Sopenharmony_ci # Trying to access a non-attribute of int fails. 177db96d56Sopenharmony_ci self.assertRaises(AttributeError, mydel.__getattr__, 'xyz') 187db96d56Sopenharmony_ci 197db96d56Sopenharmony_ci # Add real int attribute 'bit_length' by accessing it. 207db96d56Sopenharmony_ci bl = mydel.bit_length 217db96d56Sopenharmony_ci self.assertIs(bl, int.bit_length) 227db96d56Sopenharmony_ci self.assertIs(mydel.__dict__['bit_length'], int.bit_length) 237db96d56Sopenharmony_ci self.assertEqual(mydel._Delegator__cache, {'bit_length'}) 247db96d56Sopenharmony_ci 257db96d56Sopenharmony_ci # Add attribute 'numerator'. 267db96d56Sopenharmony_ci mydel.numerator 277db96d56Sopenharmony_ci self.assertEqual(mydel._Delegator__cache, {'bit_length', 'numerator'}) 287db96d56Sopenharmony_ci 297db96d56Sopenharmony_ci # Delete 'numerator'. 307db96d56Sopenharmony_ci del mydel.numerator 317db96d56Sopenharmony_ci self.assertNotIn('numerator', mydel.__dict__) 327db96d56Sopenharmony_ci # The current implementation leaves it in the name cache. 337db96d56Sopenharmony_ci # self.assertIn('numerator', mydel._Delegator__cache) 347db96d56Sopenharmony_ci # However, this is not required and not part of the specification 357db96d56Sopenharmony_ci 367db96d56Sopenharmony_ci # Change delegate to float, first resetting the attributes. 377db96d56Sopenharmony_ci mydel.setdelegate(float) # calls resetcache 387db96d56Sopenharmony_ci self.assertNotIn('bit_length', mydel.__dict__) 397db96d56Sopenharmony_ci self.assertEqual(mydel._Delegator__cache, set()) 407db96d56Sopenharmony_ci self.assertIs(mydel.delegate, float) 417db96d56Sopenharmony_ci 427db96d56Sopenharmony_ci 437db96d56Sopenharmony_ciif __name__ == '__main__': 447db96d56Sopenharmony_ci unittest.main(verbosity=2, exit=2) 45