|
| 1 | +"""Unit testing for cmd2/rich_utils.py module""" |
| 2 | + |
| 3 | +import pytest |
| 4 | +from rich.style import Style |
| 5 | +from rich.text import Text |
| 6 | + |
| 7 | +from cmd2 import ( |
| 8 | + Cmd2Style, |
| 9 | + Color, |
| 10 | +) |
| 11 | +from cmd2 import rich_utils as ru |
| 12 | +from cmd2 import string_utils as su |
| 13 | + |
| 14 | + |
| 15 | +def test_string_to_rich_text() -> None: |
| 16 | + # Line breaks recognized by str.splitlines(). |
| 17 | + # Source: https://docs.python.org/3/library/stdtypes.html#str.splitlines |
| 18 | + line_breaks = { |
| 19 | + "\n", # Line Feed |
| 20 | + "\r", # Carriage Return |
| 21 | + "\r\n", # Carriage Return + Line Feed |
| 22 | + "\v", # Vertical Tab |
| 23 | + "\f", # Form Feed |
| 24 | + "\x1c", # File Separator |
| 25 | + "\x1d", # Group Separator |
| 26 | + "\x1e", # Record Separator |
| 27 | + "\x85", # Next Line (NEL) |
| 28 | + "\u2028", # Line Separator |
| 29 | + "\u2029", # Paragraph Separator |
| 30 | + } |
| 31 | + |
| 32 | + # Test all line breaks |
| 33 | + for lb in line_breaks: |
| 34 | + input_string = f"Text{lb}" |
| 35 | + expected_output = input_string.replace(lb, "\n") |
| 36 | + assert ru.string_to_rich_text(input_string).plain == expected_output |
| 37 | + |
| 38 | + # Test string without trailing line break |
| 39 | + input_string = "No trailing\nline break" |
| 40 | + assert ru.string_to_rich_text(input_string).plain == input_string |
| 41 | + |
| 42 | + # Test empty string |
| 43 | + input_string = "" |
| 44 | + assert ru.string_to_rich_text(input_string).plain == input_string |
| 45 | + |
| 46 | + |
| 47 | +@pytest.mark.parametrize( |
| 48 | + ('rich_text', 'string'), |
| 49 | + [ |
| 50 | + (Text("Hello"), "Hello"), |
| 51 | + (Text("Hello\n"), "Hello\n"), |
| 52 | + (Text("Hello", style="blue"), su.stylize("Hello", style="blue")), |
| 53 | + ], |
| 54 | +) |
| 55 | +def test_rich_text_to_string(rich_text: Text, string: str) -> None: |
| 56 | + assert ru.rich_text_to_string(rich_text) == string |
| 57 | + |
| 58 | + |
| 59 | +def test_set_style() -> None: |
| 60 | + # Save a cmd2, rich-argparse, and rich-specific style. |
| 61 | + cmd2_style_key = Cmd2Style.ERROR |
| 62 | + argparse_style_key = "argparse.args" |
| 63 | + rich_style_key = "inspect.attr" |
| 64 | + |
| 65 | + orig_cmd2_style = ru.APP_THEME.styles[cmd2_style_key] |
| 66 | + orig_argparse_style = ru.APP_THEME.styles[argparse_style_key] |
| 67 | + orig_rich_style = ru.APP_THEME.styles[rich_style_key] |
| 68 | + |
| 69 | + # Overwrite these styles by setting a new theme. |
| 70 | + theme = { |
| 71 | + cmd2_style_key: Style(color=Color.CYAN), |
| 72 | + argparse_style_key: Style(color=Color.AQUAMARINE3, underline=True), |
| 73 | + rich_style_key: Style(color=Color.DARK_GOLDENROD, bold=True), |
| 74 | + } |
| 75 | + ru.set_theme(theme) |
| 76 | + |
| 77 | + # Verify theme styles have changed to our custom values. |
| 78 | + assert ru.APP_THEME.styles[cmd2_style_key] != orig_cmd2_style |
| 79 | + assert ru.APP_THEME.styles[cmd2_style_key] == theme[cmd2_style_key] |
| 80 | + |
| 81 | + assert ru.APP_THEME.styles[argparse_style_key] != orig_argparse_style |
| 82 | + assert ru.APP_THEME.styles[argparse_style_key] == theme[argparse_style_key] |
| 83 | + |
| 84 | + assert ru.APP_THEME.styles[rich_style_key] != orig_rich_style |
| 85 | + assert ru.APP_THEME.styles[rich_style_key] == theme[rich_style_key] |
0 commit comments