blob: 6ec71ed38fcd6a696454c47e37178877fafa018d [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:
Martin v. Löwisd9bfeac2006-03-01 23:24:34 +000038 args = list(e.args)
39 args[0] += "[in file %s]" % basename
40 e.args = tuple(args)
Martin v. Löwis15bfc3b2006-03-01 21:11:49 +000041 raise
Jeremy Hylton4336eda2004-08-07 19:25:33 +000042
Brett Cannonf4189912005-04-09 02:30:16 +000043 def testNewClassSyntax(self):
44 compiler.compile("class foo():pass\n\n","<string>","exec")
Tim Peterse8906822005-04-20 17:45:13 +000045
Jeremy Hylton566d9342004-09-07 15:28:01 +000046 def testLineNo(self):
47 # Test that all nodes except Module have a correct lineno attribute.
48 filename = __file__
49 if filename.endswith(".pyc") or filename.endswith(".pyo"):
50 filename = filename[:-1]
51 tree = compiler.parseFile(filename)
52 self.check_lineno(tree)
53
54 def check_lineno(self, node):
55 try:
56 self._check_lineno(node)
57 except AssertionError:
58 print node.__class__, node.lineno
59 raise
60
61 def _check_lineno(self, node):
62 if not node.__class__ in NOLINENO:
Tim Peters0e9980f2004-09-12 03:49:31 +000063 self.assert_(isinstance(node.lineno, int),
Jeremy Hylton566d9342004-09-07 15:28:01 +000064 "lineno=%s on %s" % (node.lineno, node.__class__))
Tim Peters0e9980f2004-09-12 03:49:31 +000065 self.assert_(node.lineno > 0,
Jeremy Hylton566d9342004-09-07 15:28:01 +000066 "lineno=%s on %s" % (node.lineno, node.__class__))
67 for child in node.getChildNodes():
68 self.check_lineno(child)
69
Neil Schemenauerf3694702005-06-02 05:55:20 +000070 def testFlatten(self):
71 self.assertEquals(flatten([1, [2]]), [1, 2])
72 self.assertEquals(flatten((1, (2,))), [1, 2])
73
Jeremy Hylton566d9342004-09-07 15:28:01 +000074NOLINENO = (compiler.ast.Module, compiler.ast.Stmt, compiler.ast.Discard)
75
76###############################################################################
77# code below is just used to trigger some possible errors, for the benefit of
78# testLineNo
79###############################################################################
80
81class Toto:
82 """docstring"""
83 pass
84
85a, b = 2, 3
86[c, d] = 5, 6
87l = [(x, y) for x, y in zip(range(5), range(5,10))]
88l[0]
89l[3:4]
90if l:
91 pass
92else:
93 a, b = b, a
94
95try:
96 print yo
97except:
98 yo = 3
99else:
100 yo += 3
Tim Peters0e9980f2004-09-12 03:49:31 +0000101
Jeremy Hylton566d9342004-09-07 15:28:01 +0000102try:
103 a += b
104finally:
105 b = 0
Tim Peters0e9980f2004-09-12 03:49:31 +0000106
Michael W. Hudsone0b855f2004-11-08 16:46:02 +0000107from math import *
108
Jeremy Hylton566d9342004-09-07 15:28:01 +0000109###############################################################################
110
Jeremy Hylton4336eda2004-08-07 19:25:33 +0000111def test_main():
Raymond Hettingered20ad82004-09-04 20:09:13 +0000112 global TEST_ALL
113 TEST_ALL = test.test_support.is_resource_enabled("compiler")
Jeremy Hylton4336eda2004-08-07 19:25:33 +0000114 test.test_support.run_unittest(CompilerTest)
115
116if __name__ == "__main__":
117 test_main()