Back to index

enigmail  1.4.3
unit-Preprocessor.py
Go to the documentation of this file.
00001 import unittest
00002 
00003 from StringIO import StringIO
00004 import os
00005 import sys
00006 import os.path
00007 sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
00008 
00009 from Preprocessor import Preprocessor
00010 
00011 class NamedIO(StringIO):
00012   def __init__(self, name, content):
00013     self.name = name
00014     StringIO.__init__(self, content)
00015 
00016 class TestPreprocessor(unittest.TestCase):
00017   """
00018   Unit tests for the Context class
00019   """
00020 
00021   def setUp(self):
00022     self.pp = Preprocessor()
00023     self.pp.out = StringIO()
00024 
00025   def test_conditional_if_0(self):
00026     f = NamedIO("conditional_if_0.in", """#if 0
00027 FAIL
00028 #else
00029 PASS
00030 #endif
00031 """)
00032     self.pp.do_include(f)
00033     self.assertEqual(self.pp.out.getvalue(), "PASS\n")
00034 
00035   def test_string_value(self):
00036     f = NamedIO("string_value.in", """#define FOO STRING
00037 #if FOO
00038 string value is true
00039 #else
00040 string value is false
00041 #endif
00042 """)
00043     self.pp.do_include(f)
00044     self.assertEqual(self.pp.out.getvalue(), "string value is false\n")
00045   
00046   def test_number_value(self):
00047     f = NamedIO("string_value.in", """#define FOO 1
00048 #if FOO
00049 number value is true
00050 #else
00051 number value is false
00052 #endif
00053 """)
00054     self.pp.do_include(f)
00055     self.assertEqual(self.pp.out.getvalue(), "number value is true\n")
00056   
00057   def test_conditional_if_0_elif_1(self):
00058     f = NamedIO('conditional_if_0_elif_1.in', '''#if 0
00059 #elif 1
00060 PASS
00061 #else
00062 FAIL
00063 #endif
00064 ''')
00065     self.pp.do_include(f)
00066     self.assertEqual(self.pp.out.getvalue(), "PASS\n")
00067   
00068   def test_conditional_if_1(self):
00069     f = NamedIO('conditional_if_1.in', '''#if 1
00070 PASS
00071 #else
00072 FAILE
00073 #endif
00074 ''')
00075     self.pp.do_include(f)
00076     self.assertEqual(self.pp.out.getvalue(), "PASS\n")
00077   
00078   def test_conditional_if_1_elif_1_else(self):
00079     f = NamedIO('conditional_if_1_elif_1_else.in', '''#if 1
00080 PASS
00081 #elif 1
00082 FAIL
00083 #else
00084 FAIL
00085 #endif
00086 ''')
00087     self.pp.do_include(f)
00088     self.assertEqual(self.pp.out.getvalue(), "PASS\n")
00089   
00090   def test_conditional_if_1_if_1(self):
00091     f = NamedIO('conditional_if_1_if_1.in', '''#if 1
00092 #if 1
00093 PASS
00094 #else
00095 FAIL
00096 #endif
00097 #else
00098 FAIL
00099 #endif
00100 ''')
00101     self.pp.do_include(f)
00102     self.assertEqual(self.pp.out.getvalue(), "PASS\n")
00103   
00104   def test_conditional_not_0(self):
00105     f = NamedIO('conditional_not_0.in', '''#if !0
00106 PASS
00107 #else
00108 FAIL
00109 #endif
00110 ''')
00111     self.pp.do_include(f)
00112     self.assertEqual(self.pp.out.getvalue(), "PASS\n")
00113   
00114   def test_conditional_not_1(self):
00115     f = NamedIO('conditional_not_1.in', '''#if !1
00116 FAIL
00117 #else
00118 PASS
00119 #endif
00120 ''')
00121     self.pp.do_include(f)
00122     self.assertEqual(self.pp.out.getvalue(), "PASS\n")
00123   
00124   def test_conditional_not_emptyval(self):
00125     f = NamedIO('conditional_not_emptyval.in', '''#define EMPTYVAL
00126 #if !EMPTYVAL
00127 FAIL
00128 #else
00129 PASS
00130 #endif
00131 #if EMPTYVAL
00132 PASS
00133 #else
00134 FAIL
00135 #endif
00136 ''')
00137     self.pp.do_include(f)
00138     self.assertEqual(self.pp.out.getvalue(), "PASS\nPASS\n")
00139   
00140   def test_conditional_not_nullval(self):
00141     f = NamedIO('conditional_not_nullval.in', '''#define NULLVAL 0
00142 #if !NULLVAL
00143 PASS
00144 #else
00145 FAIL
00146 #endif
00147 ''')
00148     self.pp.do_include(f)
00149     self.assertEqual(self.pp.out.getvalue(), "PASS\n")
00150   
00151   def test_expand(self):
00152     f = NamedIO('expand.in', '''#define ASVAR AS
00153 #expand P__ASVAR__S
00154 ''')
00155     self.pp.do_include(f)
00156     self.assertEqual(self.pp.out.getvalue(), "PASS\n")
00157 
00158   def test_undef_defined(self):
00159     f = NamedIO('undef_defined.in', '''#define BAR
00160 #undef BAR
00161 BAR
00162 ''')
00163     self.pp.do_include(f)
00164     self.assertEqual(self.pp.out.getvalue(), "BAR\n")
00165 
00166   def test_undef_undefined(self):
00167     f = NamedIO('undef_undefined.in', '''#undef VAR
00168 ''')
00169     self.pp.do_include(f)
00170     self.assertEqual(self.pp.out.getvalue(), "")
00171   
00172   def test_filter_attemptSubstitution(self):
00173     f = NamedIO('filter_attemptSubstitution.in', '''#filter attemptSubstitution
00174 P@VAR@ASS
00175 #unfilter attemptSubstitution
00176 ''')
00177     self.pp.do_include(f)
00178     self.assertEqual(self.pp.out.getvalue(), "PASS\n")
00179   
00180   def test_filter_emptyLines(self):
00181     f = NamedIO('filter_emptyLines.in', '''lines with a
00182 
00183 blank line
00184 #filter emptyLines
00185 lines with
00186 
00187 no blank lines
00188 #unfilter emptyLines
00189 yet more lines with
00190 
00191 blank lines
00192 ''')
00193     self.pp.do_include(f)
00194     self.assertEqual(self.pp.out.getvalue(), '''lines with a
00195 
00196 blank line
00197 lines with
00198 no blank lines
00199 yet more lines with
00200 
00201 blank lines
00202 ''')
00203   
00204   def test_filter_slashslash(self):
00205     f = NamedIO('filter_slashslash.in', '''#filter slashslash
00206 PASS//FAIL  // FAIL
00207 #unfilter slashslash
00208 PASS // PASS
00209 ''')
00210     self.pp.do_include(f)
00211     self.assertEqual(self.pp.out.getvalue(), "PASS\nPASS // PASS\n")
00212   
00213   def test_filter_spaces(self):
00214     f = NamedIO('filter_spaces.in', '''#filter spaces
00215 You should see two nice ascii tables
00216  +-+-+-+
00217  | |   |     |
00218  +-+-+-+
00219 #unfilter spaces
00220 +-+---+
00221 | |   |
00222 +-+---+ 
00223 ''')
00224     self.pp.do_include(f)
00225     self.assertEqual(self.pp.out.getvalue(), """You should see two nice ascii tables
00226 +-+-+-+
00227 | | | |
00228 +-+-+-+
00229 +-+---+
00230 | |   |
00231 +-+---+ 
00232 """)
00233   
00234   def test_filter_substitution(self):
00235     f = NamedIO('filter_substitution.in', '''#define VAR ASS
00236 #filter substitution
00237 P@VAR@
00238 #unfilter substitution
00239 ''')
00240     self.pp.do_include(f)
00241     self.assertEqual(self.pp.out.getvalue(), "PASS\n")
00242 
00243   def test_error(self):
00244     f = NamedIO('error.in', '''#error spit this message out
00245 ''')
00246     caught_msg = None
00247     try:
00248       self.pp.do_include(f)
00249     except Preprocessor.Error, e:
00250       caught_msg = e.args[0][-1]
00251     self.assertEqual(caught_msg, 'spit this message out')
00252   
00253   def test_javascript_line(self):
00254     f = NamedIO('javascript_line.js.in', '''// Line 1
00255 #if 0
00256 // line 3
00257 #endif
00258 // line 5
00259 # comment
00260 // line 7
00261 // line 8
00262 // line 9
00263 # another comment
00264 // line 11
00265 #define LINE 1
00266 // line 13, given line number overwritten with 2
00267 ''')
00268     self.pp.do_include(f)
00269     out = """// Line 1
00270 //@line 5 "CWDjavascript_line.js.in"
00271 // line 5
00272 //@line 7 "CWDjavascript_line.js.in"
00273 // line 7
00274 // line 8
00275 // line 9
00276 //@line 11 "CWDjavascript_line.js.in"
00277 // line 11
00278 //@line 2 "CWDjavascript_line.js.in"
00279 // line 13, given line number overwritten with 2
00280 """
00281     out = out.replace('CWD', os.getcwd() + os.path.sep)
00282     self.assertEqual(self.pp.out.getvalue(), out)
00283   
00284   def test_literal(self):
00285     f = NamedIO('literal.in', '''#literal PASS
00286 ''')
00287     self.pp.do_include(f)
00288     self.assertEqual(self.pp.out.getvalue(), "PASS\n")
00289   
00290   def test_var_directory(self):
00291     f = NamedIO('var_directory.in', '''#ifdef DIRECTORY
00292 PASS
00293 #else
00294 FAIL
00295 #endif
00296 ''')
00297     self.pp.do_include(f)
00298     self.assertEqual(self.pp.out.getvalue(), "PASS\n")
00299   
00300   def test_var_file(self):
00301     f = NamedIO('var_file.in', '''#ifdef FILE
00302 PASS
00303 #else
00304 FAIL
00305 #endif
00306 ''')
00307     self.pp.do_include(f)
00308     self.assertEqual(self.pp.out.getvalue(), "PASS\n")
00309   
00310   def test_var_if_0(self):
00311     f = NamedIO('var_if_0.in', '''#define VAR 0
00312 #if VAR
00313 FAIL
00314 #else
00315 PASS
00316 #endif
00317 ''')
00318     self.pp.do_include(f)
00319     self.assertEqual(self.pp.out.getvalue(), "PASS\n")
00320   
00321   def test_var_if_0_elifdef(self):
00322     f = NamedIO('var_if_0_elifdef.in', '''#if 0
00323 #elifdef FILE
00324 PASS
00325 #else
00326 FAIL
00327 #endif
00328 ''')
00329     self.pp.do_include(f)
00330     self.assertEqual(self.pp.out.getvalue(), "PASS\n")
00331   
00332   def test_var_if_0_elifndef(self):
00333     f = NamedIO('var_if_0_elifndef.in', '''#if 0
00334 #elifndef VAR
00335 PASS
00336 #else
00337 FAIL
00338 #endif
00339 ''')
00340     self.pp.do_include(f)
00341     self.assertEqual(self.pp.out.getvalue(), "PASS\n")
00342   
00343   def test_var_ifdef_0(self):
00344     f = NamedIO('var_ifdef_0.in', '''#define VAR 0
00345 #ifdef VAR
00346 PASS
00347 #else
00348 FAIL
00349 #endif
00350 ''')
00351     self.pp.do_include(f)
00352     self.assertEqual(self.pp.out.getvalue(), "PASS\n")
00353   
00354   def test_var_ifdef_undef(self):
00355     f = NamedIO('var_ifdef_undef.in', '''#define VAR 0
00356 #undef VAR
00357 #ifdef VAR
00358 FAIL
00359 #else
00360 PASS
00361 #endif
00362 ''')
00363     self.pp.do_include(f)
00364     self.assertEqual(self.pp.out.getvalue(), "PASS\n")
00365   
00366   def test_var_ifndef_0(self):
00367     f = NamedIO('var_ifndef_0.in', '''#define VAR 0
00368 #ifndef VAR
00369 FAIL
00370 #else
00371 PASS
00372 #endif
00373 ''')
00374     self.pp.do_include(f)
00375     self.assertEqual(self.pp.out.getvalue(), "PASS\n")
00376   
00377   def test_var_ifndef_undef(self):
00378     f = NamedIO('var_ifndef_undef.in', '''#define VAR 0
00379 #undef VAR
00380 #ifndef VAR
00381 PASS
00382 #else
00383 FAIL
00384 #endif
00385 ''')
00386     self.pp.do_include(f)
00387     self.assertEqual(self.pp.out.getvalue(), "PASS\n")
00388   
00389   def test_var_line(self):
00390     f = NamedIO('var_line.in', '''#ifdef LINE
00391 PASS
00392 #else
00393 FAIL
00394 #endif
00395 ''')
00396     self.pp.do_include(f)
00397     self.assertEqual(self.pp.out.getvalue(), "PASS\n")
00398 
00399   def test_lineEndings(self):
00400     f = NamedIO('lineEndings.in', '''first
00401 #literal second
00402 ''')
00403     self.pp.setLineEndings('cr')
00404     self.pp.do_include(f)
00405     self.assertEqual(self.pp.out.getvalue(), "first\rsecond\r")
00406 
00407   def test_number_value_equals(self):
00408     f = NamedIO("number_value_equals.in", """#define FOO 1000
00409 #if FOO == 1000
00410 number value is equal
00411 #else
00412 number value is not equal
00413 #endif
00414 """)
00415     self.pp.do_include(f)
00416     self.assertEqual(self.pp.out.getvalue(), "number value is equal\n")
00417 
00418   def test_number_value_equals_defines(self):
00419     f = NamedIO("number_value_equals_defines.in", """#if FOO == 1000
00420 number value is equal
00421 #else
00422 number value is not equal
00423 #endif
00424 """)
00425     self.pp.handleCommandLine(["-DFOO=1000"])
00426     self.pp.do_include(f)
00427     self.assertEqual(self.pp.out.getvalue(), "number value is equal\n")
00428 
00429   def test_octal_value_equals(self):
00430     f = NamedIO("octal_value_equals.in", """#define FOO 0100
00431 #if FOO == 0100
00432 octal value is equal
00433 #else
00434 octal value is not equal
00435 #endif
00436 """)
00437     self.pp.do_include(f)
00438     self.assertEqual(self.pp.out.getvalue(), "octal value is equal\n")
00439 
00440   def test_octal_value_equals_defines(self):
00441     f = NamedIO("octal_value_equals_defines.in", """#if FOO == 0100
00442 octal value is equal
00443 #else
00444 octal value is not equal
00445 #endif
00446 """)
00447     self.pp.handleCommandLine(["-DFOO=0100"])
00448     self.pp.do_include(f)
00449     self.assertEqual(self.pp.out.getvalue(), "octal value is equal\n")
00450 
00451   def test_value_quoted_expansion(self):
00452     """
00453     Quoted values on the commandline don't currently have quotes stripped.
00454     Pike says this is for compat reasons.
00455     """
00456     f = NamedIO("value_quoted_expansion.in", """#filter substitution
00457 @FOO@
00458 """)
00459     self.pp.handleCommandLine(['-DFOO="ABCD"'])
00460     self.pp.do_include(f)
00461     self.assertEqual(self.pp.out.getvalue(), '"ABCD"\n')
00462 
00463   def test_octal_value_quoted_expansion(self):
00464     f = NamedIO("value_quoted_expansion.in", """#filter substitution
00465 @FOO@
00466 """)
00467     self.pp.handleCommandLine(['-DFOO="0100"'])
00468     self.pp.do_include(f)
00469     self.assertEqual(self.pp.out.getvalue(), '"0100"\n')
00470 
00471   def test_number_value_not_equals_quoted_defines(self):
00472     f = NamedIO("number_value_not_equals_quoted_defines.in", """#if FOO == 1000
00473 number value is equal
00474 #else
00475 number value is not equal
00476 #endif
00477 """)
00478     self.pp.handleCommandLine(['-DFOO="1000"'])
00479     self.pp.do_include(f)
00480     self.assertEqual(self.pp.out.getvalue(), "number value is not equal\n")
00481 
00482   def test_octal_value_not_equals_quoted_defines(self):
00483     f = NamedIO("octal_value_not_equals_quoted_defines.in", """#if FOO == 0100
00484 octal value is equal
00485 #else
00486 octal value is not equal
00487 #endif
00488 """)
00489     self.pp.handleCommandLine(['-DFOO="0100"'])
00490     self.pp.do_include(f)
00491     self.assertEqual(self.pp.out.getvalue(), "octal value is not equal\n")
00492 
00493 if __name__ == '__main__':
00494   unittest.main()