6 from parser
import expr
, suite
, sequence2ast
7 from test_support
import verbose
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.
18 st2
= parser
.sequence2ast(t
)
20 def roundtrip_fromfile(filename
):
21 roundtrip(suite
, open(filename
).read())
35 test_expr("[1, 2, 3]")
36 test_expr("[x**3 for x in range(20)]")
37 test_expr("[x**3 for x in range(20) if x % 3]")
38 test_expr("foo(*args)")
39 test_expr("foo(*args, **kw)")
40 test_expr("foo(**kw)")
41 test_expr("foo(key=value)")
42 test_expr("foo(key=value, *args)")
43 test_expr("foo(key=value, *args, **kw)")
44 test_expr("foo(key=value, **kw)")
45 test_expr("foo(a, b, c, *args)")
46 test_expr("foo(a, b, c, *args, **kw)")
47 test_expr("foo(a, b, c, **kw)")
48 test_expr("foo + bar")
54 test_suite("print 1,")
55 test_suite("print >>fp")
56 test_suite("print >>fp, 1")
57 test_suite("print >>fp, 1,")
62 test_suite("a = b = c = d = e")
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"))
80 # Second, we take *invalid* trees and make sure we get ParserError
81 # rejections for them.
85 print "Invalid parse trees:"
87 def check_bad_tree(tree
, label
):
92 except parser
.ParserError
:
93 print "caught expected exception for invalid tree"
96 print "test failed: did not properly detect invalid tree:"
100 # not even remotely valid:
101 check_bad_tree((1, 2, 3), "<junk>")
119 (298, (299, (300, (301, (302, (303, (1, 'fp')))))))))))))),
124 check_bad_tree(tree
, "print >>fp,")
135 (296, (297, (298, (299, (300, (301, (302, (303, (1, 'a')))))))))))))),
143 (296, (297, (298, (299, (300, (301, (302, (303, (1, 'c'))))))))))))))),
147 check_bad_tree(tree
, "a,,c")
164 (299, (300, (301, (302, (303, (304, (1, 'a'))))))))))))))),
174 (299, (300, (301, (302, (303, (304, (1, 'b'))))))))))))))))),
178 check_bad_tree(tree
, "a $= b")