Visitar URL original
Constructor argument matching supports simple int to (float|double) types by sigbjorn · Pull Request #239 · pythonnet/pythonnet · GitHub
Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
added more test for illustrating super/sub-class and argument passing…
… problems, tests runs, but with warning printed out
  • Loading branch information
sigbjorn committed Jul 3, 2016
commit e698b57af58115568107f6a5275e2d3302b2b465
38 changes: 37 additions & 1 deletion src/tests/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import Python.Test as Test
import System

constructor_throw_on_arg_match_is_fixed = False # currently, failed match on super() is silently ignored, and tests will fail if set to true
constructor_to_sub_class_accepts_specific_parameters= False # currently, we can't pass arguments to super() class ct.

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

def testSubClassWithInternalArgsPassedToSuper(self):
"""
Verify we can sub-class a .NET class, in python,
and pass a working set of arguments to our super class.
"""
from Python.Test import ToDoubleConstructorTest # does the job for this test

class PySubClass(ToDoubleConstructorTest):
def __init__(self,d):
super().__init__('a',2.0,'c')
self.d = d

o = PySubClass('d')
self.assertEqual( o.d,'d')
if constructor_to_sub_class_accepts_specific_parameters:
self.assertEqual( o.a,'a')
self.assertAlmostEqual(o.b,2.0)
self.assertEqual( o.c,'c')
else:
print("\n\n*** Warning passing parameters to super class is currently not verified\n")


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

def testConstructorRaiseExceptionIfNoMatch(self):
"""
Notice: Due to the feature of .NET object as super-class, there is a
'hack' that after calling a constructor with the supplied arguments
(and they fail to match the .NET class constructor for any reason)
then it removes all the arguments, and retry the call.
Now, if this succeeds, it will return an object, with default values.
This *could* make sense, given that the .NET class *IS* subclassed,
however, if the process is *not* a part of a sub-class construction,
then this is at best very unexpected.


"""

from Python.Test import ToDoubleConstructorTest
constructor_throw_on_arg_match_is_fixed=False


if constructor_throw_on_arg_match_is_fixed:
with self.assertRaises(TypeError):
Expand Down