11using System ;
22using System . Collections . Generic ;
3- using System . Linq ;
4- using System . Text ;
53using NUnit . Framework ;
64using Python . Runtime ;
75
86namespace Python . EmbeddingTest
97{
108 class QCTests
119 {
10+ private static dynamic pythonSuperInitInt ;
11+ private static dynamic pythonSuperInitDefault ;
12+ private static dynamic pythonSuperInitNone ;
13+ private static dynamic pythonSuperInitNotCallingBase ;
14+
15+ private static dynamic withArgs_PythonSuperInitNotCallingBase ;
16+ private static dynamic withArgs_PythonSuperInitDefault ;
17+ private static dynamic withArgs_PythonSuperInitInt ;
18+
19+ private static dynamic pureCSharpConstruction ;
20+
1221 private static dynamic containsTest ;
1322 private static dynamic module ;
1423 private static string testModule = @"
1524from clr import AddReference
1625AddReference(""System"")
1726AddReference(""Python.EmbeddingTest"")
18- from Python.EmbeddingTest import Algo, Insight
27+ from Python.EmbeddingTest import *
1928class PythonModule(Algo):
2029 def TestA(self):
2130 try:
@@ -28,6 +37,37 @@ def ContainsTest(key, collection):
2837 if key in collection.Keys:
2938 return True
3039 return False
40+
41+ class WithArgs_PythonSuperInitNotCallingBase(SuperInit):
42+ def __init__(self, jose):
43+ return
44+
45+ class WithArgs_PythonSuperInitDefault(SuperInit):
46+ def __init__(self, jose):
47+ super().__init__()
48+
49+ class WithArgs_PythonSuperInitInt(SuperInit):
50+ def __init__(self, jose):
51+ super().__init__(jose)
52+
53+ class PythonSuperInitNotCallingBase(SuperInit):
54+ def __init__(self):
55+ return
56+
57+ class PythonSuperInitDefault(SuperInit):
58+ def __init__(self):
59+ super().__init__()
60+
61+ class PythonSuperInitInt(SuperInit):
62+ def __init__(self):
63+ super().__init__(1)
64+
65+ class PythonSuperInitNone(SuperInit):
66+ def jose(self):
67+ return 1
68+
69+ def PureCSharpConstruction():
70+ return SuperInit(1)
3171" ;
3272
3373 [ OneTimeSetUp ]
@@ -37,6 +77,17 @@ public void Setup()
3777 var pyModule = PyModule . FromString ( "module" , testModule ) ;
3878 containsTest = pyModule . GetAttr ( "ContainsTest" ) ;
3979 module = pyModule . GetAttr ( "PythonModule" ) . Invoke ( ) ;
80+
81+ pythonSuperInitInt = pyModule . GetAttr ( "PythonSuperInitInt" ) ;
82+ pythonSuperInitDefault = pyModule . GetAttr ( "PythonSuperInitDefault" ) ;
83+ pythonSuperInitNone = pyModule . GetAttr ( "PythonSuperInitNone" ) ;
84+ pythonSuperInitNotCallingBase = pyModule . GetAttr ( "PythonSuperInitNotCallingBase" ) ;
85+
86+ withArgs_PythonSuperInitNotCallingBase = pyModule . GetAttr ( "WithArgs_PythonSuperInitNotCallingBase" ) ;
87+ withArgs_PythonSuperInitDefault = pyModule . GetAttr ( "WithArgs_PythonSuperInitDefault" ) ;
88+ withArgs_PythonSuperInitInt = pyModule . GetAttr ( "WithArgs_PythonSuperInitInt" ) ;
89+
90+ pureCSharpConstruction = pyModule . GetAttr ( "PureCSharpConstruction" ) ;
4091 }
4192
4293 [ OneTimeTearDown ]
@@ -62,6 +113,98 @@ public void ContainsTest(string key, bool expected)
62113 var dic = new Dictionary < string , object > { { "SPY" , new object ( ) } } ;
63114 Assert . AreEqual ( expected , ( bool ) containsTest ( key , dic ) ) ;
64115 }
116+
117+ [ Test ]
118+ public void PureCSharpConstruction ( )
119+ {
120+ using ( Py . GIL ( ) )
121+ {
122+ var instance = pureCSharpConstruction ( ) ;
123+ Assert . AreEqual ( 1 , ( int ) instance . CalledInt ) ;
124+ Assert . AreEqual ( 1 , ( int ) instance . CalledDefault ) ;
125+ }
126+ }
127+
128+ [ Test ]
129+ public void WithArgs_NoBaseConstructorCall ( )
130+ {
131+ using ( Py . GIL ( ) )
132+ {
133+ var instance = withArgs_PythonSuperInitNotCallingBase ( 1 ) ;
134+ Assert . AreEqual ( 0 , ( int ) instance . CalledInt ) ;
135+ // we call the constructor always
136+ Assert . AreEqual ( 1 , ( int ) instance . CalledDefault ) ;
137+ }
138+ }
139+
140+ [ Test ]
141+ public void WithArgs_IntConstructor ( )
142+ {
143+ using ( Py . GIL ( ) )
144+ {
145+ var instance = withArgs_PythonSuperInitInt ( 1 ) ;
146+ Assert . AreEqual ( 1 , ( int ) instance . CalledInt ) ;
147+ Assert . AreEqual ( 1 , ( int ) instance . CalledDefault ) ;
148+ }
149+ }
150+
151+ [ Test ]
152+ public void WithArgs_DefaultConstructor ( )
153+ {
154+ using ( Py . GIL ( ) )
155+ {
156+ var instance = withArgs_PythonSuperInitDefault ( 1 ) ;
157+ Assert . AreEqual ( 0 , ( int ) instance . CalledInt ) ;
158+ Assert . AreEqual ( 2 , ( int ) instance . CalledDefault ) ;
159+ }
160+ }
161+
162+ [ Test ]
163+ public void NoArgs_NoBaseConstructorCall ( )
164+ {
165+ using ( Py . GIL ( ) )
166+ {
167+ var instance = pythonSuperInitNotCallingBase ( ) ;
168+ Assert . AreEqual ( 0 , ( int ) instance . CalledInt ) ;
169+ // this is true because we call the default constructor always
170+ Assert . AreEqual ( 1 , ( int ) instance . CalledDefault ) ;
171+ }
172+ }
173+
174+ [ Test ]
175+ public void NoArgs_IntConstructor ( )
176+ {
177+ using ( Py . GIL ( ) )
178+ {
179+ var instance = pythonSuperInitInt ( ) ;
180+ Assert . AreEqual ( 1 , ( int ) instance . CalledInt ) ;
181+ // this is true because we call the default constructor always
182+ Assert . AreEqual ( 1 , ( int ) instance . CalledDefault ) ;
183+ }
184+ }
185+
186+ [ Test ]
187+ public void NoArgs_DefaultConstructor ( )
188+ {
189+ using ( Py . GIL ( ) )
190+ {
191+ var instance = pythonSuperInitNone ( ) ;
192+ Assert . AreEqual ( 0 , ( int ) instance . CalledInt ) ;
193+ Assert . AreEqual ( 2 , ( int ) instance . CalledDefault ) ;
194+ }
195+ }
196+
197+ [ Test ]
198+ public void NoArgs_NoConstructor ( )
199+ {
200+ using ( Py . GIL ( ) )
201+ {
202+ var instance = pythonSuperInitDefault . Invoke ( ) ;
203+
204+ Assert . AreEqual ( 0 , ( int ) instance . CalledInt ) ;
205+ Assert . AreEqual ( 2 , ( int ) instance . CalledDefault ) ;
206+ }
207+ }
65208 }
66209
67210 public class Algo
@@ -83,6 +226,20 @@ public void EmitInsights(params Insight[] insights)
83226
84227 }
85228
229+ public class SuperInit
230+ {
231+ public int CalledInt { get ; private set ; }
232+ public int CalledDefault { get ; private set ; }
233+ public SuperInit ( int a )
234+ {
235+ CalledInt ++ ;
236+ }
237+ public SuperInit ( )
238+ {
239+ CalledDefault ++ ;
240+ }
241+ }
242+
86243 public class Insight
87244 {
88245 public string info ;
0 commit comments