blob: 80e8d78c18f9f11d37c05b13f917fae31759f12a [file] [log] [blame]
Tim Peters400cbc32006-02-28 18:44:41 +00001import sys, itertools
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002import _ast
Tim Peters400cbc32006-02-28 18:44:41 +00003
4def to_tuple(t):
5 if t is None or isinstance(t, (basestring, int, long, complex)):
6 return t
7 elif isinstance(t, list):
8 return [to_tuple(e) for e in t]
9 result = [t.__class__.__name__]
Martin v. Löwis49c5da12006-03-01 22:49:05 +000010 if hasattr(t, 'lineno') and hasattr(t, 'col_offset'):
11 result.append((t.lineno, t.col_offset))
Tim Peters400cbc32006-02-28 18:44:41 +000012 if t._fields is None:
13 return tuple(result)
14 for f in t._fields:
15 result.append(to_tuple(getattr(t, f)))
16 return tuple(result)
17
18# These tests are compiled through "exec"
19# There should be atleast one test per statement
20exec_tests = [
21 # FunctionDef
22 "def f(): pass",
23 # ClassDef
24 "class C:pass",
25 # Return
26 "def f():return 1",
27 # Delete
28 "del v",
29 # Assign
30 "v = 1",
31 # AugAssign
32 "v += 1",
33 # Print
34 "print >>f, 1, ",
35 # For
36 "for v in v:pass",
37 # While
38 "while v:pass",
39 # If
40 "if v:pass",
41 # Raise
42 "raise Exception, 'string'",
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",
53 # Exec
54 "exec 'v'",
55 # Global
56 "global v",
57 # Expr
58 "1",
59 # Pass,
60 "pass",
61 # Break
62 "break",
63 # Continue
64 "continue",
65]
66
67# These are compiled through "single"
68# because of overlap with "eval", it just tests what
69# can't be tested with "eval"
70single_tests = [
71 "1+2"
72]
73
74# These are compiled through "eval"
75# It should test all expressions
76eval_tests = [
77 # BoolOp
78 "a and b",
79 # BinOp
80 "a + b",
81 # UnaryOp
82 "not v",
83 # Lambda
84 "lambda:None",
85 # Dict
86 "{ 1:2 }",
87 # ListComp
88 "[a for b in c if d]",
89 # GeneratorExp
90 "(a for b in c if d)",
91 # Yield - yield expressions can't work outside a function
92 #
93 # Compare
94 "1 < 2 < 3",
95 # Call
96 "f(1,2,c=3,*d,**e)",
Tim Peters400cbc32006-02-28 18:44:41 +000097 # Num
98 "10L",
99 # Str
100 "'string'",
101 # Attribute
102 "a.b",
103 # Subscript
104 "a[b:c]",
105 # Name
106 "v",
107 # List
108 "[1,2,3]",
109 # Tuple
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000110 "1,2,3",
111 # Combination
112 "a.b.c.d(a.b[1:2])",
113
Tim Peters400cbc32006-02-28 18:44:41 +0000114]
115
116# TODO: expr_context, slice, boolop, operator, unaryop, cmpop, comprehension
117# excepthandler, arguments, keywords, alias
118
119if __name__=='__main__' and sys.argv[1:] == ['-g']:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000120 for statements, kind in ((exec_tests, "exec"), (single_tests, "single"),
121 (eval_tests, "eval")):
Tim Peters400cbc32006-02-28 18:44:41 +0000122 print kind+"_results = ["
123 for s in statements:
124 print repr(to_tuple(compile(s, "?", kind, 0x400)))+","
125 print "]"
126 print "run_tests()"
127 raise SystemExit
128
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000129def test_order(ast_node, parent_pos):
Tim Peters5ddfe412006-03-01 23:02:57 +0000130
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000131 if not isinstance(ast_node, _ast.AST) or ast_node._fields == None:
132 return
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000133 if isinstance(ast_node, (_ast.expr, _ast.stmt, _ast.excepthandler)):
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000134 node_pos = (ast_node.lineno, ast_node.col_offset)
135 assert node_pos >= parent_pos, (node_pos, parent_pos)
136 parent_pos = (ast_node.lineno, ast_node.col_offset)
137 for name in ast_node._fields:
138 value = getattr(ast_node, name)
139 if isinstance(value, list):
140 for child in value:
141 test_order(child, parent_pos)
142 elif value != None:
143 test_order(value, parent_pos)
144
Tim Peters400cbc32006-02-28 18:44:41 +0000145def run_tests():
146 for input, output, kind in ((exec_tests, exec_results, "exec"),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000147 (single_tests, single_results, "single"),
148 (eval_tests, eval_results, "eval")):
Tim Peters400cbc32006-02-28 18:44:41 +0000149 for i, o in itertools.izip(input, output):
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000150 ast_tree = compile(i, "?", kind, 0x400)
151 assert to_tuple(ast_tree) == o
152 test_order(ast_tree, (0, 0))
Tim Peters400cbc32006-02-28 18:44:41 +0000153
154#### EVERYTHING BELOW IS GENERATED #####
155exec_results = [
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000156('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], None, None, []), [('Pass', (1, 9))], [])]),
157('Module', [('ClassDef', (1, 0), 'C', [], [('Pass', (1, 8))])]),
158('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], None, None, []), [('Return', (1, 8), ('Num', (1, 15), 1))], [])]),
159('Module', [('Delete', (1, 0), [('Name', (1, 4), 'v', ('Del',))])]),
160('Module', [('Assign', (1, 0), [('Name', (1, 0), 'v', ('Store',))], ('Num', (1, 4), 1))]),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000161('Module', [('AugAssign', (1, 0), ('Name', (1, 0), 'v', ('Store',)), ('Add',), ('Num', (1, 5), 1))]),
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000162('Module', [('Print', (1, 0), ('Name', (1, 8), 'f', ('Load',)), [('Num', (1, 11), 1)], False)]),
163('Module', [('For', (1, 0), ('Name', (1, 4), 'v', ('Store',)), ('Name', (1, 9), 'v', ('Load',)), [('Pass', (1, 11))], [])]),
164('Module', [('While', (1, 0), ('Name', (1, 6), 'v', ('Load',)), [('Pass', (1, 8))], [])]),
165('Module', [('If', (1, 0), ('Name', (1, 3), 'v', ('Load',)), [('Pass', (1, 5))], [])]),
166('Module', [('Raise', (1, 0), ('Name', (1, 6), 'Exception', ('Load',)), ('Str', (1, 17), 'string'), None)]),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000167('Module', [('TryExcept', (1, 0), [('Pass', (2, 2))], [('excepthandler', (3, 0), ('Name', (3, 7), 'Exception', ('Load',)), None, [('Pass', (4, 2))], 3, 0)], [])]),
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000168('Module', [('TryFinally', (1, 0), [('Pass', (2, 2))], [('Pass', (4, 2))])]),
169('Module', [('Assert', (1, 0), ('Name', (1, 7), 'v', ('Load',)), None)]),
170('Module', [('Import', (1, 0), [('alias', 'sys', None)])]),
171('Module', [('ImportFrom', (1, 0), 'sys', [('alias', 'v', None)], 0)]),
172('Module', [('Exec', (1, 0), ('Str', (1, 5), 'v'), None, None)]),
173('Module', [('Global', (1, 0), ['v'])]),
174('Module', [('Expr', (1, 0), ('Num', (1, 0), 1))]),
175('Module', [('Pass', (1, 0))]),
176('Module', [('Break', (1, 0))]),
177('Module', [('Continue', (1, 0))]),
Tim Peters400cbc32006-02-28 18:44:41 +0000178]
179single_results = [
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000180('Interactive', [('Expr', (1, 0), ('BinOp', (1, 0), ('Num', (1, 0), 1), ('Add',), ('Num', (1, 2), 2)))]),
Tim Peters400cbc32006-02-28 18:44:41 +0000181]
182eval_results = [
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000183('Expression', ('BoolOp', (1, 0), ('And',), [('Name', (1, 0), 'a', ('Load',)), ('Name', (1, 6), 'b', ('Load',))])),
184('Expression', ('BinOp', (1, 0), ('Name', (1, 0), 'a', ('Load',)), ('Add',), ('Name', (1, 4), 'b', ('Load',)))),
185('Expression', ('UnaryOp', (1, 0), ('Not',), ('Name', (1, 4), 'v', ('Load',)))),
186('Expression', ('Lambda', (1, 0), ('arguments', [], None, None, []), ('Name', (1, 7), 'None', ('Load',)))),
187('Expression', ('Dict', (1, 0), [('Num', (1, 2), 1)], [('Num', (1, 4), 2)])),
188('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',))])])),
189('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',))])])),
190('Expression', ('Compare', (1, 0), ('Num', (1, 0), 1), [('Lt',), ('Lt',)], [('Num', (1, 4), 2), ('Num', (1, 8), 3)])),
191('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',)))),
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000192('Expression', ('Num', (1, 0), 10L)),
193('Expression', ('Str', (1, 0), 'string')),
194('Expression', ('Attribute', (1, 0), ('Name', (1, 0), 'a', ('Load',)), 'b', ('Load',))),
195('Expression', ('Subscript', (1, 0), ('Name', (1, 0), 'a', ('Load',)), ('Slice', ('Name', (1, 2), 'b', ('Load',)), ('Name', (1, 4), 'c', ('Load',)), None), ('Load',))),
196('Expression', ('Name', (1, 0), 'v', ('Load',))),
197('Expression', ('List', (1, 0), [('Num', (1, 1), 1), ('Num', (1, 3), 2), ('Num', (1, 5), 3)], ('Load',))),
198('Expression', ('Tuple', (1, 0), [('Num', (1, 0), 1), ('Num', (1, 2), 2), ('Num', (1, 4), 3)], ('Load',))),
199('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 Peters400cbc32006-02-28 18:44:41 +0000200]
201run_tests()