MCPcopy Create free account
hub / github.com/google-deepmind/chex / ExceptionMessageFormatTest

Class ExceptionMessageFormatTest

chex/_src/asserts_internal_test.py:52–103  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

50
51
52class ExceptionMessageFormatTest(variants.TestCase):
53
54 @parameterized.product(
55 include_default_msg=(False, True),
56 include_custom_msg=(False, True),
57 exc_type=(AssertionError, ValueError),
58 )
59 def test_format(self, include_default_msg, include_custom_msg, exc_type):
60
61 exc_msg = lambda x: f'{x} is non-positive.'
62
63 @functools.partial(ai.chex_assertion, jittable_assert_fn=None)
64 def assert_positive(x):
65 if x <= 0:
66 raise AssertionError(exc_msg(x))
67
68 @functools.partial(ai.chex_assertion, jittable_assert_fn=None)
69 def assert_each_positive(*args):
70 for x in args:
71 assert_positive(x)
72
73 # Pass.
74 assert_positive(1)
75 assert_each_positive(1, 2, 3)
76
77 # Check the format of raised exceptions' messages.
78 def expected_exc_msg(x, custom_msg):
79 msg = exc_msg(x) if include_default_msg else ''
80 msg = rf'{msg} \[{custom_msg}\]' if custom_msg else msg
81 return msg
82
83 # Run in a loop to generate different custom messages.
84 for i in range(3):
85 custom_msg = f'failed at iter {i}' if include_custom_msg else ''
86
87 with self.assertRaisesRegex(
88 exc_type, ai.get_err_regex(expected_exc_msg(-1, custom_msg))):
89 assert_positive( # pylint:disable=unexpected-keyword-arg
90 -1,
91 custom_message=custom_msg,
92 include_default_message=include_default_msg,
93 exception_type=exc_type)
94
95 with self.assertRaisesRegex(
96 exc_type, ai.get_err_regex(expected_exc_msg(-3, custom_msg))):
97 assert_each_positive( # pylint:disable=unexpected-keyword-arg
98 1,
99 -3,
100 2,
101 custom_message=custom_msg,
102 include_default_message=include_default_msg,
103 exception_type=exc_type)
104
105
106class JitCompatibleTest(variants.TestCase):

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…