blob: 5b867d75c9356928525fd010337a5c9bfa6e24c8 [file] [log] [blame]
Fred Drake79ca79d2000-08-21 22:30:53 +00001import parser
Fred Drake58422e52001-06-04 03:56:24 +00002import test_support
3import unittest
Fred Drake79ca79d2000-08-21 22:30:53 +00004
5#
6# First, we test that we can generate trees from valid source fragments,
7# and that these valid trees are indeed allowed by the tree-loading side
8# of the parser module.
9#
10
Fred Drake58422e52001-06-04 03:56:24 +000011class RoundtripLegalSyntaxTestCase(unittest.TestCase):
12 def roundtrip(self, f, s):
13 st1 = f(s)
14 t = st1.totuple()
15 try:
16 st2 = parser.sequence2ast(t)
17 except parser.ParserError:
18 self.fail("could not roundtrip %r" % s)
Fred Drake79ca79d2000-08-21 22:30:53 +000019
Fred Drake58422e52001-06-04 03:56:24 +000020 self.assertEquals(t, st2.totuple(),
21 "could not re-generate syntax tree")
Fred Drake28f739a2000-08-25 22:42:40 +000022
Fred Drake58422e52001-06-04 03:56:24 +000023 def check_expr(self, s):
24 self.roundtrip(parser.expr, s)
Fred Drake28f739a2000-08-25 22:42:40 +000025
Fred Drake58422e52001-06-04 03:56:24 +000026 def check_suite(self, s):
27 self.roundtrip(parser.suite, s)
Fred Drake28f739a2000-08-25 22:42:40 +000028
Fred Drake58422e52001-06-04 03:56:24 +000029 def test_expressions(self):
30 self.check_expr("foo(1)")
31 self.check_expr("[1, 2, 3]")
32 self.check_expr("[x**3 for x in range(20)]")
33 self.check_expr("[x**3 for x in range(20) if x % 3]")
34 self.check_expr("foo(*args)")
35 self.check_expr("foo(*args, **kw)")
36 self.check_expr("foo(**kw)")
37 self.check_expr("foo(key=value)")
38 self.check_expr("foo(key=value, *args)")
39 self.check_expr("foo(key=value, *args, **kw)")
40 self.check_expr("foo(key=value, **kw)")
41 self.check_expr("foo(a, b, c, *args)")
42 self.check_expr("foo(a, b, c, *args, **kw)")
43 self.check_expr("foo(a, b, c, **kw)")
44 self.check_expr("foo + bar")
45 self.check_expr("lambda: 0")
46 self.check_expr("lambda x: 0")
47 self.check_expr("lambda *y: 0")
48 self.check_expr("lambda *y, **z: 0")
49 self.check_expr("lambda **z: 0")
50 self.check_expr("lambda x, y: 0")
51 self.check_expr("lambda foo=bar: 0")
52 self.check_expr("lambda foo=bar, spaz=nifty+spit: 0")
53 self.check_expr("lambda foo=bar, **z: 0")
54 self.check_expr("lambda foo=bar, blaz=blat+2, **z: 0")
55 self.check_expr("lambda foo=bar, blaz=blat+2, *y, **z: 0")
56 self.check_expr("lambda x, *y, **z: 0")
Fred Drake79ca79d2000-08-21 22:30:53 +000057
Fred Drake58422e52001-06-04 03:56:24 +000058 def test_print(self):
59 self.check_suite("print")
60 self.check_suite("print 1")
61 self.check_suite("print 1,")
62 self.check_suite("print >>fp")
63 self.check_suite("print >>fp, 1")
64 self.check_suite("print >>fp, 1,")
Fred Drake79ca79d2000-08-21 22:30:53 +000065
Fred Drake58422e52001-06-04 03:56:24 +000066 def test_simple_expression(self):
67 # expr_stmt
68 self.check_suite("a")
Fred Drake79ca79d2000-08-21 22:30:53 +000069
Fred Drake58422e52001-06-04 03:56:24 +000070 def test_simple_assignments(self):
71 self.check_suite("a = b")
72 self.check_suite("a = b = c = d = e")
Fred Drake28f739a2000-08-25 22:42:40 +000073
Fred Drake58422e52001-06-04 03:56:24 +000074 def test_simple_augmented_assignments(self):
75 self.check_suite("a += b")
76 self.check_suite("a -= b")
77 self.check_suite("a *= b")
78 self.check_suite("a /= b")
79 self.check_suite("a %= b")
80 self.check_suite("a &= b")
81 self.check_suite("a |= b")
82 self.check_suite("a ^= b")
83 self.check_suite("a <<= b")
84 self.check_suite("a >>= b")
85 self.check_suite("a **= b")
Fred Drakee3fb18c2001-01-07 06:02:19 +000086
Fred Drake58422e52001-06-04 03:56:24 +000087 def test_function_defs(self):
88 self.check_suite("def f(): pass")
89 self.check_suite("def f(*args): pass")
90 self.check_suite("def f(*args, **kw): pass")
91 self.check_suite("def f(**kw): pass")
92 self.check_suite("def f(foo=bar): pass")
93 self.check_suite("def f(foo=bar, *args): pass")
94 self.check_suite("def f(foo=bar, *args, **kw): pass")
95 self.check_suite("def f(foo=bar, **kw): pass")
Fred Drakee3fb18c2001-01-07 06:02:19 +000096
Fred Drake58422e52001-06-04 03:56:24 +000097 self.check_suite("def f(a, b): pass")
98 self.check_suite("def f(a, b, *args): pass")
99 self.check_suite("def f(a, b, *args, **kw): pass")
100 self.check_suite("def f(a, b, **kw): pass")
101 self.check_suite("def f(a, b, foo=bar): pass")
102 self.check_suite("def f(a, b, foo=bar, *args): pass")
103 self.check_suite("def f(a, b, foo=bar, *args, **kw): pass")
104 self.check_suite("def f(a, b, foo=bar, **kw): pass")
Fred Drakee3fb18c2001-01-07 06:02:19 +0000105
Fred Drake58422e52001-06-04 03:56:24 +0000106 def test_import_from_statement(self):
107 self.check_suite("from sys.path import *")
108 self.check_suite("from sys.path import dirname")
109 self.check_suite("from sys.path import dirname as my_dirname")
110 self.check_suite("from sys.path import dirname, basename")
111 self.check_suite(
112 "from sys.path import dirname as my_dirname, basename")
113 self.check_suite(
114 "from sys.path import dirname, basename as my_basename")
Fred Drakee3fb18c2001-01-07 06:02:19 +0000115
Fred Drake58422e52001-06-04 03:56:24 +0000116 def test_basic_import_statement(self):
117 self.check_suite("import sys")
118 self.check_suite("import sys as system")
119 self.check_suite("import sys, math")
120 self.check_suite("import sys as system, math")
121 self.check_suite("import sys, math as my_math")
Fred Drake79ca79d2000-08-21 22:30:53 +0000122
123#
124# Second, we take *invalid* trees and make sure we get ParserError
125# rejections for them.
126#
127
Fred Drake58422e52001-06-04 03:56:24 +0000128class IllegalSyntaxTestCase(unittest.TestCase):
129 def check_bad_tree(self, tree, label):
130 try:
131 parser.sequence2ast(tree)
132 except parser.ParserError:
133 pass
134 else:
135 self.fail("did not detect invalid tree for %r" % label)
Fred Drake79ca79d2000-08-21 22:30:53 +0000136
Fred Drake58422e52001-06-04 03:56:24 +0000137 def test_junk(self):
138 # not even remotely valid:
139 self.check_bad_tree((1, 2, 3), "<junk>")
140
141 def test_print_chevron_comma(self):
142 "Illegal input: print >>fp,"""
143 tree = \
144 (257,
145 (264,
146 (265,
147 (266,
148 (268,
149 (1, 'print'),
150 (35, '>>'),
151 (290,
152 (291,
153 (292,
154 (293,
155 (295,
156 (296,
157 (297,
158 (298, (299, (300, (301, (302, (303, (1, 'fp')))))))))))))),
159 (12, ','))),
160 (4, ''))),
161 (0, ''))
162 self.check_bad_tree(tree, "print >>fp,")
163
164 def test_a_comma_comma_c(self):
165 """Illegal input: a,,c"""
166 tree = \
167 (258,
168 (311,
169 (290,
170 (291,
171 (292,
172 (293,
173 (295,
174 (296,
175 (297,
176 (298, (299, (300, (301, (302, (303, (1, 'a')))))))))))))),
177 (12, ','),
178 (12, ','),
179 (290,
180 (291,
181 (292,
182 (293,
183 (295,
184 (296,
185 (297,
186 (298, (299, (300, (301, (302, (303, (1, 'c'))))))))))))))),
187 (4, ''),
188 (0, ''))
189 self.check_bad_tree(tree, "a,,c")
190
191 def test_illegal_operator(self):
192 """Illegal input: a $= b"""
193 tree = \
194 (257,
195 (264,
196 (265,
197 (266,
198 (267,
199 (312,
200 (291,
201 (292,
202 (293,
203 (294,
204 (296,
205 (297,
206 (298,
207 (299,
208 (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,
219 (300, (301, (302, (303, (304, (1, 'b'))))))))))))))))),
220 (4, ''))),
221 (0, ''))
222 self.check_bad_tree(tree, "a $= b")
Fred Drake79ca79d2000-08-21 22:30:53 +0000223
224
Fred Drake58422e52001-06-04 03:56:24 +0000225test_support.run_unittest(RoundtripLegalSyntaxTestCase)
226test_support.run_unittest(IllegalSyntaxTestCase)