blob: 6885767b524f55e2493d7b112aab52db6580f764 [file] [log] [blame]
Fred Drake28f739a2000-08-25 22:42:40 +00001import os.path
Fred Drake79ca79d2000-08-21 22:30:53 +00002import parser
3import pprint
4import sys
5
Fredrik Lundhf7850422001-01-17 21:51:36 +00006from test_support import TestFailed
Fred Drake79ca79d2000-08-21 22:30:53 +00007
8#
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.
12#
13
14def roundtrip(f, s):
Fred Drake79ca79d2000-08-21 22:30:53 +000015 st1 = f(s)
16 t = st1.totuple()
Fred Drakee1578ce2000-12-11 22:12:09 +000017 try:
18 st2 = parser.sequence2ast(t)
19 except parser.ParserError:
Fred Drakee3fb18c2001-01-07 06:02:19 +000020 raise TestFailed, s
Fred Drake79ca79d2000-08-21 22:30:53 +000021
Fred Drake28f739a2000-08-25 22:42:40 +000022def roundtrip_fromfile(filename):
Fred Drakee3fb18c2001-01-07 06:02:19 +000023 roundtrip(parser.suite, open(filename).read())
Fred Drake28f739a2000-08-25 22:42:40 +000024
25def test_expr(s):
26 print "expr:", s
Fred Drakee3fb18c2001-01-07 06:02:19 +000027 roundtrip(parser.expr, s)
Fred Drake28f739a2000-08-25 22:42:40 +000028
29def test_suite(s):
30 print "suite:", s
Fred Drakee3fb18c2001-01-07 06:02:19 +000031 roundtrip(parser.suite, s)
Fred Drake28f739a2000-08-25 22:42:40 +000032
Fred Drake79ca79d2000-08-21 22:30:53 +000033
34print "Expressions:"
35
Fred Drake28f739a2000-08-25 22:42:40 +000036test_expr("foo(1)")
37test_expr("[1, 2, 3]")
38test_expr("[x**3 for x in range(20)]")
39test_expr("[x**3 for x in range(20) if x % 3]")
40test_expr("foo(*args)")
41test_expr("foo(*args, **kw)")
42test_expr("foo(**kw)")
43test_expr("foo(key=value)")
44test_expr("foo(key=value, *args)")
45test_expr("foo(key=value, *args, **kw)")
46test_expr("foo(key=value, **kw)")
47test_expr("foo(a, b, c, *args)")
48test_expr("foo(a, b, c, *args, **kw)")
49test_expr("foo(a, b, c, **kw)")
50test_expr("foo + bar")
Fred Drakee1578ce2000-12-11 22:12:09 +000051test_expr("lambda: 0")
52test_expr("lambda x: 0")
53test_expr("lambda *y: 0")
54test_expr("lambda *y, **z: 0")
55test_expr("lambda **z: 0")
56test_expr("lambda x, y: 0")
57test_expr("lambda foo=bar: 0")
58test_expr("lambda foo=bar, spaz=nifty+spit: 0")
59test_expr("lambda foo=bar, **z: 0")
60test_expr("lambda foo=bar, blaz=blat+2, **z: 0")
61test_expr("lambda foo=bar, blaz=blat+2, *y, **z: 0")
62test_expr("lambda x, *y, **z: 0")
Fred Drake79ca79d2000-08-21 22:30:53 +000063
64print
65print "Statements:"
Fred Drake28f739a2000-08-25 22:42:40 +000066test_suite("print")
67test_suite("print 1")
68test_suite("print 1,")
69test_suite("print >>fp")
70test_suite("print >>fp, 1")
71test_suite("print >>fp, 1,")
72
73# expr_stmt
74test_suite("a")
75test_suite("a = b")
76test_suite("a = b = c = d = e")
77test_suite("a += b")
78test_suite("a -= b")
79test_suite("a *= b")
80test_suite("a /= b")
81test_suite("a %= b")
82test_suite("a &= b")
83test_suite("a |= b")
84test_suite("a ^= b")
85test_suite("a <<= b")
86test_suite("a >>= b")
87test_suite("a **= b")
Fred Drakee3fb18c2001-01-07 06:02:19 +000088
Fred Drakee1578ce2000-12-11 22:12:09 +000089test_suite("def f(): pass")
Fred Drakee3fb18c2001-01-07 06:02:19 +000090test_suite("def f(*args): pass")
91test_suite("def f(*args, **kw): pass")
92test_suite("def f(**kw): pass")
Fred Drakee1578ce2000-12-11 22:12:09 +000093test_suite("def f(foo=bar): pass")
Fred Drakee3fb18c2001-01-07 06:02:19 +000094test_suite("def f(foo=bar, *args): pass")
95test_suite("def f(foo=bar, *args, **kw): pass")
96test_suite("def f(foo=bar, **kw): pass")
97
98test_suite("def f(a, b): pass")
99test_suite("def f(a, b, *args): pass")
100test_suite("def f(a, b, *args, **kw): pass")
101test_suite("def f(a, b, **kw): pass")
102test_suite("def f(a, b, foo=bar): pass")
103test_suite("def f(a, b, foo=bar, *args): pass")
104test_suite("def f(a, b, foo=bar, *args, **kw): pass")
105test_suite("def f(a, b, foo=bar, **kw): pass")
106
107test_suite("from sys.path import *")
108test_suite("from sys.path import dirname")
109test_suite("from sys.path import dirname as my_dirname")
110test_suite("from sys.path import dirname, basename")
111test_suite("from sys.path import dirname as my_dirname, basename")
112test_suite("from sys.path import dirname, basename as my_basename")
113
114test_suite("import sys")
115test_suite("import sys as system")
116test_suite("import sys, math")
117test_suite("import sys as system, math")
118test_suite("import sys, math as my_math")
Fred Drake28f739a2000-08-25 22:42:40 +0000119
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"))
Fred Drake79ca79d2000-08-21 22:30:53 +0000123
124#
125# Second, we take *invalid* trees and make sure we get ParserError
126# rejections for them.
127#
128
129print
130print "Invalid parse trees:"
131
132def check_bad_tree(tree, label):
133 print
134 print label
135 try:
Fred Drakee3fb18c2001-01-07 06:02:19 +0000136 parser.sequence2ast(tree)
Fred Drake79ca79d2000-08-21 22:30:53 +0000137 except parser.ParserError:
138 print "caught expected exception for invalid tree"
Fred Drake79ca79d2000-08-21 22:30:53 +0000139 else:
140 print "test failed: did not properly detect invalid tree:"
141 pprint.pprint(tree)
142
143
144# not even remotely valid:
145check_bad_tree((1, 2, 3), "<junk>")
146
147# print >>fp,
148tree = \
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, ''))
167
168check_bad_tree(tree, "print >>fp,")
169
170# a,,c
171tree = \
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, ''))
190
191check_bad_tree(tree, "a,,c")
Fred Drake28f739a2000-08-25 22:42:40 +0000192
193# a $= b
194tree = \
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, ''))
221
222check_bad_tree(tree, "a $= b")