blob: 330b9c80db7f16ae7a1384a4f5e83fd9f1718064 [file] [log] [blame]
Fred Drake1790dd42000-07-24 06:55:00 +00001"""Supporting definitions for the Python regression test."""
Guido van Rossum3bead091992-01-27 17:00:37 +00002
Fred Drakecd1b1dd2001-03-21 18:26:33 +00003import sys
4
Fred Drake1790dd42000-07-24 06:55:00 +00005
6class Error(Exception):
Fred Drake004d5e62000-10-23 17:22:08 +00007 """Base class for regression test exceptions."""
Fred Drake1790dd42000-07-24 06:55:00 +00008
9class TestFailed(Error):
Fred Drake004d5e62000-10-23 17:22:08 +000010 """Test failed."""
Fred Drake1790dd42000-07-24 06:55:00 +000011
12class TestSkipped(Error):
Fred Drake004d5e62000-10-23 17:22:08 +000013 """Test skipped.
Fred Drake1790dd42000-07-24 06:55:00 +000014
Fred Drake004d5e62000-10-23 17:22:08 +000015 This can be raised to indicate that a test was deliberatly
16 skipped, but not because a feature wasn't available. For
17 example, if some resource can't be used, such as the network
18 appears to be unavailable, this should be raised instead of
19 TestFailed.
Fred Drake004d5e62000-10-23 17:22:08 +000020 """
Fred Drake1790dd42000-07-24 06:55:00 +000021
Fred Drake004d5e62000-10-23 17:22:08 +000022verbose = 1 # Flag set to 0 by regrtest.py
Fred Drake1790dd42000-07-24 06:55:00 +000023use_large_resources = 1 # Flag set to 0 by regrtest.py
Guido van Rossum531661c1996-12-20 02:58:22 +000024
Guido van Rossum3bead091992-01-27 17:00:37 +000025def unload(name):
Fred Drake004d5e62000-10-23 17:22:08 +000026 try:
27 del sys.modules[name]
28 except KeyError:
29 pass
Guido van Rossum3bead091992-01-27 17:00:37 +000030
31def forget(modname):
Fred Drake004d5e62000-10-23 17:22:08 +000032 unload(modname)
Fred Drakecd1b1dd2001-03-21 18:26:33 +000033 import os
Fred Drake004d5e62000-10-23 17:22:08 +000034 for dirname in sys.path:
35 try:
36 os.unlink(os.path.join(dirname, modname + '.pyc'))
37 except os.error:
38 pass
Guido van Rossum3bead091992-01-27 17:00:37 +000039
Guido van Rossum35fb82a1993-01-26 13:04:43 +000040FUZZ = 1e-6
41
42def fcmp(x, y): # fuzzy comparison function
Fred Drake004d5e62000-10-23 17:22:08 +000043 if type(x) == type(0.0) or type(y) == type(0.0):
44 try:
45 x, y = coerce(x, y)
46 fuzz = (abs(x) + abs(y)) * FUZZ
47 if abs(x-y) <= fuzz:
48 return 0
49 except:
50 pass
51 elif type(x) == type(y) and type(x) in (type(()), type([])):
52 for i in range(min(len(x), len(y))):
53 outcome = fcmp(x[i], y[i])
Fred Drake132dce22000-12-12 23:11:42 +000054 if outcome != 0:
Fred Drake004d5e62000-10-23 17:22:08 +000055 return outcome
56 return cmp(len(x), len(y))
57 return cmp(x, y)
Guido van Rossum35fb82a1993-01-26 13:04:43 +000058
Guido van Rossuma8f7e592001-03-13 09:31:07 +000059import os
Barry Warsaw559f6682001-03-23 18:04:02 +000060# Filename used for testing
61if os.name == 'java':
62 # Jython disallows @ in module names
63 TESTFN = '$test'
64elif os.name != 'riscos':
65 TESTFN = '@test'
Guido van Rossuma8f7e592001-03-13 09:31:07 +000066else:
Barry Warsaw559f6682001-03-23 18:04:02 +000067 TESTFN = 'test'
Guido van Rossuma8f7e592001-03-13 09:31:07 +000068del os
69
Guido van Rossum3bead091992-01-27 17:00:37 +000070from os import unlink
Guido van Rossume26132c1998-04-23 20:13:30 +000071
72def findfile(file, here=__file__):
Fred Drake004d5e62000-10-23 17:22:08 +000073 import os
74 if os.path.isabs(file):
75 return file
Fred Drake004d5e62000-10-23 17:22:08 +000076 path = sys.path
77 path = [os.path.dirname(here)] + path
78 for dn in path:
79 fn = os.path.join(dn, file)
80 if os.path.exists(fn): return fn
81 return file
Marc-André Lemburg36619082001-01-17 19:11:13 +000082
83def verify(condition, reason='test failed'):
Guido van Rossuma1374e42001-01-19 19:01:56 +000084 """Verify that condition is true. If not, raise TestFailed.
Marc-André Lemburg36619082001-01-17 19:11:13 +000085
Skip Montanaroc955f892001-01-20 19:12:54 +000086 The optional argument reason can be given to provide
Tim Peters983874d2001-01-19 05:59:21 +000087 a better error text.
Tim Petersd2bf3b72001-01-18 02:22:22 +000088 """
Tim Peters983874d2001-01-19 05:59:21 +000089
Tim Petersd2bf3b72001-01-18 02:22:22 +000090 if not condition:
Guido van Rossuma1374e42001-01-19 19:01:56 +000091 raise TestFailed(reason)
Jeremy Hylton47793992001-02-19 15:35:26 +000092
Tim Peters2f228e72001-05-13 00:19:31 +000093def sortdict(dict):
94 "Like repr(dict), but in sorted order."
95 items = dict.items()
96 items.sort()
97 reprpairs = ["%r: %r" % pair for pair in items]
98 withcommas = ", ".join(reprpairs)
99 return "{%s}" % withcommas
100
Jeremy Hylton47793992001-02-19 15:35:26 +0000101def check_syntax(statement):
102 try:
103 compile(statement, '<string>', 'exec')
104 except SyntaxError:
105 pass
106 else:
107 print 'Missing SyntaxError: "%s"' % statement
Fred Drakecd1b1dd2001-03-21 18:26:33 +0000108
109
110
111#=======================================================================
112# Preliminary PyUNIT integration.
113
114import unittest
115
116
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000117class BasicTestRunner:
Fred Drakecd1b1dd2001-03-21 18:26:33 +0000118 def run(self, test):
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000119 result = unittest.TestResult()
Fred Drakecd1b1dd2001-03-21 18:26:33 +0000120 test(result)
121 return result
122
123
124def run_unittest(testclass):
125 """Run tests from a unittest.TestCase-derived class."""
126 if verbose:
Fred Drake84a59342001-03-23 04:21:17 +0000127 runner = unittest.TextTestRunner(sys.stdout, verbosity=2)
Fred Drakecd1b1dd2001-03-21 18:26:33 +0000128 else:
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000129 runner = BasicTestRunner()
Fred Drakecd1b1dd2001-03-21 18:26:33 +0000130
131 suite = unittest.makeSuite(testclass)
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000132 result = runner.run(suite)
133 if not result.wasSuccessful():
Fred Drakecd1b1dd2001-03-21 18:26:33 +0000134 raise TestFailed("errors occurred in %s.%s"
135 % (testclass.__module__, testclass.__name__))