15
15
from ._compat import range
16
16
17
17
18
- def interface_test_class_fixture ():
18
+ def interface_test_class_fixture (subnamespace ):
19
19
"""Delay creation of class until test starts."""
20
20
21
21
class InterfaceTestClass (IInterfaceTest ):
22
22
"""class that implements the test interface"""
23
- __namespace__ = "Python.Test"
23
+ __namespace__ = "Python.Test." + subnamespace
24
24
25
25
def foo (self ):
26
26
return "InterfaceTestClass"
@@ -31,12 +31,12 @@ def bar(self, x, i):
31
31
return InterfaceTestClass
32
32
33
33
34
- def derived_class_fixture ():
34
+ def derived_class_fixture (subnamespace ):
35
35
"""Delay creation of class until test starts."""
36
36
37
37
class DerivedClass (SubClassTest ):
38
38
"""class that derives from a class deriving from IInterfaceTest"""
39
- __namespace__ = "Python.Test"
39
+ __namespace__ = "Python.Test." + subnamespace
40
40
41
41
def foo (self ):
42
42
return "DerivedClass"
@@ -60,12 +60,12 @@ def return_list(self):
60
60
return DerivedClass
61
61
62
62
63
- def derived_event_test_class_fixture ():
63
+ def derived_event_test_class_fixture (subnamespace ):
64
64
"""Delay creation of class until test starts."""
65
65
66
66
class DerivedEventTest (IInterfaceTest ):
67
67
"""class that implements IInterfaceTest.TestEvent"""
68
- __namespace__ = "Python.Test"
68
+ __namespace__ = "Python.Test." + subnamespace
69
69
70
70
def __init__ (self ):
71
71
self .event_handlers = []
@@ -99,7 +99,7 @@ def test_base_class():
99
99
100
100
def test_interface ():
101
101
"""Test python classes can derive from C# interfaces"""
102
- InterfaceTestClass = interface_test_class_fixture ()
102
+ InterfaceTestClass = interface_test_class_fixture (test_interface . __name__ )
103
103
ob = InterfaceTestClass ()
104
104
assert ob .foo () == "InterfaceTestClass"
105
105
assert FunctionsTest .test_foo (ob ) == "InterfaceTestClass"
@@ -112,7 +112,7 @@ def test_interface():
112
112
113
113
def test_derived_class ():
114
114
"""Test python class derived from managed type"""
115
- DerivedClass = derived_class_fixture ()
115
+ DerivedClass = derived_class_fixture (test_derived_class . __name__ )
116
116
ob = DerivedClass ()
117
117
assert ob .foo () == "DerivedClass"
118
118
assert ob .base_foo () == "foo"
@@ -128,10 +128,9 @@ def test_derived_class():
128
128
assert id (x ) == id (ob )
129
129
130
130
131
- @pytest .mark .skip (reason = "FIXME: test randomly pass/fails" )
132
131
def test_create_instance ():
133
132
"""Test derived instances can be created from managed code"""
134
- DerivedClass = derived_class_fixture ()
133
+ DerivedClass = derived_class_fixture (test_create_instance . __name__ )
135
134
ob = FunctionsTest .create_instance (DerivedClass )
136
135
assert ob .foo () == "DerivedClass"
137
136
assert FunctionsTest .test_foo (ob ) == "DerivedClass"
@@ -142,7 +141,7 @@ def test_create_instance():
142
141
x = FunctionsTest .pass_through (ob )
143
142
assert id (x ) == id (ob )
144
143
145
- InterfaceTestClass = interface_test_class_fixture ()
144
+ InterfaceTestClass = interface_test_class_fixture (test_create_instance . __name__ )
146
145
ob2 = FunctionsTest .create_instance (InterfaceTestClass )
147
146
assert ob2 .foo () == "InterfaceTestClass"
148
147
assert FunctionsTest .test_foo (ob2 ) == "InterfaceTestClass"
@@ -153,7 +152,6 @@ def test_create_instance():
153
152
assert id (y ) == id (ob2 )
154
153
155
154
156
- @pytest .mark .skip (reason = "FIXME: test randomly pass/fails" )
157
155
def test_events ():
158
156
class EventHandler (object ):
159
157
def handler (self , x , args ):
@@ -166,12 +164,12 @@ def handler(self, x, args):
166
164
assert FunctionsTest .test_event (x , 1 ) == 1
167
165
assert event_handler .value == 1
168
166
169
- InterfaceTestClass = interface_test_class_fixture ()
167
+ InterfaceTestClass = interface_test_class_fixture (test_events . __name__ )
170
168
i = InterfaceTestClass ()
171
169
with pytest .raises (System .NotImplementedException ):
172
170
FunctionsTest .test_event (i , 2 )
173
171
174
- DerivedEventTest = derived_event_test_class_fixture ()
172
+ DerivedEventTest = derived_event_test_class_fixture (test_events . __name__ )
175
173
d = DerivedEventTest ()
176
174
d .add_TestEvent (event_handler .handler )
177
175
assert FunctionsTest .test_event (d , 3 ) == 3
@@ -190,3 +188,59 @@ def test_isinstance_check():
190
188
for x in b :
191
189
assert isinstance (x , System .Object )
192
190
assert isinstance (x , System .String )
191
+
192
+ def test_namespace_and_init ():
193
+ calls = []
194
+ class TestX (System .Object ):
195
+ __namespace__ = "test_clr_subclass_with_init_args"
196
+ def __init__ (self , * args , ** kwargs ):
197
+ calls .append ((args , kwargs ))
198
+ t = TestX (1 ,2 ,3 ,foo = "bar" )
199
+ assert len (calls ) == 1
200
+ assert calls [0 ][0 ] == (1 ,2 ,3 )
201
+ assert calls [0 ][1 ] == {"foo" :"bar" }
202
+
203
+ def test_namespace_and_argless_init ():
204
+ calls = []
205
+ class TestX (System .Object ):
206
+ __namespace__ = "test_clr_subclass_without_init_args"
207
+ def __init__ (self ):
208
+ calls .append (True )
209
+ t = TestX ()
210
+ assert len (calls ) == 1
211
+ assert calls [0 ] == True
212
+
213
+
214
+ def test_namespace_and_no_init ():
215
+ class TestX (System .Object ):
216
+ __namespace__ = "test_clr_subclass_without_init"
217
+ q = 1
218
+ t = TestX ()
219
+ assert t .q == 1
220
+
221
+ def test_construction_from_clr ():
222
+ import clr
223
+ calls = []
224
+ class TestX (System .Object ):
225
+ __namespace__ = "test_clr_subclass_init_from_clr"
226
+ @clr .clrmethod (None , [int , str ])
227
+ def __init__ (self , i , s ):
228
+ calls .append ((i , s ))
229
+
230
+ # Construct a TestX from Python
231
+ t = TestX (1 , "foo" )
232
+ assert len (calls ) == 1
233
+ assert calls [0 ][0 ] == 1
234
+ assert calls [0 ][1 ] == "foo"
235
+
236
+ # Reset calls and construct a TestX from CLR
237
+ calls = []
238
+ tp = t .GetType ()
239
+ t2 = tp .GetConstructors ()[0 ].Invoke (None )
240
+ assert len (calls ) == 0
241
+
242
+ # The object has only been constructed, now it needs to be initialized as well
243
+ tp .GetMethod ("__init__" ).Invoke (t2 , [1 , "foo" ])
244
+ assert len (calls ) == 1
245
+ assert calls [0 ][0 ] == 1
246
+ assert calls [0 ][1 ] == "foo"
0 commit comments