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

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

2

3 from __future__ import annotations

4

5 import os

6 import sys

7 import textwrap

8

9 from 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)

20

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"

27

28 __all__ = [

29 "TestAsBool",

30 "TestMixinLogging",

31 "TestShowWarning",

32 "TestSuppressWarnings",

33 "TestSuppressStdout",

34 "TestDescribeEnvironment",

35 "TestMultilineStr",

36 "TestMultilineRepr",

37]

38

39

40 class TestAsBool:

41 """

42 Define :func:`colour.utilities.common.as_bool` definition unit tests

43 methods.

44 """

45

46 def test_as_bool(self) -> None:

47 """Test :func:`colour.utilities.common.as_bool` definition."""

48

49 assert as_bool("1")

50

51 assert as_bool("On")

52

53 assert as_bool("True")

54

55 assert not as_bool("0")

56

57 assert not as_bool("Off")

58

59 assert not as_bool("False")

60

61 assert not as_bool("")

62

63

64 class TestMixinLogging:

65 """

66 Define :class:`colour.utilities.verbose.MixinLogging` class unit tests

67 methods.

68 """

69

70 def test_required_methods(self) -> None:

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

72

73 required_methods = ("log",)

74

75 for method in required_methods:

76 assert method in dir(MixinLogging)

77

78

79 class TestShowWarning:

80 """

81 Define :func:`colour.utilities.verbose.show_warning` definition unit tests

82 methods.

83 """

84

85 def test_show_warning(self) -> None:

86 """Test :func:`colour.utilities.verbose.show_warning` definition."""

87

88 show_warning("This is a unit test warning!", Warning, __file__, 0)

89

90 with open(os.devnull) as dev_null:

91 show_warning("This is a unit test warning!", Warning, __file__, 0, dev_null)

92

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

99

100

101 class TestSuppressWarnings:

102 """

103 Define :func:`colour.utilities.verbose.suppress_warnings` definition unit

104 tests methods.

105 """

106

107 def test_suppress_warnings(self) -> None:

108 """Test :func:`colour.utilities.verbose.suppress_warnings` definition."""

109

110 with suppress_warnings():

111 warning("This is a suppressed unit test warning!")

112

113

114 class TestSuppressStdout:

115 """

116 Define :func:`colour.utilities.verbose.suppress_stdout` definition unit

117 tests methods.

118 """

119

120 def test_suppress_stdout(self) -> None:

121 """Test :func:`colour.utilities.verbose.suppress_stdout` definition."""

122

123 with suppress_stdout():

124 print("This is a suppressed message!") # noqa: T201

125

126

127 class TestDescribeEnvironment:

128 """

129 Define :func:`colour.utilities.verbose.describe_environment` definition

130 unit tests methods.

131 """

132

133 def test_describe_environment(self) -> None:

134 """Test :func:`colour.utilities.verbose.describe_environment` definition."""

135

136 environment = describe_environment()

137 assert isinstance(environment, dict)

138 assert sorted(environment.keys()) == [

139 "Interpreter",

140 "Runtime",

141 "colour-science.org",

142 ]

143

144 environment = describe_environment(development_packages=True)

145 assert sorted(environment.keys()) == [

146 "Development",

147 "Interpreter",

148 "Runtime",

149 "colour-science.org",

150 ]

151

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 ]

162

163

164 class TestMultilineStr:

165 """

166 Define :func:`colour.utilities.verbose.multiline_str` definition unit

167 tests methods.

168 """

169

170 def test_multiline_str(self) -> None:

171 """Test :func:`colour.utilities.verbose.multiline_str` definition."""

172

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

178

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 )

206

207 assert str(Data("Foo", 1, ["John", "Doe"])) == (

208 textwrap.dedent(

209 """

210 Object - Data

211 =============

212

213 Data

214 ----

215

216 String

217 ------

218 String "a" : Foo

219

220 Integer

221 -------

222 Integer "b" : 1

223

224 List

225 ----

226 List "c" : John; Doe

227 """

228 ).strip()

229 )

230

231

232 class TestMultilineRepr:

233 """

234 Define :func:`colour.utilities.verbose.multiline_repr` definition unit

235 tests methods.

236 """

237

238 def test_multiline_repr(self) -> None:

239 """Test :func:`colour.utilities.verbose.multiline_repr` definition."""

240

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

247

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 )

266

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 )