[Zope-CVS] CVS: PythonNet/tests/python - runtests.py:1.1 test_class.py:1.1 test_conversion.py:1.1 test_delegate.py:1.1 test_enum.py:1.1 test_event.py:1.1 test_exceptions.py:1.1 test_field.py:1.1 test_interface.py:1.1 test_method.py:1.1 test_module.py:1.1 test_property.py:1.1
Brian Lloyd
brian@zope.com
Mon, 17 Feb 2003 22:44:39 -0500
Update of /cvs-repository/PythonNet/tests/python
In directory cvs.zope.org:/tmp/cvs-serv5356/tests/python
Added Files:
runtests.py test_class.py test_conversion.py test_delegate.py
test_enum.py test_event.py test_exceptions.py test_field.py
test_interface.py test_method.py test_module.py
test_property.py
Log Message:
initial commit
=== Added File PythonNet/tests/python/runtests.py ===
"""Run all of the unit tests for this package."""
def main():
for name in (
'test_module',
'test_conversion',
'test_class',
'test_interface',
'test_enum',
'test_field',
'test_property',
'test_event',
'test_method',
'test_delegate',
'test_exceptions',
):
module = __import__(name)
module.main()
if __name__ == '__main__':
main()
=== Added File PythonNet/tests/python/test_class.py ===
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
#
# All Rights Reserved.
#
# 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 CLR.Python.Test import ClassTest
import sys, os, string, unittest, types
import CLR.System as System
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.failUnless(ClassTest.__name__ == 'ClassTest')
self.failUnless(ClassTest.__module__ == 'CLR.Python.Test')
self.failUnless(type(ClassTest.__dict__) == types.DictProxyType)
self.failUnless(ClassTest.__doc__ == None)
def testNonPublicClass(self):
"""Test that non-public classes are inaccessible."""
from CLR.Python import Test
def test():
from CLR.Python.Test import InternalClass
self.failUnlessRaises(ImportError, test)
def test():
x = Test.InternalClass
self.failUnlessRaises(AttributeError, test)
def test_suite():
return unittest.makeSuite(ClassTests)
def main():
unittest.TextTestRunner().run(test_suite())
if __name__ == '__main__':
testcase.setup()
main()
=== Added File PythonNet/tests/python/test_conversion.py === (685/785 lines abridged)
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
#
# All Rights Reserved.
#
# 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.
import sys, os, string, unittest, types
from CLR.Python.Test import ConversionTest
from CLR import System
class ConversionTests(unittest.TestCase):
"""Test CLR <-> Python type conversions."""
def testBoolConversion(self):
"""Test bool conversion."""
object = ConversionTest()
self.failUnless(object.BooleanField == 0)
object.BooleanField = 1
self.failUnless(object.BooleanField == 1)
object.BooleanField = System.Boolean(None)
self.failUnless(object.BooleanField == 0)
object.BooleanField = System.Boolean('')
self.failUnless(object.BooleanField == 0)
object.BooleanField = System.Boolean(0)
self.failUnless(object.BooleanField == 0)
object.BooleanField = System.Boolean(1)
self.failUnless(object.BooleanField == 1)
object.BooleanField = System.Boolean('a')
self.failUnless(object.BooleanField == 1)
def testSByteConversion(self):
"""Test sbyte conversion."""
self.failUnless(System.SByte.MaxValue == 127)
self.failUnless(System.SByte.MinValue == -128)
object = ConversionTest()
self.failUnless(object.SByteField == 0)
object.SByteField = 127
[-=- -=- -=- 685 lines omitted -=- -=- -=-]
def test():
object = ConversionTest()
object.EnumField = 1000000
self.failUnlessRaises(OverflowError, test)
def test():
object = ConversionTest()
object.EnumField = "spam"
self.failUnlessRaises(TypeError, test)
def testNullConversion(self):
"""Test null conversion."""
object = ConversionTest()
object.StringField = None
self.failUnless(object.StringField == None)
object.ObjectField = None
self.failUnless(object.ObjectField == None)
object.SpamField = None
self.failUnless(object.SpamField == None)
# Primitive types and enums should not be set to null.
def test():
ConversionTest().Int32Field = None
self.failUnlessRaises(TypeError, test)
def test():
ConversionTest().EnumField = None
self.failUnlessRaises(TypeError, test)
def test_suite():
return unittest.makeSuite(ConversionTests)
def main():
unittest.TextTestRunner().run(test_suite())
if __name__ == '__main__':
testcase.setup()
main()
=== Added File PythonNet/tests/python/test_delegate.py ===
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
#
# All Rights Reserved.
#
# 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 CLR.Python.Test import DelegateTest, PublicDelegate
from CLR.Python.Test import StringDelegate, ObjectDelegate
import sys, os, string, unittest, types
import CLR.Python.Test as Test
import CLR.System as System
class DelegateTests(unittest.TestCase):
"""Test CLR delegate support."""
def testDelegateStandardAttrs(self):
"""Test standard delegate attributes."""
self.failUnless(PublicDelegate.__name__ == 'PublicDelegate')
self.failUnless(PublicDelegate.__module__ == 'CLR.Python.Test')
self.failUnless(type(PublicDelegate.__dict__) == types.DictProxyType)
self.failUnless(PublicDelegate.__doc__ == None)
def testGlobalDelegateVisibility(self):
"""Test visibility of module-level delegates."""
from CLR.Python.Test import PublicDelegate
self.failUnless(PublicDelegate.__name__ == 'PublicDelegate')
self.failUnless(Test.PublicDelegate.__name__ == 'PublicDelegate')
def test():
from CLR.Python.Test import InternalDelegate
self.failUnlessRaises(ImportError, test)
def test():
i = Test.InternalDelegate
self.failUnlessRaises(AttributeError, test)
def testNestedDelegateVisibility(self):
"""Test visibility of nested delegates."""
ob = DelegateTest.PublicDelegate
self.failUnless(ob.__name__ == 'PublicDelegate')
ob = DelegateTest.ProtectedDelegate
self.failUnless(ob.__name__ == 'ProtectedDelegate')
def test():
ob = DelegateTest.InternalDelegate
self.failUnlessRaises(AttributeError, test)
def test():
ob = DelegateTest.PrivateDelegate
self.failUnlessRaises(AttributeError, test)
def testDelegateFromFunction(self):
"""Test delegate implemented with a Python function."""
def sayhello():
return "hello"
d = StringDelegate(sayhello)
ob = DelegateTest()
self.failUnless(ob.CallStringDelegate(d) == "hello")
self.failUnless(d() == "hello")
ob.stringDelegate = d
self.failUnless(ob.CallStringDelegate(ob.stringDelegate) == "hello")
self.failUnless(ob.stringDelegate() == "hello")
def testDelegateFromMethod(self):
"""Test delegate implemented with a Python instance method."""
class Hello:
def sayhello(self):
return "hello"
inst = Hello()
d = StringDelegate(inst.sayhello)
ob = DelegateTest()
self.failUnless(ob.CallStringDelegate(d) == "hello")
self.failUnless(d() == "hello")
ob.stringDelegate = d
self.failUnless(ob.CallStringDelegate(ob.stringDelegate) == "hello")
self.failUnless(ob.stringDelegate() == "hello")
def testDelegateFromUnboundMethod(self):
"""Test failure mode for unbound methods."""
class Hello:
def sayhello(self):
return "hello"
def test():
d = StringDelegate(Hello.sayhello)
d()
self.failUnlessRaises(TypeError, test)
def testDelegateFromStaticMethod(self):
"""Test delegate implemented with a Python static method."""
class Hello:
def sayhello():
return "hello"
sayhello = staticmethod(sayhello)
d = StringDelegate(Hello.sayhello)
ob = DelegateTest()
self.failUnless(ob.CallStringDelegate(d) == "hello")
self.failUnless(d() == "hello")
ob.stringDelegate = d
self.failUnless(ob.CallStringDelegate(ob.stringDelegate) == "hello")
self.failUnless(ob.stringDelegate() == "hello")
inst = Hello()
d = StringDelegate(inst.sayhello)
ob = DelegateTest()
self.failUnless(ob.CallStringDelegate(d) == "hello")
self.failUnless(d() == "hello")
ob.stringDelegate = d
self.failUnless(ob.CallStringDelegate(ob.stringDelegate) == "hello")
self.failUnless(ob.stringDelegate() == "hello")
def testDelegateFromClassMethod(self):
"""Test delegate implemented with a Python class method."""
class Hello:
def sayhello(self):
return "hello"
sayhello = classmethod(sayhello)
d = StringDelegate(Hello.sayhello)
ob = DelegateTest()
self.failUnless(ob.CallStringDelegate(d) == "hello")
self.failUnless(d() == "hello")
ob.stringDelegate = d
self.failUnless(ob.CallStringDelegate(ob.stringDelegate) == "hello")
self.failUnless(ob.stringDelegate() == "hello")
inst = Hello()
d = StringDelegate(inst.sayhello)
ob = DelegateTest()
self.failUnless(ob.CallStringDelegate(d) == "hello")
self.failUnless(d() == "hello")
ob.stringDelegate = d
self.failUnless(ob.CallStringDelegate(ob.stringDelegate) == "hello")
self.failUnless(ob.stringDelegate() == "hello")
def testDelegateFromCallable(self):
"""Test delegate implemented with a Python callable object."""
class Hello:
def __call__(self):
return "hello"
inst = Hello()
d = StringDelegate(inst)
ob = DelegateTest()
self.failUnless(ob.CallStringDelegate(d) == "hello")
self.failUnless(d() == "hello")
ob.stringDelegate = d
self.failUnless(ob.CallStringDelegate(ob.stringDelegate) == "hello")
self.failUnless(ob.stringDelegate() == "hello")
def testDelegateFromManagedInstanceMethod(self):
"""Test delegate implemented with a managed instance method."""
ob = DelegateTest()
d = StringDelegate(ob.SayHello)
self.failUnless(ob.CallStringDelegate(d) == "hello")
self.failUnless(d() == "hello")
ob.stringDelegate = d
self.failUnless(ob.CallStringDelegate(ob.stringDelegate) == "hello")
self.failUnless(ob.stringDelegate() == "hello")
def testDelegateFromManagedStaticMethod(self):
"""Test delegate implemented with a managed static method."""
d = StringDelegate(DelegateTest.StaticSayHello)
ob = DelegateTest()
self.failUnless(ob.CallStringDelegate(d) == "hello")
self.failUnless(d() == "hello")
ob.stringDelegate = d
self.failUnless(ob.CallStringDelegate(ob.stringDelegate) == "hello")
self.failUnless(ob.stringDelegate() == "hello")
def testDelegateFromDelegate(self):
"""Test delegate implemented with another delegate."""
def sayhello():
return "hello"
d1 = StringDelegate(sayhello)
d2 = StringDelegate(d1)
ob = DelegateTest()
self.failUnless(ob.CallStringDelegate(d2) == "hello")
self.failUnless(d2() == "hello")
ob.stringDelegate = d2
self.failUnless(ob.CallStringDelegate(ob.stringDelegate) == "hello")
self.failUnless(ob.stringDelegate() == "hello")
# test d as event handler
# test multicast delegates
# test explicit op_
# test sig mismatch, both on managed and Python side
# test return wrong type
def testDelegateWithInvalidArgs(self):
"""Test delegate instantiation with invalid (non-callable) args."""
def test():
d = StringDelegate(None)
self.failUnlessRaises(TypeError, test)
def test():
d = StringDelegate("spam")
self.failUnlessRaises(TypeError, test)
def test():
d = StringDelegate(1)
self.failUnlessRaises(TypeError, test)
def testSubclassDelegateFails(self):
"""Test that subclassing of a delegate type fails."""
def test():
class Boom(PublicDelegate):
pass
self.failUnlessRaises(TypeError, test)
def testDelegateEquality(self):
"""Test delegate equality."""
def sayhello():
return "hello"
d = StringDelegate(sayhello)
ob = DelegateTest()
ob.stringDelegate = d
self.failUnless(ob.stringDelegate == d)
def test_suite():
return unittest.makeSuite(DelegateTests)
def main():
unittest.TextTestRunner().run(test_suite())
if __name__ == '__main__':
testcase.setup()
main()
=== Added File PythonNet/tests/python/test_enum.py ===
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
#
# All Rights Reserved.
#
# 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.
import sys, os, string, unittest, types
from CLR.System import DayOfWeek
from CLR.Python import Test
class EnumTests(unittest.TestCase):
"""Test CLR enum support."""
def testEnumStandardAttrs(self):
"""Test standard enum attributes."""
self.failUnless(DayOfWeek.__name__ == 'DayOfWeek')
self.failUnless(DayOfWeek.__module__ == 'CLR.System')
self.failUnless(type(DayOfWeek.__dict__) == types.DictProxyType)
self.failUnless(DayOfWeek.__doc__ == None)
def testEnumGetMember(self):
"""Test access to enum members."""
self.failUnless(DayOfWeek.Sunday == 0)
self.failUnless(DayOfWeek.Monday == 1)
self.failUnless(DayOfWeek.Tuesday == 2)
self.failUnless(DayOfWeek.Wednesday == 3)
self.failUnless(DayOfWeek.Thursday == 4)
self.failUnless(DayOfWeek.Friday == 5)
self.failUnless(DayOfWeek.Saturday == 6)
def testByteEnum(self):
"""Test byte enum."""
self.failUnless(Test.ByteEnum.Zero == 0)
self.failUnless(Test.ByteEnum.One == 1)
self.failUnless(Test.ByteEnum.Two == 2)
def testSByteEnum(self):
"""Test sbyte enum."""
self.failUnless(Test.SByteEnum.Zero == 0)
self.failUnless(Test.SByteEnum.One == 1)
self.failUnless(Test.SByteEnum.Two == 2)
def testShortEnum(self):
"""Test short enum."""
self.failUnless(Test.ShortEnum.Zero == 0)
self.failUnless(Test.ShortEnum.One == 1)
self.failUnless(Test.ShortEnum.Two == 2)
def testUShortEnum(self):
"""Test ushort enum."""
self.failUnless(Test.UShortEnum.Zero == 0)
self.failUnless(Test.UShortEnum.One == 1)
self.failUnless(Test.UShortEnum.Two == 2)
def testIntEnum(self):
"""Test int enum."""
self.failUnless(Test.IntEnum.Zero == 0)
self.failUnless(Test.IntEnum.One == 1)
self.failUnless(Test.IntEnum.Two == 2)
def testUIntEnum(self):
"""Test uint enum."""
self.failUnless(Test.UIntEnum.Zero == 0L)
self.failUnless(Test.UIntEnum.One == 1L)
self.failUnless(Test.UIntEnum.Two == 2L)
def testLongEnum(self):
"""Test long enum."""
self.failUnless(Test.LongEnum.Zero == 0L)
self.failUnless(Test.LongEnum.One == 1L)
self.failUnless(Test.LongEnum.Two == 2L)
def testULongEnum(self):
"""Test ulong enum."""
self.failUnless(Test.ULongEnum.Zero == 0L)
self.failUnless(Test.ULongEnum.One == 1L)
self.failUnless(Test.ULongEnum.Two == 2L)
def testInstantiateEnumFails(self):
"""Test that instantiation of an enum class fails."""
def test():
ob = DayOfWeek()
self.failUnlessRaises(TypeError, test)
def testSubclassEnumFails(self):
"""Test that subclassing of an enumeration fails."""
def test():
class Boom(DayOfWeek):
pass
self.failUnlessRaises(TypeError, test)
def testEnumSetMemberFails(self):
"""Test that setattr operations on enumerations fail."""
def test():
DayOfWeek.Sunday = 13
self.failUnlessRaises(TypeError, test)
def test():
del DayOfWeek.Sunday
self.failUnlessRaises(TypeError, test)
def testEnumWithFlagsAttrConversion(self):
"""Test enumeration conversion with FlagsAttribute set."""
from CLR.System.Windows.Forms import Label
# This works because the AnchorStyles enum has FlagsAttribute.
label = Label()
label.Anchor = 99
# This should fail because our test enum doesn't have it.
def test():
Test.FieldTest().EnumField = 99
self.failUnlessRaises(ValueError, test)
def testEnumConversion(self):
"""Test enumeration conversion."""
object = Test.FieldTest()
self.failUnless(object.EnumField == 0)
object.EnumField = Test.ShortEnum.One
self.failUnless(object.EnumField == 1)
def test():
Test.FieldTest().EnumField = 20
self.failUnlessRaises(ValueError, test)
def test():
Test.FieldTest().EnumField = 100000
self.failUnlessRaises(OverflowError, test)
def test():
Test.FieldTest().EnumField = "str"
self.failUnlessRaises(TypeError, test)
def test_suite():
return unittest.makeSuite(EnumTests)
def main():
unittest.TextTestRunner().run(test_suite())
if __name__ == '__main__':
testcase.setup()
main()
=== Added File PythonNet/tests/python/test_event.py ===
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
#
# All Rights Reserved.
#
# 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.
import sys, os, string, unittest, types
from CLR.Python.Test import EventTest
class EventTests(unittest.TestCase):
"""Test CLR event support."""
def testPublicInstanceEvent(self):
"""Test public instance events."""
pass
## def testPublicStaticField(self):
## """Test public static fields."""
## object = TestClass();
## self.failUnless(TestClass.PublicStaticField == 'public')
## TestClass.PublicStaticField = 'changed'
## self.failUnless(TestClass.PublicStaticField == 'changed')
## self.failUnless(object.PublicStaticField == 'changed')
## object.PublicStaticField = 'public'
## self.failUnless(object.PublicStaticField == 'public')
## def test():
## del TestClass.PublicStaticField
## self.failUnlessRaises(TypeError, test)
## def test():
## del TestClass().PublicStaticField
## self.failUnlessRaises(TypeError, test)
## def testProtectedInstanceField(self):
## """Test protected instance fields."""
## object = TestClass();
## self.failUnless(object.ProtectedField == 'protected')
## object.ProtectedField = 'changed'
## self.failUnless(object.ProtectedField == 'changed')
## def test():
## del TestClass().ProtectedField
## self.failUnlessRaises(TypeError, test)
## def testProtectedStaticField(self):
## """Test protected static fields."""
## object = TestClass();
## self.failUnless(TestClass.ProtectedStaticField == 'protected')
## TestClass.ProtectedStaticField = 'changed'
## self.failUnless(TestClass.ProtectedStaticField == 'changed')
## self.failUnless(object.ProtectedStaticField == 'changed')
## object.ProtectedStaticField = 'protected'
## self.failUnless(object.ProtectedStaticField == 'protected')
## def test():
## del TestClass.ProtectedStaticField
## self.failUnlessRaises(TypeError, test)
## def test():
## del TestClass().ProtectedStaticField
## self.failUnlessRaises(TypeError, test)
## def testReadOnlyInstanceField(self):
## """Test readonly instance fields."""
## self.failUnless(TestClass().ReadOnlyField == 'readonly')
## def test():
## TestClass().ReadOnlyField = 'changed'
## self.failUnlessRaises(TypeError, test)
## def test():
## del TestClass().ReadOnlyField
## self.failUnlessRaises(TypeError, test)
## def testReadOnlyStaticField(self):
## """Test readonly static fields."""
## object = TestClass();
## self.failUnless(TestClass.ReadOnlyStaticField == 'readonly')
## self.failUnless(object.ReadOnlyStaticField == 'readonly')
## def test():
## TestClass.ReadOnlyStaticField = 'changed'
## self.failUnlessRaises(TypeError, test)
## def test():
## TestClass().ReadOnlyStaticField = 'changed'
## self.failUnlessRaises(TypeError, test)
## def test():
## del TestClass.ReadOnlyStaticField
## self.failUnlessRaises(TypeError, test)
## def test():
## del TestClass().ReadOnlyStaticField
## self.failUnlessRaises(TypeError, test)
## def testConstantField(self):
## """Test const fields."""
## object = TestClass();
## self.failUnless(TestClass.ConstField == 'const')
## self.failUnless(object.ConstField == 'const')
## def test():
## TestClass().ConstField = 'changed'
## self.failUnlessRaises(TypeError, test)
## def test():
## TestClass.ConstField = 'changed'
## self.failUnlessRaises(TypeError, test)
## def test():
## del TestClass().ConstField
## self.failUnlessRaises(TypeError, test)
## def test():
## del TestClass.ConstField
## self.failUnlessRaises(TypeError, test)
## def testInternalField(self):
## """Test internal fields."""
## def test():
## f = TestClass().InternalField
## self.failUnlessRaises(AttributeError, test)
## def test():
## f = TestClass().InternalStaticField
## self.failUnlessRaises(AttributeError, test)
## def test():
## f = TestClass.InternalStaticField
## self.failUnlessRaises(AttributeError, test)
## def testPrivateField(self):
## """Test private fields."""
## def test():
## f = TestClass().PrivateField
## self.failUnlessRaises(AttributeError, test)
## def test():
## f = TestClass().PrivateStaticField
## self.failUnlessRaises(AttributeError, test)
## def test():
## f = TestClass.PrivateStaticField
## self.failUnlessRaises(AttributeError, test)
## def testFieldDescriptorGetSet(self):
## """Test field descriptor get / set."""
## # This test ensures that setting an attribute implemented with
## # a descript actually goes through the descriptor (rather than
## # silently replacing the descriptor in the instance or type dict.
## object = TestClass()
## self.failUnless(TestClass.PublicStaticField == 'public')
## self.failUnless(object.PublicStaticField == 'public')
## descriptor = TestClass.__dict__['PublicStaticField']
## self.failUnless(type(descriptor) != types.StringType)
## object.PublicStaticField = 'public'
## descriptor = TestClass.__dict__['PublicStaticField']
## self.failUnless(type(descriptor) != types.StringType)
## TestClass.PublicStaticField = 'public'
## descriptor = TestClass.__dict__['PublicStaticField']
## self.failUnless(type(descriptor) != types.StringType)
## def testFieldDescriptorWrongType(self):
## """Test setting a field using a value of the wrong type."""
## def test():
## TestClass().PublicField = 0
## self.failUnlessRaises(TypeError, test)
## def testFieldDescriptorAbuse(self):
## """Test field descriptor abuse."""
## desc = TestClass.__dict__['PublicField']
## def test():
## desc.__get__(0, 0)
## self.failUnlessRaises(TypeError, test)
## def test():
## desc.__set__(0, 0)
## self.failUnlessRaises(TypeError, test)
def test_suite():
return unittest.makeSuite(EventTests)
def main():
unittest.TextTestRunner().run(test_suite())
if __name__ == '__main__':
testcase.setup()
main()
=== Added File PythonNet/tests/python/test_exceptions.py ===
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
#
# All Rights Reserved.
#
# 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.
import sys, os, string, unittest, types
from CLR.Python.Test import ConversionTest
from CLR import System
class ExceptionTests(unittest.TestCase):
"""Test exception support."""
def testOverflowException(self):
"""Test overflow exception propagation."""
from CLR.System import Decimal, OverflowException
def test():
l = Decimal.ToInt64(Decimal.MaxValue)
self.failUnlessRaises(OverflowException, test)
def test_suite():
return unittest.makeSuite(ExceptionTests)
def main():
unittest.TextTestRunner().run(test_suite())
if __name__ == '__main__':
testcase.setup()
main()
=== Added File PythonNet/tests/python/test_field.py ===
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
#
# All Rights Reserved.
#
# 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.
import sys, os, string, unittest, types
from CLR.Python.Test import FieldTest
from CLR.Python.Test import ShortEnum
from CLR import System
class FieldTests(unittest.TestCase):
"""Test CLR field support."""
def testPublicInstanceField(self):
"""Test public instance fields."""
object = FieldTest();
self.failUnless(object.PublicField == 0)
object.PublicField = 1
self.failUnless(object.PublicField == 1)
def test():
del FieldTest().PublicField
self.failUnlessRaises(TypeError, test)
def testPublicStaticField(self):
"""Test public static fields."""
object = FieldTest();
self.failUnless(FieldTest.PublicStaticField == 0)
FieldTest.PublicStaticField = 1
self.failUnless(FieldTest.PublicStaticField == 1)
self.failUnless(object.PublicStaticField == 1)
object.PublicStaticField = 0
self.failUnless(object.PublicStaticField == 0)
def test():
del FieldTest.PublicStaticField
self.failUnlessRaises(TypeError, test)
def test():
del FieldTest().PublicStaticField
self.failUnlessRaises(TypeError, test)
def testProtectedInstanceField(self):
"""Test protected instance fields."""
object = FieldTest();
self.failUnless(object.ProtectedField == 0)
object.ProtectedField = 1
self.failUnless(object.ProtectedField == 1)
def test():
del FieldTest().ProtectedField
self.failUnlessRaises(TypeError, test)
def testProtectedStaticField(self):
"""Test protected static fields."""
object = FieldTest();
self.failUnless(FieldTest.ProtectedStaticField == 0)
FieldTest.ProtectedStaticField = 1
self.failUnless(FieldTest.ProtectedStaticField == 1)
self.failUnless(object.ProtectedStaticField == 1)
object.ProtectedStaticField = 0
self.failUnless(object.ProtectedStaticField == 0)
def test():
del FieldTest.ProtectedStaticField
self.failUnlessRaises(TypeError, test)
def test():
del FieldTest().ProtectedStaticField
self.failUnlessRaises(TypeError, test)
def testReadOnlyInstanceField(self):
"""Test readonly instance fields."""
self.failUnless(FieldTest().ReadOnlyField == 0)
def test():
FieldTest().ReadOnlyField = 1
self.failUnlessRaises(TypeError, test)
def test():
del FieldTest().ReadOnlyField
self.failUnlessRaises(TypeError, test)
def testReadOnlyStaticField(self):
"""Test readonly static fields."""
object = FieldTest();
self.failUnless(FieldTest.ReadOnlyStaticField == 0)
self.failUnless(object.ReadOnlyStaticField == 0)
def test():
FieldTest.ReadOnlyStaticField = 1
self.failUnlessRaises(TypeError, test)
def test():
FieldTest().ReadOnlyStaticField = 1
self.failUnlessRaises(TypeError, test)
def test():
del FieldTest.ReadOnlyStaticField
self.failUnlessRaises(TypeError, test)
def test():
del FieldTest().ReadOnlyStaticField
self.failUnlessRaises(TypeError, test)
def testConstantField(self):
"""Test const fields."""
object = FieldTest();
self.failUnless(FieldTest.ConstField == 0)
self.failUnless(object.ConstField == 0)
def test():
FieldTest().ConstField = 1
self.failUnlessRaises(TypeError, test)
def test():
FieldTest.ConstField = 1
self.failUnlessRaises(TypeError, test)
def test():
del FieldTest().ConstField
self.failUnlessRaises(TypeError, test)
def test():
del FieldTest.ConstField
self.failUnlessRaises(TypeError, test)
def testInternalField(self):
"""Test internal fields."""
def test():
f = FieldTest().InternalField
self.failUnlessRaises(AttributeError, test)
def test():
f = FieldTest().InternalStaticField
self.failUnlessRaises(AttributeError, test)
def test():
f = FieldTest.InternalStaticField
self.failUnlessRaises(AttributeError, test)
def testPrivateField(self):
"""Test private fields."""
def test():
f = FieldTest().PrivateField
self.failUnlessRaises(AttributeError, test)
def test():
f = FieldTest().PrivateStaticField
self.failUnlessRaises(AttributeError, test)
def test():
f = FieldTest.PrivateStaticField
self.failUnlessRaises(AttributeError, test)
def testFieldDescriptorGetSet(self):
"""Test field descriptor get / set."""
# This test ensures that setting an attribute implemented with
# a descriptor actually goes through the descriptor (rather than
# silently replacing the descriptor in the instance or type dict.
object = FieldTest()
self.failUnless(FieldTest.PublicStaticField == 0)
self.failUnless(object.PublicStaticField == 0)
descriptor = FieldTest.__dict__['PublicStaticField']
self.failUnless(type(descriptor) != types.IntType)
object.PublicStaticField = 0
descriptor = FieldTest.__dict__['PublicStaticField']
self.failUnless(type(descriptor) != types.IntType)
FieldTest.PublicStaticField = 0
descriptor = FieldTest.__dict__['PublicStaticField']
self.failUnless(type(descriptor) != types.IntType)
def testFieldDescriptorWrongType(self):
"""Test setting a field using a value of the wrong type."""
def test():
FieldTest().PublicField = "spam"
self.failUnlessRaises(TypeError, test)
def testFieldDescriptorAbuse(self):
"""Test field descriptor abuse."""
desc = FieldTest.__dict__['PublicField']
def test():
desc.__get__(0, 0)
self.failUnlessRaises(TypeError, test)
def test():
desc.__set__(0, 0)
self.failUnlessRaises(TypeError, test)
def testBooleanField(self):
"""Test boolean fields."""
# change this to true / false later for Python 2.3?
object = FieldTest()
self.failUnless(object.BooleanField == 0)
object.BooleanField = 1
self.failUnless(object.BooleanField == 1)
def testSByteField(self):
"""Test sbyte fields."""
object = FieldTest()
self.failUnless(object.SByteField == 0)
object.SByteField = 1
self.failUnless(object.SByteField == 1)
def testByteField(self):
"""Test byte fields."""
object = FieldTest()
self.failUnless(object.ByteField == 0)
object.ByteField = 1
self.failUnless(object.ByteField == 1)
def testCharField(self):
"""Test char fields."""
object = FieldTest()
self.failUnless(object.CharField == u'A')
self.failUnless(object.CharField == 'A')
object.CharField = 'B'
self.failUnless(object.CharField == u'B')
self.failUnless(object.CharField == 'B')
object.CharField = u'C'
self.failUnless(object.CharField == u'C')
self.failUnless(object.CharField == 'C')
def testInt16Field(self):
"""Test int16 fields."""
object = FieldTest()
self.failUnless(object.Int16Field == 0)
object.Int16Field = 1
self.failUnless(object.Int16Field == 1)
def testInt32Field(self):
"""Test int32 fields."""
object = FieldTest()
self.failUnless(object.Int32Field == 0)
object.Int32Field = 1
self.failUnless(object.Int32Field == 1)
def testInt64Field(self):
"""Test int64 fields."""
object = FieldTest()
self.failUnless(object.Int64Field == 0)
object.Int64Field = 1
self.failUnless(object.Int64Field == 1)
def testUInt16Field(self):
"""Test uint16 fields."""
object = FieldTest()
self.failUnless(object.UInt16Field == 0)
object.UInt16Field = 1
self.failUnless(object.UInt16Field == 1)
def testUInt32Field(self):
"""Test uint32 fields."""
object = FieldTest()
self.failUnless(object.UInt32Field == 0)
object.UInt32Field = 1
self.failUnless(object.UInt32Field == 1)
def testUInt64Field(self):
"""Test uint64 fields."""
object = FieldTest()
self.failUnless(object.UInt64Field == 0)
object.UInt64Field = 1
self.failUnless(object.UInt64Field == 1)
def testSingleField(self):
"""Test single fields."""
object = FieldTest()
self.failUnless(object.SingleField == 0.0)
object.SingleField = 1.1
self.failUnless(object.SingleField == 1.1)
def testDoubleField(self):
"""Test double fields."""
object = FieldTest()
self.failUnless(object.DoubleField == 0.0)
object.DoubleField = 1.1
self.failUnless(object.DoubleField == 1.1)
def testDecimalField(self):
"""Test decimal fields."""
object = FieldTest()
self.failUnless(object.DecimalField == System.Decimal(0))
object.DecimalField = System.Decimal(1)
self.failUnless(object.DecimalField == System.Decimal(1))
def testStringField(self):
"""Test string fields."""
object = FieldTest()
self.failUnless(object.StringField == "spam")
object.StringField = "eggs"
self.failUnless(object.StringField == "eggs")
def testInterfaceField(self):
"""Test interface fields."""
from CLR.Python.Test import Spam
object = FieldTest()
self.failUnless(object.SpamField.__class__.__name__ == "ISpam")
self.failUnless(object.SpamField.GetValue() == "spam")
object.SpamField = Spam("eggs")
self.failUnless(object.SpamField.__class__.__name__ == "ISpam")
self.failUnless(object.SpamField.GetValue() == "eggs")
def testObjectField(self):
"""Test object fields."""
object = FieldTest()
self.failUnless(object.ObjectField == None)
object.ObjectField = System.String("spam")
self.failUnless(object.ObjectField == "spam")
object.ObjectField = System.Int32(1)
self.failUnless(object.ObjectField == 1)
object.ObjectField = None
self.failUnless(object.ObjectField == None)
def testEnumField(self):
"""Test enum fields."""
object = FieldTest()
self.failUnless(object.EnumField == ShortEnum.Zero)
object.EnumField = ShortEnum.One
self.failUnless(object.EnumField == ShortEnum.One)
def testNullableField(self):
"""Test nullable fields."""
object = FieldTest()
object.StringField = None
self.failUnless(object.StringField == None)
object.ObjectField = None
self.failUnless(object.ObjectField == None)
object.SpamField = None
self.failUnless(object.SpamField == None)
# Primitive types and enums should not be set to null.
def test():
FieldTest().Int32Field = None
self.failUnlessRaises(TypeError, test)
def test():
FieldTest().EnumField = None
self.failUnlessRaises(TypeError, test)
def test_suite():
return unittest.makeSuite(FieldTests)
def main():
unittest.TextTestRunner().run(test_suite())
if __name__ == '__main__':
testcase.setup()
main()
=== Added File PythonNet/tests/python/test_interface.py ===
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
#
# All Rights Reserved.
#
# 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 CLR.Python.Test import InterfaceTest
import sys, os, string, unittest, types
import CLR.System as System
import CLR.Python.Test as Test
class InterfaceTests(unittest.TestCase):
"""Test CLR interface support."""
def testInterfaceStandardAttrs(self):
"""Test standard class attributes."""
self.failUnless(InterfaceTest.__name__ == 'InterfaceTest')
self.failUnless(InterfaceTest.__module__ == 'CLR.Python.Test')
self.failUnless(type(InterfaceTest.__dict__) == types.DictProxyType)
self.failUnless(InterfaceTest.__doc__ == None)
def testGlobalInterfaceVisibility(self):
"""Test visibility of module-level interfaces."""
from CLR.Python.Test import IPublicInterface
self.failUnless(IPublicInterface.__name__ == 'IPublicInterface')
def test():
from CLR.Python.Test import IInternalInterface
self.failUnlessRaises(ImportError, test)
def test():
i = Test.IInternalInterface
self.failUnlessRaises(AttributeError, test)
def testNestedInterfaceVisibility(self):
"""Test visibility of nested interfaces."""
ob = InterfaceTest.IPublic
self.failUnless(ob.__name__ == 'IPublic')
ob = InterfaceTest.IProtected
self.failUnless(ob.__name__ == 'IProtected')
def test():
ob = InterfaceTest.IInternal
self.failUnlessRaises(AttributeError, test)
def test():
ob = InterfaceTest.IPrivate
self.failUnlessRaises(AttributeError, test)
def testExplicitCastToInterface(self):
"""Test explicit cast to an interface."""
ob = InterfaceTest()
self.failUnless(type(ob).__name__ == 'InterfaceTest')
self.failUnless(hasattr(ob, 'HelloProperty'))
i1 = Test.ISayHello1(ob)
self.failUnless(type(i1).__name__ == 'ISayHello1')
self.failUnless(hasattr(i1, 'SayHello'))
self.failUnless(i1.SayHello() == 'hello 1')
self.failIf(hasattr(i1, 'HelloProperty'))
i2 = Test.ISayHello2(ob)
self.failUnless(type(i2).__name__ == 'ISayHello2')
self.failUnless(i2.SayHello() == 'hello 2')
self.failUnless(hasattr(i2, 'SayHello'))
self.failIf(hasattr(i2, 'HelloProperty'))
# probably should test some funky interface conversion scenarios
def test_suite():
return unittest.makeSuite(InterfaceTests)
def main():
unittest.TextTestRunner().run(test_suite())
if __name__ == '__main__':
testcase.setup()
main()
=== Added File PythonNet/tests/python/test_method.py ===
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
#
# All Rights Reserved.
#
# 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.
import sys, os, string, unittest, types
from CLR.Python.Test import MethodTest
class MethodTests(unittest.TestCase):
"""Test CLR method support."""
def testInstanceMethodDescriptor(self):
"""Test instance method descriptor behavior."""
def test():
MethodTest().PublicMethod = 0
self.failUnlessRaises(AttributeError, test)
def test():
MethodTest.PublicMethod = 0
self.failUnlessRaises(AttributeError, test)
def test():
del MethodTest().PublicMethod
self.failUnlessRaises(AttributeError, test)
def test():
del MethodTest.PublicMethod
self.failUnlessRaises(AttributeError, test)
def testStaticMethodDescriptor(self):
"""Test static method descriptor behavior."""
def test():
MethodTest().PublicStaticMethod = 0
self.failUnlessRaises(AttributeError, test)
def test():
MethodTest.PublicStaticMethod = 0
self.failUnlessRaises(AttributeError, test)
def test():
del MethodTest().PublicStaticMethod
self.failUnlessRaises(AttributeError, test)
def test():
del MethodTest.PublicStaticMethod
self.failUnlessRaises(AttributeError, test)
def testPublicInstanceMethod(self):
"""Test public instance method visibility."""
object = MethodTest();
self.failUnless(object.PublicMethod() == "public")
def testPublicStaticMethod(self):
"""Test public static method visibility."""
object = MethodTest();
self.failUnless(MethodTest.PublicStaticMethod() == "public static")
self.failUnless(object.PublicStaticMethod() == "public static")
def testProtectedInstanceMethod(self):
"""Test protected instance method visibility."""
object = MethodTest();
self.failUnless(object.ProtectedMethod() == "protected")
def testProtectedStaticMethod(self):
"""Test protected static method visibility."""
object = MethodTest();
result = "protected static"
self.failUnless(MethodTest.ProtectedStaticMethod() == result)
self.failUnless(object.ProtectedStaticMethod() == result)
def testInternalMethod(self):
"""Test internal method visibility."""
def test():
f = MethodTest().InternalMethod
self.failUnlessRaises(AttributeError, test)
def test():
f = MethodTest.InternalMethod
self.failUnlessRaises(AttributeError, test)
def test():
f = MethodTest().InternalStaticMethod
self.failUnlessRaises(AttributeError, test)
def test():
f = MethodTest.InternalStaticMethod
self.failUnlessRaises(AttributeError, test)
def testPrivateMethod(self):
"""Test private method visibility."""
def test():
f = MethodTest().PrivateMethod
self.failUnlessRaises(AttributeError, test)
def test():
f = MethodTest.PrivateMethod
self.failUnlessRaises(AttributeError, test)
def test():
f = MethodTest().PrivateStaticMethod
self.failUnlessRaises(AttributeError, test)
def test():
f = MethodTest.PrivateStaticMethod
self.failUnlessRaises(AttributeError, test)
def testMethodDescriptorAbuse(self):
"""Test method descriptor abuse."""
desc = MethodTest.__dict__['PublicMethod']
def test():
desc.__get__(0, 0)
self.failUnlessRaises(TypeError, test)
def test():
desc.__set__(0, 0)
self.failUnlessRaises(AttributeError, test)
def test_suite():
return unittest.makeSuite(MethodTests)
def main():
unittest.TextTestRunner().run(test_suite())
if __name__ == '__main__':
testcase.setup()
main()
=== Added File PythonNet/tests/python/test_module.py ===
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
#
# All Rights Reserved.
#
# 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.
import sys, os, string, unittest, types
class ModuleTests(unittest.TestCase):
"""Test CLR modules and the CLR import hook."""
def isCLRModule(self, object):
return type(object).__name__ == 'ModuleObject'
def isCLRClass(self, object):
return type(object).__name__ == 'MetaType' # for now
def testModuleInterface(self):
"""Test the interface exposed by CLR module objects."""
import CLR.System as System
self.assertEquals(type(System.__dict__), types.DictProxyType)
self.assertEquals(System.__name__, 'CLR.System')
self.assertEquals(System.__doc__, None)
self.failUnless(self.isCLRClass(System.String))
self.failUnless(self.isCLRClass(System.Int32))
def test():
# CLR modules don't define __file__
import CLR.System as System
file = System.__file__
self.failUnlessRaises(AttributeError, test)
def testSimpleImport(self):
"""Test simple import."""
import CLR
self.failUnless(self.isCLRModule(CLR))
self.failUnless(CLR.__name__ == 'CLR')
import sys
self.failUnless(type(sys) == types.ModuleType)
self.failUnless(sys.__name__ == 'sys')
import httplib
self.failUnless(type(httplib) == types.ModuleType)
self.failUnless(httplib.__name__ == 'httplib')
def testSimpleImportWithAlias(self):
"""Test simple import with aliasing."""
import CLR as myCLR
self.failUnless(self.isCLRModule(myCLR))
self.failUnless(myCLR.__name__ == 'CLR')
import sys as mySys
self.failUnless(type(mySys) == types.ModuleType)
self.failUnless(mySys.__name__ == 'sys')
import httplib as myHttplib
self.failUnless(type(myHttplib) == types.ModuleType)
self.failUnless(myHttplib.__name__ == 'httplib')
def testDottedNameImport(self):
"""Test dotted-name import."""
import CLR.System
self.failUnless(self.isCLRModule(CLR.System))
self.failUnless(CLR.System.__name__ == 'CLR.System')
import xml.dom
self.failUnless(type(xml.dom) == types.ModuleType)
self.failUnless(xml.dom.__name__ == 'xml.dom')
def testDottedNameImportWithAlias(self):
"""Test dotted-name import with aliasing."""
import CLR.System as mySystem
self.failUnless(self.isCLRModule(mySystem))
self.failUnless(mySystem.__name__ == 'CLR.System')
import xml.dom as myDom
self.failUnless(type(myDom) == types.ModuleType)
self.failUnless(myDom.__name__ == 'xml.dom')
def testSimpleImportFrom(self):
"""Test simple 'import from'."""
from CLR import System
self.failUnless(self.isCLRModule(System))
self.failUnless(System.__name__ == 'CLR.System')
from xml import dom
self.failUnless(type(dom) == types.ModuleType)
self.failUnless(dom.__name__ == 'xml.dom')
def testSimpleImportFromWithAlias(self):
"""Test simple 'import from' with aliasing."""
from CLR import System as mySystem
self.failUnless(self.isCLRModule(mySystem))
self.failUnless(mySystem.__name__ == 'CLR.System')
from xml import dom as myDom
self.failUnless(type(myDom) == types.ModuleType)
self.failUnless(myDom.__name__ == 'xml.dom')
def testDottedNameImportFrom(self):
"""Test dotted-name 'import from'."""
from CLR.System import Xml
self.failUnless(self.isCLRModule(Xml))
self.failUnless(Xml.__name__ == 'CLR.System.Xml')
from CLR.System.Xml import XmlDocument
self.failUnless(self.isCLRClass(XmlDocument))
self.failUnless(XmlDocument.__name__ == 'XmlDocument')
from xml.dom import pulldom
self.failUnless(type(pulldom) == types.ModuleType)
self.failUnless(pulldom.__name__ == 'xml.dom.pulldom')
from xml.dom.pulldom import PullDOM
self.failUnless(type(PullDOM) == types.ClassType)
self.failUnless(PullDOM.__name__ == 'PullDOM')
def testDottedNameImportFromWithAlias(self):
"""Test dotted-name 'import from' with aliasing."""
from CLR.System import Xml as myXml
self.failUnless(self.isCLRModule(myXml))
self.failUnless(myXml.__name__ == 'CLR.System.Xml')
from CLR.System.Xml import XmlDocument as myXmlDocument
self.failUnless(self.isCLRClass(myXmlDocument))
self.failUnless(myXmlDocument.__name__ == 'XmlDocument')
from xml.dom import pulldom as myPulldom
self.failUnless(type(myPulldom) == types.ModuleType)
self.failUnless(myPulldom.__name__ == 'xml.dom.pulldom')
from xml.dom.pulldom import PullDOM as myPullDOM
self.failUnless(type(myPullDOM) == types.ClassType)
self.failUnless(myPullDOM.__name__ == 'PullDOM')
def testFromModuleImportStar(self):
"""Test from module import * behavior."""
# Using 'from x import *' is considered evil generally, more so
# in this case where you may have hundreds of defined types in
# a namespace. The intended behavior is that doing 'import *'
# from a CLR module won't blow up, but it also won't really do
# anything to get names into your namespace.
m = __import__('CLR.System.Xml', globals(), locals(), ['*'])
self.failUnless(m.__name__ == 'CLR.System.Xml')
self.failUnless(self.isCLRModule(m))
def testImplicitAssemblyLoad(self):
"""Test implicit assembly loading via import."""
def test():
# This should fail until CLR.System.Windows.Forms has been
# imported or that assembly has been explicitly loaded.
import CLR.System.Windows
self.failUnlessRaises(ImportError, test)
import CLR.System.Windows.Forms as Forms
self.failUnless(self.isCLRModule(Forms))
self.failUnless(Forms.__name__ == 'CLR.System.Windows.Forms')
from CLR.System.Drawing import Graphics
self.failUnless(self.isCLRClass(Graphics))
self.failUnless(Graphics.__name__ == 'Graphics')
def testExplicitAssemblyLoad(self):
"""Test explicit assembly loading using standard CLR tools."""
from CLR.System.Reflection import Assembly
import sys
assembly = Assembly.LoadWithPartialName('System.Data')
self.failUnless(assembly != None)
import CLR.System.Data
self.failUnless(sys.modules.has_key('CLR.System.Data'))
assembly = Assembly.LoadWithPartialName('SpamSpamSpamSpamEggsAndSpam')
self.failUnless(assembly == None)
def testImplicitLoadAlreadyValidNamespace(self):
"""Test implicit assembly load over an already valid namespace."""
# In this case, the mscorlib assembly (loaded by default) defines
# a number of types in the System namespace. There is also a System
# assembly, which is _not_ loaded by default, which also contains
# types in the System namespace. The desired behavior is for the
# Python runtime to "do the right thing", allowing types from both
# assemblies to be found in the CLR.System module implicitly.
import CLR.System
self.failUnless(self.isCLRClass(CLR.System.UriBuilder))
def testImportNonExistantModule(self):
"""Test import failure for a non-existant module."""
def test():
import CLR.System.SpamSpamSpam
self.failUnlessRaises(ImportError, test)
def testLookupNoNamespaceType(self):
"""Test lookup of types without a qualified namespace."""
import CLR.Python.Test
import CLR
self.failUnless(self.isCLRClass(CLR.NoNamespaceType))
def testModuleLookupRecursion(self):
"""Test for recursive lookup handling."""
def test1():
from CLR import CLR
self.failUnlessRaises(ImportError, test1)
def test2():
import CLR
x = CLR.CLR
self.failUnlessRaises(AttributeError, test2)
def testModuleGetAttr(self):
"""Test module getattr behavior."""
import CLR.System as System
int_type = System.Int32
self.failUnless(self.isCLRClass(int_type))
module = System.Xml
self.failUnless(self.isCLRModule(module))
def test():
spam = System.Spam
self.failUnlessRaises(AttributeError, test)
def test():
spam = getattr(System, 1)
self.failUnlessRaises(TypeError, test)
def testModuleAttrAbuse(self):
"""Test handling of attempts to set module attributes."""
def test():
import CLR.System
CLR.System.__dict__['foo'] = 0
self.failUnlessRaises(TypeError, test)
def test():
import CLR.System
del CLR.System.__dict__['String']
self.failUnlessRaises(TypeError, test)
def test():
import CLR.System
del CLR.System.__dict__
self.failUnlessRaises(TypeError, test)
def test():
import CLR.System
CLR.System.Int32 = 0
self.failUnlessRaises(TypeError, test)
def test():
import CLR.System
del CLR.System.Int32
self.failUnlessRaises(TypeError, test)
def test():
import CLR
CLR.LoadAssembly = None
self.failUnlessRaises(TypeError, test)
def test():
import CLR
del CLR.LoadAssembly
self.failUnlessRaises(TypeError, test)
def testModuleTypeAbuse(self):
"""Test handling of attempts to break the module type."""
import CLR.System as System
mtype = type(System)
def test():
mtype.__getattribute__(0, 'spam')
self.failUnlessRaises(TypeError, test)
def test():
mtype.__setattr__(0, 'spam', 1)
self.failUnlessRaises(TypeError, test)
def test():
mtype.__repr__(0)
self.failUnlessRaises(TypeError, test)
def test_suite():
return unittest.makeSuite(ModuleTests)
def main():
unittest.TextTestRunner().run(test_suite())
if __name__ == '__main__':
testcase.setup()
main()
=== Added File PythonNet/tests/python/test_property.py ===
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
#
# All Rights Reserved.
#
# 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.
import sys, os, string, unittest, types
from CLR.Python.Test import PropertyTest
class PropertyTests(unittest.TestCase):
"""Test CLR property support."""
def testPublicInstanceProperty(self):
"""Test public instance properties."""
object = PropertyTest();
self.failUnless(object.PublicProperty == 0)
object.PublicProperty = 1
self.failUnless(object.PublicProperty == 1)
def test():
del PropertyTest().PublicProperty
self.failUnlessRaises(TypeError, test)
def testPublicStaticProperty(self):
"""Test public static properties."""
object = PropertyTest();
self.failUnless(PropertyTest.PublicStaticProperty == 0)
PropertyTest.PublicStaticProperty = 1
self.failUnless(PropertyTest.PublicStaticProperty == 1)
self.failUnless(object.PublicStaticProperty == 1)
object.PublicStaticProperty = 0
self.failUnless(object.PublicStaticProperty == 0)
def test():
del PropertyTest.PublicStaticProperty
self.failUnlessRaises(TypeError, test)
def test():
del PropertyTest().PublicStaticProperty
self.failUnlessRaises(TypeError, test)
def testProtectedInstanceProperty(self):
"""Test protected instance properties."""
object = PropertyTest();
self.failUnless(object.ProtectedProperty == 0)
object.ProtectedProperty = 1
self.failUnless(object.ProtectedProperty == 1)
def test():
del PropertyTest().ProtectedProperty
self.failUnlessRaises(TypeError, test)
def testProtectedStaticProperty(self):
"""Test protected static properties."""
object = PropertyTest();
self.failUnless(PropertyTest.ProtectedStaticProperty == 0)
PropertyTest.ProtectedStaticProperty = 1
self.failUnless(PropertyTest.ProtectedStaticProperty == 1)
self.failUnless(object.ProtectedStaticProperty == 1)
object.ProtectedStaticProperty = 0
self.failUnless(object.ProtectedStaticProperty == 0)
def test():
del PropertyTest.ProtectedStaticProperty
self.failUnlessRaises(TypeError, test)
def test():
del PropertyTest().ProtectedStaticProperty
self.failUnlessRaises(TypeError, test)
def testInternalProperty(self):
"""Test internal properties."""
def test():
f = PropertyTest().InternalProperty
self.failUnlessRaises(AttributeError, test)
def test():
f = PropertyTest().InternalStaticProperty
self.failUnlessRaises(AttributeError, test)
def test():
f = PropertyTest.InternalStaticProperty
self.failUnlessRaises(AttributeError, test)
def testPrivateProperty(self):
"""Test private properties."""
def test():
f = PropertyTest().PrivateProperty
self.failUnlessRaises(AttributeError, test)
def test():
f = PropertyTest().PrivateStaticProperty
self.failUnlessRaises(AttributeError, test)
def test():
f = PropertyTest.PrivateStaticProperty
self.failUnlessRaises(AttributeError, test)
def testPropertyDescriptorGetSet(self):
"""Test property descriptor get / set."""
# This test ensures that setting an attribute implemented with
# a descript actually goes through the descriptor (rather than
# silently replacing the descriptor in the instance or type dict.
object = PropertyTest()
self.failUnless(PropertyTest.PublicStaticProperty == 0)
self.failUnless(object.PublicStaticProperty == 0)
descriptor = PropertyTest.__dict__['PublicStaticProperty']
self.failUnless(type(descriptor) != types.IntType)
object.PublicStaticProperty = 0
descriptor = PropertyTest.__dict__['PublicStaticProperty']
self.failUnless(type(descriptor) != types.IntType)
PropertyTest.PublicStaticProperty = 0
descriptor = PropertyTest.__dict__['PublicStaticProperty']
self.failUnless(type(descriptor) != types.IntType)
def testPropertyDescriptorWrongType(self):
"""Test setting a property using a value of the wrong type."""
def test():
object = PropertyTest()
object.PublicProperty = "spam"
self.failUnlessRaises(TypeError, test)
def testPropertyDescriptorAbuse(self):
"""Test property descriptor abuse."""
desc = PropertyTest.__dict__['PublicProperty']
def test():
desc.__get__(0, 0)
self.failUnlessRaises(TypeError, test)
def test():
desc.__set__(0, 0)
self.failUnlessRaises(TypeError, test)
def test_suite():
return unittest.makeSuite(PropertyTests)
def main():
unittest.TextTestRunner().run(test_suite())
if __name__ == '__main__':
testcase.setup()
main()