1from PyObjCTools.TestSupport import *
2import objc
3
4
5
6gDict = {}
7
8"""
9try:
10    import Foundation
11except ImportError:
12    pass
13
14try:
15    import AppKit
16except ImportError:
17    pass
18
19try:
20    import PreferencePanes
21except ImportError:
22    pass
23
24try:
25    import ScreenSaver
26except ImportError:
27    pass
28
29try:
30    import InterfaceBuilder
31except ImportError:
32    pass
33
34try:
35    import WebKit
36except ImportError:
37    pass
38"""
39
40import sys
41
42
43class SplitSignatureTest (TestCase):
44
45    def testSplitSignature(self):
46    # This is a very expensive test, with 1 goal: Verify that all method
47    # signatures, and therefore all signatures changed by PyObjC, are
48    # valid.
49        for cls in objc.getClassList():
50            for selName in cls.__dict__.keys():
51                try:
52                    sel = getattr(cls, selName)
53                except AttributeError:
54                    continue
55
56                if not isinstance(sel, objc.selector): continue
57                elems = objc.splitSignature(sel.signature)
58
59
60    def testSimple(self):
61        self.assertEquals(objc.splitSignature("@:@"), ('@',':','@'))
62        self.assertEquals(objc.splitSignature("@:10{NSRect=ff}"), ('@',':','{NSRect=ff}'))
63        self.assertEquals(objc.splitSignature("@:o^@"), ('@',':','o^@'))
64
65        # struct definition in an struct objc_ivar
66        self.assertEquals(objc.splitSignature('{_NSRect="origin"{_NSPoint="x"f"y"f}"size"{_NSSize="width"f"height"f}}'), ('{_NSRect="origin"{_NSPoint="x"f"y"f}"size"{_NSSize="width"f"height"f}}',))
67
68    def testSignatureCount(self):
69        EXCEPTIONS=[
70
71            # For some reason this signature doesn't seem to be correct, even
72            # though we don't touch it...
73            "initWithDocument_URL_windowProperties_locationProperties_interpreterBuiltins_",
74
75            # Some unittests...
76            "setKey4",
77            "get_key2",
78            "read_bar",
79            "setFoo_",
80            "method_with_embedded_underscores",
81            "methodWithArg_",
82            "myMethod",
83            "twoargs",
84            "set_helper",
85
86            # dictionary methods
87            'get',
88        ]
89
90        for cls in objc.getClassList():
91            if cls.__name__.startswith('OC_'): continue
92            for selName in cls.__dict__.keys():
93                if selName in EXCEPTIONS: continue
94                if selName.startswith('__') and selName.endswith('__'): continue
95
96                try:
97                    sel = getattr(cls, selName)
98                except AttributeError:
99                    continue
100
101                if not isinstance(sel, objc.selector): continue
102                elems = objc.splitSignature(sel.signature)
103
104                argcount = len(elems) - 3 # retval, self, _sel
105                coloncount = sel.selector.count(':')
106
107                self.assertEquals(argcount, coloncount,
108                        '%s [%d:%d] %r %r'%(sel.selector, argcount, coloncount, elems, cls))
109
110
111if __name__ == "__main__":
112    main()
113