Neal Norwitz | ee9b10a | 2008-03-31 05:29:39 +0000 | [diff] [blame] | 1 | import sys, unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 2 | from test import support |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3 | import _ast |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 4 | |
| 5 | def to_tuple(t): |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 6 | if t is None or isinstance(t, (str, int, complex)): |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 7 | return t |
| 8 | elif isinstance(t, list): |
| 9 | return [to_tuple(e) for e in t] |
| 10 | result = [t.__class__.__name__] |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 11 | if hasattr(t, 'lineno') and hasattr(t, 'col_offset'): |
| 12 | result.append((t.lineno, t.col_offset)) |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 13 | if t._fields is None: |
| 14 | return tuple(result) |
| 15 | for f in t._fields: |
| 16 | result.append(to_tuple(getattr(t, f))) |
| 17 | return tuple(result) |
| 18 | |
Neal Norwitz | ee9b10a | 2008-03-31 05:29:39 +0000 | [diff] [blame] | 19 | |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 20 | # These tests are compiled through "exec" |
| 21 | # There should be atleast one test per statement |
| 22 | exec_tests = [ |
| 23 | # FunctionDef |
| 24 | "def f(): pass", |
| 25 | # ClassDef |
| 26 | "class C:pass", |
| 27 | # Return |
| 28 | "def f():return 1", |
| 29 | # Delete |
| 30 | "del v", |
| 31 | # Assign |
| 32 | "v = 1", |
| 33 | # AugAssign |
| 34 | "v += 1", |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 35 | # For |
| 36 | "for v in v:pass", |
| 37 | # While |
| 38 | "while v:pass", |
| 39 | # If |
| 40 | "if v:pass", |
| 41 | # Raise |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 42 | "raise Exception('string')", |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 43 | # TryExcept |
| 44 | "try:\n pass\nexcept Exception:\n pass", |
| 45 | # TryFinally |
| 46 | "try:\n pass\nfinally:\n pass", |
| 47 | # Assert |
| 48 | "assert v", |
| 49 | # Import |
| 50 | "import sys", |
| 51 | # ImportFrom |
| 52 | "from sys import v", |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 53 | # Global |
| 54 | "global v", |
| 55 | # Expr |
| 56 | "1", |
| 57 | # Pass, |
| 58 | "pass", |
| 59 | # Break |
| 60 | "break", |
| 61 | # Continue |
| 62 | "continue", |
| 63 | ] |
| 64 | |
| 65 | # These are compiled through "single" |
| 66 | # because of overlap with "eval", it just tests what |
| 67 | # can't be tested with "eval" |
| 68 | single_tests = [ |
| 69 | "1+2" |
| 70 | ] |
| 71 | |
| 72 | # These are compiled through "eval" |
| 73 | # It should test all expressions |
| 74 | eval_tests = [ |
| 75 | # BoolOp |
| 76 | "a and b", |
| 77 | # BinOp |
| 78 | "a + b", |
| 79 | # UnaryOp |
| 80 | "not v", |
| 81 | # Lambda |
| 82 | "lambda:None", |
| 83 | # Dict |
| 84 | "{ 1:2 }", |
| 85 | # ListComp |
| 86 | "[a for b in c if d]", |
| 87 | # GeneratorExp |
| 88 | "(a for b in c if d)", |
| 89 | # Yield - yield expressions can't work outside a function |
| 90 | # |
| 91 | # Compare |
| 92 | "1 < 2 < 3", |
| 93 | # Call |
| 94 | "f(1,2,c=3,*d,**e)", |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 95 | # Num |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 96 | "10", |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 97 | # Str |
| 98 | "'string'", |
| 99 | # Attribute |
| 100 | "a.b", |
| 101 | # Subscript |
| 102 | "a[b:c]", |
| 103 | # Name |
| 104 | "v", |
| 105 | # List |
| 106 | "[1,2,3]", |
| 107 | # Tuple |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 108 | "1,2,3", |
| 109 | # Combination |
| 110 | "a.b.c.d(a.b[1:2])", |
| 111 | |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 112 | ] |
| 113 | |
| 114 | # TODO: expr_context, slice, boolop, operator, unaryop, cmpop, comprehension |
| 115 | # excepthandler, arguments, keywords, alias |
| 116 | |
Neal Norwitz | ee9b10a | 2008-03-31 05:29:39 +0000 | [diff] [blame] | 117 | class AST_Tests(unittest.TestCase): |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 118 | |
Neal Norwitz | ee9b10a | 2008-03-31 05:29:39 +0000 | [diff] [blame] | 119 | def _assert_order(self, ast_node, parent_pos): |
| 120 | if not isinstance(ast_node, _ast.AST) or ast_node._fields is None: |
| 121 | return |
| 122 | if isinstance(ast_node, (_ast.expr, _ast.stmt, _ast.excepthandler)): |
| 123 | node_pos = (ast_node.lineno, ast_node.col_offset) |
| 124 | self.assert_(node_pos >= parent_pos) |
| 125 | parent_pos = (ast_node.lineno, ast_node.col_offset) |
| 126 | for name in ast_node._fields: |
| 127 | value = getattr(ast_node, name) |
| 128 | if isinstance(value, list): |
| 129 | for child in value: |
| 130 | self._assert_order(child, parent_pos) |
| 131 | elif value is not None: |
| 132 | self._assert_order(value, parent_pos) |
Tim Peters | 5ddfe41 | 2006-03-01 23:02:57 +0000 | [diff] [blame] | 133 | |
Neal Norwitz | ee9b10a | 2008-03-31 05:29:39 +0000 | [diff] [blame] | 134 | def test_snippets(self): |
| 135 | for input, output, kind in ((exec_tests, exec_results, "exec"), |
| 136 | (single_tests, single_results, "single"), |
| 137 | (eval_tests, eval_results, "eval")): |
| 138 | for i, o in zip(input, output): |
| 139 | ast_tree = compile(i, "?", kind, _ast.PyCF_ONLY_AST) |
| 140 | self.assertEquals(to_tuple(ast_tree), o) |
| 141 | self._assert_order(ast_tree, (0, 0)) |
| 142 | |
| 143 | def test_nodeclasses(self): |
| 144 | x = _ast.BinOp(1, 2, 3, lineno=0) |
| 145 | self.assertEquals(x.left, 1) |
| 146 | self.assertEquals(x.op, 2) |
| 147 | self.assertEquals(x.right, 3) |
| 148 | self.assertEquals(x.lineno, 0) |
| 149 | |
| 150 | # node raises exception when not given enough arguments |
| 151 | self.assertRaises(TypeError, _ast.BinOp, 1, 2) |
| 152 | |
| 153 | # can set attributes through kwargs too |
| 154 | x = _ast.BinOp(left=1, op=2, right=3, lineno=0) |
| 155 | self.assertEquals(x.left, 1) |
| 156 | self.assertEquals(x.op, 2) |
| 157 | self.assertEquals(x.right, 3) |
| 158 | self.assertEquals(x.lineno, 0) |
| 159 | |
| 160 | # this used to fail because Sub._fields was None |
| 161 | x = _ast.Sub() |
| 162 | |
| 163 | def test_pickling(self): |
| 164 | import pickle |
| 165 | mods = [pickle] |
| 166 | try: |
| 167 | import cPickle |
| 168 | mods.append(cPickle) |
| 169 | except ImportError: |
| 170 | pass |
| 171 | protocols = [0, 1, 2] |
| 172 | for mod in mods: |
| 173 | for protocol in protocols: |
| 174 | for ast in (compile(i, "?", "exec", 0x400) for i in exec_tests): |
| 175 | ast2 = mod.loads(mod.dumps(ast, protocol)) |
| 176 | self.assertEquals(to_tuple(ast2), to_tuple(ast)) |
| 177 | |
| 178 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 179 | support.run_unittest(AST_Tests) |
Neal Norwitz | ee9b10a | 2008-03-31 05:29:39 +0000 | [diff] [blame] | 180 | |
| 181 | def main(): |
| 182 | if __name__ != '__main__': |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 183 | return |
Neal Norwitz | ee9b10a | 2008-03-31 05:29:39 +0000 | [diff] [blame] | 184 | if sys.argv[1:] == ['-g']: |
| 185 | for statements, kind in ((exec_tests, "exec"), (single_tests, "single"), |
| 186 | (eval_tests, "eval")): |
| 187 | print(kind+"_results = [") |
| 188 | for s in statements: |
| 189 | print(repr(to_tuple(compile(s, "?", kind, 0x400)))+",") |
| 190 | print("]") |
| 191 | print("main()") |
| 192 | raise SystemExit |
| 193 | test_main() |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 194 | |
| 195 | #### EVERYTHING BELOW IS GENERATED ##### |
| 196 | exec_results = [ |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 197 | ('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], None, None, [], None, None, [], []), [('Pass', (1, 9))], [], None)]), |
Neal Norwitz | ad74aa8 | 2008-03-31 05:14:30 +0000 | [diff] [blame] | 198 | ('Module', [('ClassDef', (1, 0), 'C', [], [], None, None, [('Pass', (1, 8))], [])]), |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 199 | ('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], None, None, [], None, None, [], []), [('Return', (1, 8), ('Num', (1, 15), 1))], [], None)]), |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 200 | ('Module', [('Delete', (1, 0), [('Name', (1, 4), 'v', ('Del',))])]), |
| 201 | ('Module', [('Assign', (1, 0), [('Name', (1, 0), 'v', ('Store',))], ('Num', (1, 4), 1))]), |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 202 | ('Module', [('AugAssign', (1, 0), ('Name', (1, 0), 'v', ('Store',)), ('Add',), ('Num', (1, 5), 1))]), |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 203 | ('Module', [('For', (1, 0), ('Name', (1, 4), 'v', ('Store',)), ('Name', (1, 9), 'v', ('Load',)), [('Pass', (1, 11))], [])]), |
| 204 | ('Module', [('While', (1, 0), ('Name', (1, 6), 'v', ('Load',)), [('Pass', (1, 8))], [])]), |
| 205 | ('Module', [('If', (1, 0), ('Name', (1, 3), 'v', ('Load',)), [('Pass', (1, 5))], [])]), |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 206 | ('Module', [('Raise', (1, 0), ('Call', (1, 6), ('Name', (1, 6), 'Exception', ('Load',)), [('Str', (1, 16), 'string')], [], None, None), None)]), |
Neal Norwitz | ad74aa8 | 2008-03-31 05:14:30 +0000 | [diff] [blame] | 207 | ('Module', [('TryExcept', (1, 0), [('Pass', (2, 2))], [('ExceptHandler', (3, 0), ('Name', (3, 7), 'Exception', ('Load',)), None, [('Pass', (4, 2))])], [])]), |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 208 | ('Module', [('TryFinally', (1, 0), [('Pass', (2, 2))], [('Pass', (4, 2))])]), |
| 209 | ('Module', [('Assert', (1, 0), ('Name', (1, 7), 'v', ('Load',)), None)]), |
| 210 | ('Module', [('Import', (1, 0), [('alias', 'sys', None)])]), |
| 211 | ('Module', [('ImportFrom', (1, 0), 'sys', [('alias', 'v', None)], 0)]), |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 212 | ('Module', [('Global', (1, 0), ['v'])]), |
| 213 | ('Module', [('Expr', (1, 0), ('Num', (1, 0), 1))]), |
| 214 | ('Module', [('Pass', (1, 0))]), |
| 215 | ('Module', [('Break', (1, 0))]), |
| 216 | ('Module', [('Continue', (1, 0))]), |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 217 | ] |
| 218 | single_results = [ |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 219 | ('Interactive', [('Expr', (1, 0), ('BinOp', (1, 0), ('Num', (1, 0), 1), ('Add',), ('Num', (1, 2), 2)))]), |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 220 | ] |
| 221 | eval_results = [ |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 222 | ('Expression', ('BoolOp', (1, 0), ('And',), [('Name', (1, 0), 'a', ('Load',)), ('Name', (1, 6), 'b', ('Load',))])), |
| 223 | ('Expression', ('BinOp', (1, 0), ('Name', (1, 0), 'a', ('Load',)), ('Add',), ('Name', (1, 4), 'b', ('Load',)))), |
| 224 | ('Expression', ('UnaryOp', (1, 0), ('Not',), ('Name', (1, 4), 'v', ('Load',)))), |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 225 | ('Expression', ('Lambda', (1, 0), ('arguments', [], None, None, [], None, None, [], []), ('Name', (1, 7), 'None', ('Load',)))), |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 226 | ('Expression', ('Dict', (1, 0), [('Num', (1, 2), 1)], [('Num', (1, 4), 2)])), |
| 227 | ('Expression', ('ListComp', (1, 1), ('Name', (1, 1), 'a', ('Load',)), [('comprehension', ('Name', (1, 7), 'b', ('Store',)), ('Name', (1, 12), 'c', ('Load',)), [('Name', (1, 17), 'd', ('Load',))])])), |
| 228 | ('Expression', ('GeneratorExp', (1, 1), ('Name', (1, 1), 'a', ('Load',)), [('comprehension', ('Name', (1, 7), 'b', ('Store',)), ('Name', (1, 12), 'c', ('Load',)), [('Name', (1, 17), 'd', ('Load',))])])), |
| 229 | ('Expression', ('Compare', (1, 0), ('Num', (1, 0), 1), [('Lt',), ('Lt',)], [('Num', (1, 4), 2), ('Num', (1, 8), 3)])), |
| 230 | ('Expression', ('Call', (1, 0), ('Name', (1, 0), 'f', ('Load',)), [('Num', (1, 2), 1), ('Num', (1, 4), 2)], [('keyword', 'c', ('Num', (1, 8), 3))], ('Name', (1, 11), 'd', ('Load',)), ('Name', (1, 15), 'e', ('Load',)))), |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 231 | ('Expression', ('Num', (1, 0), 10)), |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 232 | ('Expression', ('Str', (1, 0), 'string')), |
| 233 | ('Expression', ('Attribute', (1, 0), ('Name', (1, 0), 'a', ('Load',)), 'b', ('Load',))), |
| 234 | ('Expression', ('Subscript', (1, 0), ('Name', (1, 0), 'a', ('Load',)), ('Slice', ('Name', (1, 2), 'b', ('Load',)), ('Name', (1, 4), 'c', ('Load',)), None), ('Load',))), |
| 235 | ('Expression', ('Name', (1, 0), 'v', ('Load',))), |
| 236 | ('Expression', ('List', (1, 0), [('Num', (1, 1), 1), ('Num', (1, 3), 2), ('Num', (1, 5), 3)], ('Load',))), |
| 237 | ('Expression', ('Tuple', (1, 0), [('Num', (1, 0), 1), ('Num', (1, 2), 2), ('Num', (1, 4), 3)], ('Load',))), |
| 238 | ('Expression', ('Call', (1, 0), ('Attribute', (1, 0), ('Attribute', (1, 0), ('Attribute', (1, 0), ('Name', (1, 0), 'a', ('Load',)), 'b', ('Load',)), 'c', ('Load',)), 'd', ('Load',)), [('Subscript', (1, 8), ('Attribute', (1, 8), ('Name', (1, 8), 'a', ('Load',)), 'b', ('Load',)), ('Slice', ('Num', (1, 12), 1), ('Num', (1, 14), 2), None), ('Load',))], [], None, None)), |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 239 | ] |
Neal Norwitz | ee9b10a | 2008-03-31 05:29:39 +0000 | [diff] [blame] | 240 | main() |