python (3.12.0)
1 class ESC[4;38;5;81mExceptionIsLikeMixin:
2 def assertExceptionIsLike(self, exc, template):
3 """
4 Passes when the provided `exc` matches the structure of `template`.
5 Individual exceptions don't have to be the same objects or even pass
6 an equality test: they only need to be the same type and contain equal
7 `exc_obj.args`.
8 """
9 if exc is None and template is None:
10 return
11
12 if template is None:
13 self.fail(f"unexpected exception: {exc}")
14
15 if exc is None:
16 self.fail(f"expected an exception like {template!r}, got None")
17
18 if not isinstance(exc, ExceptionGroup):
19 self.assertEqual(exc.__class__, template.__class__)
20 self.assertEqual(exc.args[0], template.args[0])
21 else:
22 self.assertEqual(exc.message, template.message)
23 self.assertEqual(len(exc.exceptions), len(template.exceptions))
24 for e, t in zip(exc.exceptions, template.exceptions):
25 self.assertExceptionIsLike(e, t)