blob: 0e87131cfbe4f05c3ea215ece27f78b2c5662634 [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
Barry Warsawc0fb6052001-08-20 22:29:23 +000022verbose = 1 # Flag set to 0 by regrtest.py
Guido van Rossumfe3f6962001-09-06 16:09:41 +000023use_resources = None # Flag set to [] 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
Barry Warsawc0fb6052001-08-20 22:29:23 +000040def requires(resource, msg=None):
Guido van Rossumfe3f6962001-09-06 16:09:41 +000041 if use_resources is not None and resource not in use_resources:
Barry Warsawc0fb6052001-08-20 22:29:23 +000042 if msg is None:
43 msg = "Use of the `%s' resource not enabled" % resource
44 raise TestSkipped(msg)
45
Guido van Rossum35fb82a1993-01-26 13:04:43 +000046FUZZ = 1e-6
47
48def fcmp(x, y): # fuzzy comparison function
Fred Drake004d5e62000-10-23 17:22:08 +000049 if type(x) == type(0.0) or type(y) == type(0.0):
50 try:
51 x, y = coerce(x, y)
52 fuzz = (abs(x) + abs(y)) * FUZZ
53 if abs(x-y) <= fuzz:
54 return 0
55 except:
56 pass
57 elif type(x) == type(y) and type(x) in (type(()), type([])):
58 for i in range(min(len(x), len(y))):
59 outcome = fcmp(x[i], y[i])
Fred Drake132dce22000-12-12 23:11:42 +000060 if outcome != 0:
Fred Drake004d5e62000-10-23 17:22:08 +000061 return outcome
62 return cmp(len(x), len(y))
63 return cmp(x, y)
Guido van Rossum35fb82a1993-01-26 13:04:43 +000064
Martin v. Löwis339d0f72001-08-17 18:39:25 +000065try:
66 unicode
67 have_unicode = 1
68except NameError:
69 have_unicode = 0
70
Guido van Rossuma8f7e592001-03-13 09:31:07 +000071import os
Barry Warsaw559f6682001-03-23 18:04:02 +000072# Filename used for testing
73if os.name == 'java':
74 # Jython disallows @ in module names
75 TESTFN = '$test'
76elif os.name != 'riscos':
77 TESTFN = '@test'
Mark Hammondef8b6542001-05-13 08:04:26 +000078 # Unicode name only used if TEST_FN_ENCODING exists for the platform.
Martin v. Löwis339d0f72001-08-17 18:39:25 +000079 if have_unicode:
80 TESTFN_UNICODE=unicode("@test-\xe0\xf2", "latin-1") # 2 latin characters.
81 if os.name=="nt":
82 TESTFN_ENCODING="mbcs"
Guido van Rossuma8f7e592001-03-13 09:31:07 +000083else:
Barry Warsaw559f6682001-03-23 18:04:02 +000084 TESTFN = 'test'
Guido van Rossuma8f7e592001-03-13 09:31:07 +000085del os
86
Guido van Rossum3bead091992-01-27 17:00:37 +000087from os import unlink
Guido van Rossume26132c1998-04-23 20:13:30 +000088
89def findfile(file, here=__file__):
Fred Drake004d5e62000-10-23 17:22:08 +000090 import os
91 if os.path.isabs(file):
92 return file
Fred Drake004d5e62000-10-23 17:22:08 +000093 path = sys.path
94 path = [os.path.dirname(here)] + path
95 for dn in path:
96 fn = os.path.join(dn, file)
97 if os.path.exists(fn): return fn
98 return file
Marc-André Lemburg36619082001-01-17 19:11:13 +000099
100def verify(condition, reason='test failed'):
Guido van Rossuma1374e42001-01-19 19:01:56 +0000101 """Verify that condition is true. If not, raise TestFailed.
Marc-André Lemburg36619082001-01-17 19:11:13 +0000102
Skip Montanaroc955f892001-01-20 19:12:54 +0000103 The optional argument reason can be given to provide
Tim Peters983874d2001-01-19 05:59:21 +0000104 a better error text.
Tim Petersd2bf3b72001-01-18 02:22:22 +0000105 """
Tim Peters983874d2001-01-19 05:59:21 +0000106
Tim Petersd2bf3b72001-01-18 02:22:22 +0000107 if not condition:
Guido van Rossuma1374e42001-01-19 19:01:56 +0000108 raise TestFailed(reason)
Jeremy Hylton47793992001-02-19 15:35:26 +0000109
Tim Peters2f228e72001-05-13 00:19:31 +0000110def sortdict(dict):
111 "Like repr(dict), but in sorted order."
112 items = dict.items()
113 items.sort()
114 reprpairs = ["%r: %r" % pair for pair in items]
115 withcommas = ", ".join(reprpairs)
116 return "{%s}" % withcommas
117
Jeremy Hylton47793992001-02-19 15:35:26 +0000118def check_syntax(statement):
119 try:
120 compile(statement, '<string>', 'exec')
121 except SyntaxError:
122 pass
123 else:
124 print 'Missing SyntaxError: "%s"' % statement
Fred Drakecd1b1dd2001-03-21 18:26:33 +0000125
126
127
128#=======================================================================
129# Preliminary PyUNIT integration.
130
131import unittest
132
133
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000134class BasicTestRunner:
Fred Drakecd1b1dd2001-03-21 18:26:33 +0000135 def run(self, test):
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000136 result = unittest.TestResult()
Fred Drakecd1b1dd2001-03-21 18:26:33 +0000137 test(result)
138 return result
139
140
141def run_unittest(testclass):
142 """Run tests from a unittest.TestCase-derived class."""
143 if verbose:
Fred Drake84a59342001-03-23 04:21:17 +0000144 runner = unittest.TextTestRunner(sys.stdout, verbosity=2)
Fred Drakecd1b1dd2001-03-21 18:26:33 +0000145 else:
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000146 runner = BasicTestRunner()
Fred Drakecd1b1dd2001-03-21 18:26:33 +0000147
148 suite = unittest.makeSuite(testclass)
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000149 result = runner.run(suite)
150 if not result.wasSuccessful():
Fred Drake14f6c182001-07-16 18:51:32 +0000151 if len(result.errors) == 1 and not result.failures:
152 err = result.errors[0][1]
153 elif len(result.failures) == 1 and not result.errors:
154 err = result.failures[0][1]
155 else:
156 raise TestFailed("errors occurred in %s.%s"
157 % (testclass.__module__, testclass.__name__))
Tim Peters2d84f2c2001-09-08 03:37:56 +0000158 raise TestFailed(err)