Coverage for utilities/tests/test_callback.py: 100%

1"""Define the unit tests for the :mod:`colour.utilities.callback` module."""

2

3 from __future__ import annotations

4

5 from colour.utilities import MixinCallback

6

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"

13

14 __all__ = [

15 "TestMixinCallback",

16]

17

18

19 class TestMixinCallback:

20 """

21 Define :class:`colour.utilities.callback.MixinCallback` class unit

22 tests methods.

23 """

24

25 def setup_method(self) -> None:

26 """Initialise the common tests attributes."""

27

28 class WithCallback(MixinCallback):

29 """Test :class:`MixinCallback` class."""

30

31 def __init__(self) -> None:

32 super().__init__()

33

34 self.attribute_a = "a"

35

36 self._with_callback = WithCallback()

37

38 def _on_attribute_a_changed(

39 self: TestMixinCallback, name: str, value: str

40 ) -> str:

41 """Transform *self._attribute_a* to uppercase."""

42

43 value = value.upper()

44

45 if getattr(self, name) != "a": # pragma: no cover

46 error = '"self" was not able to retrieve class instance value!'

47

48 raise RuntimeError(error)

49

50 return value

51

52 self._on_attribute_a_changed = _on_attribute_a_changed

53

54 def test_required_attributes(self) -> None:

55 """Test the presence of required attributes."""

56

57 required_attributes = ("callbacks",)

58

59 for attribute in required_attributes:

60 assert attribute in dir(MixinCallback)

61

62 def test_required_methods(self) -> None:

63 """Test the presence of required methods."""

64

65 required_methods = (

66 "__init__",

67 "register_callback",

68 "unregister_callback",

69 )

70

71 for method in required_methods:

72 assert method in dir(MixinCallback)

73

74 def test_register_callback(self) -> None:

75 """

76 Test :class:`colour.utilities.callback.MixinCallback.register_callback`

77 method.

78 """

79

80 self._with_callback.register_callback(

81 "attribute_a",

82 "on_attribute_a_changed",

83 self._on_attribute_a_changed,

84 )

85

86 self._with_callback.attribute_a = "a"

87 assert self._with_callback.attribute_a == "A"

88 assert len(self._with_callback.callbacks) == 1

89

90 def test_unregister_callback(self) -> None:

91 """

92 Test :class:`colour.utilities.callback.MixinCallback.unregister_callback`

93 method.

94 """

95

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 )

102

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"