Updated for 2.1a3
[python/dscho.git] / Lib / test / test_support.py
blobd77120e015b756ab9e3dd37a9b108e1c61a5b0a4
1 """Supporting definitions for the Python regression test."""
4 class Error(Exception):
5 """Base class for regression test exceptions."""
7 class TestFailed(Error):
8 """Test failed."""
10 class TestSkipped(Error):
11 """Test skipped.
13 This can be raised to indicate that a test was deliberatly
14 skipped, but not because a feature wasn't available. For
15 example, if some resource can't be used, such as the network
16 appears to be unavailable, this should be raised instead of
17 TestFailed.
18 """
20 verbose = 1 # Flag set to 0 by regrtest.py
21 use_large_resources = 1 # Flag set to 0 by regrtest.py
23 def unload(name):
24 import sys
25 try:
26 del sys.modules[name]
27 except KeyError:
28 pass
30 def forget(modname):
31 unload(modname)
32 import sys, os
33 for dirname in sys.path:
34 try:
35 os.unlink(os.path.join(dirname, modname + '.pyc'))
36 except os.error:
37 pass
39 FUZZ = 1e-6
41 def fcmp(x, y): # fuzzy comparison function
42 if type(x) == type(0.0) or type(y) == type(0.0):
43 try:
44 x, y = coerce(x, y)
45 fuzz = (abs(x) + abs(y)) * FUZZ
46 if abs(x-y) <= fuzz:
47 return 0
48 except:
49 pass
50 elif type(x) == type(y) and type(x) in (type(()), type([])):
51 for i in range(min(len(x), len(y))):
52 outcome = fcmp(x[i], y[i])
53 if outcome != 0:
54 return outcome
55 return cmp(len(x), len(y))
56 return cmp(x, y)
58 TESTFN = '@test' # Filename used for testing
59 from os import unlink
61 def findfile(file, here=__file__):
62 import os
63 if os.path.isabs(file):
64 return file
65 import sys
66 path = sys.path
67 path = [os.path.dirname(here)] + path
68 for dn in path:
69 fn = os.path.join(dn, file)
70 if os.path.exists(fn): return fn
71 return file
73 def verify(condition, reason='test failed'):
74 """Verify that condition is true. If not, raise TestFailed.
76 The optional argument reason can be given to provide
77 a better error text.
78 """
80 if not condition:
81 raise TestFailed(reason)
83 def check_syntax(statement):
84 try:
85 compile(statement, '<string>', 'exec')
86 except SyntaxError:
87 pass
88 else:
89 print 'Missing SyntaxError: "%s"' % statement