blob: a290812a10fd9da0175531f1e09b9ae8d2bb837a [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.
Jeremy Hylton4336eda2004-08-07 19:25:33 +000015 libdir = os.path.dirname(unittest.__file__)
16 testdir = os.path.dirname(test.test_support.__file__)
17
18 for dir in [libdir, testdir]:
Tim Peters2a5f6562004-08-08 16:37:37 +000019 for basename in os.listdir(dir):
20 if not basename.endswith(".py"):
Jeremy Hylton4336eda2004-08-07 19:25:33 +000021 continue
Raymond Hettingered20ad82004-09-04 20:09:13 +000022 if not TEST_ALL and random() < 0.98:
23 continue
Tim Peters2a5f6562004-08-08 16:37:37 +000024 path = os.path.join(dir, basename)
Tim Petersb6ecc162004-08-08 16:32:54 +000025 if test.test_support.verbose:
Tim Peters2a5f6562004-08-08 16:37:37 +000026 print "compiling", path
Michael W. Hudson29589a02004-10-11 15:34:31 +000027 f = open(path, "U")
Jeremy Hylton4336eda2004-08-07 19:25:33 +000028 buf = f.read()
29 f.close()
Neal Norwitzf8950652005-10-24 00:01:37 +000030 if "badsyntax" in basename or "bad_coding" in basename:
Tim Peters0955f292004-08-08 16:43:59 +000031 self.assertRaises(SyntaxError, compiler.compile,
32 buf, basename, "exec")
33 else:
34 compiler.compile(buf, basename, "exec")
Jeremy Hylton4336eda2004-08-07 19:25:33 +000035
Brett Cannonf4189912005-04-09 02:30:16 +000036 def testNewClassSyntax(self):
37 compiler.compile("class foo():pass\n\n","<string>","exec")
Georg Brandl39cdfff2006-01-06 19:28:15 +000038
39 def testSyntaxErrors(self):
40 self.assertRaises(SyntaxError, compiler.compile,
41 "def foo(a=1,b):pass\n\n", "<string>", "exec")
Tim Peterse8906822005-04-20 17:45:13 +000042
Jeremy Hylton566d9342004-09-07 15:28:01 +000043 def testLineNo(self):
44 # Test that all nodes except Module have a correct lineno attribute.
45 filename = __file__
46 if filename.endswith(".pyc") or filename.endswith(".pyo"):
47 filename = filename[:-1]
48 tree = compiler.parseFile(filename)
49 self.check_lineno(tree)
50
51 def check_lineno(self, node):
52 try:
53 self._check_lineno(node)
54 except AssertionError:
55 print node.__class__, node.lineno
56 raise
57
58 def _check_lineno(self, node):
59 if not node.__class__ in NOLINENO:
Tim Peters0e9980f2004-09-12 03:49:31 +000060 self.assert_(isinstance(node.lineno, int),
Jeremy Hylton566d9342004-09-07 15:28:01 +000061 "lineno=%s on %s" % (node.lineno, node.__class__))
Tim Peters0e9980f2004-09-12 03:49:31 +000062 self.assert_(node.lineno > 0,
Jeremy Hylton566d9342004-09-07 15:28:01 +000063 "lineno=%s on %s" % (node.lineno, node.__class__))
64 for child in node.getChildNodes():
65 self.check_lineno(child)
66
Neil Schemenauerf3694702005-06-02 05:55:20 +000067 def testFlatten(self):
68 self.assertEquals(flatten([1, [2]]), [1, 2])
69 self.assertEquals(flatten((1, (2,))), [1, 2])
70
Jeremy Hylton566d9342004-09-07 15:28:01 +000071NOLINENO = (compiler.ast.Module, compiler.ast.Stmt, compiler.ast.Discard)
72
73###############################################################################
74# code below is just used to trigger some possible errors, for the benefit of
75# testLineNo
76###############################################################################
77
78class Toto:
79 """docstring"""
80 pass
81
82a, b = 2, 3
83[c, d] = 5, 6
84l = [(x, y) for x, y in zip(range(5), range(5,10))]
85l[0]
86l[3:4]
87if l:
88 pass
89else:
90 a, b = b, a
91
92try:
93 print yo
94except:
95 yo = 3
96else:
97 yo += 3
Tim Peters0e9980f2004-09-12 03:49:31 +000098
Jeremy Hylton566d9342004-09-07 15:28:01 +000099try:
100 a += b
101finally:
102 b = 0
Tim Peters0e9980f2004-09-12 03:49:31 +0000103
Michael W. Hudsone0b855f2004-11-08 16:46:02 +0000104from math import *
105
Jeremy Hylton566d9342004-09-07 15:28:01 +0000106###############################################################################
107
Jeremy Hylton4336eda2004-08-07 19:25:33 +0000108def test_main():
Raymond Hettingered20ad82004-09-04 20:09:13 +0000109 global TEST_ALL
110 TEST_ALL = test.test_support.is_resource_enabled("compiler")
Jeremy Hylton4336eda2004-08-07 19:25:33 +0000111 test.test_support.run_unittest(CompilerTest)
112
113if __name__ == "__main__":
114 test_main()