Skip to content

Commit 46a3fd2

Browse files
[3.14] gh-133346: add tests for _colorize.Theme (GH-139687) (#140622)
* gh-133346: add tests for `_colorize.Theme` (GH-139687) (cherry picked from commit 37827c1) Co-authored-by: Bénédikt Tran <[email protected]>
1 parent d664102 commit 46a3fd2

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

Lib/test/test__colorize.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import contextlib
2+
import dataclasses
23
import io
34
import sys
45
import unittest
@@ -21,6 +22,41 @@ def supports_virtual_terminal():
2122
return contextlib.nullcontext()
2223

2324

25+
class TestTheme(unittest.TestCase):
26+
27+
def test_attributes(self):
28+
# only theme configurations attributes by default
29+
for field in dataclasses.fields(_colorize.Theme):
30+
with self.subTest(field.name):
31+
self.assertIsSubclass(field.type, _colorize.ThemeSection)
32+
self.assertIsNotNone(field.default_factory)
33+
34+
def test_copy_with(self):
35+
theme = _colorize.Theme()
36+
37+
copy = theme.copy_with()
38+
self.assertEqual(theme, copy)
39+
40+
unittest_no_colors = _colorize.Unittest.no_colors()
41+
copy = theme.copy_with(unittest=unittest_no_colors)
42+
self.assertEqual(copy.argparse, theme.argparse)
43+
self.assertEqual(copy.syntax, theme.syntax)
44+
self.assertEqual(copy.traceback, theme.traceback)
45+
self.assertEqual(copy.unittest, unittest_no_colors)
46+
47+
def test_no_colors(self):
48+
# idempotence test
49+
theme_no_colors = _colorize.Theme().no_colors()
50+
theme_no_colors_no_colors = theme_no_colors.no_colors()
51+
self.assertEqual(theme_no_colors, theme_no_colors_no_colors)
52+
53+
# attributes check
54+
for section in dataclasses.fields(_colorize.Theme):
55+
with self.subTest(section.name):
56+
section_theme = getattr(theme_no_colors, section.name)
57+
self.assertEqual(section_theme, section.type.no_colors())
58+
59+
2460
class TestColorizeFunction(unittest.TestCase):
2561
def test_colorized_detection_checks_for_environment_variables(self):
2662
def check(env, fallback, expected):

0 commit comments

Comments
 (0)