summaryrefslogtreecommitdiff
path: root/python/helpers/pydev/tests/test_simpleTipper.py
blob: f759ad60f678051a953e5a28c9fbd4610258943b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
'''
@author Fabio Zadrozny 
'''
import os
import sys
#make it as if we were executing from the directory above this one (so that we can use pycompletionserver
#without the need for it being in the pythonpath)
#twice the dirname to get the previous level from this file.
sys.path.insert(1, os.path.split(os.path.split(__file__)[0])[0])

try:
    import __builtin__ #@UnusedImport
    BUILTIN_MOD = '__builtin__'
except ImportError:
    BUILTIN_MOD = 'builtins'


if sys.platform.find('java') == -1:
    
    HAS_WX = False
    
    import unittest
    import _pydev_imports_tipper
    import inspect
    
    class Test(unittest.TestCase):
    
        def p(self, t):
            for a in t:
                sys.stdout.write('%s\n' % (a,))
     
        def testImports3(self):
            tip = _pydev_imports_tipper.GenerateTip('os')
            ret = self.assertIn('path', tip)
            self.assertEquals('', ret[2])
    
        def testImports2(self):
            try:
                tip = _pydev_imports_tipper.GenerateTip('OpenGL.GLUT')
                self.assertIn('glutDisplayFunc', tip)
                self.assertIn('glutInitDisplayMode', tip)
            except ImportError:
                pass
    
        def testImports4(self):
            try:
                tip = _pydev_imports_tipper.GenerateTip('mx.DateTime.mxDateTime.mxDateTime')
                self.assertIn('now', tip)
            except ImportError:
                pass
    
        def testImports5(self):
            tip = _pydev_imports_tipper.GenerateTip('__builtin__.list')
            s = self.assertIn('sort', tip)
            self.CheckArgs(
                s, 
                '(cmp=None, key=None, reverse=False)', 
                '(self, object cmp, object key, bool reverse)',
                '(self, cmp: object, key: object, reverse: bool)'
            )
            
        def testImports2a(self):
            tips = _pydev_imports_tipper.GenerateTip('%s.RuntimeError' % BUILTIN_MOD)
            self.assertIn('__doc__', tips)
            
        def testImports2b(self):
            tips = _pydev_imports_tipper.GenerateTip('%s' % BUILTIN_MOD)
            t = self.assertIn('file' , tips)
            self.assert_('->' in t[1].strip() or 'file' in t[1])
            
        def testImports2c(self):
            tips = _pydev_imports_tipper.GenerateTip('%s.file' % BUILTIN_MOD)
            t = self.assertIn('readlines' , tips)
            self.assert_('->' in t[1] or 'sizehint' in t[1])
            
        def testImports(self):
            '''
            You can print_ the results to check...
            '''
            if HAS_WX:
                tip = _pydev_imports_tipper.GenerateTip('wxPython.wx')
                self.assertIn('wxApp'        , tip)
                
                tip = _pydev_imports_tipper.GenerateTip('wxPython.wx.wxApp')
                
                try:
                    tip = _pydev_imports_tipper.GenerateTip('qt')
                    self.assertIn('QWidget'        , tip)
                    self.assertIn('QDialog'        , tip)
                    
                    tip = _pydev_imports_tipper.GenerateTip('qt.QWidget')
                    self.assertIn('rect'           , tip)
                    self.assertIn('rect'           , tip)
                    self.assertIn('AltButton'      , tip)
            
                    tip = _pydev_imports_tipper.GenerateTip('qt.QWidget.AltButton')
                    self.assertIn('__xor__'      , tip)
            
                    tip = _pydev_imports_tipper.GenerateTip('qt.QWidget.AltButton.__xor__')
                    self.assertIn('__class__'      , tip)
                except ImportError:
                    pass
                
            tip = _pydev_imports_tipper.GenerateTip(BUILTIN_MOD)
    #        for t in tip[1]:
    #            print_ t
            self.assertIn('object'         , tip)
            self.assertIn('tuple'          , tip)
            self.assertIn('list'          , tip)
            self.assertIn('RuntimeError'   , tip)
            self.assertIn('RuntimeWarning' , tip)
            
            t = self.assertIn('cmp' , tip)
            
            self.CheckArgs(t, '(x, y)', '(object x, object y)', '(x: object, y: object)') #args
            
            t = self.assertIn('isinstance' , tip)
            self.CheckArgs(t, '(object, class_or_type_or_tuple)', '(object o, type typeinfo)', '(o: object, typeinfo: type)') #args
            
            t = self.assertIn('compile' , tip)
            self.CheckArgs(t, '(source, filename, mode)', '()', '(o: object, name: str, val: object)') #args
            
            t = self.assertIn('setattr' , tip)
            self.CheckArgs(t, '(object, name, value)', '(object o, str name, object val)', '(o: object, name: str, val: object)') #args
            
            try:
                import compiler
                compiler_module = 'compiler'
            except ImportError:
                try:
                    import ast
                    compiler_module = 'ast'
                except ImportError:
                    compiler_module = None
                
            if compiler_module is not None: #Not available in iron python
                tip = _pydev_imports_tipper.GenerateTip(compiler_module) 
                if compiler_module == 'compiler':
                    self.assertArgs('parse', '(buf, mode)', tip)
                    self.assertArgs('walk', '(tree, visitor, walker, verbose)', tip)
                    self.assertIn('parseFile'      , tip)
                else:
                    self.assertArgs('parse', '(source, filename, mode)', tip)
                    self.assertArgs('walk', '(node)', tip)
                self.assertIn('parse'          , tip)
            
            
        def CheckArgs(self, t, *expected):
            for x in expected:
                if x == t[2]:
                    return
            self.fail('Found: %s. Expected: %s' % (t[2], expected))
            
            
        def assertArgs(self, tok, args, tips):
            for a in tips[1]:
                if tok == a[0]:
                    self.assertEquals(args, a[2])
                    return
            raise AssertionError('%s not in %s', tok, tips)
    
        def assertIn(self, tok, tips):
            for a in tips[1]:
                if tok == a[0]:
                    return a
            raise AssertionError('%s not in %s' % (tok, tips))
        
        
        def testSearch(self):
            s = _pydev_imports_tipper.Search('inspect.ismodule')
            (f, line, col), foundAs = s
            self.assert_(line > 0)
            
            
        def testDotNetLibraries(self):
            if sys.platform == 'cli':
                tip = _pydev_imports_tipper.GenerateTip('System.Drawing')
                self.assertIn('Brushes' , tip)
                
                tip = _pydev_imports_tipper.GenerateTip('System.Drawing.Brushes')
                self.assertIn('Aqua' , tip)
            
    
        def testInspect(self):
            
            class C(object):
                def metA(self, a, b):
                    pass
            
            obj = C.metA
            if inspect.ismethod (obj):
                pass
    #            print_ obj.im_func
    #            print_ inspect.getargspec(obj.im_func)
                
            
    def suite():
        s = unittest.TestSuite()
        s.addTest(Test("testImports5"))
        unittest.TextTestRunner(verbosity=2).run(s)


if __name__ == '__main__':
    if sys.platform.find('java') == -1:
#        suite()
        unittest.main()
    else:
        sys.stdout.write('Not running python tests in platform: %s\n' % (sys.platform,))