|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import itertools |
| 4 | +import operator |
| 5 | +from typing import TYPE_CHECKING |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +from sphinx.util.console import blue, reset, strip_colors, strip_escape_sequences |
| 10 | + |
| 11 | +if TYPE_CHECKING: |
| 12 | + from collections.abc import Callable, Sequence |
| 13 | + from typing import Final, TypeVar |
| 14 | + |
| 15 | + _T = TypeVar('_T') |
| 16 | + |
| 17 | +CURSOR_UP: Final[str] = '\x1b[2A' # ignored ANSI code |
| 18 | +ERASE_LINE: Final[str] = '\x1b[2K' # supported ANSI code |
| 19 | +TEXT: Final[str] = '\x07 Hello world!' |
| 20 | + |
| 21 | + |
| 22 | +@pytest.mark.parametrize( |
| 23 | + ('strip_function', 'ansi_base_blocks', 'text_base_blocks'), |
| 24 | + [ |
| 25 | + ( |
| 26 | + strip_colors, |
| 27 | + # double ERASE_LINE so that the tested strings may have 2 of them |
| 28 | + [TEXT, blue(TEXT), reset(TEXT), ERASE_LINE, ERASE_LINE, CURSOR_UP], |
| 29 | + # :func:`strip_colors` removes color codes but keeps ERASE_LINE and CURSOR_UP |
| 30 | + [TEXT, TEXT, TEXT, ERASE_LINE, ERASE_LINE, CURSOR_UP], |
| 31 | + ), |
| 32 | + ( |
| 33 | + strip_escape_sequences, |
| 34 | + # double ERASE_LINE so that the tested strings may have 2 of them |
| 35 | + [TEXT, blue(TEXT), reset(TEXT), ERASE_LINE, ERASE_LINE, CURSOR_UP], |
| 36 | + # :func:`strip_escape_sequences` strips ANSI codes known by Sphinx |
| 37 | + [TEXT, TEXT, TEXT, '', '', CURSOR_UP], |
| 38 | + ), |
| 39 | + ], |
| 40 | + ids=[strip_colors.__name__, strip_escape_sequences.__name__], |
| 41 | +) |
| 42 | +def test_strip_ansi( |
| 43 | + strip_function: Callable[[str], str], |
| 44 | + ansi_base_blocks: Sequence[str], |
| 45 | + text_base_blocks: Sequence[str], |
| 46 | +) -> None: |
| 47 | + assert callable(strip_function) |
| 48 | + assert len(text_base_blocks) == len(ansi_base_blocks) |
| 49 | + N = len(ansi_base_blocks) |
| 50 | + |
| 51 | + def next_ansi_blocks(choices: Sequence[str], n: int) -> Sequence[str]: |
| 52 | + # Get a list of *n* words from a cyclic sequence of *choices*. |
| 53 | + # |
| 54 | + # For instance ``next_ansi_blocks(['a', 'b'], 3) == ['a', 'b', 'a']``. |
| 55 | + stream = itertools.cycle(choices) |
| 56 | + return list(map(operator.itemgetter(0), zip(stream, range(n)))) |
| 57 | + |
| 58 | + # generate all permutations of length N |
| 59 | + for sigma in itertools.permutations(range(N), N): |
| 60 | + # apply the permutation on the blocks with ANSI codes |
| 61 | + ansi_blocks = list(map(ansi_base_blocks.__getitem__, sigma)) |
| 62 | + # apply the permutation on the blocks with stripped codes |
| 63 | + text_blocks = list(map(text_base_blocks.__getitem__, sigma)) |
| 64 | + |
| 65 | + for glue, n in itertools.product(['.', '\n', '\r\n'], range(4 * N)): |
| 66 | + ansi_strings = next_ansi_blocks(ansi_blocks, n) |
| 67 | + text_strings = next_ansi_blocks(text_blocks, n) |
| 68 | + assert len(ansi_strings) == len(text_strings) == n |
| 69 | + |
| 70 | + ansi_string = glue.join(ansi_strings) |
| 71 | + text_string = glue.join(text_strings) |
| 72 | + assert strip_function(ansi_string) == text_string |
| 73 | + |
| 74 | + |
| 75 | +def test_strip_ansi_short_forms(): |
| 76 | + # In Sphinx, we always "normalize" the color codes so that they |
| 77 | + # match "\x1b\[(\d\d;){0,2}(\d\d)m" but it might happen that |
| 78 | + # some messages use '\x1b[0m' instead of ``reset(s)``, so we |
| 79 | + # test whether this alternative form is supported or not. |
| 80 | + |
| 81 | + for strip_function in [strip_colors, strip_escape_sequences]: |
| 82 | + # \x1b[m and \x1b[0m are equivalent to \x1b[00m |
| 83 | + assert strip_function('\x1b[m') == '' |
| 84 | + assert strip_function('\x1b[0m') == '' |
| 85 | + |
| 86 | + # \x1b[1m is equivalent to \x1b[01m |
| 87 | + assert strip_function('\x1b[1mbold\x1b[0m') == 'bold' |
| 88 | + |
| 89 | + # \x1b[K is equivalent to \x1b[0K |
| 90 | + assert strip_escape_sequences('\x1b[K') == '' |
0 commit comments