Debian Codegraph @

third_party/python/Mako/mako/testing/assertions.py

raw
1 import contextlib
2 import re
3 import sys
4
5
6 def eq_(a, b, msg=None):
7 """Assert a == b, with repr messaging on failure."""
8 assert a == b, msg or "%r != %r" % (a, b)
9
10
11 def ne_(a, b, msg=None):
12 """Assert a != b, with repr messaging on failure."""
13 assert a != b, msg or "%r == %r" % (a, b)
14
15
16 def in_(a, b, msg=None):
17 """Assert a in b, with repr messaging on failure."""
18 assert a in b, msg or "%r not in %r" % (a, b)
19
20
21 def not_in(a, b, msg=None):
22 """Assert a in not b, with repr messaging on failure."""
23 assert a not in b, msg or "%r is in %r" % (a, b)
24
25
26 def _assert_proper_exception_context(exception):
27 """assert that any exception we're catching does not have a __context__
28 without a __cause__, and that __suppress_context__ is never set.
29
30 Python 3 will report nested as exceptions as "during the handling of
31 error X, error Y occurred". That's not what we want to do. We want
32 these exceptions in a cause chain.
33
34 """
35
36 if (
37 exception.__context__ is not exception.__cause__
38 and not exception.__suppress_context__
39 ):
40 assert False, (
41 "Exception %r was correctly raised but did not set a cause, "
42 "within context %r as its cause."
43 % (exception, exception.__context__)
44 )
45
46
47 def _assert_proper_cause_cls(exception, cause_cls):
48 """assert that any exception we're catching does not have a __context__
49 without a __cause__, and that __suppress_context__ is never set.
50
51 Python 3 will report nested as exceptions as "during the handling of
52 error X, error Y occurred". That's not what we want to do. We want
53 these exceptions in a cause chain.
54
55 """
56 assert isinstance(exception.__cause__, cause_cls), (
57 "Exception %r was correctly raised but has cause %r, which does not "
58 "have the expected cause type %r."
59 % (exception, exception.__cause__, cause_cls)
60 )
61
62
63 def assert_raises(except_cls, callable_, *args, **kw):
64 return _assert_raises(except_cls, callable_, args, kw)
65
66
67 def assert_raises_with_proper_context(except_cls, callable_, *args, **kw):
68 return _assert_raises(except_cls, callable_, args, kw, check_context=True)
69
70
71 def assert_raises_with_given_cause(
72 except_cls, cause_cls, callable_, *args, **kw
73 ):
74 return _assert_raises(except_cls, callable_, args, kw, cause_cls=cause_cls)
75
76
77 def assert_raises_message(except_cls, msg, callable_, *args, **kwargs):
78 return _assert_raises(except_cls, callable_, args, kwargs, msg=msg)
79
80
81 def assert_raises_message_with_proper_context(
82 except_cls, msg, callable_, *args, **kwargs
83 ):
84 return _assert_raises(
85 except_cls, callable_, args, kwargs, msg=msg, check_context=True
86 )
87
88
89 def assert_raises_message_with_given_cause(
90 except_cls, msg, cause_cls, callable_, *args, **kwargs
91 ):
92 return _assert_raises(
93 except_cls, callable_, args, kwargs, msg=msg, cause_cls=cause_cls
94 )
95
96
97 def _assert_raises(
98 except_cls,
99 callable_,
100 args,
101 kwargs,
102 msg=None,
103 check_context=False,
104 cause_cls=None,
105 ):
106
107 with _expect_raises(except_cls, msg, check_context, cause_cls) as ec:
108 callable_(*args, **kwargs)
109 return ec.error
110
111
112 class _ErrorContainer:
113 error = None
114
115
116 @contextlib.contextmanager
117 def _expect_raises(except_cls, msg=None, check_context=False, cause_cls=None):
118 ec = _ErrorContainer()
119 if check_context:
120 are_we_already_in_a_traceback = sys.exc_info()[0]
121 try:
122 yield ec
123 success = False
124 except except_cls as err:
125 ec.error = err
126 success = True
127 if msg is not None:
128 # I'm often pdbing here, and "err" above isn't
129 # in scope, so assign the string explicitly
130 error_as_string = str(err)
131 assert re.search(msg, error_as_string, re.UNICODE), "%r !~ %s" % (
132 msg,
133 error_as_string,
134 )
135 if cause_cls is not None:
136 _assert_proper_cause_cls(err, cause_cls)
137 if check_context and not are_we_already_in_a_traceback:
138 _assert_proper_exception_context(err)
139 print(str(err).encode("utf-8"))
140
141 # it's generally a good idea to not carry traceback objects outside
142 # of the except: block, but in this case especially we seem to have
143 # hit some bug in either python 3.10.0b2 or greenlet or both which
144 # this seems to fix:
145 # https://github.com/python-greenlet/greenlet/issues/242
146 del ec
147
148 # assert outside the block so it works for AssertionError too !
149 assert success, "Callable did not raise an exception"
150
151
152 def expect_raises(except_cls, check_context=False):
153 return _expect_raises(except_cls, check_context=check_context)
154
155
156 def expect_raises_message(except_cls, msg, check_context=False):
157 return _expect_raises(except_cls, msg=msg, check_context=check_context)
158
159
160 def expect_raises_with_proper_context(except_cls, check_context=True):
161 return _expect_raises(except_cls, check_context=check_context)
162
163
164 def expect_raises_message_with_proper_context(
165 except_cls, msg, check_context=True
166 ):
167 return _expect_raises(except_cls, msg=msg, check_context=check_context)