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/>.
23 " Unit tests for the brief python parser "
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
37 if not os
.path
.exists( name2
):
38 print >> sys
.stderr
, "Cannot open " + name2
43 content1
= file1
.read()
44 content2
= file2
.read()
47 return content1
.strip() == content2
.strip()
50 class CDMBriefParserTest( unittest
.TestCase
):
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 )
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
)
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() )
80 okFileName
= pythonFile
.replace( ".py", ".ok" )
81 self
.failUnless( files_equal( outFileName
, okFileName
),
85 def test_empty( self
):
87 self
.meat( self
.dir + "empty.py",
88 "empty file test failed" )
91 def test_import( self
):
93 self
.meat( self
.dir + "import.py",
94 "import test failed" )
97 def test_coding1( self
):
99 self
.meat( self
.dir + "coding1.py",
100 "error retrieving coding" )
103 def test_coding2( self
):
105 self
.meat( self
.dir + "coding2.py",
106 "error retrieving coding" )
109 def test_coding3( self
):
111 self
.meat( self
.dir + "coding3.py",
112 "error retrieving coding" )
115 def test_function_definitions( self
):
116 " Test function definitions"
117 self
.meat( self
.dir + "func_defs.py",
118 "function definition test failed" )
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" )
127 def test_globals( self
):
128 " Test global variables "
129 self
.meat( self
.dir + "globals.py",
130 "global variables test failed" )
133 def test_class_definitions( self
):
134 " Test class definitions "
135 self
.meat( self
.dir + "class_defs.py",
136 "class definitions test failed" )
139 def test_nested_classes( self
):
140 " Test nested classes "
141 self
.meat( self
.dir + "nested_classes.py",
142 "nested classes test failed" )
145 def test_docstrings( self
):
147 self
.meat( self
.dir + "docstring.py",
148 "docstring test failed" )
151 def test_docstrings2( self
):
152 " Test docstrings 2 "
153 self
.meat( self
.dir + "docstring2.py",
154 "docstring test failed (the only file content)" )
157 def test_docstrings3( self
):
158 " Test docstrings 3 "
159 self
.meat( self
.dir + "docstring3.py",
160 "docstring test failed (many literal parts)" )
163 def test_decorators( self
):
165 self
.meat( self
.dir + "decorators.py",
166 "decorators test failed" )
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" )
175 def test_class_members( self
):
176 " Test class members "
177 self
.meat( self
.dir + "class_members.py",
178 "class members test failed" )
181 def test_errors( self
):
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
)
195 okFileName
= pythonFile
.replace( ".py", ".ok" )
196 self
.failUnless( files_equal( outFileName
, okFileName
),
197 "errors test failed" )
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
)
214 okFileName
= pythonFile
.replace( ".py", ".ok" )
215 self
.failUnless( files_equal( outFileName
, okFileName
),
216 "wrong indent test failed" )
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
)
235 okFileName
= pythonFile
.replace( ".py", ".ok" )
236 self
.failUnless( files_equal( outFileName
, okFileName
),
237 "wrong stop of the parser test failed" )
240 def test_print( self
):
241 " Test print statements "
242 pythonFile
= self
.dir + "print.py"
243 info
= cdmbriefparser
.getBriefModuleInfoFromFile( pythonFile
)
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
)
252 self
.failUnless( info
.isOK
== True )
255 def test_print_func( self
):
256 " Test print statements "
257 pythonFile
= self
.dir + "print2.py"
258 info
= cdmbriefparser
.getBriefModuleInfoFromFile( pythonFile
)
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
)
267 self
.failUnless( info
.isOK
== True )
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" )
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" )
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
)
307 info
= cdmbriefparser
.getBriefModuleInfoFromMemory( content
)
308 self
.failUnless( info
.isOK
!= True )
313 if __name__
== '__main__':
314 print "Testing parser version: " + cdmbriefparser
.getVersion()