Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fixes breaking tests in .Net Core 2.0 #605

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 16 additions & 15 deletions src/tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -1143,33 +1143,34 @@ def test_boxed_value_type_mutation_result():
# to accidentally write code like the following which is not really
# mutating value types in-place but changing boxed copies.

from System.Drawing import Point
# Uses ValueType DictionaryEntry instead of Point
from System.Collections import DictionaryEntry
from System import Array

items = Array.CreateInstance(Point, 5)
items = Array.CreateInstance(DictionaryEntry, 5)

for i in range(5):
items[i] = Point(i, i)
items[i] = DictionaryEntry(i, i)

for i in range(5):
# Boxed items, so set_attr will not change the array member.
assert items[i].X == i
assert items[i].Y == i
items[i].X = i + 1
items[i].Y = i + 1
assert items[i].X == i
assert items[i].Y == i
assert items[i].Key == i
assert items[i].Value == i
items[i].Key = i + 1
items[i].Value = i + 1
assert items[i].Key == i
assert items[i].Value == i

for i in range(5):
# Demonstrates the workaround that will change the members.
assert items[i].X == i
assert items[i].Y == i
assert items[i].Key == i
assert items[i].Value == i
item = items[i]
item.X = i + 1
item.Y = i + 1
item.Key = i + 1
item.Value = i + 1
items[i] = item
assert items[i].X == i + 1
assert items[i].Y == i + 1
assert items[i].Key == i + 1
assert items[i].Value == i + 1


def test_special_array_creation():
Expand Down
117 changes: 65 additions & 52 deletions src/tests/test_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,26 +61,28 @@ def test_non_public_class():

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

class MyTable(Hashtable):
class MyArrayList(ArrayList):
def how_many(self):
return self.Count

table = MyTable()
array_list = MyArrayList()

assert table.__class__.__name__.endswith('MyTable')
assert type(table).__name__.endswith('MyTable')
assert len(table.__class__.__bases__) == 1
assert table.__class__.__bases__[0] == Hashtable
assert array_list.__class__.__name__.endswith('MyArrayList')
assert type(array_list).__name__.endswith('MyArrayList')
assert len(array_list.__class__.__bases__) == 1
assert array_list.__class__.__bases__[0] == ArrayList

assert table.how_many() == 0
assert table.Count == 0
assert array_list.how_many() == 0
assert array_list.Count == 0

table.set_Item('one', 'one')
array_list.Add('one')

assert table.how_many() == 1
assert table.Count == 1
assert array_list.how_many() == 1
assert array_list.Count == 1


def test_subclass_with_no_arg_constructor():
Expand Down Expand Up @@ -118,21 +120,23 @@ def __init__(self, v):

def test_struct_construction():
"""Test construction of structs."""
from System.Drawing import Point
# Uses DateTime instead of Point
# As it's available from .Net Core 2.0 without an addref
from System import DateTime

p = Point()
assert p.X == 0
assert p.Y == 0
dt1 = DateTime(2017, 2, 27)
assert dt1.Day == 27
assert dt1.Month == 2
assert dt1.Year == 2017

p = Point(0, 0)
assert p.X == 0
assert p.Y == 0
# Static method calls default constructor
dt2 = DateTime.MinValue
assert dt2.Day == 1
assert dt2.Month == 1
assert dt2.Year == 1

p.X = 10
p.Y = 10

assert p.X == 10
assert p.Y == 10
# mutation tests removed
# structs are not typically mutable

# test strange __new__ interactions

Expand Down Expand Up @@ -169,44 +173,53 @@ def test_ienumerator_iteration():

def test_override_get_item():
"""Test managed subclass overriding __getitem__."""
from System.Collections import Hashtable

class MyTable(Hashtable):
from System.Collections import ArrayList

# Uses ArrayList instead of Hashtable
# As it's available from .Net Core 2.0 without an addref
class MyArrayList(ArrayList):
def __getitem__(self, key):
value = Hashtable.__getitem__(self, key)
value = ArrayList.__getitem__(self, key)
return 'my ' + str(value)

table = MyTable()
table['one'] = 'one'
table['two'] = 'two'
table['three'] = 'three'
array_list = MyArrayList()
array_list.Add('zero')
array_list.Add('one')
array_list.Add('two')

assert table['one'] == 'my one'
assert table['two'] == 'my two'
assert table['three'] == 'my three'
assert array_list[0] == 'my zero'
assert array_list[1] == 'my one'
assert array_list[2] == 'my two'

assert table.Count == 3
assert array_list.Count == 3


def test_override_set_item():
"""Test managed subclass overriding __setitem__."""
from System.Collections import Hashtable

class MyTable(Hashtable):
from System.Collections import ArrayList

# Uses ArrayList instead of Hashtable
# As it's available from .Net Core 2.0 without an addref
class MyArrayList(ArrayList):
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'

assert table['one'] == 'my one'
assert table['two'] == 'my two'
assert table['three'] == 'my three'

assert table.Count == 3
value = 'your ' + str(value)
ArrayList.__setitem__(self, key, value)

array_list = MyArrayList()
# need to add three items first
array_list.Add("a")
array_list.Add("b")
array_list.Add("c")

array_list[0] = 'zero'
array_list[1] = 'one'
array_list[2] = 'two'

assert array_list[0] == 'your zero'
assert array_list[1] == 'your one'
assert array_list[2] == 'your two'

assert array_list.Count == 3


def test_add_and_remove_class_attribute():
Expand Down
61 changes: 37 additions & 24 deletions src/tests/test_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,15 @@ def test_simple_import_from_with_alias():

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

from CLR.System.Xml import XmlDocument
assert is_clr_class(XmlDocument)
assert XmlDocument.__name__ == 'XmlDocument'
from CLR.System.IO import Path
assert is_clr_class(Path)
assert Path.__name__ == 'Path'

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

def test_dotted_name_import_from_with_alias():
"""Test dotted-name 'import from' with aliasing."""
from CLR.System import Xml as myXml
assert is_clr_module(myXml)
assert myXml.__name__ == 'System.Xml'
# Uses IO instead of Xml
# As it's available from .Net Core 2.0 without an addref
from CLR.System import IO as myIO
assert is_clr_module(myIO)
assert myIO.__name__ == 'System.IO'

from CLR.System.Xml import XmlDocument as myXmlDocument
assert is_clr_class(myXmlDocument)
assert myXmlDocument.__name__ == 'XmlDocument'
from CLR.System.IO import Path as myPath
assert is_clr_class(myPath)
assert myPath.__name__ == 'Path'

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

m2 = __import__('System.Management', globals(), locals(), ['*'])
assert m2.__name__ == 'System.Management'
m2 = __import__('System.IO', globals(), locals(), ['*'])
assert m2.__name__ == 'System.IO'
assert is_clr_module(m2)
assert len(locals().keys()) > count + 1

Expand All @@ -168,16 +174,22 @@ def test_explicit_assembly_load():
"""Test explicit assembly loading using standard CLR tools."""
from CLR.System.Reflection import Assembly
from CLR import System
from CLR.System.IO import FileNotFoundException
import sys

assembly = Assembly.LoadWithPartialName('System.Data')
assembly = Assembly.LoadWithPartialName('System.IO')
assert assembly is not None
# Uses IO instead of Data
# As it's available from .Net Core 2.0 without an addref
import CLR.System.IO
assert 'System.IO' in sys.modules

import CLR.System.Data
assert 'System.Data' in sys.modules

assembly = Assembly.LoadWithPartialName('SpamSpamSpamSpamEggsAndSpam')
assert assembly is None
# Assembly.LoadWithPartialName is obsolete, and delegates to Assembly.Load
# in .Net Core (which then throws)
# assembly = Assembly.LoadWithPartialName('SpamSpamSpamSpamEggsAndSpam')
# assert assembly is None
with pytest.raises(FileNotFoundException):
assembly = Assembly.LoadFrom('SpamSpamSpamSpamEggsAndSpam')


def test_implicit_load_already_valid_namespace():
Expand Down Expand Up @@ -224,8 +236,9 @@ def test_module_get_attr():

int_type = System.Int32
assert is_clr_class(int_type)

module = System.Xml
# Uses IO instead of Xml
# As it's available from .Net Core 2.0 without an addref
module = System.IO
assert is_clr_module(module)

with pytest.raises(AttributeError):
Expand Down
11 changes: 6 additions & 5 deletions src/tests/test_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,9 @@ def test_interface_property():
"""Test properties of interfaces. Added after a bug report
that an IsAbstract check was inappropriate and prevented
use of properties when only the interface is known."""
from System.Collections import Hashtable, ICollection

mapping = Hashtable()
coll = ICollection(mapping)
assert coll.Count == 0
from System.Collections import ArrayList, IList
# Uses ArrayList instead of Hashtable
# As it's available from .Net Core 2.0 without an addref
mapping = ArrayList()
lst = IList(mapping)
assert lst.Count == 0