orestis / pysmell

PySmell is an attempt to create an IDE completion helper for python.

This URL has Read+Write access

pysmell / functional_test.py
100644 202 lines (166 sloc) 8.211 kb
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
import unittest
from textwrap import dedent
import subprocess
import os
 
import pysmelltags
 
class FunctionalTest(unittest.TestCase):
    def setUp(self):
        self.packageA = {
            'CONSTANTS': [
                'PackageA.SneakyConstant',
                'PackageA.ModuleA.CONSTANT',
                'PackageA.NestedPackage.EvenMore.ModuleC.NESTED',
            ],
            'FUNCTIONS': [
                ('PackageA.SneakyFunction', [], ""),
                ('PackageA.ModuleA.TopLevelFunction', ['arg1', 'arg2'], ""),
            ],
            'CLASSES': {
                'PackageA.ModuleA.ClassA': {
                    'bases': ['object'],
                    'docstring': '',
                    'constructor': [],
                    'properties': ['classPropertyA', 'classPropertyB', 'propertyA', 'propertyB', 'propertyC', 'propertyD'],
                    'methods': [('methodA', ['argA', 'argB', '*args', '**kwargs'], '')]
                },
                'PackageA.ModuleA.ChildClassA': {
                    'bases': ['PackageA.ModuleA.ClassA', 'object'],
                    'docstring': 'a class docstring, imagine that',
                    'constructor': ['conArg'],
                    'properties': ['extraProperty'],
                    'methods': [('extraMethod', [], 'i have a docstring')],
                },
                'PackageA.SneakyClass': {
                    'bases': [],
                    'docstring': '',
                    'constructor': [],
                    'properties': [],
                    'methods': []
                },
            },
            'POINTERS': {
                'PackageA.NESTED': 'PackageA.NestedPackage.EvenMore.ModuleC.NESTED',
                'PackageA.MC': 'PackageA.NestedPackage.EvenMore.ModuleC',
            
            },
        }
        
        self.packageB = {
            'CONSTANTS': ['PackageB.SneakyConstant'],
            'FUNCTIONS': [('PackageB.SneakyFunction', [], "")],
            'CLASSES':{
                'PackageB.SneakyClass': {
                    'bases': [],
                    'docstring': '',
                    'constructor': [],
                    'properties': [],
                    'methods': []
                }
            },
            'POINTERS': {}
        }
 
    def assertDictsEqual(self, actualDict, expectedDict):
        self.assertEquals(len(actualDict.keys()), len(expectedDict.keys()),
            "dicts don't have equal number of keys: %r != %r" % (actualDict.keys(), expectedDict.keys()))
        self.assertEquals(set(actualDict.keys()), set(expectedDict.keys()), "dicts don't have equal keys")
        for key, value in actualDict.items():
            if isinstance(value, dict):
                self.assertTrue(isinstance(expectedDict[key], dict), "incompatible types found for key %s" % key)
                self.assertDictsEqual(value, expectedDict[key])
            elif isinstance(value, list):
                self.assertTrue(isinstance(expectedDict[key], list), "incompatible types found for key %s" % key)
                self.assertEquals(sorted(value), sorted(expectedDict[key]), 'wrong sorted(list) for key %s:\n%r != %r' % (key, value, expectedDict[key]))
            else:
                self.assertEquals(value, expectedDict[key], "wrong value for key %s" % key)
 
 
    def testMultiPackage(self):
        if os.path.exists('Tests/PYSMELLTAGS'):
            os.remove('Tests/PYSMELLTAGS')
        subprocess.call(["python", "../pysmelltags.py", "PackageA", "PackageB"], cwd='Tests')
        self.assertTrue(os.path.exists('Tests/PYSMELLTAGS'))
        PYSMELLDICT = eval(open('Tests/PYSMELLTAGS').read())
        expectedDict = {}
        expectedDict.update(self.packageA)
        expectedDict['CLASSES'].update(self.packageB['CLASSES'])
        expectedDict['CONSTANTS'].extend(self.packageB['CONSTANTS'])
        expectedDict['FUNCTIONS'].extend(self.packageB['FUNCTIONS'])
        self.assertDictsEqual(PYSMELLDICT, expectedDict)
 
 
    def testPackageA(self):
        if os.path.exists('Tests/PYSMELLTAGS'):
            os.remove('Tests/PYSMELLTAGS')
        subprocess.call(["python", "../pysmelltags.py", "PackageA"], cwd='Tests')
        self.assertTrue(os.path.exists('Tests/PYSMELLTAGS'))
        PYSMELLDICT = eval(open('Tests/PYSMELLTAGS').read())
        expectedDict = self.packageA
        self.assertDictsEqual(PYSMELLDICT, expectedDict)
 
 
    def testPackageB(self):
        if os.path.exists('Tests/PYSMELLTAGS'):
            os.remove('Tests/PYSMELLTAGS')
        subprocess.call(["python", "../pysmelltags.py", "PackageB"], cwd='Tests')
        self.assertTrue(os.path.exists('Tests/PYSMELLTAGS'))
        PYSMELLDICT = eval(open('Tests/PYSMELLTAGS').read())
        expectedDict = self.packageB
        self.assertDictsEqual(PYSMELLDICT, expectedDict)
 
 
    def testPackageDot(self):
        if os.path.exists('Tests/PackageA/PYSMELLTAGS'):
            os.remove('Tests/PackageA/PYSMELLTAGS')
        subprocess.call(["python", "../../pysmelltags.py", "."], cwd='Tests/PackageA')
        self.assertTrue(os.path.exists('Tests/PackageA/PYSMELLTAGS'))
        PYSMELLDICT = eval(open('Tests/PackageA/PYSMELLTAGS').read())
        expectedDict = self.packageA
        self.assertDictsEqual(PYSMELLDICT, expectedDict)
 
        self.fail("when the current dir is not a package, search for packages")
 
    
    def testSingleFile(self):
        "should recurse up until it doesn't find __init__.py"
        path = 'Tests/PackageA/NestedPackage/EvenMore/'
        if os.path.exists('%sPYSMELLTAGS' % path):
            os.remove('%sPYSMELLTAGS' % path)
        subprocess.call(["python", "../../../../pysmelltags.py", "ModuleC.py"], cwd=path)
        self.assertTrue(os.path.exists('%sPYSMELLTAGS' % path ))
        PYSMELLDICT = eval(open('%sPYSMELLTAGS' % path).read())
        expectedDict = {
            'FUNCTIONS': [],
            'CONSTANTS': ['PackageA.NestedPackage.EvenMore.ModuleC.NESTED'],
            'CLASSES': {},
            'POINTERS': {},
                        
        }
        self.assertDictsEqual(PYSMELLDICT, expectedDict)
 
 
    def testOutputRedirect(self):
        if os.path.exists('Tests/OUTPUTREDIR'):
            os.remove('Tests/OUTPUTREDIR')
        subprocess.call(["python", "../pysmelltags.py", "PackageA", "-o",
            "OUTPUTREDIR"], cwd='Tests')
        self.assertTrue(os.path.exists('Tests/OUTPUTREDIR'))
        PYSMELLDICT = eval(open('Tests/OUTPUTREDIR').read())
        expectedDict = self.packageA
        self.assertDictsEqual(PYSMELLDICT, expectedDict)
 
        absPath = os.path.join(os.getcwd(), 'Tests', 'OUTPUTREDIR2')
        if os.path.exists(absPath):
            os.remove(absPath)
        subprocess.call(["python", "../pysmelltags.py", "PackageA", "-o", absPath], cwd='Tests')
        self.assertTrue(os.path.exists(absPath))
        PYSMELLDICT = eval(open(absPath).read())
        expectedDict = self.packageA
        self.assertDictsEqual(PYSMELLDICT, expectedDict)
 
 
    def testNoArgs(self):
        proc = subprocess.Popen(["python", "pysmelltags.py"], stdout=subprocess.PIPE)
        proc.wait()
        stdout = proc.stdout.read()
        expected = dedent("""\
PySmell v0.4
 
usage: python pysmelltags.py package [package, ...] [-x excluded, ...] [options]
 
Generate a PYSMELLTAGS file with information about the Python code contained
in the specified packages (recursively). This file is then used to
provide autocompletion for various IDEs and editors that support it.
 
Options:
 
-x args Will not analyze files in directories that match the argument.
Useful for excluding tests or version control directories.
 
-o FILE Will redirect the output to FILE instead of PYSMELLTAGS
 
-t Will print timing information.
 
""").splitlines()
        self.assertEquals(stdout.splitlines(), expected)
 
 
    def testCompleteModuleMembers(self):
        self.fail("""
from django.db import models
 
models.
 
should return all top-level members of django.db.models
""")
 
 
if __name__ == '__main__':
    unittest.main()