Skip to content
This repository was archived by the owner on Jul 22, 2023. It is now read-only.

Commit 30cc22a

Browse files
committed
Add new tests for subclassing .net classes.
1 parent 3ba653e commit 30cc22a

File tree

3 files changed

+199
-0
lines changed

3 files changed

+199
-0
lines changed

src/testing/Python.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@
120120
<Compile Include="propertytest.cs" />
121121
<Compile Include="threadtest.cs" />
122122
<Compile Include="doctest.cs" />
123+
<Compile Include="subclasstest.cs" />
123124
</ItemGroup>
124125
<ItemGroup>
125126
<Reference Include="System" />

src/testing/subclasstest.cs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace Python.Test
7+
{
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
18+
{
19+
public SubClassTest()
20+
{
21+
}
22+
23+
// simple test with no arguments
24+
public virtual string foo()
25+
{
26+
return "foo";
27+
}
28+
29+
// test passing objects and boxing primitives
30+
public virtual string bar(string s, int i)
31+
{
32+
return s;
33+
}
34+
35+
// virtual methods that aren't overriden in python still work
36+
public virtual string not_overriden()
37+
{
38+
return "not_overriden";
39+
}
40+
41+
public virtual IList<string> return_list()
42+
{
43+
return new List<string> { "a", "b", "c" };
44+
}
45+
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)
56+
{
57+
// calls into python if foo is overriden
58+
return x.foo();
59+
}
60+
61+
public static string test_bar(IInterfaceTest x, string s, int i)
62+
{
63+
// calls into python if bar is overriden
64+
return x.bar(s, i);
65+
}
66+
67+
// test instances can be constructed in managed code
68+
public static IInterfaceTest create_instance(Type t)
69+
{
70+
return (IInterfaceTest)t.GetConstructor(new Type[] {}).Invoke(new Object[] {});
71+
}
72+
73+
// test instances pass through managed code unchanged
74+
public static IInterfaceTest pass_through(IInterfaceTest s)
75+
{
76+
return s;
77+
}
78+
}
79+
}

src/tests/test_subclass.py

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# ===========================================================================
2+
# This software is subject to the provisions of the Zope Public License,
3+
# Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
4+
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
5+
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
6+
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
7+
# FOR A PARTICULAR PURPOSE.
8+
# ===========================================================================
9+
import clr
10+
clr.AddReference('Python.Test')
11+
clr.AddReference('System')
12+
13+
import sys, os, string, unittest, types
14+
from Python.Test import TestFunctions, SubClassTest, IInterfaceTest
15+
from System.Collections.Generic import List
16+
17+
# class that implements the test interface
18+
class InterfaceTestClass(IInterfaceTest):
19+
__namespace__ = "Python.Test"
20+
21+
def foo(self):
22+
return "InterfaceTestClass"
23+
24+
def bar(self, x, i):
25+
return "/".join([x] * i)
26+
27+
# class that derives from a class deriving from IInterfaceTest
28+
class DerivedClass(SubClassTest):
29+
__namespace__ = "Python.Test"
30+
31+
def foo(self):
32+
return "DerivedClass"
33+
34+
def base_foo(self):
35+
return SubClassTest.foo(self)
36+
37+
def super_foo(self):
38+
return super(DerivedClass, self).foo()
39+
40+
def bar(self, x, i):
41+
return "_".join([x] * i)
42+
43+
def return_list(self):
44+
l = List[str]()
45+
l.Add("A")
46+
l.Add("B")
47+
l.Add("C")
48+
return l
49+
50+
class SubClassTests(unittest.TestCase):
51+
"""Test subclassing managed types"""
52+
53+
def testBaseClass(self):
54+
"""Test base class managed type"""
55+
object = SubClassTest()
56+
self.assertEqual(object.foo(), "foo")
57+
self.assertEqual(TestFunctions.test_foo(object), "foo")
58+
self.assertEqual(object.bar("bar", 2), "bar")
59+
self.assertEqual(TestFunctions.test_bar(object, "bar", 2), "bar")
60+
self.assertEqual(object.not_overriden(), "not_overriden")
61+
self.assertEqual(list(object.return_list()), ["a", "b", "c"])
62+
self.assertEqual(list(SubClassTest.test_list(object)), ["a", "b", "c"])
63+
64+
def testInterface(self):
65+
"""Test python classes can derive from C# interfaces"""
66+
object = InterfaceTestClass()
67+
self.assertEqual(object.foo(), "InterfaceTestClass")
68+
self.assertEqual(TestFunctions.test_foo(object), "InterfaceTestClass")
69+
self.assertEqual(object.bar("bar", 2), "bar/bar")
70+
self.assertEqual(TestFunctions.test_bar(object, "bar", 2), "bar/bar")
71+
72+
x = TestFunctions.pass_through(object)
73+
self.assertEqual(id(x), id(object))
74+
75+
def testDerivedClass(self):
76+
"""Test python class derived from managed type"""
77+
object = DerivedClass()
78+
self.assertEqual(object.foo(), "DerivedClass")
79+
self.assertEqual(object.base_foo(), "foo")
80+
self.assertEqual(object.super_foo(), "foo")
81+
self.assertEqual(TestFunctions.test_foo(object), "DerivedClass")
82+
self.assertEqual(object.bar("bar", 2), "bar_bar")
83+
self.assertEqual(TestFunctions.test_bar(object, "bar", 2), "bar_bar")
84+
self.assertEqual(object.not_overriden(), "not_overriden")
85+
self.assertEqual(list(object.return_list()), ["A", "B", "C"])
86+
self.assertEqual(list(SubClassTest.test_list(object)), ["A", "B", "C"])
87+
88+
x = TestFunctions.pass_through(object)
89+
self.assertEqual(id(x), id(object))
90+
91+
def testCreateInstance(self):
92+
"""Test derived instances can be created from managed code"""
93+
object = TestFunctions.create_instance(DerivedClass)
94+
self.assertEqual(object.foo(), "DerivedClass")
95+
self.assertEqual(TestFunctions.test_foo(object), "DerivedClass")
96+
self.assertEqual(object.bar("bar", 2), "bar_bar")
97+
self.assertEqual(TestFunctions.test_bar(object, "bar", 2), "bar_bar")
98+
self.assertEqual(object.not_overriden(), "not_overriden")
99+
100+
x = TestFunctions.pass_through(object)
101+
self.assertEqual(id(x), id(object))
102+
103+
object2 = TestFunctions.create_instance(InterfaceTestClass)
104+
self.assertEqual(object2.foo(), "InterfaceTestClass")
105+
self.assertEqual(TestFunctions.test_foo(object2), "InterfaceTestClass")
106+
self.assertEqual(object2.bar("bar", 2), "bar/bar")
107+
self.assertEqual(TestFunctions.test_bar(object2, "bar", 2), "bar/bar")
108+
109+
y = TestFunctions.pass_through(object2)
110+
self.assertEqual(id(y), id(object2))
111+
112+
def test_suite():
113+
return unittest.makeSuite(SubClassTests)
114+
115+
def main():
116+
unittest.TextTestRunner().run(test_suite())
117+
118+
if __name__ == '__main__':
119+
main()

0 commit comments

Comments
 (0)