Debian Codegraph @

Lib/test/support/testcase.py

raw
1 from math import copysign, isnan
2
3
4 class ExtraAssertions:
5
6 def assertIsSubclass(self, cls, superclass, msg=None):
7 if issubclass(cls, superclass):
8 return
9 standardMsg = f'{cls!r} is not a subclass of {superclass!r}'
10 self.fail(self._formatMessage(msg, standardMsg))
11
12 def assertNotIsSubclass(self, cls, superclass, msg=None):
13 if not issubclass(cls, superclass):
14 return
15 standardMsg = f'{cls!r} is a subclass of {superclass!r}'
16 self.fail(self._formatMessage(msg, standardMsg))
17
18 def assertHasAttr(self, obj, name, msg=None):
19 if not hasattr(obj, name):
20 if isinstance(obj, types.ModuleType):
21 standardMsg = f'module {obj.__name__!r} has no attribute {name!r}'
22 elif isinstance(obj, type):
23 standardMsg = f'type object {obj.__name__!r} has no attribute {name!r}'
24 else:
25 standardMsg = f'{type(obj).__name__!r} object has no attribute {name!r}'
26 self.fail(self._formatMessage(msg, standardMsg))
27
28 def assertNotHasAttr(self, obj, name, msg=None):
29 if hasattr(obj, name):
30 if isinstance(obj, types.ModuleType):
31 standardMsg = f'module {obj.__name__!r} has unexpected attribute {name!r}'
32 elif isinstance(obj, type):
33 standardMsg = f'type object {obj.__name__!r} has unexpected attribute {name!r}'
34 else:
35 standardMsg = f'{type(obj).__name__!r} object has unexpected attribute {name!r}'
36 self.fail(self._formatMessage(msg, standardMsg))
37
38 def assertStartsWith(self, s, prefix, msg=None):
39 if s.startswith(prefix):
40 return
41 standardMsg = f"{s!r} doesn't start with {prefix!r}"
42 self.fail(self._formatMessage(msg, standardMsg))
43
44 def assertNotStartsWith(self, s, prefix, msg=None):
45 if not s.startswith(prefix):
46 return
47 self.fail(self._formatMessage(msg, f"{s!r} starts with {prefix!r}"))
48
49 def assertEndsWith(self, s, suffix, msg=None):
50 if s.endswith(suffix):
51 return
52 standardMsg = f"{s!r} doesn't end with {suffix!r}"
53 self.fail(self._formatMessage(msg, standardMsg))
54
55 def assertNotEndsWith(self, s, suffix, msg=None):
56 if not s.endswith(suffix):
57 return
58 self.fail(self._formatMessage(msg, f"{s!r} ends with {suffix!r}"))
59
60
61 class ExceptionIsLikeMixin:
62 def assertExceptionIsLike(self, exc, template):
63 """
64 Passes when the provided `exc` matches the structure of `template`.
65 Individual exceptions don't have to be the same objects or even pass
66 an equality test: they only need to be the same type and contain equal
67 `exc_obj.args`.
68 """
69 if exc is None and template is None:
70 return
71
72 if template is None:
73 self.fail(f"unexpected exception: {exc}")
74
75 if exc is None:
76 self.fail(f"expected an exception like {template!r}, got None")
77
78 if not isinstance(exc, ExceptionGroup):
79 self.assertEqual(exc.__class__, template.__class__)
80 self.assertEqual(exc.args[0], template.args[0])
81 else:
82 self.assertEqual(exc.message, template.message)
83 self.assertEqual(len(exc.exceptions), len(template.exceptions))
84 for e, t in zip(exc.exceptions, template.exceptions):
85 self.assertExceptionIsLike(e, t)
86
87
88 class FloatsAreIdenticalMixin:
89 def assertFloatsAreIdentical(self, x, y):
90 """Fail unless floats x and y are identical, in the sense that:
91 (1) both x and y are nans, or
92 (2) both x and y are infinities, with the same sign, or
93 (3) both x and y are zeros, with the same sign, or
94 (4) x and y are both finite and nonzero, and x == y
95
96 """
97 msg = 'floats {!r} and {!r} are not identical'
98
99 if isnan(x) or isnan(y):
100 if isnan(x) and isnan(y):
101 return
102 elif x == y:
103 if x != 0.0:
104 return
105 # both zero; check that signs match
106 elif copysign(1.0, x) == copysign(1.0, y):
107 return
108 else:
109 msg += ': zeros have different signs'
110 self.fail(msg.format(x, y))
111
112
113 class ComplexesAreIdenticalMixin(FloatsAreIdenticalMixin):
114 def assertComplexesAreIdentical(self, x, y):
115 """Fail unless complex numbers x and y have equal values and signs.
116
117 In particular, if x and y both have real (or imaginary) part
118 zero, but the zeros have different signs, this test will fail.
119
120 """
121 self.assertFloatsAreIdentical(x.real, y.real)
122 self.assertFloatsAreIdentical(x.imag, y.imag)