1"""Assertions for Blueberry package."""
2
3import contextlib
4from typing import Iterator, Type
5
6from mobly import signals
7
8
9@contextlib.contextmanager
10def assert_not_raises(
11    exception: Type[Exception] = Exception) -> Iterator[None]:
12  """Asserts that the exception is not raised.
13
14  This assertion function is used to catch a specified exception
15  (or any exceptions) and raise signal.TestFailure instead.
16
17  Usage:
18    ```
19    with asserts.assert_not_raises(signals.TestError):
20      foo()
21    ```
22
23  Args:
24    exception: Exception to be catched. If not specify, catch any exceptions as
25      default.
26
27  Yields:
28    A context which may raise the exception.
29
30  Raises:
31    signals.TestFailure: raised when the exception is catched in the context.
32  """
33
34  try:
35    yield
36  except exception as e:  # pylint: disable=broad-except
37    raise signals.TestFailure(e)
38