diff -r a4b66e51e0bf Lib/idlelib/idle_test/test_delegator.py --- a/Lib/idlelib/idle_test/test_delegator.py Mon Apr 13 22:11:34 2015 +0200 +++ b/Lib/idlelib/idle_test/test_delegator.py Thu Apr 16 18:17:33 2015 -0400 @@ -6,32 +6,49 @@ def test_mydel(self): # test a simple use scenario - # initialize + # create a delegator for the int type mydel = Delegator(int) - self.assertIs(mydel.delegate, int) + + # "delegate" attr is the same as the original type + self.assertIs(mydel.delegate, int, + 'Delegate should have been for type int but instead was for type %s' % (mydel.delegate.__name__,)) + + # Delegator obj must have a cache + self.assertEqual(mydel._Delegator__cache, set(), + 'Delegator object\'s __cache attr should be an empty set, but instead was %s' % (str(mydel._Delegator__cache)[:80],)) + + self.assertRaises(AttributeError, mydel.__getattr__, 'ThisAttrDoesNotExist') + + # check that this Delegator obj has the same methods as the int type + self.assertIs(mydel.bit_length, int.bit_length, + 'Delegator object for type int doesn\'t have int\'s bit_length() method.') + + # check that cache cached bit_length correctly + self.assertEqual(mydel._Delegator__cache, {'bit_length'}) + + mydel.numerator # caches numerator attr + self.assertEqual(mydel._Delegator__cache, {'bit_length', 'numerator'}) + + # delete the numerator attr (but leaves it in the name cache) + del mydel.numerator + self.assertNotIn('numerator', mydel.__dict__) + self.assertIn('numerator', mydel._Delegator__cache, + 'Delegator object\'s cache doesn\'t have the numerator() method, but even after it was deleted from the object\'s namespace.') + + # resetcache() should remove everything from the cache + mydel.resetcache() + self.assertIs(mydel.delegate, int, + 'After resetcache() is called, Delegate should still be for type int but instead was for type %s' % (mydel.delegate.__name__,)) + self.assertEqual(mydel._Delegator__cache, set(), + 'After resetcache() is called, the cache should be an empty set, but instead was %s' % (str(mydel._Delegator__cache)[:80],)) + + # reset by calling setdelegate() (which calls resetcache()) + mydel.setdelegate(float) + self.assertIsNot(mydel.delegate, int, + 'Delegate is still for int type even after setdelegate(float) was called to change it to be for float type.') + self.assertIs(mydel.delegate, float) self.assertEqual(mydel._Delegator__cache, set()) - # add an attribute: - self.assertRaises(AttributeError, mydel.__getattr__, 'xyz') - 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'}) - - # add a second attribute - mydel.numerator - self.assertEqual(mydel._Delegator__cache, {'bit_length', 'numerator'}) - - # delete the second (which, however, leaves it in the name cache) - del mydel.numerator - self.assertNotIn('numerator', mydel.__dict__) - self.assertIn('numerator', mydel._Delegator__cache) - - # reset by calling .setdelegate, which calls .resetcache - mydel.setdelegate(float) - self.assertIs(mydel.delegate, float) - self.assertNotIn('bit_length', mydel.__dict__) - self.assertEqual(mydel._Delegator__cache, set()) if __name__ == '__main__': unittest.main(verbosity=2, exit=2)