Jeremy Hylton | 4336eda | 2004-08-07 19:25:33 +0000 | [diff] [blame] | 1 | import compiler |
Neil Schemenauer | f369470 | 2005-06-02 05:55:20 +0000 | [diff] [blame] | 2 | from compiler.ast import flatten |
Jeremy Hylton | 4336eda | 2004-08-07 19:25:33 +0000 | [diff] [blame] | 3 | import os |
| 4 | import test.test_support |
| 5 | import unittest |
Raymond Hettinger | ed20ad8 | 2004-09-04 20:09:13 +0000 | [diff] [blame] | 6 | from random import random |
Jeremy Hylton | 4336eda | 2004-08-07 19:25:33 +0000 | [diff] [blame] | 7 | |
| 8 | class 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 Peters | 2841af4 | 2006-01-07 23:20:46 +0000 | [diff] [blame] | 15 | |
Jeremy Hylton | 4336eda | 2004-08-07 19:25:33 +0000 | [diff] [blame] | 16 | libdir = os.path.dirname(unittest.__file__) |
| 17 | testdir = os.path.dirname(test.test_support.__file__) |
| 18 | |
| 19 | for dir in [libdir, testdir]: |
Tim Peters | 2a5f656 | 2004-08-08 16:37:37 +0000 | [diff] [blame] | 20 | for basename in os.listdir(dir): |
| 21 | if not basename.endswith(".py"): |
Jeremy Hylton | 4336eda | 2004-08-07 19:25:33 +0000 | [diff] [blame] | 22 | continue |
Guido van Rossum | 5bde08d | 2006-03-02 04:24:01 +0000 | [diff] [blame] | 23 | if not TEST_ALL and random() < 0.98: |
Raymond Hettinger | ed20ad8 | 2004-09-04 20:09:13 +0000 | [diff] [blame] | 24 | continue |
Tim Peters | 2a5f656 | 2004-08-08 16:37:37 +0000 | [diff] [blame] | 25 | path = os.path.join(dir, basename) |
Tim Peters | b6ecc16 | 2004-08-08 16:32:54 +0000 | [diff] [blame] | 26 | if test.test_support.verbose: |
Tim Peters | 2a5f656 | 2004-08-08 16:37:37 +0000 | [diff] [blame] | 27 | print "compiling", path |
Michael W. Hudson | 29589a0 | 2004-10-11 15:34:31 +0000 | [diff] [blame] | 28 | f = open(path, "U") |
Jeremy Hylton | 4336eda | 2004-08-07 19:25:33 +0000 | [diff] [blame] | 29 | buf = f.read() |
| 30 | f.close() |
Neal Norwitz | f895065 | 2005-10-24 00:01:37 +0000 | [diff] [blame] | 31 | if "badsyntax" in basename or "bad_coding" in basename: |
Tim Peters | 0955f29 | 2004-08-08 16:43:59 +0000 | [diff] [blame] | 32 | self.assertRaises(SyntaxError, compiler.compile, |
| 33 | buf, basename, "exec") |
| 34 | else: |
Martin v. Löwis | 15bfc3b | 2006-03-01 21:11:49 +0000 | [diff] [blame] | 35 | try: |
| 36 | compiler.compile(buf, basename, "exec") |
| 37 | except Exception, e: |
Martin v. Löwis | d9bfeac | 2006-03-01 23:24:34 +0000 | [diff] [blame] | 38 | args = list(e.args) |
| 39 | args[0] += "[in file %s]" % basename |
| 40 | e.args = tuple(args) |
Martin v. Löwis | 15bfc3b | 2006-03-01 21:11:49 +0000 | [diff] [blame] | 41 | raise |
Jeremy Hylton | 4336eda | 2004-08-07 19:25:33 +0000 | [diff] [blame] | 42 | |
Brett Cannon | f418991 | 2005-04-09 02:30:16 +0000 | [diff] [blame] | 43 | def testNewClassSyntax(self): |
| 44 | compiler.compile("class foo():pass\n\n","<string>","exec") |
Tim Peters | e890682 | 2005-04-20 17:45:13 +0000 | [diff] [blame] | 45 | |
Guido van Rossum | 5bde08d | 2006-03-02 04:24:01 +0000 | [diff] [blame] | 46 | def testYieldExpr(self): |
| 47 | compiler.compile("def g(): yield\n\n", "<string>", "exec") |
| 48 | |
Jeremy Hylton | 566d934 | 2004-09-07 15:28:01 +0000 | [diff] [blame] | 49 | def testLineNo(self): |
| 50 | # Test that all nodes except Module have a correct lineno attribute. |
| 51 | filename = __file__ |
| 52 | if filename.endswith(".pyc") or filename.endswith(".pyo"): |
| 53 | filename = filename[:-1] |
| 54 | tree = compiler.parseFile(filename) |
| 55 | self.check_lineno(tree) |
| 56 | |
| 57 | def check_lineno(self, node): |
| 58 | try: |
| 59 | self._check_lineno(node) |
| 60 | except AssertionError: |
| 61 | print node.__class__, node.lineno |
| 62 | raise |
| 63 | |
| 64 | def _check_lineno(self, node): |
| 65 | if not node.__class__ in NOLINENO: |
Tim Peters | 0e9980f | 2004-09-12 03:49:31 +0000 | [diff] [blame] | 66 | self.assert_(isinstance(node.lineno, int), |
Jeremy Hylton | 566d934 | 2004-09-07 15:28:01 +0000 | [diff] [blame] | 67 | "lineno=%s on %s" % (node.lineno, node.__class__)) |
Tim Peters | 0e9980f | 2004-09-12 03:49:31 +0000 | [diff] [blame] | 68 | self.assert_(node.lineno > 0, |
Jeremy Hylton | 566d934 | 2004-09-07 15:28:01 +0000 | [diff] [blame] | 69 | "lineno=%s on %s" % (node.lineno, node.__class__)) |
| 70 | for child in node.getChildNodes(): |
| 71 | self.check_lineno(child) |
| 72 | |
Neil Schemenauer | f369470 | 2005-06-02 05:55:20 +0000 | [diff] [blame] | 73 | def testFlatten(self): |
| 74 | self.assertEquals(flatten([1, [2]]), [1, 2]) |
| 75 | self.assertEquals(flatten((1, (2,))), [1, 2]) |
| 76 | |
Jeremy Hylton | 566d934 | 2004-09-07 15:28:01 +0000 | [diff] [blame] | 77 | NOLINENO = (compiler.ast.Module, compiler.ast.Stmt, compiler.ast.Discard) |
| 78 | |
| 79 | ############################################################################### |
| 80 | # code below is just used to trigger some possible errors, for the benefit of |
| 81 | # testLineNo |
| 82 | ############################################################################### |
| 83 | |
| 84 | class Toto: |
| 85 | """docstring""" |
| 86 | pass |
| 87 | |
| 88 | a, b = 2, 3 |
| 89 | [c, d] = 5, 6 |
| 90 | l = [(x, y) for x, y in zip(range(5), range(5,10))] |
| 91 | l[0] |
| 92 | l[3:4] |
| 93 | if l: |
| 94 | pass |
| 95 | else: |
| 96 | a, b = b, a |
| 97 | |
| 98 | try: |
| 99 | print yo |
| 100 | except: |
| 101 | yo = 3 |
| 102 | else: |
| 103 | yo += 3 |
Tim Peters | 0e9980f | 2004-09-12 03:49:31 +0000 | [diff] [blame] | 104 | |
Jeremy Hylton | 566d934 | 2004-09-07 15:28:01 +0000 | [diff] [blame] | 105 | try: |
| 106 | a += b |
| 107 | finally: |
| 108 | b = 0 |
Tim Peters | 0e9980f | 2004-09-12 03:49:31 +0000 | [diff] [blame] | 109 | |
Michael W. Hudson | e0b855f | 2004-11-08 16:46:02 +0000 | [diff] [blame] | 110 | from math import * |
| 111 | |
Jeremy Hylton | 566d934 | 2004-09-07 15:28:01 +0000 | [diff] [blame] | 112 | ############################################################################### |
| 113 | |
Jeremy Hylton | 4336eda | 2004-08-07 19:25:33 +0000 | [diff] [blame] | 114 | def test_main(): |
Raymond Hettinger | ed20ad8 | 2004-09-04 20:09:13 +0000 | [diff] [blame] | 115 | global TEST_ALL |
| 116 | TEST_ALL = test.test_support.is_resource_enabled("compiler") |
Jeremy Hylton | 4336eda | 2004-08-07 19:25:33 +0000 | [diff] [blame] | 117 | test.test_support.run_unittest(CompilerTest) |
| 118 | |
| 119 | if __name__ == "__main__": |
| 120 | test_main() |