1from PyObjCTools.TestSupport import *
2import sys
3import struct
4
5import Foundation
6import os
7
8class GlobalFunctionTest (TestCase):
9    if sys.platform == 'darwin':
10        def testNSFileTypeForHFSTypeCode(self):
11            self.assertEqual("'rtfx'",
12                    Foundation.NSFileTypeForHFSTypeCode(b'rtfx'))
13
14            # The cannonical representation for four-character-codes in python
15            # is a string of 4 characters, but at least some ObjC API's return
16            # longs (because these methods haven't been wrapped correctly yet).
17            # NSFileTypeForHFSTypeCode therefore also accepts integers.
18            fourchar = struct.unpack('i', b'rtfx')[0]
19            if sys.byteorder == 'little':
20                    self.assertEqual("'xftr'",
21                            Foundation.NSFileTypeForHFSTypeCode(fourchar))
22            else:
23                    self.assertEqual("'rtfx'",
24                            Foundation.NSFileTypeForHFSTypeCode(fourchar))
25
26        def testNSHFSTypeCodeFromFileType(self):
27            self.assertEqual(b"rtfx",
28                    Foundation.NSHFSFTypeCodeFromFileType("'rtfx'"))
29
30
31    def testMakeNSRect(self):
32        self.assert_(hasattr(Foundation, 'NSMakeRect'))
33
34        self.assertEqual(
35                Foundation.NSMakeRect(1.5, 2.5, 3.5, 4.5),
36                ((1.5, 2.5), (3.5, 4.5))
37        )
38        self.assertEqual(
39                Foundation.NSMakeRect(1, 2, 3, 4),
40                ((1.0, 2.0), (3.0, 4.0))
41        )
42
43        self.assertRaises(ValueError, Foundation.NSMakeRect, 1.0, 2.0, 3.0, '4')
44
45    def test_NSDivideRect(self):
46        rect1 = Foundation.NSMakeRect(1.0, 2.0, 3.0, 4.0)
47
48        slice, rem = Foundation.NSDivideRect(rect1, None, None, 0.5, Foundation.NSMinXEdge)
49        self.assertEqual(slice, ((1.0, 2.0), (0.5, 4.0)))
50        self.assertEqual(rem,   ((1.5, 2.0), (2.5, 4.0)))
51
52        slice, rem = Foundation.NSDivideRect(rect1, None, None, 0.5, Foundation.NSMinYEdge)
53        self.assertEqual(slice, ((1.0, 2.0), (3.0, 0.5)))
54        self.assertEqual(rem,   ((1.0, 2.5), (3.0, 3.5)))
55
56    def testMisc(self):
57        self.assert_(hasattr(Foundation, 'NSLogPageSize'))
58        self.assert_(hasattr(Foundation, 'NSRangeFromString'))
59        self.assert_(hasattr(Foundation, 'NSTemporaryDirectory'))
60        self.assert_(hasattr(Foundation, 'NSDecrementExtraRefCountWasZero'))
61
62class GlobalVariablesTest (TestCase):
63    def testMisc(self):
64        # enum
65        self.assert_(hasattr(Foundation, 'NS_LittleEndian'))
66
67        # NSString
68        self.assert_(hasattr(Foundation, 'NSConnectionReplyMode'))
69
70        # VAR
71        if sys.platform == 'darwin':
72            self.assert_(hasattr(Foundation, 'NSFoundationVersionNumber'))
73
74class NSLogTest (TestCase):
75    def startCaptureStderr(self):
76        self.realStderr = os.dup(2)
77        self.capturedStderr = open("/tmp/stderr.$$", "wb")
78        os.dup2(self.capturedStderr.fileno(), 2)
79
80    def stopCaptureStderr(self):
81        os.dup2(self.realStderr, 2)
82        self.capturedStderr.close()
83        data = open("/tmp/stderr.$$", "rb").read()
84        return data
85
86    def testLogging(self):
87        self.startCaptureStderr()
88        try:
89            Foundation.NSLog("This is a test")
90        finally:
91
92            data = self.stopCaptureStderr()
93            self.assert_(b"This is a test" in data)
94
95    def testLoggingWithFormattingChars(self):
96        self.assertRaises(ValueError, Foundation.NSLog, "This is a test %@")
97
98        self.startCaptureStderr()
99        try:
100            Foundation.NSLog("This is a test%@", ", ronald")
101        finally:
102
103            data = self.stopCaptureStderr()
104            self.assert_(b"This is a test, ronald" in data, data)
105
106    def testSpotlight(self):
107        if hasattr(Foundation, 'NSMetadataQuery'):
108            self.assert_(hasattr(Foundation, 'NSMetadataQueryDidFinishGatheringNotification'))
109            self.assert_(isinstance(Foundation.NSMetadataQueryDidFinishGatheringNotification, unicode))
110
111
112if __name__ == "__main__":
113    main()
114