Skip to content

Commit 686eba2

Browse files
committed
add unit tests to check python classes can derive from C# interfaces
1 parent 7417342 commit 686eba2

2 files changed

Lines changed: 66 additions & 25 deletions

File tree

pythonnet/src/testing/subclasstest.cs

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,16 @@
55

66
namespace Python.Test
77
{
8-
public class SubClassTest
8+
public interface IInterfaceTest
9+
{
10+
// simple test with no arguments
11+
string foo();
12+
13+
// test passing objects and boxing primitives
14+
string bar(string s, int i);
15+
}
16+
17+
public class SubClassTest : IInterfaceTest
918
{
1019
public SubClassTest()
1120
{
@@ -34,32 +43,35 @@ public virtual IList<string> return_list()
3443
return new List<string> { "a", "b", "c" };
3544
}
3645

37-
public static string test_foo(SubClassTest x)
46+
public static IList<string> test_list(SubClassTest x)
47+
{
48+
// calls into python if return_list is overriden
49+
return x.return_list();
50+
}
51+
}
52+
53+
public class TestFunctions
54+
{
55+
public static string test_foo(IInterfaceTest x)
3856
{
3957
// calls into python if foo is overriden
4058
return x.foo();
4159
}
4260

43-
public static string test_bar(SubClassTest x, string s, int i)
61+
public static string test_bar(IInterfaceTest x, string s, int i)
4462
{
4563
// calls into python if bar is overriden
4664
return x.bar(s, i);
4765
}
4866

49-
public static IList<string> test_list(SubClassTest x)
50-
{
51-
// calls into python if return_list is overriden
52-
return x.return_list();
53-
}
54-
5567
// test instances can be constructed in managed code
56-
public static SubClassTest create_instance(Type t)
68+
public static IInterfaceTest create_instance(Type t)
5769
{
58-
return (SubClassTest)t.GetConstructor(new Type[] {}).Invoke(new Object[] {});
70+
return (IInterfaceTest)t.GetConstructor(new Type[] {}).Invoke(new Object[] {});
5971
}
6072

6173
// test instances pass through managed code unchanged
62-
public static SubClassTest pass_through(SubClassTest s)
74+
public static IInterfaceTest pass_through(IInterfaceTest s)
6375
{
6476
return s;
6577
}

pythonnet/src/tests/test_subclass.py

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,22 @@
1111
clr.AddReference('System')
1212

1313
import sys, os, string, unittest, types
14-
from Python.Test import SubClassTest
14+
from Python.Test import TestFunctions, SubClassTest, IInterfaceTest
1515
from System.Collections.Generic import List
1616

17+
# class that implements the test interface
18+
class InterfaceTestClass(IInterfaceTest):
19+
def foo(self):
20+
return "InterfaceTestClass"
21+
22+
def bar(self, x, i):
23+
return "/".join([x] * i)
24+
25+
# class that derives from a class deriving from IInterfaceTest
1726
class DerivedClass(SubClassTest):
1827

1928
def foo(self):
20-
return "bar"
29+
return "DerivedClass"
2130

2231
def base_foo(self):
2332
return SubClassTest.foo(self)
@@ -42,41 +51,61 @@ def testBaseClass(self):
4251
"""Test base class managed type"""
4352
object = SubClassTest()
4453
self.assertEqual(object.foo(), "foo")
45-
self.assertEqual(SubClassTest.test_foo(object), "foo")
54+
self.assertEqual(TestFunctions.test_foo(object), "foo")
4655
self.assertEqual(object.bar("bar", 2), "bar")
47-
self.assertEqual(SubClassTest.test_bar(object, "bar", 2), "bar")
56+
self.assertEqual(TestFunctions.test_bar(object, "bar", 2), "bar")
4857
self.assertEqual(object.not_overriden(), "not_overriden")
4958
self.assertEqual(list(object.return_list()), ["a", "b", "c"])
5059
self.assertEqual(list(SubClassTest.test_list(object)), ["a", "b", "c"])
5160

61+
def testInterface(self):
62+
"""Test python classes can derive from C# interfaces"""
63+
object = InterfaceTestClass()
64+
self.assertEqual(object.foo(), "InterfaceTestClass")
65+
self.assertEqual(TestFunctions.test_foo(object), "InterfaceTestClass")
66+
self.assertEqual(object.bar("bar", 2), "bar/bar")
67+
self.assertEqual(TestFunctions.test_bar(object, "bar", 2), "bar/bar")
68+
69+
x = TestFunctions.pass_through(object)
70+
self.assertEqual(id(x), id(object))
71+
5272
def testDerivedClass(self):
5373
"""Test python class derived from managed type"""
5474
object = DerivedClass()
55-
self.assertEqual(object.foo(), "bar")
75+
self.assertEqual(object.foo(), "DerivedClass")
5676
self.assertEqual(object.base_foo(), "foo")
5777
self.assertEqual(object.super_foo(), "foo")
58-
self.assertEqual(SubClassTest.test_foo(object), "bar")
78+
self.assertEqual(TestFunctions.test_foo(object), "DerivedClass")
5979
self.assertEqual(object.bar("bar", 2), "bar_bar")
60-
self.assertEqual(SubClassTest.test_bar(object, "bar", 2), "bar_bar")
80+
self.assertEqual(TestFunctions.test_bar(object, "bar", 2), "bar_bar")
6181
self.assertEqual(object.not_overriden(), "not_overriden")
6282
self.assertEqual(list(object.return_list()), ["A", "B", "C"])
6383
self.assertEqual(list(SubClassTest.test_list(object)), ["A", "B", "C"])
6484

65-
x = SubClassTest.pass_through(object)
85+
x = TestFunctions.pass_through(object)
6686
self.assertEqual(id(x), id(object))
6787

6888
def testCreateInstance(self):
6989
"""Test derived instances can be created from managed code"""
70-
object = SubClassTest.create_instance(DerivedClass)
71-
self.assertEqual(object.foo(), "bar")
72-
self.assertEqual(SubClassTest.test_foo(object), "bar")
90+
object = TestFunctions.create_instance(DerivedClass)
91+
self.assertEqual(object.foo(), "DerivedClass")
92+
self.assertEqual(TestFunctions.test_foo(object), "DerivedClass")
7393
self.assertEqual(object.bar("bar", 2), "bar_bar")
74-
self.assertEqual(SubClassTest.test_bar(object, "bar", 2), "bar_bar")
94+
self.assertEqual(TestFunctions.test_bar(object, "bar", 2), "bar_bar")
7595
self.assertEqual(object.not_overriden(), "not_overriden")
7696

77-
x = SubClassTest.pass_through(object)
97+
x = TestFunctions.pass_through(object)
7898
self.assertEqual(id(x), id(object))
7999

100+
object2 = TestFunctions.create_instance(InterfaceTestClass)
101+
self.assertEqual(object2.foo(), "InterfaceTestClass")
102+
self.assertEqual(TestFunctions.test_foo(object2), "InterfaceTestClass")
103+
self.assertEqual(object2.bar("bar", 2), "bar/bar")
104+
self.assertEqual(TestFunctions.test_bar(object2, "bar", 2), "bar/bar")
105+
106+
y = TestFunctions.pass_through(object2)
107+
self.assertEqual(id(y), id(object2))
108+
80109
def test_suite():
81110
return unittest.makeSuite(SubClassTests)
82111

0 commit comments

Comments
 (0)