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