test_class.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_class.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.
# ===========================================================================

from System.Collections import Hashtable
from Python.Test import ClassTest
import sys, os, string, unittest, types
import Python.Test as Test
import 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__ == 'Python.Test')
        self.failUnless(type(ClassTest.__dict__) == types.DictProxyType)
        self.failUnless(len(ClassTest.__doc__) > 0)


    def testClassDocstrings(self):
        """Test standard class docstring generation"""
        value = 'Void .ctor()'
        self.failUnless(ClassTest.__doc__ == value)


    def testClassDefaultStr(self):
        """Test the default __str__ implementation for managed objects."""
        s = System.String("this is a test")
        self.failUnless(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.failUnless(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.failUnlessRaises(ImportError, test)

        def test():
            x = Test.InternalClass

        self.failUnlessRaises(AttributeError, test)


    def testBasicSubclass(self):
        """Test basic subclass of a managed class."""

        class MyTable(Hashtable):
            def howMany(self):
                return self.Count

        table = MyTable()

        self.failUnless(table.__class__.__name__.endswith('MyTable'))
        self.failUnless(type(table).__name__.endswith('MyTable'))
        self.failUnless(len(table.__class__.__bases__) == 1)
        self.failUnless(table.__class__.__bases__[0] == Hashtable)

        self.failUnless(table.howMany() == 0)
        self.failUnless(table.Count == 0)

        table.set_Item('one', 'one')
        
        self.failUnless(table.howMany() == 1)
        self.failUnless(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.failUnless(inst.value == 'test')

        class SubClass2(ClassCtorTest2):
            def __init__(self, v):
                ClassCtorTest2.__init__(self)
                self.value = v

        inst = SubClass2('test')
        self.failUnless(inst.value == 'test')


    def testStructConstruction(self):
        """Test construction of structs."""
        from System.Drawing import Point

        def test():
            p = Point()

        self.failUnlessRaises(TypeError, test)

        p = Point(0, 0)
        self.failUnless(p.X == 0)
        self.failUnless(p.Y == 0)

        p.X = 10
        p.Y = 10

        self.failUnless(p.X == 10)
        self.failUnless(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.failUnless((item > -1) and (item < 10))

        dict = Test.ClassTest.GetHashtable()

        for item in dict:
            cname = item.__class__.__name__
            self.failUnless(cname.endswith('DictionaryEntry'))


    def testIEnumeratorIteration(self):
        """Test iteration over objects supporting IEnumerator."""
        chars = Test.ClassTest.GetEnumerator()

        for item in chars:
            self.failUnless(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.failUnless(table['one'] == 'my one')
        self.failUnless(table['two'] == 'my two')
        self.failUnless(table['three'] == 'my three')

        self.failUnless(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.failUnless(table['one'] == 'my one')
        self.failUnless(table['two'] == 'my two')
        self.failUnless(table['three'] == 'my three')

        self.failUnless(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()

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