| 1 |  |  | from collections import OrderedDict | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | from typing import List, Union, Tuple | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | from unittest import TestCase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | from typish import ClsDict | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 8 |  | View Code Duplication | class TestClsDict(TestCase): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     def test_invalid_initialization(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |         # Only one positional argument is accepted. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |         with self.assertRaises(TypeError): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |             ClsDict({}, {}) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |         # The positional argument must be a dict | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |         with self.assertRaises(TypeError): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |             ClsDict(1) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |         # The dict must have types as keys | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         with self.assertRaises(TypeError): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |             ClsDict({ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |                 int: 1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |                 str: 2, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |                 'float': 3, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |             }) | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     def test_getitem(self): | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |         cd = ClsDict({int: 123, str: '456'}) | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |         self.assertEqual(123, cd[42]) | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |         self.assertEqual('456', cd['test']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     def test_getitem_more_complicated(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         cd = ClsDict({ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |             List[Union[str, int]]: 1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |             Tuple[float, ...]: 2, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         }) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         self.assertEqual(1, cd[[1, 2, '3', 4]]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         self.assertEqual(2, cd[(1.0, 2.0, 3.0, 4.0)]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         with self.assertRaises(KeyError): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             self.assertEqual(2, cd[(1.0, 2.0, '3.0', 4.0)]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     def test_no_match(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         cd = ClsDict({str: '456'}) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         with self.assertRaises(KeyError): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             cd[42] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     def test_get(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         cd = ClsDict({str: '456'}) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         self.assertEqual('456', cd.get('test')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         self.assertEqual(None, cd.get(42)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         self.assertEqual(123, cd.get(42, 123)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     def test_ordereddict(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         od = OrderedDict([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             (int, 1), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             (object, 2), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         ]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         cd = ClsDict(od) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         self.assertEqual(1, cd[123]) | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 64 |  |  |         self.assertEqual(2, cd['123']) | 
            
                                                        
            
                                    
            
            
                | 65 |  |  |  |