blob: f58e6e5273f4f89743f8e74f53efd25b9f8b9e17 [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
Guido van Rossum7ad94f02006-02-28 00:32:16 +000023 if not TEST_ALL and random() < 0.98 and basename != "test_with.py":
Raymond Hettingered20ad82004-09-04 20:09:13 +000024 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:
Martin v. Löwis15bfc3b2006-03-01 21:11:49 +000035 try:
36 compiler.compile(buf, basename, "exec")
37 except Exception, e:
38 e.args[0] += "[in file %s]" % basename
39 raise
Jeremy Hylton4336eda2004-08-07 19:25:33 +000040
Brett Cannonf4189912005-04-09 02:30:16 +000041 def testNewClassSyntax(self):
42 compiler.compile("class foo():pass\n\n","<string>","exec")
Tim Peterse8906822005-04-20 17:45:13 +000043
Jeremy Hylton566d9342004-09-07 15:28:01 +000044 def testLineNo(self):
45 # Test that all nodes except Module have a correct lineno attribute.
46 filename = __file__
47 if filename.endswith(".pyc") or filename.endswith(".pyo"):
48 filename = filename[:-1]
49 tree = compiler.parseFile(filename)
50 self.check_lineno(tree)
51
52 def check_lineno(self, node):
53 try:
54 self._check_lineno(node)
55 except AssertionError:
56 print node.__class__, node.lineno
57 raise
58
59 def _check_lineno(self, node):
60 if not node.__class__ in NOLINENO:
Tim Peters0e9980f2004-09-12 03:49:31 +000061 self.assert_(isinstance(node.lineno, int),
Jeremy Hylton566d9342004-09-07 15:28:01 +000062 "lineno=%s on %s" % (node.lineno, node.__class__))
Tim Peters0e9980f2004-09-12 03:49:31 +000063 self.assert_(node.lineno > 0,
Jeremy Hylton566d9342004-09-07 15:28:01 +000064 "lineno=%s on %s" % (node.lineno, node.__class__))
65 for child in node.getChildNodes():
66 self.check_lineno(child)
67
Neil Schemenauerf3694702005-06-02 05:55:20 +000068 def testFlatten(self):
69 self.assertEquals(flatten([1, [2]]), [1, 2])
70 self.assertEquals(flatten((1, (2,))), [1, 2])
71
Jeremy Hylton566d9342004-09-07 15:28:01 +000072NOLINENO = (compiler.ast.Module, compiler.ast.Stmt, compiler.ast.Discard)
73
74###############################################################################
75# code below is just used to trigger some possible errors, for the benefit of
76# testLineNo
77###############################################################################
78
79class Toto:
80 """docstring"""
81 pass
82
83a, b = 2, 3
84[c, d] = 5, 6
85l = [(x, y) for x, y in zip(range(5), range(5,10))]
86l[0]
87l[3:4]
88if l:
89 pass
90else:
91 a, b = b, a
92
93try:
94 print yo
95except:
96 yo = 3
97else:
98 yo += 3
Tim Peters0e9980f2004-09-12 03:49:31 +000099
Jeremy Hylton566d9342004-09-07 15:28:01 +0000100try:
101 a += b
102finally:
103 b = 0
Tim Peters0e9980f2004-09-12 03:49:31 +0000104
Michael W. Hudsone0b855f2004-11-08 16:46:02 +0000105from math import *
106
Jeremy Hylton566d9342004-09-07 15:28:01 +0000107###############################################################################
108
Jeremy Hylton4336eda2004-08-07 19:25:33 +0000109def test_main():
Raymond Hettingered20ad82004-09-04 20:09:13 +0000110 global TEST_ALL
111 TEST_ALL = test.test_support.is_resource_enabled("compiler")
Jeremy Hylton4336eda2004-08-07 19:25:33 +0000112 test.test_support.run_unittest(CompilerTest)
113
114if __name__ == "__main__":
115 test_main()