mirror of
https://github.com/python/cpython.git
synced 2025-07-24 03:35:53 +00:00
gh-133346: Make theming support in _colorize extensible (GH-133347)
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
This commit is contained in:
parent
9cc77aaf9d
commit
f610bbdf74
20 changed files with 585 additions and 371 deletions
|
@ -6,9 +6,11 @@ import unittest
|
|||
import subprocess
|
||||
|
||||
from test import support
|
||||
from test.support import force_not_colorized, os_helper
|
||||
from test.support import force_colorized, force_not_colorized, os_helper
|
||||
from test.support.script_helper import assert_python_ok
|
||||
|
||||
from _colorize import get_theme
|
||||
|
||||
|
||||
@support.requires_subprocess()
|
||||
class TestMain(unittest.TestCase):
|
||||
|
@ -246,34 +248,39 @@ class TestMain(unittest.TestCase):
|
|||
proc.communicate(b'"{}"')
|
||||
self.assertEqual(proc.returncode, errno.EPIPE)
|
||||
|
||||
@force_colorized
|
||||
def test_colors(self):
|
||||
infile = os_helper.TESTFN
|
||||
self.addCleanup(os.remove, infile)
|
||||
|
||||
t = get_theme().syntax
|
||||
ob = "{"
|
||||
cb = "}"
|
||||
|
||||
cases = (
|
||||
('{}', b'{}'),
|
||||
('[]', b'[]'),
|
||||
('null', b'\x1b[1;36mnull\x1b[0m'),
|
||||
('true', b'\x1b[1;36mtrue\x1b[0m'),
|
||||
('false', b'\x1b[1;36mfalse\x1b[0m'),
|
||||
('NaN', b'NaN'),
|
||||
('Infinity', b'Infinity'),
|
||||
('-Infinity', b'-Infinity'),
|
||||
('"foo"', b'\x1b[1;32m"foo"\x1b[0m'),
|
||||
(r'" \"foo\" "', b'\x1b[1;32m" \\"foo\\" "\x1b[0m'),
|
||||
('"α"', b'\x1b[1;32m"\\u03b1"\x1b[0m'),
|
||||
('123', b'123'),
|
||||
('-1.2345e+23', b'-1.2345e+23'),
|
||||
('{}', '{}'),
|
||||
('[]', '[]'),
|
||||
('null', f'{t.keyword}null{t.reset}'),
|
||||
('true', f'{t.keyword}true{t.reset}'),
|
||||
('false', f'{t.keyword}false{t.reset}'),
|
||||
('NaN', f'{t.number}NaN{t.reset}'),
|
||||
('Infinity', f'{t.number}Infinity{t.reset}'),
|
||||
('-Infinity', f'{t.number}-Infinity{t.reset}'),
|
||||
('"foo"', f'{t.string}"foo"{t.reset}'),
|
||||
(r'" \"foo\" "', f'{t.string}" \\"foo\\" "{t.reset}'),
|
||||
('"α"', f'{t.string}"\\u03b1"{t.reset}'),
|
||||
('123', f'{t.number}123{t.reset}'),
|
||||
('-1.2345e+23', f'{t.number}-1.2345e+23{t.reset}'),
|
||||
(r'{"\\": ""}',
|
||||
b'''\
|
||||
{
|
||||
\x1b[94m"\\\\"\x1b[0m: \x1b[1;32m""\x1b[0m
|
||||
}'''),
|
||||
f'''\
|
||||
{ob}
|
||||
{t.definition}"\\\\"{t.reset}: {t.string}""{t.reset}
|
||||
{cb}'''),
|
||||
(r'{"\\\\": ""}',
|
||||
b'''\
|
||||
{
|
||||
\x1b[94m"\\\\\\\\"\x1b[0m: \x1b[1;32m""\x1b[0m
|
||||
}'''),
|
||||
f'''\
|
||||
{ob}
|
||||
{t.definition}"\\\\\\\\"{t.reset}: {t.string}""{t.reset}
|
||||
{cb}'''),
|
||||
('''\
|
||||
{
|
||||
"foo": "bar",
|
||||
|
@ -281,30 +288,32 @@ class TestMain(unittest.TestCase):
|
|||
"qux": [true, false, null],
|
||||
"xyz": [NaN, -Infinity, Infinity]
|
||||
}''',
|
||||
b'''\
|
||||
{
|
||||
\x1b[94m"foo"\x1b[0m: \x1b[1;32m"bar"\x1b[0m,
|
||||
\x1b[94m"baz"\x1b[0m: 1234,
|
||||
\x1b[94m"qux"\x1b[0m: [
|
||||
\x1b[1;36mtrue\x1b[0m,
|
||||
\x1b[1;36mfalse\x1b[0m,
|
||||
\x1b[1;36mnull\x1b[0m
|
||||
f'''\
|
||||
{ob}
|
||||
{t.definition}"foo"{t.reset}: {t.string}"bar"{t.reset},
|
||||
{t.definition}"baz"{t.reset}: {t.number}1234{t.reset},
|
||||
{t.definition}"qux"{t.reset}: [
|
||||
{t.keyword}true{t.reset},
|
||||
{t.keyword}false{t.reset},
|
||||
{t.keyword}null{t.reset}
|
||||
],
|
||||
\x1b[94m"xyz"\x1b[0m: [
|
||||
NaN,
|
||||
-Infinity,
|
||||
Infinity
|
||||
{t.definition}"xyz"{t.reset}: [
|
||||
{t.number}NaN{t.reset},
|
||||
{t.number}-Infinity{t.reset},
|
||||
{t.number}Infinity{t.reset}
|
||||
]
|
||||
}'''),
|
||||
{cb}'''),
|
||||
)
|
||||
|
||||
for input_, expected in cases:
|
||||
with self.subTest(input=input_):
|
||||
with open(infile, "w", encoding="utf-8") as fp:
|
||||
fp.write(input_)
|
||||
_, stdout, _ = assert_python_ok('-m', self.module, infile,
|
||||
PYTHON_COLORS='1')
|
||||
stdout = stdout.replace(b'\r\n', b'\n') # normalize line endings
|
||||
_, stdout_b, _ = assert_python_ok(
|
||||
'-m', self.module, infile, FORCE_COLOR='1', __isolated='1'
|
||||
)
|
||||
stdout = stdout_b.decode()
|
||||
stdout = stdout.replace('\r\n', '\n') # normalize line endings
|
||||
stdout = stdout.strip()
|
||||
self.assertEqual(stdout, expected)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue