Module devstatusdecos.tests.tests

tests

Holds tests for the module.

Classes

class Test (methodName: str = 'runTest')
Expand source code
class Test(TestCase):
    """
    `Test`

    Tests the decorators provided in this module.
    """

    OK_EXITS = {0}
    try:
        from os import EX_OK  # pylint:disable=import-outside-toplevel
    except ImportError:
        pass
    else:
        OK_EXITS.add(EX_OK)

    @staticmethod
    def _gen_wrap_example() -> Tuple[Callable[[], object], object]:
        o = object()
        return (lambda: o), o

    def __init__(self, methodName: str = "runTest") -> None:
        super().__init__(methodName)
        simplefilter('always', DeprecationWarning)
        simplefilter('always', PendingDeprecationWarning)
        simplefilter('always', ExperimentalWarning)
        simplefilter('always', TodoWarning)

    def test_deprecated_behaviour(self):
        """
        `test_deprecated_behaviour`

        Tests the `deprecated` decorator.
        Since this decorator is builtin,
        this should always pass.
        In cases where this doesn't,
        it likely indicates a sanity error.
        """
        reason = "Example reason..."
        c, v = self._gen_wrap_example()
        deco = deprecated(reason)
        wr = deco(c)
        with self.assertWarns(DeprecationWarning):
            r = wr()
        self.assertEqual(reason, wr.__deprecated__)
        self.assertIs(r, v)

    def test_pending_deprecation_behaviour(self):
        """
        `test_pending_deprecation_behaviour`

        Tests the `pending_deprecation` decorator.
        """
        reason = "Example reason..."

        c, v = self._gen_wrap_example()
        deco = pending_deprecation(reason)
        wr = deco(c)
        with self.assertWarns(PendingDeprecationWarning):
            r = wr()
        self.assertEqual(reason, wr.__deprecated__)  # type:ignore
        self.assertIs(r, v)

        c, v = self._gen_wrap_example()
        deco = pending_deprecation(reason, deprecate_py_ver = (0, 0))
        wr = deco(c)
        with self.assertWarns(DeprecationWarning):
            r = wr()
        self.assertEqual(reason, wr.__deprecated__)  # type:ignore
        self.assertIs(r, v)

        c, v = self._gen_wrap_example()
        deco = pending_deprecation(reason, deprecate_py_ver = version_info)
        wr = deco(c)
        with self.assertWarns(DeprecationWarning):
            r = wr()
        self.assertEqual(reason, wr.__deprecated__)  # type:ignore
        self.assertIs(r, v)

        c, v = self._gen_wrap_example()
        deco = pending_deprecation(reason, deprecate_py_ver = (100000, 1000000))
        wr = deco(c)
        with self.assertWarns(PendingDeprecationWarning):
            r = wr()
        self.assertEqual(reason, wr.__deprecated__)  # type:ignore
        self.assertIs(r, v)

    def test_experimental_behaviour(self):
        """
        `test_experimental_behaviour`

        Tests the `experimental` decorator.
        """
        reason = "Example reason..."

        c, v = self._gen_wrap_example()
        deco = experimental(reason)
        wr = deco(c)
        with self.assertWarns(ExperimentalWarning):
            r = wr()
        self.assertEqual(reason, wr.__deprecated__)
        self.assertIs(r, v)

    def test_todo_behaviour(self):
        """
        `test_todo_behaviour`

        Tests the `todo` decorator.
        """
        reason_no_prefix = "Example reason..."
        reason = todo.TODO_PREFIX + reason_no_prefix

        c, v = self._gen_wrap_example()
        deco = todo(reason)
        wr = deco(c)
        with self.assertWarns(TodoWarning):
            r = wr()
        self.assertEqual(reason, wr.__deprecated__)  # type:ignore
        self.assertTrue(wr.__doc__ is not None and wr.__doc__.startswith(reason))
        self.assertIs(r, v)

        c, v = self._gen_wrap_example()
        deco = todo(reason_no_prefix)
        wr = deco(c)
        with self.assertWarns(TodoWarning):
            r = wr()
        self.assertEqual(reason, wr.__deprecated__)  # type:ignore
        self.assertTrue(wr.__doc__ is not None and wr.__doc__.startswith(reason))
        self.assertIs(r, v)

        c, v = self._gen_wrap_example()
        deco = todo(reason, lambda _: False)
        wr = deco(c)
        with self.assertWarns(TodoWarning):
            r = wr()
        self.assertEqual(reason, wr.__deprecated__)  # type:ignore
        self.assertTrue(wr.__doc__ is not None and wr.__doc__.startswith(reason))
        self.assertIs(r, v)

        c, v = self._gen_wrap_example()
        deco = todo(reason, lambda _: True)
        wr = deco(c)
        with self.assertRaises(TodoError) as ex_cont:
            wr()
        self.assertEqual(reason, ex_cont.exception.args[0])
        self.assertEqual(reason, wr.__deprecated__)  # type:ignore
        self.assertTrue(wr.__doc__ is not None and wr.__doc__.startswith(reason))

    def test_insecure_behaviour(self):
        """
        `test_insecure_behaviour`

        Tests the `insecure` decorator.
        """
        reason = "Example reason..."

        c, _ = self._gen_wrap_example()
        deco = insecure(reason, False)
        wr = deco(c)
        with self.assertRaises(InsecureError):
            wr()

        c, _ = self._gen_wrap_example()
        deco = insecure(reason, True)
        wr = deco(c)
        with self.assertRaises(FatallyInsecureError) as ex_cont:
            wr()
        self.assertEqual(reason, ex_cont.exception.args[0])
        self.assertIsInstance(ex_cont.exception, SystemExit)
        self.assertNotIn(ex_cont.exception.code, self.OK_EXITS)

Test

Tests the decorators provided in this module.

Create an instance of the class that will use the named test method when executed. Raises a ValueError if the instance does not have a method with the specified name.

Ancestors

  • unittest.case.TestCase

Class variables

var EX_OK
var OK_EXITS

Methods

def test_deprecated_behaviour(self)
Expand source code
def test_deprecated_behaviour(self):
    """
    `test_deprecated_behaviour`

    Tests the `deprecated` decorator.
    Since this decorator is builtin,
    this should always pass.
    In cases where this doesn't,
    it likely indicates a sanity error.
    """
    reason = "Example reason..."
    c, v = self._gen_wrap_example()
    deco = deprecated(reason)
    wr = deco(c)
    with self.assertWarns(DeprecationWarning):
        r = wr()
    self.assertEqual(reason, wr.__deprecated__)
    self.assertIs(r, v)

test_deprecated_behaviour

Tests the deprecated decorator. Since this decorator is builtin, this should always pass. In cases where this doesn't, it likely indicates a sanity error.

def test_experimental_behaviour(self)
Expand source code
def test_experimental_behaviour(self):
    """
    `test_experimental_behaviour`

    Tests the `experimental` decorator.
    """
    reason = "Example reason..."

    c, v = self._gen_wrap_example()
    deco = experimental(reason)
    wr = deco(c)
    with self.assertWarns(ExperimentalWarning):
        r = wr()
    self.assertEqual(reason, wr.__deprecated__)
    self.assertIs(r, v)

test_experimental_behaviour

Tests the experimental decorator.

def test_insecure_behaviour(self)
Expand source code
def test_insecure_behaviour(self):
    """
    `test_insecure_behaviour`

    Tests the `insecure` decorator.
    """
    reason = "Example reason..."

    c, _ = self._gen_wrap_example()
    deco = insecure(reason, False)
    wr = deco(c)
    with self.assertRaises(InsecureError):
        wr()

    c, _ = self._gen_wrap_example()
    deco = insecure(reason, True)
    wr = deco(c)
    with self.assertRaises(FatallyInsecureError) as ex_cont:
        wr()
    self.assertEqual(reason, ex_cont.exception.args[0])
    self.assertIsInstance(ex_cont.exception, SystemExit)
    self.assertNotIn(ex_cont.exception.code, self.OK_EXITS)

test_insecure_behaviour

Tests the insecure decorator.

def test_pending_deprecation_behaviour(self)
Expand source code
def test_pending_deprecation_behaviour(self):
    """
    `test_pending_deprecation_behaviour`

    Tests the `pending_deprecation` decorator.
    """
    reason = "Example reason..."

    c, v = self._gen_wrap_example()
    deco = pending_deprecation(reason)
    wr = deco(c)
    with self.assertWarns(PendingDeprecationWarning):
        r = wr()
    self.assertEqual(reason, wr.__deprecated__)  # type:ignore
    self.assertIs(r, v)

    c, v = self._gen_wrap_example()
    deco = pending_deprecation(reason, deprecate_py_ver = (0, 0))
    wr = deco(c)
    with self.assertWarns(DeprecationWarning):
        r = wr()
    self.assertEqual(reason, wr.__deprecated__)  # type:ignore
    self.assertIs(r, v)

    c, v = self._gen_wrap_example()
    deco = pending_deprecation(reason, deprecate_py_ver = version_info)
    wr = deco(c)
    with self.assertWarns(DeprecationWarning):
        r = wr()
    self.assertEqual(reason, wr.__deprecated__)  # type:ignore
    self.assertIs(r, v)

    c, v = self._gen_wrap_example()
    deco = pending_deprecation(reason, deprecate_py_ver = (100000, 1000000))
    wr = deco(c)
    with self.assertWarns(PendingDeprecationWarning):
        r = wr()
    self.assertEqual(reason, wr.__deprecated__)  # type:ignore
    self.assertIs(r, v)

test_pending_deprecation_behaviour

Tests the pending_deprecation decorator.

def test_todo_behaviour(self)
Expand source code
def test_todo_behaviour(self):
    """
    `test_todo_behaviour`

    Tests the `todo` decorator.
    """
    reason_no_prefix = "Example reason..."
    reason = todo.TODO_PREFIX + reason_no_prefix

    c, v = self._gen_wrap_example()
    deco = todo(reason)
    wr = deco(c)
    with self.assertWarns(TodoWarning):
        r = wr()
    self.assertEqual(reason, wr.__deprecated__)  # type:ignore
    self.assertTrue(wr.__doc__ is not None and wr.__doc__.startswith(reason))
    self.assertIs(r, v)

    c, v = self._gen_wrap_example()
    deco = todo(reason_no_prefix)
    wr = deco(c)
    with self.assertWarns(TodoWarning):
        r = wr()
    self.assertEqual(reason, wr.__deprecated__)  # type:ignore
    self.assertTrue(wr.__doc__ is not None and wr.__doc__.startswith(reason))
    self.assertIs(r, v)

    c, v = self._gen_wrap_example()
    deco = todo(reason, lambda _: False)
    wr = deco(c)
    with self.assertWarns(TodoWarning):
        r = wr()
    self.assertEqual(reason, wr.__deprecated__)  # type:ignore
    self.assertTrue(wr.__doc__ is not None and wr.__doc__.startswith(reason))
    self.assertIs(r, v)

    c, v = self._gen_wrap_example()
    deco = todo(reason, lambda _: True)
    wr = deco(c)
    with self.assertRaises(TodoError) as ex_cont:
        wr()
    self.assertEqual(reason, ex_cont.exception.args[0])
    self.assertEqual(reason, wr.__deprecated__)  # type:ignore
    self.assertTrue(wr.__doc__ is not None and wr.__doc__.startswith(reason))

test_todo_behaviour

Tests the todo decorator.