Updated for 2.1b2 distribution.
[python/dscho.git] / Lib / test / test_parser.py
blob6885767b524f55e2493d7b112aab52db6580f764
1 import os.path
2 import parser
3 import pprint
4 import sys
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.
14 def roundtrip(f, s):
15 st1 = f(s)
16 t = st1.totuple()
17 try:
18 st2 = parser.sequence2ast(t)
19 except parser.ParserError:
20 raise TestFailed, s
22 def roundtrip_fromfile(filename):
23 roundtrip(parser.suite, open(filename).read())
25 def test_expr(s):
26 print "expr:", s
27 roundtrip(parser.expr, s)
29 def test_suite(s):
30 print "suite:", s
31 roundtrip(parser.suite, s)
34 print "Expressions:"
36 test_expr("foo(1)")
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")
64 print
65 print "Statements:"
66 test_suite("print")
67 test_suite("print 1")
68 test_suite("print 1,")
69 test_suite("print >>fp")
70 test_suite("print >>fp, 1")
71 test_suite("print >>fp, 1,")
73 # expr_stmt
74 test_suite("a")
75 test_suite("a = b")
76 test_suite("a = b = c = d = e")
77 test_suite("a += b")
78 test_suite("a -= b")
79 test_suite("a *= b")
80 test_suite("a /= b")
81 test_suite("a %= b")
82 test_suite("a &= b")
83 test_suite("a |= b")
84 test_suite("a ^= b")
85 test_suite("a <<= b")
86 test_suite("a >>= b")
87 test_suite("a **= b")
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.
129 print
130 print "Invalid parse trees:"
132 def check_bad_tree(tree, label):
133 print
134 print label
135 try:
136 parser.sequence2ast(tree)
137 except parser.ParserError:
138 print "caught expected exception for invalid tree"
139 else:
140 print "test failed: did not properly detect invalid tree:"
141 pprint.pprint(tree)
144 # not even remotely valid:
145 check_bad_tree((1, 2, 3), "<junk>")
147 # print >>fp,
148 tree = \
149 (257,
150 (264,
151 (265,
152 (266,
153 (268,
154 (1, 'print'),
155 (35, '>>'),
156 (290,
157 (291,
158 (292,
159 (293,
160 (295,
161 (296,
162 (297,
163 (298, (299, (300, (301, (302, (303, (1, 'fp')))))))))))))),
164 (12, ','))),
165 (4, ''))),
166 (0, ''))
168 check_bad_tree(tree, "print >>fp,")
170 # a,,c
171 tree = \
172 (258,
173 (311,
174 (290,
175 (291,
176 (292,
177 (293,
178 (295,
179 (296, (297, (298, (299, (300, (301, (302, (303, (1, 'a')))))))))))))),
180 (12, ','),
181 (12, ','),
182 (290,
183 (291,
184 (292,
185 (293,
186 (295,
187 (296, (297, (298, (299, (300, (301, (302, (303, (1, 'c'))))))))))))))),
188 (4, ''),
189 (0, ''))
191 check_bad_tree(tree, "a,,c")
193 # a $= b
194 tree = \
195 (257,
196 (264,
197 (265,
198 (266,
199 (267,
200 (312,
201 (291,
202 (292,
203 (293,
204 (294,
205 (296,
206 (297,
207 (298,
208 (299, (300, (301, (302, (303, (304, (1, 'a'))))))))))))))),
209 (268, (37, '$=')),
210 (312,
211 (291,
212 (292,
213 (293,
214 (294,
215 (296,
216 (297,
217 (298,
218 (299, (300, (301, (302, (303, (304, (1, 'b'))))))))))))))))),
219 (4, ''))),
220 (0, ''))
222 check_bad_tree(tree, "a $= b")