test_enum.py :  » Language-Interface » Python-for-.NET » pythonnet-2.0-alpha2 » src » tests » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Language Interface » Python for .NET 
Python for .NET » pythonnet 2.0 alpha2 » src » tests » test_enum.py
# ===========================================================================
# 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 System import DayOfWeek
from 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__ == 'System')
        self.failUnless(type(DayOfWeek.__dict__) == types.DictProxyType)
        self.failUnless(DayOfWeek.__doc__ == '')


    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 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__':
    main()

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.