blob: bc41de42676465df8372fca3971b19f715038e19 [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()
18 st2 = parser.sequence2ast(t)
19
Fred Drake28f739a2000-08-25 22:42:40 +000020def roundtrip_fromfile(filename):
21 roundtrip(suite, open(filename).read())
22
23def test_expr(s):
24 print "expr:", s
25 roundtrip(expr, s)
26
27def test_suite(s):
28 print "suite:", s
29 roundtrip(suite, s)
30
Fred Drake79ca79d2000-08-21 22:30:53 +000031
32print "Expressions:"
33
Fred Drake28f739a2000-08-25 22:42:40 +000034test_expr("foo(1)")
35test_expr("[1, 2, 3]")
36test_expr("[x**3 for x in range(20)]")
37test_expr("[x**3 for x in range(20) if x % 3]")
38test_expr("foo(*args)")
39test_expr("foo(*args, **kw)")
40test_expr("foo(**kw)")
41test_expr("foo(key=value)")
42test_expr("foo(key=value, *args)")
43test_expr("foo(key=value, *args, **kw)")
44test_expr("foo(key=value, **kw)")
45test_expr("foo(a, b, c, *args)")
46test_expr("foo(a, b, c, *args, **kw)")
47test_expr("foo(a, b, c, **kw)")
48test_expr("foo + bar")
Fred Drake79ca79d2000-08-21 22:30:53 +000049
50print
51print "Statements:"
Fred Drake28f739a2000-08-25 22:42:40 +000052test_suite("print")
53test_suite("print 1")
54test_suite("print 1,")
55test_suite("print >>fp")
56test_suite("print >>fp, 1")
57test_suite("print >>fp, 1,")
58
59# expr_stmt
60test_suite("a")
61test_suite("a = b")
62test_suite("a = b = c = d = e")
63test_suite("a += b")
64test_suite("a -= b")
65test_suite("a *= b")
66test_suite("a /= b")
67test_suite("a %= b")
68test_suite("a &= b")
69test_suite("a |= b")
70test_suite("a ^= b")
71test_suite("a <<= b")
72test_suite("a >>= b")
73test_suite("a **= b")
74
75#d = os.path.dirname(os.__file__)
76#roundtrip_fromfile(os.path.join(d, "os.py"))
77#roundtrip_fromfile(os.path.join(d, "test", "test_parser.py"))
Fred Drake79ca79d2000-08-21 22:30:53 +000078
79#
80# Second, we take *invalid* trees and make sure we get ParserError
81# rejections for them.
82#
83
84print
85print "Invalid parse trees:"
86
87def check_bad_tree(tree, label):
88 print
89 print label
90 try:
91 sequence2ast(tree)
92 except parser.ParserError:
93 print "caught expected exception for invalid tree"
94 pass
95 else:
96 print "test failed: did not properly detect invalid tree:"
97 pprint.pprint(tree)
98
99
100# not even remotely valid:
101check_bad_tree((1, 2, 3), "<junk>")
102
103# print >>fp,
104tree = \
105(257,
106 (264,
107 (265,
108 (266,
109 (268,
110 (1, 'print'),
111 (35, '>>'),
112 (290,
113 (291,
114 (292,
115 (293,
116 (295,
117 (296,
118 (297,
119 (298, (299, (300, (301, (302, (303, (1, 'fp')))))))))))))),
120 (12, ','))),
121 (4, ''))),
122 (0, ''))
123
124check_bad_tree(tree, "print >>fp,")
125
126# a,,c
127tree = \
128(258,
129 (311,
130 (290,
131 (291,
132 (292,
133 (293,
134 (295,
135 (296, (297, (298, (299, (300, (301, (302, (303, (1, 'a')))))))))))))),
136 (12, ','),
137 (12, ','),
138 (290,
139 (291,
140 (292,
141 (293,
142 (295,
143 (296, (297, (298, (299, (300, (301, (302, (303, (1, 'c'))))))))))))))),
144 (4, ''),
145 (0, ''))
146
147check_bad_tree(tree, "a,,c")
Fred Drake28f739a2000-08-25 22:42:40 +0000148
149# a $= b
150tree = \
151(257,
152 (264,
153 (265,
154 (266,
155 (267,
156 (312,
157 (291,
158 (292,
159 (293,
160 (294,
161 (296,
162 (297,
163 (298,
164 (299, (300, (301, (302, (303, (304, (1, 'a'))))))))))))))),
165 (268, (37, '$=')),
166 (312,
167 (291,
168 (292,
169 (293,
170 (294,
171 (296,
172 (297,
173 (298,
174 (299, (300, (301, (302, (303, (304, (1, 'b'))))))))))))))))),
175 (4, ''))),
176 (0, ''))
177
178check_bad_tree(tree, "a $= b")