Uh oh!
There was an error while loading. Please reload this page.
forked from pythonnet/pythonnet
- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_subclass.py
More file actions
Latest commit
116 lines (94 loc) · 4.47 KB
/
Copy pathtest_subclass.py
File metadata and controls
116 lines (94 loc) · 4.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# ===========================================================================
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
# ===========================================================================
importclr
clr.AddReference('Python.Test')
clr.AddReference('System')
importsys, os, string, unittest, types
fromPython.TestimportTestFunctions, SubClassTest, IInterfaceTest
fromSystem.Collections.GenericimportList
# class that implements the test interface
classInterfaceTestClass(IInterfaceTest):
deffoo(self):
return"InterfaceTestClass"
defbar(self, x, i):
return"/".join([x] *i)
# class that derives from a class deriving from IInterfaceTest
classDerivedClass(SubClassTest):
deffoo(self):
return"DerivedClass"
defbase_foo(self):
returnSubClassTest.foo(self)
defsuper_foo(self):
returnsuper(DerivedClass, self).foo()
defbar(self, x, i):
return"_".join([x] *i)
defreturn_list(self):
l=List[str]()
l.Add("A")
l.Add("B")
l.Add("C")
returnl
classSubClassTests(unittest.TestCase):
"""Test subclassing managed types"""
deftestBaseClass(self):
"""Test base class managed type"""
object=SubClassTest()
self.assertEqual(object.foo(), "foo")
self.assertEqual(TestFunctions.test_foo(object), "foo")
self.assertEqual(object.bar("bar", 2), "bar")
self.assertEqual(TestFunctions.test_bar(object, "bar", 2), "bar")
self.assertEqual(object.not_overriden(), "not_overriden")
self.assertEqual(list(object.return_list()), ["a", "b", "c"])
self.assertEqual(list(SubClassTest.test_list(object)), ["a", "b", "c"])
deftestInterface(self):
"""Test python classes can derive from C# interfaces"""
object=InterfaceTestClass()
self.assertEqual(object.foo(), "InterfaceTestClass")
self.assertEqual(TestFunctions.test_foo(object), "InterfaceTestClass")
self.assertEqual(object.bar("bar", 2), "bar/bar")
self.assertEqual(TestFunctions.test_bar(object, "bar", 2), "bar/bar")
x=TestFunctions.pass_through(object)
self.assertEqual(id(x), id(object))
deftestDerivedClass(self):
"""Test python class derived from managed type"""
object=DerivedClass()
self.assertEqual(object.foo(), "DerivedClass")
self.assertEqual(object.base_foo(), "foo")
self.assertEqual(object.super_foo(), "foo")
self.assertEqual(TestFunctions.test_foo(object), "DerivedClass")
self.assertEqual(object.bar("bar", 2), "bar_bar")
self.assertEqual(TestFunctions.test_bar(object, "bar", 2), "bar_bar")
self.assertEqual(object.not_overriden(), "not_overriden")
self.assertEqual(list(object.return_list()), ["A", "B", "C"])
self.assertEqual(list(SubClassTest.test_list(object)), ["A", "B", "C"])
x=TestFunctions.pass_through(object)
self.assertEqual(id(x), id(object))
deftestCreateInstance(self):
"""Test derived instances can be created from managed code"""
object=TestFunctions.create_instance(DerivedClass)
self.assertEqual(object.foo(), "DerivedClass")
self.assertEqual(TestFunctions.test_foo(object), "DerivedClass")
self.assertEqual(object.bar("bar", 2), "bar_bar")
self.assertEqual(TestFunctions.test_bar(object, "bar", 2), "bar_bar")
self.assertEqual(object.not_overriden(), "not_overriden")
x=TestFunctions.pass_through(object)
self.assertEqual(id(x), id(object))
object2=TestFunctions.create_instance(InterfaceTestClass)
self.assertEqual(object2.foo(), "InterfaceTestClass")
self.assertEqual(TestFunctions.test_foo(object2), "InterfaceTestClass")
self.assertEqual(object2.bar("bar", 2), "bar/bar")
self.assertEqual(TestFunctions.test_bar(object2, "bar", 2), "bar/bar")
y=TestFunctions.pass_through(object2)
self.assertEqual(id(y), id(object2))
deftest_suite():
returnunittest.makeSuite(SubClassTests)
defmain():
unittest.TextTestRunner().run(test_suite())
if__name__=='__main__':
main()