Fred Drake | 1790dd4 | 2000-07-24 06:55:00 +0000 | [diff] [blame] | 1 | """Supporting definitions for the Python regression test.""" |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 2 | |
Fred Drake | cd1b1dd | 2001-03-21 18:26:33 +0000 | [diff] [blame] | 3 | import sys |
| 4 | |
Fred Drake | 1790dd4 | 2000-07-24 06:55:00 +0000 | [diff] [blame] | 5 | |
| 6 | class Error(Exception): |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 7 | """Base class for regression test exceptions.""" |
Fred Drake | 1790dd4 | 2000-07-24 06:55:00 +0000 | [diff] [blame] | 8 | |
| 9 | class TestFailed(Error): |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 10 | """Test failed.""" |
Fred Drake | 1790dd4 | 2000-07-24 06:55:00 +0000 | [diff] [blame] | 11 | |
| 12 | class TestSkipped(Error): |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 13 | """Test skipped. |
Fred Drake | 1790dd4 | 2000-07-24 06:55:00 +0000 | [diff] [blame] | 14 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 15 | 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 Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 20 | """ |
Fred Drake | 1790dd4 | 2000-07-24 06:55:00 +0000 | [diff] [blame] | 21 | |
Barry Warsaw | c0fb605 | 2001-08-20 22:29:23 +0000 | [diff] [blame] | 22 | verbose = 1 # Flag set to 0 by regrtest.py |
Guido van Rossum | fe3f696 | 2001-09-06 16:09:41 +0000 | [diff] [blame] | 23 | use_resources = None # Flag set to [] by regrtest.py |
Guido van Rossum | 531661c | 1996-12-20 02:58:22 +0000 | [diff] [blame] | 24 | |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 25 | def unload(name): |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 26 | try: |
| 27 | del sys.modules[name] |
| 28 | except KeyError: |
| 29 | pass |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 30 | |
| 31 | def forget(modname): |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 32 | unload(modname) |
Fred Drake | cd1b1dd | 2001-03-21 18:26:33 +0000 | [diff] [blame] | 33 | import os |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 34 | for dirname in sys.path: |
| 35 | try: |
| 36 | os.unlink(os.path.join(dirname, modname + '.pyc')) |
| 37 | except os.error: |
| 38 | pass |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 39 | |
Barry Warsaw | c0fb605 | 2001-08-20 22:29:23 +0000 | [diff] [blame] | 40 | def requires(resource, msg=None): |
Guido van Rossum | fe3f696 | 2001-09-06 16:09:41 +0000 | [diff] [blame] | 41 | if use_resources is not None and resource not in use_resources: |
Barry Warsaw | c0fb605 | 2001-08-20 22:29:23 +0000 | [diff] [blame] | 42 | if msg is None: |
| 43 | msg = "Use of the `%s' resource not enabled" % resource |
| 44 | raise TestSkipped(msg) |
| 45 | |
Guido van Rossum | 35fb82a | 1993-01-26 13:04:43 +0000 | [diff] [blame] | 46 | FUZZ = 1e-6 |
| 47 | |
| 48 | def fcmp(x, y): # fuzzy comparison function |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 49 | 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 Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 60 | if outcome != 0: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 61 | return outcome |
| 62 | return cmp(len(x), len(y)) |
| 63 | return cmp(x, y) |
Guido van Rossum | 35fb82a | 1993-01-26 13:04:43 +0000 | [diff] [blame] | 64 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 65 | try: |
| 66 | unicode |
| 67 | have_unicode = 1 |
| 68 | except NameError: |
| 69 | have_unicode = 0 |
| 70 | |
Guido van Rossum | a8f7e59 | 2001-03-13 09:31:07 +0000 | [diff] [blame] | 71 | import os |
Barry Warsaw | 559f668 | 2001-03-23 18:04:02 +0000 | [diff] [blame] | 72 | # Filename used for testing |
| 73 | if os.name == 'java': |
| 74 | # Jython disallows @ in module names |
| 75 | TESTFN = '$test' |
| 76 | elif os.name != 'riscos': |
| 77 | TESTFN = '@test' |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 78 | # Unicode name only used if TEST_FN_ENCODING exists for the platform. |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 79 | 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 Rossum | a8f7e59 | 2001-03-13 09:31:07 +0000 | [diff] [blame] | 83 | else: |
Barry Warsaw | 559f668 | 2001-03-23 18:04:02 +0000 | [diff] [blame] | 84 | TESTFN = 'test' |
Guido van Rossum | a8f7e59 | 2001-03-13 09:31:07 +0000 | [diff] [blame] | 85 | del os |
| 86 | |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 87 | from os import unlink |
Guido van Rossum | e26132c | 1998-04-23 20:13:30 +0000 | [diff] [blame] | 88 | |
| 89 | def findfile(file, here=__file__): |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 90 | import os |
| 91 | if os.path.isabs(file): |
| 92 | return file |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 93 | 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é Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 99 | |
| 100 | def verify(condition, reason='test failed'): |
Guido van Rossum | a1374e4 | 2001-01-19 19:01:56 +0000 | [diff] [blame] | 101 | """Verify that condition is true. If not, raise TestFailed. |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 102 | |
Skip Montanaro | c955f89 | 2001-01-20 19:12:54 +0000 | [diff] [blame] | 103 | The optional argument reason can be given to provide |
Tim Peters | 983874d | 2001-01-19 05:59:21 +0000 | [diff] [blame] | 104 | a better error text. |
Tim Peters | d2bf3b7 | 2001-01-18 02:22:22 +0000 | [diff] [blame] | 105 | """ |
Tim Peters | 983874d | 2001-01-19 05:59:21 +0000 | [diff] [blame] | 106 | |
Tim Peters | d2bf3b7 | 2001-01-18 02:22:22 +0000 | [diff] [blame] | 107 | if not condition: |
Guido van Rossum | a1374e4 | 2001-01-19 19:01:56 +0000 | [diff] [blame] | 108 | raise TestFailed(reason) |
Jeremy Hylton | 4779399 | 2001-02-19 15:35:26 +0000 | [diff] [blame] | 109 | |
Tim Peters | 2f228e7 | 2001-05-13 00:19:31 +0000 | [diff] [blame] | 110 | def 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 Hylton | 4779399 | 2001-02-19 15:35:26 +0000 | [diff] [blame] | 118 | def check_syntax(statement): |
| 119 | try: |
| 120 | compile(statement, '<string>', 'exec') |
| 121 | except SyntaxError: |
| 122 | pass |
| 123 | else: |
| 124 | print 'Missing SyntaxError: "%s"' % statement |
Fred Drake | cd1b1dd | 2001-03-21 18:26:33 +0000 | [diff] [blame] | 125 | |
| 126 | |
| 127 | |
| 128 | #======================================================================= |
| 129 | # Preliminary PyUNIT integration. |
| 130 | |
| 131 | import unittest |
| 132 | |
| 133 | |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 134 | class BasicTestRunner: |
Fred Drake | cd1b1dd | 2001-03-21 18:26:33 +0000 | [diff] [blame] | 135 | def run(self, test): |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 136 | result = unittest.TestResult() |
Fred Drake | cd1b1dd | 2001-03-21 18:26:33 +0000 | [diff] [blame] | 137 | test(result) |
| 138 | return result |
| 139 | |
| 140 | |
| 141 | def run_unittest(testclass): |
| 142 | """Run tests from a unittest.TestCase-derived class.""" |
| 143 | if verbose: |
Fred Drake | 84a5934 | 2001-03-23 04:21:17 +0000 | [diff] [blame] | 144 | runner = unittest.TextTestRunner(sys.stdout, verbosity=2) |
Fred Drake | cd1b1dd | 2001-03-21 18:26:33 +0000 | [diff] [blame] | 145 | else: |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 146 | runner = BasicTestRunner() |
Fred Drake | cd1b1dd | 2001-03-21 18:26:33 +0000 | [diff] [blame] | 147 | |
| 148 | suite = unittest.makeSuite(testclass) |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 149 | result = runner.run(suite) |
| 150 | if not result.wasSuccessful(): |
Fred Drake | 14f6c18 | 2001-07-16 18:51:32 +0000 | [diff] [blame] | 151 | 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__)) |
| 158 | if err[0] is AssertionError: |
| 159 | raise TestFailed(str(err[1])) |
| 160 | else: |
| 161 | raise TestFailed("%s: %s" % err[:2]) |