blob: 63cbc0080d86baad1b825293c820d40fa63afdb7 [file] [log] [blame]
Jeremy Hylton4336eda2004-08-07 19:25:33 +00001import compiler
2import os
3import test.test_support
4import unittest
Raymond Hettingered20ad82004-09-04 20:09:13 +00005from random import random
Jeremy Hylton4336eda2004-08-07 19:25:33 +00006
7class CompilerTest(unittest.TestCase):
8
9 def testCompileLibrary(self):
10 # A simple but large test. Compile all the code in the
11 # standard library and its test suite. This doesn't verify
12 # that any of the code is correct, merely the compiler is able
13 # to generate some kind of code for it.
14
15 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
27 f = open(path)
Jeremy Hylton4336eda2004-08-07 19:25:33 +000028 buf = f.read()
29 f.close()
Tim Peters0955f292004-08-08 16:43:59 +000030 if "badsyntax" in basename:
31 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
36def test_main():
Raymond Hettingered20ad82004-09-04 20:09:13 +000037 global TEST_ALL
38 TEST_ALL = test.test_support.is_resource_enabled("compiler")
Jeremy Hylton4336eda2004-08-07 19:25:33 +000039 test.test_support.run_unittest(CompilerTest)
40
41if __name__ == "__main__":
42 test_main()