forked from fkarb/pythonnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_class.py
More file actions
247 lines (165 loc) · 6.84 KB
/
test_class.py
File metadata and controls
247 lines (165 loc) · 6.84 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# ===========================================================================
# 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.
# ===========================================================================
from System.Collections import Hashtable
from Python.Test import ClassTest
import sys, os, string, unittest, types
import Python.Test as Test
import System
import six
if six.PY3:
DictProxyType = type(object.__dict__)
else:
DictProxyType = types.DictProxyType
class ClassTests(unittest.TestCase):
"""Test CLR class support."""
def testBasicReferenceType(self):
"""Test usage of CLR defined reference types."""
String = System.String
self.assertEquals(String.Empty, "")
def testBasicValueType(self):
"""Test usage of CLR defined value types."""
Int32 = System.Int32
self.assertEquals(Int32.MaxValue, 2147483647)
def testClassStandardAttrs(self):
"""Test standard class attributes."""
self.assertTrue(ClassTest.__name__ == 'ClassTest')
self.assertTrue(ClassTest.__module__ == 'Python.Test')
self.assertTrue(type(ClassTest.__dict__) == DictProxyType)
self.assertTrue(len(ClassTest.__doc__) > 0)
def testClassDocstrings(self):
"""Test standard class docstring generation"""
value = 'Void .ctor()'
self.assertTrue(ClassTest.__doc__ == value)
def testClassDefaultStr(self):
"""Test the default __str__ implementation for managed objects."""
s = System.String("this is a test")
self.assertTrue(str(s) == "this is a test")
def testClassDefaultRepr(self):
"""Test the default __repr__ implementation for managed objects."""
s = System.String("this is a test")
self.assertTrue(repr(s).startswith("<System.String object"))
def testNonPublicClass(self):
"""Test that non-public classes are inaccessible."""
from Python import Test
def test():
from Python.Test import InternalClass
self.assertRaises(ImportError, test)
def test():
x = Test.InternalClass
self.assertRaises(AttributeError, test)
def testBasicSubclass(self):
"""Test basic subclass of a managed class."""
class MyTable(Hashtable):
def howMany(self):
return self.Count
table = MyTable()
self.assertTrue(table.__class__.__name__.endswith('MyTable'))
self.assertTrue(type(table).__name__.endswith('MyTable'))
self.assertTrue(len(table.__class__.__bases__) == 1)
self.assertTrue(table.__class__.__bases__[0] == Hashtable)
self.assertTrue(table.howMany() == 0)
self.assertTrue(table.Count == 0)
table.set_Item('one', 'one')
self.assertTrue(table.howMany() == 1)
self.assertTrue(table.Count == 1)
MyTable = None
def testSubclassWithNoArgConstructor(self):
"""Test subclass of a managed class with a no-arg constructor."""
from Python.Test import ClassCtorTest1
class SubClass(ClassCtorTest1):
def __init__(self, name):
self.name = name
# This failed in earlier versions
inst = SubClass('test')
def testSubclassWithVariousConstructors(self):
"""Test subclass of a managed class with various constructors."""
from Python.Test import ClassCtorTest2
class SubClass(ClassCtorTest2):
def __init__(self, v):
ClassCtorTest2.__init__(self)
self.value = v
inst = SubClass('test')
self.assertTrue(inst.value == 'test')
class SubClass2(ClassCtorTest2):
def __init__(self, v):
ClassCtorTest2.__init__(self)
self.value = v
inst = SubClass2('test')
self.assertTrue(inst.value == 'test')
def testStructConstruction(self):
"""Test construction of structs."""
from System.Drawing import Point
p = Point()
self.assertTrue(p.X == 0)
self.assertTrue(p.Y == 0)
p = Point(0, 0)
self.assertTrue(p.X == 0)
self.assertTrue(p.Y == 0)
p.X = 10
p.Y = 10
self.assertTrue(p.X == 10)
self.assertTrue(p.Y == 10)
# test strange __new__ interactions
# test weird metatype
# test recursion
# test
def testIEnumerableIteration(self):
"""Test iteration over objects supporting IEnumerable."""
list = Test.ClassTest.GetArrayList()
for item in list:
self.assertTrue((item > -1) and (item < 10))
dict = Test.ClassTest.GetHashtable()
for item in dict:
cname = item.__class__.__name__
self.assertTrue(cname.endswith('DictionaryEntry'))
def testIEnumeratorIteration(self):
"""Test iteration over objects supporting IEnumerator."""
chars = Test.ClassTest.GetEnumerator()
for item in chars:
self.assertTrue(item in 'test string')
def testOverrideGetItem(self):
"""Test managed subclass overriding __getitem__."""
class MyTable(Hashtable):
def __getitem__(self, key):
value = Hashtable.__getitem__(self, key)
return 'my ' + str(value)
table = MyTable()
table['one'] = 'one'
table['two'] = 'two'
table['three'] = 'three'
self.assertTrue(table['one'] == 'my one')
self.assertTrue(table['two'] == 'my two')
self.assertTrue(table['three'] == 'my three')
self.assertTrue(table.Count == 3)
def testOverrideSetItem(self):
"""Test managed subclass overriding __setitem__."""
class MyTable(Hashtable):
def __setitem__(self, key, value):
value = 'my ' + str(value)
Hashtable.__setitem__(self, key, value)
table = MyTable()
table['one'] = 'one'
table['two'] = 'two'
table['three'] = 'three'
self.assertTrue(table['one'] == 'my one')
self.assertTrue(table['two'] == 'my two')
self.assertTrue(table['three'] == 'my three')
self.assertTrue(table.Count == 3)
class ClassicClass:
def kind(self):
return 'classic'
class NewStyleClass(object):
def kind(self):
return 'new-style'
def test_suite():
return unittest.makeSuite(ClassTests)
def main():
unittest.TextTestRunner().run(test_suite())
if __name__ == '__main__':
main()