6 from test_support
import TestFailed
9 # First, we test that we can generate trees from valid source fragments,
10 # and that these valid trees are indeed allowed by the tree-loading side
11 # of the parser module.
18 st2
= parser
.sequence2ast(t
)
19 except parser
.ParserError
:
22 def roundtrip_fromfile(filename
):
23 roundtrip(parser
.suite
, open(filename
).read())
27 roundtrip(parser
.expr
, s
)
31 roundtrip(parser
.suite
, s
)
37 test_expr("[1, 2, 3]")
38 test_expr("[x**3 for x in range(20)]")
39 test_expr("[x**3 for x in range(20) if x % 3]")
40 test_expr("foo(*args)")
41 test_expr("foo(*args, **kw)")
42 test_expr("foo(**kw)")
43 test_expr("foo(key=value)")
44 test_expr("foo(key=value, *args)")
45 test_expr("foo(key=value, *args, **kw)")
46 test_expr("foo(key=value, **kw)")
47 test_expr("foo(a, b, c, *args)")
48 test_expr("foo(a, b, c, *args, **kw)")
49 test_expr("foo(a, b, c, **kw)")
50 test_expr("foo + bar")
51 test_expr("lambda: 0")
52 test_expr("lambda x: 0")
53 test_expr("lambda *y: 0")
54 test_expr("lambda *y, **z: 0")
55 test_expr("lambda **z: 0")
56 test_expr("lambda x, y: 0")
57 test_expr("lambda foo=bar: 0")
58 test_expr("lambda foo=bar, spaz=nifty+spit: 0")
59 test_expr("lambda foo=bar, **z: 0")
60 test_expr("lambda foo=bar, blaz=blat+2, **z: 0")
61 test_expr("lambda foo=bar, blaz=blat+2, *y, **z: 0")
62 test_expr("lambda x, *y, **z: 0")
68 test_suite("print 1,")
69 test_suite("print >>fp")
70 test_suite("print >>fp, 1")
71 test_suite("print >>fp, 1,")
76 test_suite("a = b = c = d = e")
89 test_suite("def f(): pass")
90 test_suite("def f(*args): pass")
91 test_suite("def f(*args, **kw): pass")
92 test_suite("def f(**kw): pass")
93 test_suite("def f(foo=bar): pass")
94 test_suite("def f(foo=bar, *args): pass")
95 test_suite("def f(foo=bar, *args, **kw): pass")
96 test_suite("def f(foo=bar, **kw): pass")
98 test_suite("def f(a, b): pass")
99 test_suite("def f(a, b, *args): pass")
100 test_suite("def f(a, b, *args, **kw): pass")
101 test_suite("def f(a, b, **kw): pass")
102 test_suite("def f(a, b, foo=bar): pass")
103 test_suite("def f(a, b, foo=bar, *args): pass")
104 test_suite("def f(a, b, foo=bar, *args, **kw): pass")
105 test_suite("def f(a, b, foo=bar, **kw): pass")
107 test_suite("from sys.path import *")
108 test_suite("from sys.path import dirname")
109 test_suite("from sys.path import dirname as my_dirname")
110 test_suite("from sys.path import dirname, basename")
111 test_suite("from sys.path import dirname as my_dirname, basename")
112 test_suite("from sys.path import dirname, basename as my_basename")
114 test_suite("import sys")
115 test_suite("import sys as system")
116 test_suite("import sys, math")
117 test_suite("import sys as system, math")
118 test_suite("import sys, math as my_math")
120 #d = os.path.dirname(os.__file__)
121 #roundtrip_fromfile(os.path.join(d, "os.py"))
122 #roundtrip_fromfile(os.path.join(d, "test", "test_parser.py"))
125 # Second, we take *invalid* trees and make sure we get ParserError
126 # rejections for them.
130 print "Invalid parse trees:"
132 def check_bad_tree(tree
, label
):
136 parser
.sequence2ast(tree
)
137 except parser
.ParserError
:
138 print "caught expected exception for invalid tree"
140 print "test failed: did not properly detect invalid tree:"
144 # not even remotely valid:
145 check_bad_tree((1, 2, 3), "<junk>")
163 (298, (299, (300, (301, (302, (303, (1, 'fp')))))))))))))),
168 check_bad_tree(tree
, "print >>fp,")
179 (296, (297, (298, (299, (300, (301, (302, (303, (1, 'a')))))))))))))),
187 (296, (297, (298, (299, (300, (301, (302, (303, (1, 'c'))))))))))))))),
191 check_bad_tree(tree
, "a,,c")
208 (299, (300, (301, (302, (303, (304, (1, 'a'))))))))))))))),
218 (299, (300, (301, (302, (303, (304, (1, 'b'))))))))))))))))),
222 check_bad_tree(tree
, "a $= b")