|
| 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