| 1 |  |  | from typing import Tuple, Any, Union | 
            
                                                        
            
                                    
            
            
                | 2 |  |  | from unittest import TestCase | 
            
                                                        
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 4 |  |  | from typish import ClsDict, EllipsisType, Literal | 
            
                                                        
            
                                    
            
            
                | 5 |  |  | from typish._classes import ClsFunction | 
            
                                                        
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 8 |  |  | class TestClsFunction(TestCase): | 
            
                                                        
            
                                    
            
            
                | 9 |  |  |     def test_invalid_initialization(self): | 
            
                                                        
            
                                    
            
            
                | 10 |  |  |         # Only ClsDict or dict is allowed | 
            
                                                        
            
                                    
            
            
                | 11 |  |  |         with self.assertRaises(TypeError): | 
            
                                                        
            
                                    
            
            
                | 12 |  |  |             ClsFunction(123) | 
            
                                                        
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 14 |  |  |     def test_instantiation_with_no_callable(self): | 
            
                                                        
            
                                    
            
            
                | 15 |  |  |         with self.assertRaises(TypeError): | 
            
                                                        
            
                                    
            
            
                | 16 |  |  |             ClsFunction({ | 
            
                                                        
            
                                    
            
            
                | 17 |  |  |                 int: lambda: 1, | 
            
                                                        
            
                                    
            
            
                | 18 |  |  |                 str: lambda: 2, | 
            
                                                        
            
                                    
            
            
                | 19 |  |  |                 object: 3,  # Invalid! | 
            
                                                        
            
                                    
            
            
                | 20 |  |  |             }) | 
            
                                                        
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 22 |  |  |     def test_with_dict(self): | 
            
                                                        
            
                                    
            
            
                | 23 |  |  |         function = ClsFunction({ | 
            
                                                        
            
                                    
            
            
                | 24 |  |  |             int: lambda x: x * 2, | 
            
                                                        
            
                                    
            
            
                | 25 |  |  |             str: lambda x: '{}_'.format(x), | 
            
                                                        
            
                                    
            
            
                | 26 |  |  |         }) | 
            
                                                        
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 28 |  |  |         self.assertEqual(4, function(2)) | 
            
                                                        
            
                                    
            
            
                | 29 |  |  |         self.assertEqual('2_', function('2')) | 
            
                                                        
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 31 |  |  |     def test_with_cls_dict(self): | 
            
                                                        
            
                                    
            
            
                | 32 |  |  |         function = ClsFunction(ClsDict({ | 
            
                                                        
            
                                    
            
            
                | 33 |  |  |             int: lambda x: x * 2, | 
            
                                                        
            
                                    
            
            
                | 34 |  |  |             str: lambda x: '{}_'.format(x), | 
            
                                                        
            
                                    
            
            
                | 35 |  |  |         })) | 
            
                                                        
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 37 |  |  |         self.assertEqual(4, function(2)) | 
            
                                                        
            
                                    
            
            
                | 38 |  |  |         self.assertEqual('2_', function('2')) | 
            
                                                        
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 40 |  |  |     def test_with_callables(self): | 
            
                                                        
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 42 |  |  |         def f1(x: int): | 
            
                                                        
            
                                    
            
            
                | 43 |  |  |             return 1 | 
            
                                                        
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 45 |  |  |         class C: | 
            
                                                        
            
                                    
            
            
                | 46 |  |  |             def m1(self, x: str): | 
            
                                                        
            
                                    
            
            
                | 47 |  |  |                 return 2 | 
            
                                                        
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 49 |  |  |             @classmethod | 
            
                                                        
            
                                    
            
            
                | 50 |  |  |             def m2(cls, x: float): | 
            
                                                        
            
                                    
            
            
                | 51 |  |  |                 return 3 | 
            
                                                        
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 53 |  |  |             @staticmethod | 
            
                                                        
            
                                    
            
            
                | 54 |  |  |             def m3(x: list): | 
            
                                                        
            
                                    
            
            
                | 55 |  |  |                 return 4 | 
            
                                                        
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 57 |  |  |         def f2(x):  # No type hint | 
            
                                                        
            
                                    
            
            
                | 58 |  |  |             return 5 | 
            
                                                        
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 60 |  |  |         function = ClsFunction([f1, C().m1, C.m2, C.m3, f2]) | 
            
                                                        
            
                                    
            
            
                | 61 |  |  |         self.assertEqual(1, function(42)) | 
            
                                                        
            
                                    
            
            
                | 62 |  |  |         self.assertEqual(2, function('hello')) | 
            
                                                        
            
                                    
            
            
                | 63 |  |  |         self.assertEqual(3, function(42.0)) | 
            
                                                        
            
                                    
            
            
                | 64 |  |  |         self.assertEqual(4, function([42])) | 
            
                                                        
            
                                    
            
            
                | 65 |  |  |         self.assertEqual(5, function({})) | 
            
                                                        
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 67 |  |  |     def test_with_invalid_callables(self): | 
            
                                                        
            
                                    
            
            
                | 68 |  |  |         def f(): | 
            
                                                        
            
                                    
            
            
                | 69 |  |  |             ... | 
            
                                                        
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 71 |  |  |         with self.assertRaises(TypeError): | 
            
                                                        
            
                                    
            
            
                | 72 |  |  |             ClsFunction([f]) | 
            
                                                        
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 74 |  |  |     def test_multiple_args(self): | 
            
                                                        
            
                                    
            
            
                | 75 |  |  |         function = ClsFunction({ | 
            
                                                        
            
                                    
            
            
                | 76 |  |  |             int: lambda x, y: x * y, | 
            
                                                        
            
                                    
            
            
                | 77 |  |  |             str: lambda x, y: '{}{}'.format(x, y), | 
            
                                                        
            
                                    
            
            
                | 78 |  |  |         }) | 
            
                                                        
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 80 |  |  |         self.assertEqual(6, function(2, 3)) | 
            
                                                        
            
                                    
            
            
                | 81 |  |  |         self.assertEqual('23', function('2', 3)) | 
            
                                                        
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 83 |  |  |     def test_understands(self): | 
            
                                                        
            
                                    
            
            
                | 84 |  |  |         function = ClsFunction({ | 
            
                                                        
            
                                    
            
            
                | 85 |  |  |             int: lambda _: ..., | 
            
                                                        
            
                                    
            
            
                | 86 |  |  |             str: lambda _: ..., | 
            
                                                        
            
                                    
            
            
                | 87 |  |  |         }) | 
            
                                                        
            
                                    
            
            
                | 88 |  |  |         self.assertTrue(function.understands(1)) | 
            
                                                        
            
                                    
            
            
                | 89 |  |  |         self.assertTrue(function.understands('2')) | 
            
                                                        
            
                                    
            
            
                | 90 |  |  |         self.assertTrue(not function.understands(3.0)) | 
            
                                                        
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 92 |  |  |     def test_call_no_args(self): | 
            
                                                        
            
                                    
            
            
                | 93 |  |  |         function = ClsFunction({ | 
            
                                                        
            
                                    
            
            
                | 94 |  |  |             int: lambda x: 1, | 
            
                                                        
            
                                    
            
            
                | 95 |  |  |         }) | 
            
                                                        
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 97 |  |  |         with self.assertRaises(TypeError): | 
            
                                                        
            
                                    
            
            
                | 98 |  |  |             function() | 
            
                                                        
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 100 |  |  |     def test_call_invalid_args(self): | 
            
                                                        
            
                                    
            
            
                | 101 |  |  |         function = ClsFunction({ | 
            
                                                        
            
                                    
            
            
                | 102 |  |  |             int: lambda x: 1, | 
            
                                                        
            
                                    
            
            
                | 103 |  |  |         }) | 
            
                                                        
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 105 |  |  |         with self.assertRaises(TypeError): | 
            
                                                        
            
                                    
            
            
                | 106 |  |  |             function(1, 2)  # The lambda expects only 1 argument. | 
            
                                                        
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 108 |  |  |     def test_complex_cls_function(self): | 
            
                                                        
            
                                    
            
            
                | 109 |  |  |         # Test if a more complex ClsFunction can be created without problems. | 
            
                                                        
            
                                    
            
            
                | 110 |  |  |         _Size = Union[int, Literal[Any]] | 
            
                                                        
            
                                    
            
            
                | 111 |  |  |         _Type = Union[type, Literal[Any]] | 
            
                                                        
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 113 |  |  |         ClsFunction({ | 
            
                                                        
            
                                    
            
            
                | 114 |  |  |             _Size: lambda: 1, | 
            
                                                        
            
                                    
            
            
                | 115 |  |  |             _Type: lambda: 2, | 
            
                                                        
            
                                    
            
            
                | 116 |  |  |             Tuple[_Size, _Type]: lambda: 3, | 
            
                                                        
            
                                    
            
            
                | 117 |  |  |             Tuple[_Size, ...]: lambda: 4, | 
            
                                                        
            
                                    
            
            
                | 118 |  |  |             Tuple[Tuple[_Size, ...], _Type]: lambda: 5, | 
            
                                                        
            
                                    
            
            
                | 119 |  |  |             Tuple[Tuple[_Size, EllipsisType], _Type]: lambda: 6, | 
            
                                                        
            
                                    
            
            
                | 120 |  |  |             Tuple[Tuple[Literal[Any], EllipsisType], Literal[Any]]: lambda: 7, | 
            
                                                        
            
                                    
            
            
                | 121 |  |  |         }) | 
            
                                                        
            
                                    
            
            
                | 122 |  |  |  |