8000 Merge pull request #605 from Cronan/netcore_tests · attackgithub/pythonnet@77c3216 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 77c3216

Browse files
authored
Merge pull request pythonnet#605 from Cronan/netcore_tests
Fixes breaking tests in .Net Core 2.0
2 parents f018085 + 80eb41b commit 77c3216

File tree

4 files changed

+124
-96
lines changed

4 files changed

+124
-96
lines changed

src/tests/test_array.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,33 +1143,34 @@ def test_boxed_value_type_mutation_result():
11431143
# to accidentally write code like the following which is not really
11441144
# mutating value types in-place but changing boxed copies.
11451145

1146-
from System.Drawing import Point
1146+
# Uses ValueType DictionaryEntry instead of Point
1147+
from System.Collections import DictionaryEntry
11471148
from System import Array
11481149

1149-
items = Array.CreateInstance(Point, 5)
1150+
items = Array.CreateInstance(DictionaryEntry, 5)
11501151

11511152
for i in range(5):
1152-
items[i] = Point(i, i)
1153+
items[i] = DictionaryEntry(i, i)
11531154

11541155
for i in range(5):
11551156
# Boxed items, so set_attr will not change the array member.
1156-
assert items[i].X == i
1157-
assert items[i].Y == i
1158-
items[i].X = i + 1
1159-
items[i].Y = i + 1
1160-
assert items[i].X == i
1161-
assert items[i].Y == i
1157+
assert items[i].Key == i
1158+
assert items[i].Value == i
1159+
items[i].Key = i + 1
1160+
items[i].Value = i + 1
1161+
assert items[i].Key == i
1162+
assert items[i].Value == i
11621163

11631164
for i in range(5):
11641165
# Demonstrates the workaround that will change the members.
1165-
assert items[i].X == i
1166-
assert items[i].Y == i
1166+
assert items[i].Key == i
1167+
assert items[i].Value == i
11671168
item = items[i]
1168-
item.X = i + 1
1169-
item.Y = i + 1
1169+
item.Key = i + 1
1170+
item.Value = i + 1
11701171
items[i] = item
1171-
assert items[i].X == i + 1
1172-
assert items[i].Y == i + 1
1172+
assert items[i].Key == i + 1
1173+
assert items[i].Value == i + 1
11731174

11741175

11751176
def test_special_array_creation():

src/tests/test_class.py

Lines changed: 65 additions & 52 deletions
< 10000 td data-grid-cell-id="diff-650b19b30be5f05a8bbd7c678000665d90089ce408127917a095d7bcb3e9797b-174-176-0" data-selected="false" role="gridcell" style="background-color:var(--diffBlob-additionNum-bgColor, var(--diffBlob-addition-bgColor-num));text-align:center" tabindex="-1" valign="top" class="focusable-grid-cell diff-line-number position-relative left-side">
Original file line numberDiff line numberDiff line change
@@ -61,26 +61,28 @@ def test_non_public_class():
6161

6262
def test_basic_subclass():
6363
"""Test basic subclass of a managed class."""
64-
from System.Collections import Hashtable
64+
# Uses ArrayList instead of Hashtable
65+
# As it's available from .Net Core 2.0 without an addref
66+
from System.Collections import ArrayList
6567

66-
class MyTable(Hashtable):
68+
class MyArrayList(ArrayList):
6769
def how_many(self):
6870
return self.Count
6971

70-
table = MyTable()
72+
array_list = MyArrayList()
7173

72-
assert table.__class__.__name__.endswith('MyTable')
73-
assert type(table).__name__.endswith('MyTable')
74-
assert len(table.__class__.__bases__) == 1
75-
assert table.__class__.__bases__[0] == Hashtable
74+
assert array_list.__class__.__name__.endswith('MyArrayList')
75+
assert type(array_list).__name__.endswith('MyArrayList')
76+
assert len(array_list.__class__.__bases__) == 1
77+
assert array_list.__class__.__bases__[0] == ArrayList
7678

77-
assert table.how_many() == 0
78-
assert table.Count == 0
79+
assert array_list.how_many() == 0
80+
assert array_list.Count == 0
7981

80-
table.set_Item('one', 'one')
82+
array_list.Add('one')
8183

82-
assert table.how_many() == 1
83-
assert table.Count == 1
84+
assert array_list.how_many() == 1
85+
assert array_list.Count == 1
8486

8587

8688
def test_subclass_with_no_arg_constructor():
@@ -118,21 +120,23 @@ def __init__(self, v):
118120

119121
def test_struct_construction():
120122
"""Test construction of structs."""
121-
from System.Drawing import Point
123+
# Uses DateTime instead of Point
124+
# As it's available from .Net Core 2.0 without an addref
125+
from System import DateTime
122126

123-
p = Point()
124-
assert p.X == 0
125-
assert p.Y == 0
127+
dt1 = DateTime(2017, 2, 27)
128+
assert dt1.Day == 27
129+
assert dt1.Month == 2
130+
assert dt1.Year == 2017
126131

127-
p = Point(0, 0)
128-
assert p.X == 0
129-
assert p.Y == 0
132+
# Static method calls default constructor
133+
dt2 = DateTime.MinValue
134+
assert dt2.Day == 1
135+
assert dt2.Month == 1
136+
assert dt2.Year == 1
130137

131-
p.X = 10
132-
p.Y = 10
133-
134-
assert p.X == 10
135-
assert p.Y == 10
138+
# mutation tests removed
139+
# structs are not typically mutable
136140

137141
# test strange __new__ interactions
138142

@@ -169,44 +173,53 @@ def test_ienumerator_iteration():
169173

170174
def test_override_get_item():
171175
"""Test managed subclass overriding __getitem__."""
172-
from System.Collections import Hashtable
173-
174-
class MyTable(Hashtable):
176+
from System.Collections import ArrayList
177+
178+
# Uses ArrayList instead of Hashtable
179+
# As it's available from .Net Core 2.0 without an addref
180+
class MyArrayList(ArrayList):
175181
def __getitem__(self, key):
176-
value = Hashtable.__getitem__(self, key)
182+
value = ArrayList.__getitem__(self, key)
177183
return 'my ' + str(value)
178184

179-
table = MyTable()
180-
table['one'] = 'one'
181-
table['two'] = 'two'
182-
table['three'] = 'three'
185+
array_list = MyArrayList()
186+
array_list.Add('zero')
187+
array_list.Add('one')
188+
array_list.Add('two')
183189

184-
assert table['one'] == 'my one'
185-
assert table['two'] == 'my two'
186-
assert table['three'] == 'my three'
190+
assert array_list[0] == 'my zero'
191+
assert array_list[1] == 'my one'
192+
assert array_list[2] == 'my two'
187193

188-
assert table.Count == 3
194+
assert array_list.Count == 3
189195

190196

191197
def test_override_set_item():
192198
"""Test managed subclass overriding __setitem__."""
193-
from System.Collections import Hashtable
194-
195-
class MyTable(Hashtable):
199+
from System.Collections import ArrayList
200+
201+
# Uses ArrayList instead of Hashtable
202+
# As it's available from .Net Core 2.0 without an addref
203+
class MyArrayList(ArrayList):
196204
def __setitem__(self, key, value):
197-
value = 'my ' + str(value)
198-
Hashtable.__setitem__(self, key, value)
199-
200-
table = MyTable()
201-
table['one'] = 'one'
202-
table['two'] = 'two'
203-
table['three'] = 'three'
204-
205-
assert table['one'] == 'my one'
206-
assert table['two'] == 'my two'
207-
assert table['three'] == 'my three'
208-
209-
assert table.Count == 3
205+
value = 'your ' + str(value)
206+
ArrayList.__setitem__(self, key, value)
207+
208+
array_list = MyArrayList()
209+
# need to add three items first
210+
array_list.Add("a")
211+
array_list.Add("b")
212+
array_list.Add("c")
213+
214+
array_list[0] = 'zero'
215+
array_list[1] = 'one'
216+
array_list[2] = 'two'
217+
218+
assert array_list[0] == 'your zero'
219+
assert array_list[1] == 'your one'
220+
assert array_list[2] == 'your two'
221+
222+
assert array_list.Count == 3
210223

211224

212225
def test_add_and_remove_class_attribute():

src/tests/test_compat.py

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,15 @@ def test_simple_import_from_with_alias():
112112

113113
def test_dotted_name_import_from():
114114
"""Test dotted-name 'import from'."""
115-
from CLR.System import Xml
116-
assert is_clr_module(Xml)
117-
assert Xml.__name__ == 'System.Xml'
115+
# Uses IO instead of Xml
116+
# As it's available from .Net Core 2.0 without an addref
117+
from CLR.System import IO
118+
assert is_clr_module(IO)
119+
assert IO.__name__ == 'System.IO'
118120

119-
from CLR.System.Xml import XmlDocument
120-
assert is_clr_class(XmlDocument)
121-
assert XmlDocument.__name__ == 'XmlDocument'
121+
from CLR.System.IO import Path
122+
assert is_clr_class(Path)
123+
assert Path.__name__ == 'Path'
122124

123125
from xml.dom import pulldom
124126
assert isinstance(pulldom, types.ModuleType)
@@ -131,13 +133,15 @@ def test_dotted_name_import_from():
131133

132134
def test_dotted_name_import_from_with_alias():
133135
"""Test dotted-name 'import from' with aliasing."""
134-
from CLR.System import Xml as myXml
135-
assert is_clr_module(myXml)
136-
assert myXml.__name__ == 'System.Xml'
136+
# Uses IO instead of Xml
137+
# As it's available from .Net Core 2.0 without an addref
138+
from CLR.System import IO as myIO
139+
assert is_clr_module(myIO)
140+
assert myIO.__name__ == 'System.IO'
137141

138-
from CLR.System.Xml import XmlDocument as myXmlDocument
139-
assert is_clr_class(myXmlDocument)
140-
assert myXmlDocument.__name__ == 'XmlDocument'
142+
from CLR.System.IO import Path as myPath
143+
assert is_clr_class(myPath)
144+
assert myPath.__name__ == 'Path'
141145

142146
from xml.dom import pulldom as myPulldom
143147
assert isinstance(myPulldom, types.ModuleType)
@@ -151,13 +155,15 @@ def test_dotted_name_import_from_with_alias():
151155
def test_from_module_import_star():
152156
"""Test from module import * behavior."""
153157
count = len(locals().keys())
154-
m = __import__('CLR.System.Management', globals(), locals(), ['*'])
155-
assert m.__name__ == 'System.Management'
158+
# Uses IO instead of Xml
159+
# As it's available from .Net Core 2.0 without an addref
160+
m = __import__('CLR.System.IO', globals(), locals(), ['*'])
161+
assert m.__name__ == 'System.IO'
156162
assert is_clr_module(m)
157163
assert len(locals().keys()) > count + 1
158164

159-
m2 = __import__('System.Management', globals(), locals(), ['*'])
160-
assert m2.__name__ == 'System.Management'
165+
m2 = __import__('System.IO', globals(), locals(), ['*'])
166+
assert m2.__name__ == 'System.IO'
161167
assert is_clr_module(m2)
162168
assert len(locals().keys()) > count + 1
163169

@@ -168,16 +174,22 @@ def test_explicit_assembly_load():
168174
"""Test explicit assembly loading using standard CLR tools."""
169175
from CLR.System.Reflection import Assembly
170176
from CLR import System
177+
from CLR.System.IO import FileNotFoundException
171178
import sys
172179

173-
assembly = Assembly.LoadWithPartialName('System.Data')
180+
assembly = Assembly.LoadWithPartialName('System.IO')
174181
assert assembly is not None
182+
# Uses IO instead of Data
183+
# As it's available from .Net Core 2.0 without an addref
184+
import CLR.System.IO
185+
assert 'System.IO' in sys.modules
175186

176-
import CLR.System.Data
177-
assert 'System.Data' in sys.modules
178-
179-
assembly = Assembly.LoadWithPartialName('SpamSpamSpamSpamEggsAndSpam')
180-
assert assembly is None
187+
# Assembly.LoadWithPartialName is obsolete, and delegates to Assembly.Load
188+
# in .Net Core (which then throws)
189+
# assembly = Assembly.LoadWithPartialName('SpamSpamSpamSpamEggsAndSpam')
190+
# assert assembly is None
191+
with pytest.raises(FileNotFoundException):
192+
assembly = Assembly.LoadFrom('SpamSpamSpamSpamEggsAndSpam')
181193

182194

183195
def test_implicit_load_already_valid_namespace():
@@ -224,8 +236,9 @@ def test_module_get_attr():
224236

225237
int_type = System.Int32
226238
assert is_clr_class(int_type)
227-
228-
module = System.Xml
239+
# Uses IO instead of Xml
240+
# As it's available from .Net Core 2.0 without an addref
241+
module = System.IO
229242
assert is_clr_module(module)
230243

231244
with pytest.raises(AttributeError):

src/tests/test_property.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,9 @@ def test_interface_property():
141141
"""Test properties of interfaces. Added after a bug report
142142
that an IsAbstract check was inappropriate and prevented
143143
use of properties when only the interface is known."""
144-
from System.Collections import Hashtable, ICollection
145-
146-
mapping = Hashtable()
147-
coll = ICollection(mapping)
148-
assert coll.Count == 0
144+
from System.Collections import ArrayList, IList
145+
# Uses ArrayList instead of Hashtable
146+
# As it's available from .Net Core 2.0 without an addref
147+
mapping = ArrayList()
148+
lst = IList(mapping)
149+
assert lst.Count == 0

0 commit comments

Comments
 (0)
0