blob: bb22f6a7107e6bba293f8ef5ce2b578e133f8790 [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
6from parser import expr, suite, sequence2ast
7from test_support import verbose
8
9#
10# First, we test that we can generate trees from valid source fragments,
11# and that these valid trees are indeed allowed by the tree-loading side
12# of the parser module.
13#
14
15def roundtrip(f, s):
Fred Drake79ca79d2000-08-21 22:30:53 +000016 st1 = f(s)
17 t = st1.totuple()
Fred Drakee1578ce2000-12-11 22:12:09 +000018 try:
19 st2 = parser.sequence2ast(t)
20 except parser.ParserError:
21 print "Failing syntax tree:"
22 pprint.pprint(t)
23 raise
Fred Drake79ca79d2000-08-21 22:30:53 +000024
Fred Drake28f739a2000-08-25 22:42:40 +000025def roundtrip_fromfile(filename):
26 roundtrip(suite, open(filename).read())
27
28def test_expr(s):
29 print "expr:", s
30 roundtrip(expr, s)
31
32def test_suite(s):
33 print "suite:", s
34 roundtrip(suite, s)
35
Fred Drake79ca79d2000-08-21 22:30:53 +000036
37print "Expressions:"
38
Fred Drake28f739a2000-08-25 22:42:40 +000039test_expr("foo(1)")
40test_expr("[1, 2, 3]")
41test_expr("[x**3 for x in range(20)]")
42test_expr("[x**3 for x in range(20) if x % 3]")
43test_expr("foo(*args)")
44test_expr("foo(*args, **kw)")
45test_expr("foo(**kw)")
46test_expr("foo(key=value)")
47test_expr("foo(key=value, *args)")
48test_expr("foo(key=value, *args, **kw)")
49test_expr("foo(key=value, **kw)")
50test_expr("foo(a, b, c, *args)")
51test_expr("foo(a, b, c, *args, **kw)")
52test_expr("foo(a, b, c, **kw)")
53test_expr("foo + bar")
Fred Drakee1578ce2000-12-11 22:12:09 +000054test_expr("lambda: 0")
55test_expr("lambda x: 0")
56test_expr("lambda *y: 0")
57test_expr("lambda *y, **z: 0")
58test_expr("lambda **z: 0")
59test_expr("lambda x, y: 0")
60test_expr("lambda foo=bar: 0")
61test_expr("lambda foo=bar, spaz=nifty+spit: 0")
62test_expr("lambda foo=bar, **z: 0")
63test_expr("lambda foo=bar, blaz=blat+2, **z: 0")
64test_expr("lambda foo=bar, blaz=blat+2, *y, **z: 0")
65test_expr("lambda x, *y, **z: 0")
Fred Drake79ca79d2000-08-21 22:30:53 +000066
67print
68print "Statements:"
Fred Drake28f739a2000-08-25 22:42:40 +000069test_suite("print")
70test_suite("print 1")
71test_suite("print 1,")
72test_suite("print >>fp")
73test_suite("print >>fp, 1")
74test_suite("print >>fp, 1,")
75
76# expr_stmt
77test_suite("a")
78test_suite("a = b")
79test_suite("a = b = c = d = e")
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")
88test_suite("a <<= b")
89test_suite("a >>= b")
90test_suite("a **= b")
Fred Drakee1578ce2000-12-11 22:12:09 +000091test_suite("def f(): pass")
92test_suite("def f(foo=bar): pass")
Fred Drake28f739a2000-08-25 22:42:40 +000093
94#d = os.path.dirname(os.__file__)
95#roundtrip_fromfile(os.path.join(d, "os.py"))
96#roundtrip_fromfile(os.path.join(d, "test", "test_parser.py"))
Fred Drake79ca79d2000-08-21 22:30:53 +000097
98#
99# Second, we take *invalid* trees and make sure we get ParserError
100# rejections for them.
101#
102
103print
104print "Invalid parse trees:"
105
106def check_bad_tree(tree, label):
107 print
108 print label
109 try:
110 sequence2ast(tree)
111 except parser.ParserError:
112 print "caught expected exception for invalid tree"
113 pass
114 else:
115 print "test failed: did not properly detect invalid tree:"
116 pprint.pprint(tree)
117
118
119# not even remotely valid:
120check_bad_tree((1, 2, 3), "<junk>")
121
122# print >>fp,
123tree = \
124(257,
125 (264,
126 (265,
127 (266,
128 (268,
129 (1, 'print'),
130 (35, '>>'),
131 (290,
132 (291,
133 (292,
134 (293,
135 (295,
136 (296,
137 (297,
138 (298, (299, (300, (301, (302, (303, (1, 'fp')))))))))))))),
139 (12, ','))),
140 (4, ''))),
141 (0, ''))
142
143check_bad_tree(tree, "print >>fp,")
144
145# a,,c
146tree = \
147(258,
148 (311,
149 (290,
150 (291,
151 (292,
152 (293,
153 (295,
154 (296, (297, (298, (299, (300, (301, (302, (303, (1, 'a')))))))))))))),
155 (12, ','),
156 (12, ','),
157 (290,
158 (291,
159 (292,
160 (293,
161 (295,
162 (296, (297, (298, (299, (300, (301, (302, (303, (1, 'c'))))))))))))))),
163 (4, ''),
164 (0, ''))
165
166check_bad_tree(tree, "a,,c")
Fred Drake28f739a2000-08-25 22:42:40 +0000167
168# a $= b
169tree = \
170(257,
171 (264,
172 (265,
173 (266,
174 (267,
175 (312,
176 (291,
177 (292,
178 (293,
179 (294,
180 (296,
181 (297,
182 (298,
183 (299, (300, (301, (302, (303, (304, (1, 'a'))))))))))))))),
184 (268, (37, '$=')),
185 (312,
186 (291,
187 (292,
188 (293,
189 (294,
190 (296,
191 (297,
192 (298,
193 (299, (300, (301, (302, (303, (304, (1, 'b'))))))))))))))))),
194 (4, ''))),
195 (0, ''))
196
197check_bad_tree(tree, "a $= b")