blob: d2f062c928dfdc5cf49da9a5ded59efa994cb584 [file] [log] [blame]
Jeremy Hylton4336eda2004-08-07 19:25:33 +00001import compiler
Neil Schemenauerf3694702005-06-02 05:55:20 +00002from compiler.ast import flatten
Jeremy Hylton4336eda2004-08-07 19:25:33 +00003import os
4import test.test_support
5import unittest
Raymond Hettingered20ad82004-09-04 20:09:13 +00006from random import random
Jeremy Hylton4336eda2004-08-07 19:25:33 +00007
8class CompilerTest(unittest.TestCase):
9
10 def testCompileLibrary(self):
11 # A simple but large test. Compile all the code in the
12 # standard library and its test suite. This doesn't verify
13 # that any of the code is correct, merely the compiler is able
14 # to generate some kind of code for it.
Tim Peters2841af42006-01-07 23:20:46 +000015
Jeremy Hylton4336eda2004-08-07 19:25:33 +000016 libdir = os.path.dirname(unittest.__file__)
17 testdir = os.path.dirname(test.test_support.__file__)
18
19 for dir in [libdir, testdir]:
Tim Peters2a5f6562004-08-08 16:37:37 +000020 for basename in os.listdir(dir):
21 if not basename.endswith(".py"):
Jeremy Hylton4336eda2004-08-07 19:25:33 +000022 continue
Raymond Hettingered20ad82004-09-04 20:09:13 +000023 if not TEST_ALL and random() < 0.98:
24 continue
Tim Peters2a5f6562004-08-08 16:37:37 +000025 path = os.path.join(dir, basename)
Tim Petersb6ecc162004-08-08 16:32:54 +000026 if test.test_support.verbose:
Tim Peters2a5f6562004-08-08 16:37:37 +000027 print "compiling", path
Michael W. Hudson29589a02004-10-11 15:34:31 +000028 f = open(path, "U")
Jeremy Hylton4336eda2004-08-07 19:25:33 +000029 buf = f.read()
30 f.close()
Neal Norwitzf8950652005-10-24 00:01:37 +000031 if "badsyntax" in basename or "bad_coding" in basename:
Tim Peters0955f292004-08-08 16:43:59 +000032 self.assertRaises(SyntaxError, compiler.compile,
33 buf, basename, "exec")
34 else:
35 compiler.compile(buf, basename, "exec")
Jeremy Hylton4336eda2004-08-07 19:25:33 +000036
Brett Cannonf4189912005-04-09 02:30:16 +000037 def testNewClassSyntax(self):
38 compiler.compile("class foo():pass\n\n","<string>","exec")
Tim Peterse8906822005-04-20 17:45:13 +000039
Jeremy Hylton566d9342004-09-07 15:28:01 +000040 def testLineNo(self):
41 # Test that all nodes except Module have a correct lineno attribute.
42 filename = __file__
43 if filename.endswith(".pyc") or filename.endswith(".pyo"):
44 filename = filename[:-1]
45 tree = compiler.parseFile(filename)
46 self.check_lineno(tree)
47
48 def check_lineno(self, node):
49 try:
50 self._check_lineno(node)
51 except AssertionError:
52 print node.__class__, node.lineno
53 raise
54
55 def _check_lineno(self, node):
56 if not node.__class__ in NOLINENO:
Tim Peters0e9980f2004-09-12 03:49:31 +000057 self.assert_(isinstance(node.lineno, int),
Jeremy Hylton566d9342004-09-07 15:28:01 +000058 "lineno=%s on %s" % (node.lineno, node.__class__))
Tim Peters0e9980f2004-09-12 03:49:31 +000059 self.assert_(node.lineno > 0,
Jeremy Hylton566d9342004-09-07 15:28:01 +000060 "lineno=%s on %s" % (node.lineno, node.__class__))
61 for child in node.getChildNodes():
62 self.check_lineno(child)
63
Neil Schemenauerf3694702005-06-02 05:55:20 +000064 def testFlatten(self):
65 self.assertEquals(flatten([1, [2]]), [1, 2])
66 self.assertEquals(flatten((1, (2,))), [1, 2])
67
Jeremy Hylton566d9342004-09-07 15:28:01 +000068NOLINENO = (compiler.ast.Module, compiler.ast.Stmt, compiler.ast.Discard)
69
70###############################################################################
71# code below is just used to trigger some possible errors, for the benefit of
72# testLineNo
73###############################################################################
74
75class Toto:
76 """docstring"""
77 pass
78
79a, b = 2, 3
80[c, d] = 5, 6
81l = [(x, y) for x, y in zip(range(5), range(5,10))]
82l[0]
83l[3:4]
84if l:
85 pass
86else:
87 a, b = b, a
88
89try:
90 print yo
91except:
92 yo = 3
93else:
94 yo += 3
Tim Peters0e9980f2004-09-12 03:49:31 +000095
Jeremy Hylton566d9342004-09-07 15:28:01 +000096try:
97 a += b
98finally:
99 b = 0
Tim Peters0e9980f2004-09-12 03:49:31 +0000100
Michael W. Hudsone0b855f2004-11-08 16:46:02 +0000101from math import *
102
Jeremy Hylton566d9342004-09-07 15:28:01 +0000103###############################################################################
104
Jeremy Hylton4336eda2004-08-07 19:25:33 +0000105def test_main():
Raymond Hettingered20ad82004-09-04 20:09:13 +0000106 global TEST_ALL
107 TEST_ALL = test.test_support.is_resource_enabled("compiler")
Jeremy Hylton4336eda2004-08-07 19:25:33 +0000108 test.test_support.run_unittest(CompilerTest)
109
110if __name__ == "__main__":
111 test_main()