test_structs.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_structs.py
"""
XXX: Add tests that check that the type actually works as expected:

* Use struct value as method argument
* Return struct value from a method

Add tests for nested structs as well (that is assert that NSRect.location is 
an NSPoint, but using our own types)
"""
from PyObjCTools.TestSupport import *
import objc
from PyObjCTest.structs import *
from PyObjCTest.fnd import NSObject


class TestStructs (TestCase):
    def testCreateExplicit(self):
        tp = objc.createStructType("FooStruct", b"{_FooStruct=ffff}", ["a","b","c","d"])
        self.assertIsInstance(tp, type)
        self.assertEquals(tp.__typestr__, b"{_FooStruct=ffff}")

        o = tp()
        self.assertHasAttr(o, 'a')
        self.assertHasAttr(o, 'b')
        self.assertHasAttr(o, 'c')
        self.assertHasAttr(o, 'd')

    def testCreateImplicit(self):
        tp = objc.createStructType("BarStruct", b'{_BarStruct="e"f"f"f"g"f"h"f}', None)
        self.assertIsInstance(tp, type)
        self.assertEquals(tp.__typestr__, b"{_BarStruct=ffff}")

        o = tp()
        self.assertHasAttr(o, 'e')
        self.assertHasAttr(o, 'f')
        self.assertHasAttr(o, 'g')
        self.assertHasAttr(o, 'h')

        self.assertRaises(ValueError, objc.createStructType, "Foo2", b'{_Foo=f"a"}', None) 
        self.assertRaises(ValueError, objc.createStructType, "Foo3", b'{_Foo="a"f', None) 
        self.assertRaises(ValueError, objc.createStructType, "Foo4", b'^{_Foo="a"f}', None) 

    def testPointerFields(self):
        # Note: the created type won't be all that useful unless the pointer
        # happens to be something that PyObjC knows how to deal with, this is
        # more a check to see if createStructType knows how to cope with 
        # non-trivial types.
        tp = objc.createStructType("XBarStruct", b'{_XBarStruct="e"^f"f"^f"g"^@"h"f}', None)
        self.assertIsInstance(tp, type)
        self.assertEquals(tp.__typestr__, b"{_XBarStruct=^f^f^@f}")

        o = tp()
        self.assertHasAttr(o, 'e')
        self.assertHasAttr(o, 'f')
        self.assertHasAttr(o, 'g')
        self.assertHasAttr(o, 'h')

    def testEmbeddedFields(self):
        tp = objc.createStructType("FooStruct", b'{FooStruct="first"i"second"i}', None)

        v = OC_StructTest.createWithFirst_andSecond_(1, 2)
        self.assertIsInstance(v, tp)

        x = OC_StructTest.sumFields_(v)
        self.assertEquals(x, v.first + v.second)
        self.assertEquals(v.first, 1)
        self.assertEquals(v.second, 2)

    def testStructCallback(self):
        """
        Regression test for an issue reported on the PyObjC mailinglist.
        """
        tp = objc.createStructType("FooStruct", b'{FooStruct="first"i"second"i}', None)

        StructArrayDelegate = objc.informal_protocol(
            "StructArrayDelegate",
            [
                objc.selector(None, b"arrayOf4Structs:",
                    signature=b"@@:[4{FooStruct=ii}]"),
            ])

        class OC_PyStruct (NSObject):
            
            def arrayOf4Structs_(self, value):
                return value

        self.assertEquals(OC_PyStruct.arrayOf4Structs_.signature, b"@@:[4{FooStruct=" + objc._C_INT + objc._C_INT + b"}]")

        o = OC_PyStruct.alloc().init()
        v = OC_StructTest.callArrayOf4Structs_(o)
        self.assertEquals(len(v), 4)
        for i in range(3):
            self.assertIsInstance(v[i], tp)

        self.assertEquals(v[0], tp(1, 2))
        self.assertEquals(v[1], tp(3, 4))
        self.assertEquals(v[2], tp(5, 6))
        self.assertEquals(v[3], tp(7, 8))


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.