test_globals.py :  » Development » PyObjC » trunk » pyobjc » pyobjc-framework-Cocoa » 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 framework Cocoa » PyObjCTest » test_globals.py
from PyObjCTools.TestSupport import *
import sys
import struct

import Foundation
import os

class GlobalFunctionTest (TestCase):
    if sys.platform == 'darwin':
        def testNSFileTypeForHFSTypeCode(self):
            self.assertEqual("'rtfx'",
                    Foundation.NSFileTypeForHFSTypeCode(b'rtfx'))

            # The cannonical representation for four-character-codes in python
            # is a string of 4 characters, but at least some ObjC API's return
            # longs (because these methods haven't been wrapped correctly yet).
            # NSFileTypeForHFSTypeCode therefore also accepts integers.
            fourchar = struct.unpack('i', b'rtfx')[0]
            if sys.byteorder == 'little':
                    self.assertEqual("'xftr'",
                            Foundation.NSFileTypeForHFSTypeCode(fourchar))
            else:
                    self.assertEqual("'rtfx'",
                            Foundation.NSFileTypeForHFSTypeCode(fourchar))

        def testNSHFSTypeCodeFromFileType(self):
            self.assertEqual(b"rtfx",
                    Foundation.NSHFSFTypeCodeFromFileType("'rtfx'"))


    def testMakeNSRect(self):
        self.assert_(hasattr(Foundation, 'NSMakeRect'))

        self.assertEqual(
                Foundation.NSMakeRect(1.5, 2.5, 3.5, 4.5),
                ((1.5, 2.5), (3.5, 4.5))
        )
        self.assertEqual(
                Foundation.NSMakeRect(1, 2, 3, 4),
                ((1.0, 2.0), (3.0, 4.0))
        )

        self.assertRaises(ValueError, Foundation.NSMakeRect, 1.0, 2.0, 3.0, '4')

    def test_NSDivideRect(self):
        rect1 = Foundation.NSMakeRect(1.0, 2.0, 3.0, 4.0)

        slice, rem = Foundation.NSDivideRect(rect1, None, None, 0.5, Foundation.NSMinXEdge)
        self.assertEqual(slice, ((1.0, 2.0), (0.5, 4.0)))
        self.assertEqual(rem,   ((1.5, 2.0), (2.5, 4.0)))

        slice, rem = Foundation.NSDivideRect(rect1, None, None, 0.5, Foundation.NSMinYEdge)
        self.assertEqual(slice, ((1.0, 2.0), (3.0, 0.5)))
        self.assertEqual(rem,   ((1.0, 2.5), (3.0, 3.5)))

    def testMisc(self):
        self.assert_(hasattr(Foundation, 'NSLogPageSize'))
        self.assert_(hasattr(Foundation, 'NSRangeFromString'))
        self.assert_(hasattr(Foundation, 'NSTemporaryDirectory'))
        self.assert_(hasattr(Foundation, 'NSDecrementExtraRefCountWasZero'))

class GlobalVariablesTest (TestCase):
    def testMisc(self):
        # enum
        self.assert_(hasattr(Foundation, 'NS_LittleEndian'))

        # NSString
        self.assert_(hasattr(Foundation, 'NSConnectionReplyMode'))

        # VAR
        if sys.platform == 'darwin':
            self.assert_(hasattr(Foundation, 'NSFoundationVersionNumber'))

class NSLogTest (TestCase):
    def startCaptureStderr(self):
        self.realStderr = os.dup(2)
        self.capturedStderr = open("/tmp/stderr.$$", "wb")
        os.dup2(self.capturedStderr.fileno(), 2)

    def stopCaptureStderr(self):
        os.dup2(self.realStderr, 2)
        self.capturedStderr.close()
        data = open("/tmp/stderr.$$", "rb").read()
        return data

    def testLogging(self):
        self.startCaptureStderr()
        try:
            Foundation.NSLog("This is a test")
        finally:

            data = self.stopCaptureStderr()
            self.assert_(b"This is a test" in data)

    def testLoggingWithFormattingChars(self):
        self.assertRaises(ValueError, Foundation.NSLog, "This is a test %@")

        self.startCaptureStderr()
        try:
            Foundation.NSLog("This is a test%@", ", ronald")
        finally:

            data = self.stopCaptureStderr()
            self.assert_(b"This is a test, ronald" in data, data)

    def testSpotlight(self):
        if hasattr(Foundation, 'NSMetadataQuery'):
            self.assert_(hasattr(Foundation, 'NSMetadataQueryDidFinishGatheringNotification'))
            self.assert_(isinstance(Foundation.NSMetadataQueryDidFinishGatheringNotification, unicode))


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.