Skip to content

Commit e698b57

Browse files
committed
added more test for illustrating super/sub-class and argument passing problems, tests runs, but with warning printed out
1 parent a65d793 commit e698b57

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

src/tests/test_constructors.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import Python.Test as Test
66
import System
77

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.
810

911
class ConstructorTests(unittest.TestCase):
1012
"""Test CLR class constructor support."""
@@ -46,6 +48,27 @@ class sub(System.Exception):
4648
ob = SubclassConstructorTest(instance)
4749
self.assertTrue(isinstance(ob.value, System.Exception))
4850

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+
4972

5073
def testConstructorArgumentMatching(self):
5174
""" Test that simple type promitions works for int->double """
@@ -81,8 +104,21 @@ def testIntToFloatConstructorArguments(self):
81104
o = ToFloatConstructorTest()
82105

83106
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+
84120
from Python.Test import ToDoubleConstructorTest
85-
constructor_throw_on_arg_match_is_fixed=False
121+
86122

87123
if constructor_throw_on_arg_match_is_fixed:
88124
with self.assertRaises(TypeError):

0 commit comments

Comments
 (0)