Skip to content
Myr

test_style.py

This file

Path

tests/test_utils/test_style.py

127 lines · 4.7 KiB · Python

Last changed by

Strip all ANSI sequences (#3681)Rowlando13before adoption8c1a0a7

History of this file

Take it

1import pytest23import click456@pytest.mark.parametrize(7    ("styles", "ref"),8    [9        ({}, "x y\x1b[0m"),10        ({"fg": "black"}, "\x1b[30mx y\x1b[0m"),11        ({"fg": "red"}, "\x1b[31mx y\x1b[0m"),12        ({"fg": "green"}, "\x1b[32mx y\x1b[0m"),13        ({"fg": "yellow"}, "\x1b[33mx y\x1b[0m"),14        ({"fg": "blue"}, "\x1b[34mx y\x1b[0m"),15        ({"fg": "magenta"}, "\x1b[35mx y\x1b[0m"),16        ({"fg": "cyan"}, "\x1b[36mx y\x1b[0m"),17        ({"fg": "white"}, "\x1b[37mx y\x1b[0m"),18        ({"fg": "bright_black"}, "\x1b[90mx y\x1b[0m"),19        ({"fg": "bright_red"}, "\x1b[91mx y\x1b[0m"),20        ({"fg": "bright_green"}, "\x1b[92mx y\x1b[0m"),21        ({"fg": "bright_yellow"}, "\x1b[93mx y\x1b[0m"),22        ({"fg": "bright_blue"}, "\x1b[94mx y\x1b[0m"),23        ({"fg": "bright_magenta"}, "\x1b[95mx y\x1b[0m"),24        ({"fg": "bright_cyan"}, "\x1b[96mx y\x1b[0m"),25        ({"fg": "bright_white"}, "\x1b[97mx y\x1b[0m"),26        ({"fg": "reset"}, "\x1b[39mx y\x1b[0m"),27        ({"bg": "black"}, "\x1b[40mx y\x1b[0m"),28        ({"bg": "red"}, "\x1b[41mx y\x1b[0m"),29        ({"bg": "green"}, "\x1b[42mx y\x1b[0m"),30        ({"bg": "yellow"}, "\x1b[43mx y\x1b[0m"),31        ({"bg": "blue"}, "\x1b[44mx y\x1b[0m"),32        ({"bg": "magenta"}, "\x1b[45mx y\x1b[0m"),33        ({"bg": "cyan"}, "\x1b[46mx y\x1b[0m"),34        ({"bg": "white"}, "\x1b[47mx y\x1b[0m"),35        ({"bg": "bright_black"}, "\x1b[100mx y\x1b[0m"),36        ({"bg": "bright_red"}, "\x1b[101mx y\x1b[0m"),37        ({"bg": "bright_green"}, "\x1b[102mx y\x1b[0m"),38        ({"bg": "bright_yellow"}, "\x1b[103mx y\x1b[0m"),39        ({"bg": "bright_blue"}, "\x1b[104mx y\x1b[0m"),40        ({"bg": "bright_magenta"}, "\x1b[105mx y\x1b[0m"),41        ({"bg": "bright_cyan"}, "\x1b[106mx y\x1b[0m"),42        ({"bg": "bright_white"}, "\x1b[107mx y\x1b[0m"),43        ({"bg": "reset"}, "\x1b[49mx y\x1b[0m"),44        ({"fg": 91}, "\x1b[38;5;91mx y\x1b[0m"),45        ({"bg": 91}, "\x1b[48;5;91mx y\x1b[0m"),46        ({"fg": 255}, "\x1b[38;5;255mx y\x1b[0m"),47        ({"bg": 255}, "\x1b[48;5;255mx y\x1b[0m"),48        ({"fg": (135, 0, 175)}, "\x1b[38;2;135;0;175mx y\x1b[0m"),49        ({"bg": (135, 0, 175)}, "\x1b[48;2;135;0;175mx y\x1b[0m"),50        ({"bg": [135, 0, 175]}, "\x1b[48;2;135;0;175mx y\x1b[0m"),51        ({"fg": (0, 0, 0)}, "\x1b[38;2;0;0;0mx y\x1b[0m"),52        ({"fg": (255, 255, 255)}, "\x1b[38;2;255;255;255mx y\x1b[0m"),53        # 256-color index 0 (black) is valid and must not be dropped by a54        # truthiness check on fg/bg.55        ({"fg": 0}, "\x1b[38;5;0mx y\x1b[0m"),56        ({"bg": 0}, "\x1b[48;5;0mx y\x1b[0m"),57        ({"bold": True}, "\x1b[1mx y\x1b[0m"),58        ({"dim": True}, "\x1b[2mx y\x1b[0m"),59        ({"underline": True}, "\x1b[4mx y\x1b[0m"),60        ({"overline": True}, "\x1b[53mx y\x1b[0m"),61        ({"italic": True}, "\x1b[3mx y\x1b[0m"),62        ({"blink": True}, "\x1b[5mx y\x1b[0m"),63        ({"reverse": True}, "\x1b[7mx y\x1b[0m"),64        ({"strikethrough": True}, "\x1b[9mx y\x1b[0m"),65        ({"bold": False}, "\x1b[22mx y\x1b[0m"),66        ({"dim": False}, "\x1b[22mx y\x1b[0m"),67        ({"underline": False}, "\x1b[24mx y\x1b[0m"),68        ({"overline": False}, "\x1b[55mx y\x1b[0m"),69        ({"italic": False}, "\x1b[23mx y\x1b[0m"),70        ({"blink": False}, "\x1b[25mx y\x1b[0m"),71        ({"reverse": False}, "\x1b[27mx y\x1b[0m"),72        ({"strikethrough": False}, "\x1b[29mx y\x1b[0m"),73        ({"fg": "black", "reset": False}, "\x1b[30mx y"),74    ],75)76def test_styling(styles, ref):77    assert click.style("x y", **styles) == ref78    assert click.unstyle(ref) == "x y"798081@pytest.mark.parametrize("param", ["fg", "bg"])82@pytest.mark.parametrize(83    "value",84    [85        "",86        "banana",87        "BLACK",88        b"red",89        True,90        False,91        -1,92        256,93        0.0,94        (),95        [],96        (0, 0),97        (0, 0, 0, 0),98        ("0", "0", "0"),99        (True, False, True),100        (-1, 0, 0),101        (0, 256, 0),102        (0.0, 0.0, 0.0),103    ],104)105def test_styling_invalid_color(param, value):106    with pytest.raises(ValueError, match="Unknown color"):107        click.style("x y", **{param: value})108109110@pytest.mark.parametrize(111    ("text", "expect"),112    [113        ("\x1b[?25lx y\x1b[?25h", "x y"),114        # Colon-delimited true-color SGR (ISO 8613-6).115        ("\x1b[38:2:255:0:0mx y\x1b[0m", "x y"),116        # 256-color with colon sub-parameters.117        ("\x1b[38:5:200mx y\x1b[0m", "x y"),118        # SGR mouse reporting.119        ("\x1b[<0;1;1Mx y\x1b[<0;1;1m", "x y"),120        # Intermediate byte before the final byte.121        ("\x1b[0 qx y", "x y"),122        # Non-letter final byte.123        ("x\x1b[3~y", "xy"),124    ],125)126def test_unstyle_other_ansi(text, expect):127    assert click.unstyle(text) == expect