Fred Drake | 28f739a | 2000-08-25 22:42:40 +0000 | [diff] [blame] | 1 | import os.path |
Fred Drake | 79ca79d | 2000-08-21 22:30:53 +0000 | [diff] [blame] | 2 | import parser |
| 3 | import pprint |
| 4 | import sys |
| 5 | |
| 6 | from parser import expr, suite, sequence2ast |
| 7 | from 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 | |
| 15 | def roundtrip(f, s): |
Fred Drake | 79ca79d | 2000-08-21 22:30:53 +0000 | [diff] [blame] | 16 | st1 = f(s) |
| 17 | t = st1.totuple() |
| 18 | st2 = parser.sequence2ast(t) |
| 19 | |
Fred Drake | 28f739a | 2000-08-25 22:42:40 +0000 | [diff] [blame] | 20 | def roundtrip_fromfile(filename): |
| 21 | roundtrip(suite, open(filename).read()) |
| 22 | |
| 23 | def test_expr(s): |
| 24 | print "expr:", s |
| 25 | roundtrip(expr, s) |
| 26 | |
| 27 | def test_suite(s): |
| 28 | print "suite:", s |
| 29 | roundtrip(suite, s) |
| 30 | |
Fred Drake | 79ca79d | 2000-08-21 22:30:53 +0000 | [diff] [blame] | 31 | |
| 32 | print "Expressions:" |
| 33 | |
Fred Drake | 28f739a | 2000-08-25 22:42:40 +0000 | [diff] [blame] | 34 | test_expr("foo(1)") |
| 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") |
Fred Drake | 79ca79d | 2000-08-21 22:30:53 +0000 | [diff] [blame] | 49 | |
| 50 | print |
| 51 | print "Statements:" |
Fred Drake | 28f739a | 2000-08-25 22:42:40 +0000 | [diff] [blame] | 52 | test_suite("print") |
| 53 | test_suite("print 1") |
| 54 | test_suite("print 1,") |
| 55 | test_suite("print >>fp") |
| 56 | test_suite("print >>fp, 1") |
| 57 | test_suite("print >>fp, 1,") |
| 58 | |
| 59 | # expr_stmt |
| 60 | test_suite("a") |
| 61 | test_suite("a = b") |
| 62 | test_suite("a = b = c = d = e") |
| 63 | test_suite("a += b") |
| 64 | test_suite("a -= b") |
| 65 | test_suite("a *= b") |
| 66 | test_suite("a /= b") |
| 67 | test_suite("a %= b") |
| 68 | test_suite("a &= b") |
| 69 | test_suite("a |= b") |
| 70 | test_suite("a ^= b") |
| 71 | test_suite("a <<= b") |
| 72 | test_suite("a >>= b") |
| 73 | test_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 Drake | 79ca79d | 2000-08-21 22:30:53 +0000 | [diff] [blame] | 78 | |
| 79 | # |
| 80 | # Second, we take *invalid* trees and make sure we get ParserError |
| 81 | # rejections for them. |
| 82 | # |
| 83 | |
| 84 | print |
| 85 | print "Invalid parse trees:" |
| 86 | |
| 87 | def 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: |
| 101 | check_bad_tree((1, 2, 3), "<junk>") |
| 102 | |
| 103 | # print >>fp, |
| 104 | tree = \ |
| 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 | |
| 124 | check_bad_tree(tree, "print >>fp,") |
| 125 | |
| 126 | # a,,c |
| 127 | tree = \ |
| 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 | |
| 147 | check_bad_tree(tree, "a,,c") |
Fred Drake | 28f739a | 2000-08-25 22:42:40 +0000 | [diff] [blame] | 148 | |
| 149 | # a $= b |
| 150 | tree = \ |
| 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 | |
| 178 | check_bad_tree(tree, "a $= b") |