1import Foundation, objc
2import AppKit
3from Foundation import NSLog, NSAutoreleasePool, NSObject
4from threading import Thread
5import os
6from PyObjCTools.TestSupport import *
7
8class TestRegr (TestCase):
9    def testFSRepr(self):
10        fm = Foundation.NSFileManager.defaultManager()
11        self.assertRaises(TypeError, fm.stringWithFileSystemRepresentation_length_, "/var")
12        self.assertEquals(u"/var", fm.stringWithFileSystemRepresentation_length_("/var/boo", 4))
13
14    def testThreadHang(self):
15
16        # Temporarily redirect stderr to a file, this allows us to check
17        # that NSLog actually wrote some text.
18        fp = os.open('/tmp/pyobjc-thread.txt', os.O_RDWR|os.O_CREAT, 0666)
19        dupped = os.dup(2)
20        os.dup2(fp, 2)
21
22        try:
23
24            class ThreadHangObject(NSObject):
25                def init(self):
26                    self.t = MyThread()
27                    self.t.start()
28                    return self
29
30            aList = []
31            class MyThread(Thread):
32                def run(self):
33                    pool = NSAutoreleasePool.alloc().init()
34                    aList.append("before")
35                    NSLog(u"does this print?")
36                    aList.append("after")
37
38            o = ThreadHangObject.alloc().init()
39            o.t.join()
40
41
42        finally:
43            os.close(fp)
44            os.dup2(dupped, 2)
45
46        data = open('/tmp/pyobjc-thread.txt', 'r').read()
47        self.assert_('does this print?' in data)
48
49        self.assertEquals(aList, ["before", "after"])
50
51    def testMemoryInit(self):
52        """
53        Regression test for bug #814683, that didn't initialize the memory
54        space for output parameters correctly.
55        """
56        if not hasattr(Foundation, 'NSPropertyListSerialization'): return
57
58        plist = 0
59
60        r = Foundation.NSPropertyListSerialization.dataFromPropertyList_format_errorDescription_(plist, Foundation.NSPropertyListXMLFormat_v1_0, None)
61        self.assertEquals(r[1], None)
62        r = Foundation.NSPropertyListSerialization.dataFromPropertyList_format_errorDescription_(plist, Foundation.NSPropertyListXMLFormat_v1_0, None)
63        self.assertEquals(r[1], None)
64
65    def testTypeOverrideProblem(self):
66        """
67        A bug in the medatata machinery caused a crash.
68        """
69        cls = AppKit.NSOpenGLPixelFormat
70        dir (cls)
71
72        o = cls.alloc().initWithAttributes_(
73                (AppKit.NSOpenGLPFAAccelerated,
74                AppKit.NSOpenGLPFANoRecovery, AppKit.NSOpenGLPFAColorSize, 32))
75
76if __name__ == "__main__":
77    main()
78