Coverage for utilities/tests/test_callback.py: 100%
41 statements
« prev ^ index » next coverage.py v7.11.0, created at 2025-11-16 22:49 +1300
« prev ^ index » next coverage.py v7.11.0, created at 2025-11-16 22:49 +1300
1"""Define the unit tests for the :mod:`colour.utilities.callback` module."""
3from __future__ import annotations
5from colour.utilities import MixinCallback
7__author__ = "Colour Developers"
8__copyright__ = "Copyright 2013 Colour Developers"
9__license__ = "BSD-3-Clause - https://opensource.org/licenses/BSD-3-Clause"
10__maintainer__ = "Colour Developers"
11__email__ = "colour-developers@colour-science.org"
12__status__ = "Production"
14__all__ = [
15 "TestMixinCallback",
16]
19class TestMixinCallback:
20 """
21 Define :class:`colour.utilities.callback.MixinCallback` class unit
22 tests methods.
23 """
25 def setup_method(self) -> None:
26 """Initialise the common tests attributes."""
28 class WithCallback(MixinCallback):
29 """Test :class:`MixinCallback` class."""
31 def __init__(self) -> None:
32 super().__init__()
34 self.attribute_a = "a"
36 self._with_callback = WithCallback()
38 def _on_attribute_a_changed(
39 self: TestMixinCallback, name: str, value: str
40 ) -> str:
41 """Transform *self._attribute_a* to uppercase."""
43 value = value.upper()
45 if getattr(self, name) != "a": # pragma: no cover
46 error = '"self" was not able to retrieve class instance value!'
48 raise RuntimeError(error)
50 return value
52 self._on_attribute_a_changed = _on_attribute_a_changed
54 def test_required_attributes(self) -> None:
55 """Test the presence of required attributes."""
57 required_attributes = ("callbacks",)
59 for attribute in required_attributes:
60 assert attribute in dir(MixinCallback)
62 def test_required_methods(self) -> None:
63 """Test the presence of required methods."""
65 required_methods = (
66 "__init__",
67 "register_callback",
68 "unregister_callback",
69 )
71 for method in required_methods:
72 assert method in dir(MixinCallback)
74 def test_register_callback(self) -> None:
75 """
76 Test :class:`colour.utilities.callback.MixinCallback.register_callback`
77 method.
78 """
80 self._with_callback.register_callback(
81 "attribute_a",
82 "on_attribute_a_changed",
83 self._on_attribute_a_changed,
84 )
86 self._with_callback.attribute_a = "a"
87 assert self._with_callback.attribute_a == "A"
88 assert len(self._with_callback.callbacks) == 1
90 def test_unregister_callback(self) -> None:
91 """
92 Test :class:`colour.utilities.callback.MixinCallback.unregister_callback`
93 method.
94 """
96 if len(self._with_callback.callbacks) == 0:
97 self._with_callback.register_callback(
98 "attribute_a",
99 "on_attribute_a_changed",
100 self._on_attribute_a_changed,
101 )
103 assert len(self._with_callback.callbacks) == 1
104 self._with_callback.unregister_callback("attribute_a", "on_attribute_a_changed")
105 assert len(self._with_callback.callbacks) == 0
106 self._with_callback.attribute_a = "a"
107 assert self._with_callback.attribute_a == "a"