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. |
Jeremy Hylton | 4336eda | 2004-08-07 19:25:33 +0000 | [diff] [blame] | 15 | libdir = os.path.dirname(unittest.__file__) |
| 16 | testdir = os.path.dirname(test.test_support.__file__) |
| 17 | |
| 18 | for dir in [libdir, testdir]: |
Tim Peters | 2a5f656 | 2004-08-08 16:37:37 +0000 | [diff] [blame] | 19 | for basename in os.listdir(dir): |
| 20 | if not basename.endswith(".py"): |
Jeremy Hylton | 4336eda | 2004-08-07 19:25:33 +0000 | [diff] [blame] | 21 | continue |
Raymond Hettinger | ed20ad8 | 2004-09-04 20:09:13 +0000 | [diff] [blame] | 22 | if not TEST_ALL and random() < 0.98: |
| 23 | continue |
Tim Peters | 2a5f656 | 2004-08-08 16:37:37 +0000 | [diff] [blame] | 24 | path = os.path.join(dir, basename) |
Tim Peters | b6ecc16 | 2004-08-08 16:32:54 +0000 | [diff] [blame] | 25 | if test.test_support.verbose: |
Tim Peters | 2a5f656 | 2004-08-08 16:37:37 +0000 | [diff] [blame] | 26 | print "compiling", path |
Michael W. Hudson | 29589a0 | 2004-10-11 15:34:31 +0000 | [diff] [blame] | 27 | f = open(path, "U") |
Jeremy Hylton | 4336eda | 2004-08-07 19:25:33 +0000 | [diff] [blame] | 28 | buf = f.read() |
| 29 | f.close() |
Neal Norwitz | f895065 | 2005-10-24 00:01:37 +0000 | [diff] [blame] | 30 | if "badsyntax" in basename or "bad_coding" in basename: |
Tim Peters | 0955f29 | 2004-08-08 16:43:59 +0000 | [diff] [blame] | 31 | self.assertRaises(SyntaxError, compiler.compile, |
| 32 | buf, basename, "exec") |
| 33 | else: |
| 34 | compiler.compile(buf, basename, "exec") |
Jeremy Hylton | 4336eda | 2004-08-07 19:25:33 +0000 | [diff] [blame] | 35 | |
Brett Cannon | f418991 | 2005-04-09 02:30:16 +0000 | [diff] [blame] | 36 | def testNewClassSyntax(self): |
| 37 | compiler.compile("class foo():pass\n\n","<string>","exec") |
Georg Brandl | 39cdfff | 2006-01-06 19:28:15 +0000 | [diff] [blame^] | 38 | |
| 39 | def testSyntaxErrors(self): |
| 40 | self.assertRaises(SyntaxError, compiler.compile, |
| 41 | "def foo(a=1,b):pass\n\n", "<string>", "exec") |
Tim Peters | e890682 | 2005-04-20 17:45:13 +0000 | [diff] [blame] | 42 | |
Jeremy Hylton | 566d934 | 2004-09-07 15:28:01 +0000 | [diff] [blame] | 43 | 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 Peters | 0e9980f | 2004-09-12 03:49:31 +0000 | [diff] [blame] | 60 | self.assert_(isinstance(node.lineno, int), |
Jeremy Hylton | 566d934 | 2004-09-07 15:28:01 +0000 | [diff] [blame] | 61 | "lineno=%s on %s" % (node.lineno, node.__class__)) |
Tim Peters | 0e9980f | 2004-09-12 03:49:31 +0000 | [diff] [blame] | 62 | self.assert_(node.lineno > 0, |
Jeremy Hylton | 566d934 | 2004-09-07 15:28:01 +0000 | [diff] [blame] | 63 | "lineno=%s on %s" % (node.lineno, node.__class__)) |
| 64 | for child in node.getChildNodes(): |
| 65 | self.check_lineno(child) |
| 66 | |
Neil Schemenauer | f369470 | 2005-06-02 05:55:20 +0000 | [diff] [blame] | 67 | def testFlatten(self): |
| 68 | self.assertEquals(flatten([1, [2]]), [1, 2]) |
| 69 | self.assertEquals(flatten((1, (2,))), [1, 2]) |
| 70 | |
Jeremy Hylton | 566d934 | 2004-09-07 15:28:01 +0000 | [diff] [blame] | 71 | NOLINENO = (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 | |
| 78 | class Toto: |
| 79 | """docstring""" |
| 80 | pass |
| 81 | |
| 82 | a, b = 2, 3 |
| 83 | [c, d] = 5, 6 |
| 84 | l = [(x, y) for x, y in zip(range(5), range(5,10))] |
| 85 | l[0] |
| 86 | l[3:4] |
| 87 | if l: |
| 88 | pass |
| 89 | else: |
| 90 | a, b = b, a |
| 91 | |
| 92 | try: |
| 93 | print yo |
| 94 | except: |
| 95 | yo = 3 |
| 96 | else: |
| 97 | yo += 3 |
Tim Peters | 0e9980f | 2004-09-12 03:49:31 +0000 | [diff] [blame] | 98 | |
Jeremy Hylton | 566d934 | 2004-09-07 15:28:01 +0000 | [diff] [blame] | 99 | try: |
| 100 | a += b |
| 101 | finally: |
| 102 | b = 0 |
Tim Peters | 0e9980f | 2004-09-12 03:49:31 +0000 | [diff] [blame] | 103 | |
Michael W. Hudson | e0b855f | 2004-11-08 16:46:02 +0000 | [diff] [blame] | 104 | from math import * |
| 105 | |
Jeremy Hylton | 566d934 | 2004-09-07 15:28:01 +0000 | [diff] [blame] | 106 | ############################################################################### |
| 107 | |
Jeremy Hylton | 4336eda | 2004-08-07 19:25:33 +0000 | [diff] [blame] | 108 | def test_main(): |
Raymond Hettinger | ed20ad8 | 2004-09-04 20:09:13 +0000 | [diff] [blame] | 109 | global TEST_ALL |
| 110 | TEST_ALL = test.test_support.is_resource_enabled("compiler") |
Jeremy Hylton | 4336eda | 2004-08-07 19:25:33 +0000 | [diff] [blame] | 111 | test.test_support.run_unittest(CompilerTest) |
| 112 | |
| 113 | if __name__ == "__main__": |
| 114 | test_main() |