Coverage for colour/utilities/tests/test_verbose.py: 100%
74 statements
« prev ^ index » next coverage.py v7.11.0, created at 2025-11-15 19:01 +1300
« prev ^ index » next coverage.py v7.11.0, created at 2025-11-15 19:01 +1300
1"""Define the unit tests for the :mod:`colour.utilities.verbose` module."""
3from __future__ import annotations
5import os
6import sys
7import textwrap
9from colour.utilities import (
10 MixinLogging,
11 as_bool,
12 describe_environment,
13 multiline_repr,
14 multiline_str,
15 show_warning,
16 suppress_stdout,
17 suppress_warnings,
18 warning,
19)
21__author__ = "Colour Developers"
22__copyright__ = "Copyright 2013 Colour Developers"
23__license__ = "BSD-3-Clause - https://opensource.org/licenses/BSD-3-Clause"
24__maintainer__ = "Colour Developers"
25__email__ = "colour-developers@colour-science.org"
26__status__ = "Production"
28__all__ = [
29 "TestAsBool",
30 "TestMixinLogging",
31 "TestShowWarning",
32 "TestSuppressWarnings",
33 "TestSuppressStdout",
34 "TestDescribeEnvironment",
35 "TestMultilineStr",
36 "TestMultilineRepr",
37]
40class TestAsBool:
41 """
42 Define :func:`colour.utilities.common.as_bool` definition unit tests
43 methods.
44 """
46 def test_as_bool(self) -> None:
47 """Test :func:`colour.utilities.common.as_bool` definition."""
49 assert as_bool("1")
51 assert as_bool("On")
53 assert as_bool("True")
55 assert not as_bool("0")
57 assert not as_bool("Off")
59 assert not as_bool("False")
61 assert not as_bool("")
64class TestMixinLogging:
65 """
66 Define :class:`colour.utilities.verbose.MixinLogging` class unit tests
67 methods.
68 """
70 def test_required_methods(self) -> None:
71 """Test the presence of required methods."""
73 required_methods = ("log",)
75 for method in required_methods:
76 assert method in dir(MixinLogging)
79class TestShowWarning:
80 """
81 Define :func:`colour.utilities.verbose.show_warning` definition unit tests
82 methods.
83 """
85 def test_show_warning(self) -> None:
86 """Test :func:`colour.utilities.verbose.show_warning` definition."""
88 show_warning("This is a unit test warning!", Warning, __file__, 0)
90 with open(os.devnull) as dev_null:
91 show_warning("This is a unit test warning!", Warning, __file__, 0, dev_null)
93 stderr = sys.stderr
94 try:
95 sys.stderr = None
96 show_warning("This is a unit test warning!", Warning, __file__, 0)
97 finally:
98 sys.stderr = stderr
101class TestSuppressWarnings:
102 """
103 Define :func:`colour.utilities.verbose.suppress_warnings` definition unit
104 tests methods.
105 """
107 def test_suppress_warnings(self) -> None:
108 """Test :func:`colour.utilities.verbose.suppress_warnings` definition."""
110 with suppress_warnings():
111 warning("This is a suppressed unit test warning!")
114class TestSuppressStdout:
115 """
116 Define :func:`colour.utilities.verbose.suppress_stdout` definition unit
117 tests methods.
118 """
120 def test_suppress_stdout(self) -> None:
121 """Test :func:`colour.utilities.verbose.suppress_stdout` definition."""
123 with suppress_stdout():
124 print("This is a suppressed message!") # noqa: T201
127class TestDescribeEnvironment:
128 """
129 Define :func:`colour.utilities.verbose.describe_environment` definition
130 unit tests methods.
131 """
133 def test_describe_environment(self) -> None:
134 """Test :func:`colour.utilities.verbose.describe_environment` definition."""
136 environment = describe_environment()
137 assert isinstance(environment, dict)
138 assert sorted(environment.keys()) == [
139 "Interpreter",
140 "Runtime",
141 "colour-science.org",
142 ]
144 environment = describe_environment(development_packages=True)
145 assert sorted(environment.keys()) == [
146 "Development",
147 "Interpreter",
148 "Runtime",
149 "colour-science.org",
150 ]
152 environment = describe_environment(
153 development_packages=True, extras_packages=True
154 )
155 assert sorted(environment.keys()) == [
156 "Development",
157 "Extras",
158 "Interpreter",
159 "Runtime",
160 "colour-science.org",
161 ]
164class TestMultilineStr:
165 """
166 Define :func:`colour.utilities.verbose.multiline_str` definition unit
167 tests methods.
168 """
170 def test_multiline_str(self) -> None:
171 """Test :func:`colour.utilities.verbose.multiline_str` definition."""
173 class Data:
174 def __init__(self, a: str, b: int, c: list) -> None:
175 self._a = a
176 self._b = b
177 self._c = c
179 def __str__(self) -> str:
180 return multiline_str(
181 self,
182 [
183 {
184 "formatter": lambda x: ( # noqa: ARG005
185 f"Object - {self.__class__.__name__}"
186 ),
187 "header": True,
188 },
189 {"line_break": True},
190 {"label": "Data", "section": True},
191 {"line_break": True},
192 {"label": "String", "section": True},
193 {"name": "_a", "label": 'String "a"'},
194 {"line_break": True},
195 {"label": "Integer", "section": True},
196 {"name": "_b", "label": 'Integer "b"'},
197 {"line_break": True},
198 {"label": "List", "section": True},
199 {
200 "name": "_c",
201 "label": 'List "c"',
202 "formatter": lambda x: "; ".join(x),
203 },
204 ],
205 )
207 assert str(Data("Foo", 1, ["John", "Doe"])) == (
208 textwrap.dedent(
209 """
210 Object - Data
211 =============
213 Data
214 ----
216 String
217 ------
218 String "a" : Foo
220 Integer
221 -------
222 Integer "b" : 1
224 List
225 ----
226 List "c" : John; Doe
227 """
228 ).strip()
229 )
232class TestMultilineRepr:
233 """
234 Define :func:`colour.utilities.verbose.multiline_repr` definition unit
235 tests methods.
236 """
238 def test_multiline_repr(self) -> None:
239 """Test :func:`colour.utilities.verbose.multiline_repr` definition."""
241 class Data:
242 def __init__(self, a: str, b: int, c: list, d: str | None = None) -> None:
243 self._a = a
244 self._b = b
245 self._c = c
246 self._d = d
248 def __repr__(self) -> str:
249 return multiline_repr(
250 self,
251 [
252 {"name": "_a"},
253 {"name": "_b"},
254 {
255 "name": "_c",
256 "formatter": lambda x: repr(x)
257 .replace("[", "(")
258 .replace("]", ")"),
259 },
260 {
261 "name": "_d",
262 "formatter": lambda x: None, # noqa: ARG005
263 },
264 ],
265 )
267 assert repr(Data("Foo", 1, ["John", "Doe"])) == (
268 textwrap.dedent(
269 """
270 Data('Foo',
271 1,
272 ('John', 'Doe'),
273 None)
274 """
275 ).strip()
276 )