|
5 | 5 | import Python.Test as Test
|
6 | 6 | import System
|
7 | 7 |
|
| 8 | +constructor_throw_on_arg_match_is_fixed = False # currently, failed match on super() is silently ignored, and tests will fail if set to true |
| 9 | +constructor_to_sub_class_accepts_specific_parameters= False # currently, we can't pass arguments to super() class ct. |
8 | 10 |
|
9 | 11 | class ConstructorTests(unittest.TestCase):
|
10 | 12 | """Test CLR class constructor support."""
|
@@ -46,6 +48,27 @@ class sub(System.Exception):
|
46 | 48 | ob = SubclassConstructorTest(instance)
|
47 | 49 | self.assertTrue(isinstance(ob.value, System.Exception))
|
48 | 50 |
|
| 51 | + def testSubClassWithInternalArgsPassedToSuper(self): |
| 52 | + """ |
| 53 | + Verify we can sub-class a .NET class, in python, |
| 54 | + and pass a working set of arguments to our super class. |
| 55 | + """ |
| 56 | + from Python.Test import ToDoubleConstructorTest # does the job for this test |
| 57 | + |
| 58 | + class PySubClass(ToDoubleConstructorTest): |
| 59 | + def __init__(self,d): |
| 60 | + super().__init__('a',2.0,'c') |
| 61 | + self.d = d |
| 62 | + |
| 63 | + o = PySubClass('d') |
| 64 | + self.assertEqual( o.d,'d') |
| 65 | + if constructor_to_sub_class_accepts_specific_parameters: |
| 66 | + self.assertEqual( o.a,'a') |
| 67 | + self.assertAlmostEqual(o.b,2.0) |
| 68 | + self.assertEqual( o.c,'c') |
| 69 | + else: |
| 70 | + print("\n\n*** Warning passing parameters to super class is currently not verified\n") |
| 71 | + |
49 | 72 |
|
50 | 73 | def testConstructorArgumentMatching(self):
|
51 | 74 | """ Test that simple type promitions works for int->double """
|
@@ -81,8 +104,21 @@ def testIntToFloatConstructorArguments(self):
|
81 | 104 | o = ToFloatConstructorTest()
|
82 | 105 |
|
83 | 106 | def testConstructorRaiseExceptionIfNoMatch(self):
|
| 107 | + """ |
| 108 | + Notice: Due to the feature of .NET object as super-class, there is a |
| 109 | + 'hack' that after calling a constructor with the supplied arguments |
| 110 | + (and they fail to match the .NET class constructor for any reason) |
| 111 | + then it removes all the arguments, and retry the call. |
| 112 | + Now, if this succeeds, it will return an object, with default values. |
| 113 | + This *could* make sense, given that the .NET class *IS* subclassed, |
| 114 | + however, if the process is *not* a part of a sub-class construction, |
| 115 | + then this is at best very unexpected. |
| 116 | +
|
| 117 | +
|
| 118 | + """ |
| 119 | + |
84 | 120 | from Python.Test import ToDoubleConstructorTest
|
85 |
| - constructor_throw_on_arg_match_is_fixed=False |
| 121 | + |
86 | 122 |
|
87 | 123 | if constructor_throw_on_arg_match_is_fixed:
|
88 | 124 | with self.assertRaises(TypeError):
|
|
0 commit comments