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