test_clinmeth.py :  » Development » PyObjC » trunk » pyobjc » pyobjc-core » PyObjCTest » 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 » Development » PyObjC 
PyObjC » trunk » pyobjc » pyobjc core » PyObjCTest » test_clinmeth.py
"""
Tests for accessing methods through classes and instances
"""
from PyObjCTools.TestSupport import *
from PyObjCTest.clinmeth import *
import objc

class TestClassMethods (TestCase):
    # Some very basic tests that check that getattr on instances doesn't
    # return a class method and that getattr on classes prefers classmethods
    # over instance methods (and v.v. for getattr on instances)

    def testViaClass(self):
        m = PyObjC_ClsInst1.clsmeth
        self.assertIsInstance(m, objc.selector)
        self.assertTrue(m.isClassMethod)

        self.assertEquals(m(), 4)

    def testViaInstance(self):
        o = PyObjC_ClsInst1.alloc().init()
        self.assertRaises(AttributeError, getattr, o, "clsmeth")

    def testClassAndInstanceViaClass(self):
        m = PyObjC_ClsInst1.both
        self.assertIsInstance(m, objc.selector)
        self.assertTrue( m.__metadata__()['classmethod'] )

        self.assertEquals(m(), 3)

    def testClassAndInstanceViaInstance(self):
        o = PyObjC_ClsInst1.alloc().init()
        m = o.both
        self.assertTrue( isinstance(m, objc.selector) )
        self.assertTrue( not m.isClassMethod )

        self.assertEquals(m(), 2)


class TestInstanceMethods (TestCase):
    # Check that instance methods can be accessed through the instance, and 
    # also through the class when no class method of the same name is 
    # available.

    def testViaClass(self):
        m = PyObjC_ClsInst1.instance
        self.assertTrue( isinstance(m, objc.selector) )
        self.assertTrue( not m.isClassMethod )

        self.assertRaises(TypeError, m)

    def testViaInstance(self):
        o = PyObjC_ClsInst1.alloc().init()
        m = o.instance

        self.assertIsInstance(m, objc.selector)
        self.assertFalse(m.isClassMethod)

        self.assertEquals(m(), 1)

class TestSuper (TestCase):
    # Tests that check if super() behaves as expected (which is the most likely
    # reason for failure).

    def testClassMethod(self):
        cls = PyObjC_ClsInst2

        self.assertEquals(cls.clsmeth(), 40)
        self.assertEquals(objc.super(cls, cls).clsmeth(), 4)

    def testInstanceMethod(self):
        o = PyObjC_ClsInst2.alloc().init()

        self.assertEquals(o.instance(), 10)
        self.assertEquals(super(PyObjC_ClsInst2, o).instance(), 1)

    def testBoth(self):
        o = PyObjC_ClsInst2.alloc().init()

        self.assertEquals(o.both(), 20)
        self.assertEquals(objc.super(PyObjC_ClsInst2, o).both(), 2)

        cls = PyObjC_ClsInst2

        self.assertEquals(cls.both(), 30)
        self.assertEquals(objc.super(cls, cls).both(), 3)


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.