More unit tests
[codimension.git] / pythonparser-exp / unit_test.py
blob8524f229f3b50e630a184088fca6311ce079d8ae
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
4 # codimension - graphics python two-way code editor and analyzer
5 # Copyright (C) 2010 Sergey Satskiy <sergey.satskiy@gmail.com>
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 # $Id$
23 " Unit tests for the brief python parser "
25 import cdmbriefparser
26 import unittest
27 import os.path
28 import sys
30 def files_equal( name1, name2 ):
31 " Compares two files. Returns True if their content matches "
33 if not os.path.exists( name1 ):
34 print >> sys.stderr, "Cannot open " + name1
35 return False
37 if not os.path.exists( name2 ):
38 print >> sys.stderr, "Cannot open " + name2
39 return False
41 file1 = open( name1 )
42 file2 = open( name2 )
43 content1 = file1.read()
44 content2 = file2.read()
45 file1.close()
46 file2.close()
47 return content1.strip() == content2.strip()
50 class CDMBriefParserTest( unittest.TestCase ):
51 " Unit test class "
53 def setUp( self ):
54 " Initialisation "
56 self.dir = "unittest" + os.path.sep
57 if not os.path.isdir( self.dir ):
58 raise Exception( "Cannot find directory with tests. " \
59 "Expected here: " + self.dir )
60 return
62 def meat( self, pythonFile, errorMsg ):
63 " The test process meat "
65 info = cdmbriefparser.getBriefModuleInfoFromFile( pythonFile )
66 self.failUnless( info.isOK == True )
68 f = open( pythonFile )
69 content = f.read()
70 f.close()
72 info = cdmbriefparser.getBriefModuleInfoFromMemory( content )
73 self.failUnless( info.isOK == True )
75 outFileName = pythonFile.replace( ".py", ".out" )
76 outFile = open( outFileName, "w" )
77 outFile.write( info.niceStringify() )
78 outFile.close()
80 okFileName = pythonFile.replace( ".py", ".ok" )
81 self.failUnless( files_equal( outFileName, okFileName ),
82 errorMsg )
83 return
85 def test_empty( self ):
86 " Test empty file "
87 self.meat( self.dir + "empty.py",
88 "empty file test failed" )
89 return
91 def test_import( self ):
92 " Tests imports "
93 self.meat( self.dir + "import.py",
94 "import test failed" )
95 return
97 def test_coding1( self ):
98 " Test coding 1"
99 self.meat( self.dir + "coding1.py",
100 "error retrieving coding" )
101 return
103 def test_coding2( self ):
104 " Test coding 2"
105 self.meat( self.dir + "coding2.py",
106 "error retrieving coding" )
107 return
109 def test_coding3( self ):
110 " Test coding 3"
111 self.meat( self.dir + "coding3.py",
112 "error retrieving coding" )
113 return
115 def test_function_definitions( self ):
116 " Test function definitions"
117 self.meat( self.dir + "func_defs.py",
118 "function definition test failed" )
119 return
121 def test_nested_func_definitions( self ):
122 " Test nested functions definitions "
123 self.meat( self.dir + "nested_funcs.py",
124 "nested functions definitions test failed" )
125 return
127 def test_globals( self ):
128 " Test global variables "
129 self.meat( self.dir + "globals.py",
130 "global variables test failed" )
131 return
133 def test_class_definitions( self ):
134 " Test class definitions "
135 self.meat( self.dir + "class_defs.py",
136 "class definitions test failed" )
137 return
139 def test_nested_classes( self ):
140 " Test nested classes "
141 self.meat( self.dir + "nested_classes.py",
142 "nested classes test failed" )
143 return
145 def test_docstrings( self ):
146 " Test docstrings "
147 self.meat( self.dir + "docstring.py",
148 "docstring test failed" )
149 return
151 def test_docstrings2( self ):
152 " Test docstrings 2 "
153 self.meat( self.dir + "docstring2.py",
154 "docstring test failed (the only file content)" )
155 return
157 def test_docstrings3( self ):
158 " Test docstrings 3 "
159 self.meat( self.dir + "docstring3.py",
160 "docstring test failed (many literal parts)" )
161 return
163 def test_decorators( self ):
164 " Test decorators "
165 self.meat( self.dir + "decorators.py",
166 "decorators test failed" )
167 return
169 def test_static_members( self ):
170 " Test class static members "
171 self.meat( self.dir + "static_members.py",
172 "class static members test failed" )
173 return
175 def test_class_members( self ):
176 " Test class members "
177 self.meat( self.dir + "class_members.py",
178 "class members test failed" )
179 return
181 def test_errors( self ):
182 " Test errors "
184 pythonFile = self.dir + "errors.py"
185 info = cdmbriefparser.getBriefModuleInfoFromFile( pythonFile )
186 self.failUnless( info.isOK != True )
188 outFileName = pythonFile.replace( ".py", ".out" )
189 outFile = open( outFileName, "w" )
190 outFile.write( info.niceStringify() )
191 for item in info.errors:
192 outFile.write( "\n" + item )
193 outFile.close()
195 okFileName = pythonFile.replace( ".py", ".ok" )
196 self.failUnless( files_equal( outFileName, okFileName ),
197 "errors test failed" )
198 return
200 def test_wrong_indent( self ):
201 " Test wrong indent "
203 pythonFile = self.dir + "wrong_indent.py"
204 info = cdmbriefparser.getBriefModuleInfoFromFile( pythonFile )
205 self.failUnless( info.isOK != True )
207 outFileName = pythonFile.replace( ".py", ".out" )
208 outFile = open( outFileName, "w" )
209 outFile.write( info.niceStringify() )
210 for item in info.errors:
211 outFile.write( "\n" + item )
212 outFile.close()
214 okFileName = pythonFile.replace( ".py", ".ok" )
215 self.failUnless( files_equal( outFileName, okFileName ),
216 "wrong indent test failed" )
217 return
219 def test_wrong_stop( self ):
220 " Test wrong stop of the parser "
222 pythonFile = self.dir + "wrong_stop.py"
223 info = cdmbriefparser.getBriefModuleInfoFromFile( pythonFile )
224 self.failUnless( info.isOK != True )
226 outFileName = pythonFile.replace( ".py", ".out" )
227 outFile = open( outFileName, "w" )
228 outFile.write( info.niceStringify() )
229 for item in info.errors:
230 outFile.write( "\n" + item )
231 for item in info.lexerErrors:
232 outFile.write( "\n" + item )
233 outFile.close()
235 okFileName = pythonFile.replace( ".py", ".ok" )
236 self.failUnless( files_equal( outFileName, okFileName ),
237 "wrong stop of the parser test failed" )
238 return
240 def test_print( self ):
241 " Test print statements "
242 pythonFile = self.dir + "print.py"
243 info = cdmbriefparser.getBriefModuleInfoFromFile( pythonFile )
244 if not info.isOK:
245 outFileName = pythonFile.replace( ".py", ".out" )
246 outFile = open( outFileName, "w" )
247 for item in info.errors:
248 outFile.write( "\n" + item )
249 for item in info.lexerErrors:
250 outFile.write( "\n" + item )
251 outFile.close()
252 self.failUnless( info.isOK == True )
253 return
255 def test_print_func( self ):
256 " Test print statements "
257 pythonFile = self.dir + "print2.py"
258 info = cdmbriefparser.getBriefModuleInfoFromFile( pythonFile )
259 if not info.isOK:
260 outFileName = pythonFile.replace( ".py", ".out" )
261 outFile = open( outFileName, "w" )
262 for item in info.errors:
263 outFile.write( "\n" + item )
264 for item in info.lexerErrors:
265 outFile.write( "\n" + item )
266 outFile.close()
267 self.failUnless( info.isOK == True )
268 return
270 def test_one_comment( self ):
271 " Test for a file which consists of a single comment line "
273 self.meat( self.dir + "one_comment.py",
274 "one comment line test failed" )
275 return
277 def test_comments_only( self ):
278 " Test for a file with no other lines except of comments "
280 self.meat( self.dir + "commentsonly.py",
281 "comments only with no other empty lines test failed" )
283 def test_noendofline( self ):
284 " Test for a file which has no EOL at the end "
286 self.meat( self.dir + "noendofline.py",
287 "No end of line at the end of the file test failed" )
289 def test_empty_brackets( self ):
290 " Test for empty brackets "
292 self.meat( self.dir + "empty_brackets.py",
293 "empty brackets test failed" )
294 return
296 def test_lone_import( self ):
297 " Test for lone import keyword "
299 pythonFile = self.dir + "loneimport.py"
300 info = cdmbriefparser.getBriefModuleInfoFromFile( pythonFile )
301 self.failUnless( info.isOK != True )
303 f = open( pythonFile )
304 content = f.read()
305 f.close()
307 info = cdmbriefparser.getBriefModuleInfoFromMemory( content )
308 self.failUnless( info.isOK != True )
309 return
312 # Run the unit tests
313 if __name__ == '__main__':
314 print "Testing parser version: " + cdmbriefparser.getVersion()
315 unittest.main()