0001 import parser 0002 import unittest 0003 from test import test_support 0004 0005 # 0006 # First, we test that we can generate trees from valid source fragments, 0007 # and that these valid trees are indeed allowed by the tree-loading side 0008 # of the parser module. 0009 # 0010 0011 class RoundtripLegalSyntaxTestCase(unittest.TestCase): 0012 0013 def roundtrip(self, f, s): 0014 st1 = f(s) 0015 t = st1.totuple() 0016 try: 0017 st2 = parser.sequence2st(t) 0018 except parser.ParserError, why: 0019 self.fail("could not roundtrip %r: %s" % (s, why)) 0020 0021 self.assertEquals(t, st2.totuple(), 0022 "could not re-generate syntax tree") 0023 0024 def check_expr(self, s): 0025 self.roundtrip(parser.expr, s) 0026 0027 def check_suite(self, s): 0028 self.roundtrip(parser.suite, s) 0029 0030 def test_yield_statement(self): 0031 self.check_suite("def f(): yield 1") 0032 self.check_suite("def f(): return; yield 1") 0033 self.check_suite("def f(): yield 1; return") 0034 self.check_suite("def f():\n" 0035 " for x in range(30):\n" 0036 " yield x\n") 0037 0038 def test_expressions(self): 0039 self.check_expr("foo(1)") 0040 self.check_expr("[1, 2, 3]") 0041 self.check_expr("[x**3 for x in range(20)]") 0042 self.check_expr("[x**3 for x in range(20) if x % 3]") 0043 self.check_expr("foo(*args)") 0044 self.check_expr("foo(*args, **kw)") 0045 self.check_expr("foo(**kw)") 0046 self.check_expr("foo(key=value)") 0047 self.check_expr("foo(key=value, *args)") 0048 self.check_expr("foo(key=value, *args, **kw)") 0049 self.check_expr("foo(key=value, **kw)") 0050 self.check_expr("foo(a, b, c, *args)") 0051 self.check_expr("foo(a, b, c, *args, **kw)") 0052 self.check_expr("foo(a, b, c, **kw)") 0053 self.check_expr("foo + bar") 0054 self.check_expr("foo - bar") 0055 self.check_expr("foo * bar") 0056 self.check_expr("foo / bar") 0057 self.check_expr("foo // bar") 0058 self.check_expr("lambda: 0") 0059 self.check_expr("lambda x: 0") 0060 self.check_expr("lambda *y: 0") 0061 self.check_expr("lambda *y, **z: 0") 0062 self.check_expr("lambda **z: 0") 0063 self.check_expr("lambda x, y: 0") 0064 self.check_expr("lambda foo=bar: 0") 0065 self.check_expr("lambda foo=bar, spaz=nifty+spit: 0") 0066 self.check_expr("lambda foo=bar, **z: 0") 0067 self.check_expr("lambda foo=bar, blaz=blat+2, **z: 0") 0068 self.check_expr("lambda foo=bar, blaz=blat+2, *y, **z: 0") 0069 self.check_expr("lambda x, *y, **z: 0") 0070 self.check_expr("(x for x in range(10))") 0071 self.check_expr("foo(x for x in range(10))") 0072 0073 def test_print(self): 0074 self.check_suite("print") 0075 self.check_suite("print 1") 0076 self.check_suite("print 1,") 0077 self.check_suite("print >>fp") 0078 self.check_suite("print >>fp, 1") 0079 self.check_suite("print >>fp, 1,") 0080 0081 def test_simple_expression(self): 0082 # expr_stmt 0083 self.check_suite("a") 0084 0085 def test_simple_assignments(self): 0086 self.check_suite("a = b") 0087 self.check_suite("a = b = c = d = e") 0088 0089 def test_simple_augmented_assignments(self): 0090 self.check_suite("a += b") 0091 self.check_suite("a -= b") 0092 self.check_suite("a *= b") 0093 self.check_suite("a /= b") 0094 self.check_suite("a //= b") 0095 self.check_suite("a %= b") 0096 self.check_suite("a &= b") 0097 self.check_suite("a |= b") 0098 self.check_suite("a ^= b") 0099 self.check_suite("a <<= b") 0100 self.check_suite("a >>= b") 0101 self.check_suite("a **= b") 0102 0103 def test_function_defs(self): 0104 self.check_suite("def f(): pass") 0105 self.check_suite("def f(*args): pass") 0106 self.check_suite("def f(*args, **kw): pass") 0107 self.check_suite("def f(**kw): pass") 0108 self.check_suite("def f(foo=bar): pass") 0109 self.check_suite("def f(foo=bar, *args): pass") 0110 self.check_suite("def f(foo=bar, *args, **kw): pass") 0111 self.check_suite("def f(foo=bar, **kw): pass") 0112 0113 self.check_suite("def f(a, b): pass") 0114 self.check_suite("def f(a, b, *args): pass") 0115 self.check_suite("def f(a, b, *args, **kw): pass") 0116 self.check_suite("def f(a, b, **kw): pass") 0117 self.check_suite("def f(a, b, foo=bar): pass") 0118 self.check_suite("def f(a, b, foo=bar, *args): pass") 0119 self.check_suite("def f(a, b, foo=bar, *args, **kw): pass") 0120 self.check_suite("def f(a, b, foo=bar, **kw): pass") 0121 0122 self.check_suite("@staticmethod\n" 0123 "def f(): pass") 0124 self.check_suite("@staticmethod\n" 0125 "@funcattrs(x, y)\n" 0126 "def f(): pass") 0127 self.check_suite("@funcattrs()\n" 0128 "def f(): pass") 0129 0130 def test_import_from_statement(self): 0131 self.check_suite("from sys.path import *") 0132 self.check_suite("from sys.path import dirname") 0133 self.check_suite("from sys.path import (dirname)") 0134 self.check_suite("from sys.path import (dirname,)") 0135 self.check_suite("from sys.path import dirname as my_dirname") 0136 self.check_suite("from sys.path import (dirname as my_dirname)") 0137 self.check_suite("from sys.path import (dirname as my_dirname,)") 0138 self.check_suite("from sys.path import dirname, basename") 0139 self.check_suite("from sys.path import (dirname, basename)") 0140 self.check_suite("from sys.path import (dirname, basename,)") 0141 self.check_suite( 0142 "from sys.path import dirname as my_dirname, basename") 0143 self.check_suite( 0144 "from sys.path import (dirname as my_dirname, basename)") 0145 self.check_suite( 0146 "from sys.path import (dirname as my_dirname, basename,)") 0147 self.check_suite( 0148 "from sys.path import dirname, basename as my_basename") 0149 self.check_suite( 0150 "from sys.path import (dirname, basename as my_basename)") 0151 self.check_suite( 0152 "from sys.path import (dirname, basename as my_basename,)") 0153 0154 def test_basic_import_statement(self): 0155 self.check_suite("import sys") 0156 self.check_suite("import sys as system") 0157 self.check_suite("import sys, math") 0158 self.check_suite("import sys as system, math") 0159 self.check_suite("import sys, math as my_math") 0160 0161 def test_pep263(self): 0162 self.check_suite("# -*- coding: iso-8859-1 -*-\n" 0163 "pass\n") 0164 0165 def test_assert(self): 0166 self.check_suite("assert alo < ahi and blo < bhi\n") 0167 0168 # 0169 # Second, we take *invalid* trees and make sure we get ParserError 0170 # rejections for them. 0171 # 0172 0173 class IllegalSyntaxTestCase(unittest.TestCase): 0174 0175 def check_bad_tree(self, tree, label): 0176 try: 0177 parser.sequence2st(tree) 0178 except parser.ParserError: 0179 pass 0180 else: 0181 self.fail("did not detect invalid tree for %r" % label) 0182 0183 def test_junk(self): 0184 # not even remotely valid: 0185 self.check_bad_tree((1, 2, 3), "<junk>") 0186 0187 def test_illegal_yield_1(self): 0188 # Illegal yield statement: def f(): return 1; yield 1 0189 tree = \ 0190 (257, 0191 (264, 0192 (285, 0193 (259, 0194 (1, 'def'), 0195 (1, 'f'), 0196 (260, (7, '('), (8, ')')), 0197 (11, ':'), 0198 (291, 0199 (4, ''), 0200 (5, ''), 0201 (264, 0202 (265, 0203 (266, 0204 (272, 0205 (275, 0206 (1, 'return'), 0207 (313, 0208 (292, 0209 (293, 0210 (294, 0211 (295, 0212 (297, 0213 (298, 0214 (299, 0215 (300, 0216 (301, 0217 (302, (303, (304, (305, (2, '1')))))))))))))))))), 0218 (264, 0219 (265, 0220 (266, 0221 (272, 0222 (276, 0223 (1, 'yield'), 0224 (313, 0225 (292, 0226 (293, 0227 (294, 0228 (295, 0229 (297, 0230 (298, 0231 (299, 0232 (300, 0233 (301, 0234 (302, 0235 (303, (304, (305, (2, '1')))))))))))))))))), 0236 (4, ''))), 0237 (6, ''))))), 0238 (4, ''), 0239 (0, '')))) 0240 self.check_bad_tree(tree, "def f():\n return 1\n yield 1") 0241 0242 def test_illegal_yield_2(self): 0243 # Illegal return in generator: def f(): return 1; yield 1 0244 tree = \ 0245 (257, 0246 (264, 0247 (265, 0248 (266, 0249 (278, 0250 (1, 'from'), 0251 (281, (1, '__future__')), 0252 (1, 'import'), 0253 (279, (1, 'generators')))), 0254 (4, ''))), 0255 (264, 0256 (285, 0257 (259, 0258 (1, 'def'), 0259 (1, 'f'), 0260 (260, (7, '('), (8, ')')), 0261 (11, ':'), 0262 (291, 0263 (4, ''), 0264 (5, ''), 0265 (264, 0266 (265, 0267 (266, 0268 (272, 0269 (275, 0270 (1, 'return'), 0271 (313, 0272 (292, 0273 (293, 0274 (294, 0275 (295, 0276 (297, 0277 (298, 0278 (299, 0279 (300, 0280 (301, 0281 (302, (303, (304, (305, (2, '1')))))))))))))))))), 0282 (264, 0283 (265, 0284 (266, 0285 (272, 0286 (276, 0287 (1, 'yield'), 0288 (313, 0289 (292, 0290 (293, 0291 (294, 0292 (295, 0293 (297, 0294 (298, 0295 (299, 0296 (300, 0297 (301, 0298 (302, 0299 (303, (304, (305, (2, '1')))))))))))))))))), 0300 (4, ''))), 0301 (6, ''))))), 0302 (4, ''), 0303 (0, '')))) 0304 self.check_bad_tree(tree, "def f():\n return 1\n yield 1") 0305 0306 def test_print_chevron_comma(self): 0307 # Illegal input: print >>fp, 0308 tree = \ 0309 (257, 0310 (264, 0311 (265, 0312 (266, 0313 (268, 0314 (1, 'print'), 0315 (35, '>>'), 0316 (290, 0317 (291, 0318 (292, 0319 (293, 0320 (295, 0321 (296, 0322 (297, 0323 (298, (299, (300, (301, (302, (303, (1, 'fp')))))))))))))), 0324 (12, ','))), 0325 (4, ''))), 0326 (0, '')) 0327 self.check_bad_tree(tree, "print >>fp,") 0328 0329 def test_a_comma_comma_c(self): 0330 # Illegal input: a,,c 0331 tree = \ 0332 (258, 0333 (311, 0334 (290, 0335 (291, 0336 (292, 0337 (293, 0338 (295, 0339 (296, 0340 (297, 0341 (298, (299, (300, (301, (302, (303, (1, 'a')))))))))))))), 0342 (12, ','), 0343 (12, ','), 0344 (290, 0345 (291, 0346 (292, 0347 (293, 0348 (295, 0349 (296, 0350 (297, 0351 (298, (299, (300, (301, (302, (303, (1, 'c'))))))))))))))), 0352 (4, ''), 0353 (0, '')) 0354 self.check_bad_tree(tree, "a,,c") 0355 0356 def test_illegal_operator(self): 0357 # Illegal input: a $= b 0358 tree = \ 0359 (257, 0360 (264, 0361 (265, 0362 (266, 0363 (267, 0364 (312, 0365 (291, 0366 (292, 0367 (293, 0368 (294, 0369 (296, 0370 (297, 0371 (298, 0372 (299, 0373 (300, (301, (302, (303, (304, (1, 'a'))))))))))))))), 0374 (268, (37, '$=')), 0375 (312, 0376 (291, 0377 (292, 0378 (293, 0379 (294, 0380 (296, 0381 (297, 0382 (298, 0383 (299, 0384 (300, (301, (302, (303, (304, (1, 'b'))))))))))))))))), 0385 (4, ''))), 0386 (0, '')) 0387 self.check_bad_tree(tree, "a $= b") 0388 0389 def test_malformed_global(self): 0390 #doesn't have global keyword in ast 0391 tree = (257, 0392 (264, 0393 (265, 0394 (266, 0395 (282, (1, 'foo'))), (4, ''))), 0396 (4, ''), 0397 (0, '')) 0398 self.check_bad_tree(tree, "malformed global ast") 0399 0400 def test_main(): 0401 test_support.run_unittest( 0402 RoundtripLegalSyntaxTestCase, 0403 IllegalSyntaxTestCase 0404 ) 0405 0406 0407 if __name__ == "__main__": 0408 test_main() 0409
Generated by PyXR 0.9.4