Coverage for idle_test/test_delegator.py: 92%

22 statements  

« prev     ^ index     » next       coverage.py v7.2.5, created at 2023-05-11 13:22 -0700

1"Test delegator, coverage 100%." 

2 

3from idlelib.delegator import Delegator 

4import unittest 

5 

6 

7class DelegatorTest(unittest.TestCase): 

8 

9 def test_mydel(self): 

10 # Test a simple use scenario. 

11 

12 # Initialize an int delegator. 

13 mydel = Delegator(int) 1a

14 self.assertIs(mydel.delegate, int) 1a

15 self.assertEqual(mydel._Delegator__cache, set()) 1a

16 # Trying to access a non-attribute of int fails. 

17 self.assertRaises(AttributeError, mydel.__getattr__, 'xyz') 1a

18 

19 # Add real int attribute 'bit_length' by accessing it. 

20 bl = mydel.bit_length 1a

21 self.assertIs(bl, int.bit_length) 1a

22 self.assertIs(mydel.__dict__['bit_length'], int.bit_length) 1a

23 self.assertEqual(mydel._Delegator__cache, {'bit_length'}) 1a

24 

25 # Add attribute 'numerator'. 

26 mydel.numerator 1a

27 self.assertEqual(mydel._Delegator__cache, {'bit_length', 'numerator'}) 1a

28 

29 # Delete 'numerator'. 

30 del mydel.numerator 1a

31 self.assertNotIn('numerator', mydel.__dict__) 1a

32 # The current implementation leaves it in the name cache. 

33 # self.assertIn('numerator', mydel._Delegator__cache) 

34 # However, this is not required and not part of the specification 

35 

36 # Change delegate to float, first resetting the attributes. 

37 mydel.setdelegate(float) # calls resetcache 1a

38 self.assertNotIn('bit_length', mydel.__dict__) 1a

39 self.assertEqual(mydel._Delegator__cache, set()) 1a

40 self.assertIs(mydel.delegate, float) 1a

41 

42 

43if __name__ == '__main__': 43 ↛ 44line 43 didn't jump to line 44, because the condition on line 43 was never true

44 unittest.main(verbosity=2, exit=2)