|
1 | 1 | """Unit testing for cmd2/rich_utils.py module""" |
2 | 2 |
|
| 3 | +from unittest import mock |
| 4 | + |
3 | 5 | import pytest |
4 | 6 | import rich.box |
5 | 7 | from pytest_mock import MockerFixture |
|
14 | 16 | ) |
15 | 17 | from cmd2 import rich_utils as ru |
16 | 18 |
|
| 19 | +from .conftest import with_ansi_style |
| 20 | + |
17 | 21 |
|
18 | 22 | def test_cmd2_base_console() -> None: |
19 | 23 | # Test the keyword arguments which are not allowed. |
| 24 | + with pytest.raises(TypeError) as excinfo: |
| 25 | + ru.Cmd2BaseConsole(color_system="auto") |
| 26 | + assert 'color_system' in str(excinfo.value) |
| 27 | + |
20 | 28 | with pytest.raises(TypeError) as excinfo: |
21 | 29 | ru.Cmd2BaseConsole(force_terminal=True) |
22 | 30 | assert 'force_terminal' in str(excinfo.value) |
@@ -73,7 +81,12 @@ def test_indented_table() -> None: |
73 | 81 | [ |
74 | 82 | (Text("Hello"), "Hello"), |
75 | 83 | (Text("Hello\n"), "Hello\n"), |
76 | | - (Text("Hello", style="blue"), "\x1b[34mHello\x1b[0m"), |
| 84 | + # Test standard color support |
| 85 | + (Text("Standard", style="blue"), "\x1b[34mStandard\x1b[0m"), |
| 86 | + # Test 256-color support |
| 87 | + (Text("256-color", style=Color.NAVY_BLUE), "\x1b[38;5;17m256-color\x1b[0m"), |
| 88 | + # Test 24-bit color (TrueColor) support |
| 89 | + (Text("TrueColor", style="#123456"), "\x1b[38;2;18;52;86mTrueColor\x1b[0m"), |
77 | 90 | ], |
78 | 91 | ) |
79 | 92 | def test_rich_text_to_string(rich_text: Text, string: str) -> None: |
@@ -155,3 +168,88 @@ def test_cmd2_base_console_log(mocker: MockerFixture) -> None: |
155 | 168 | args, kwargs = mock_super_log.call_args |
156 | 169 | assert args == prepared_val |
157 | 170 | assert kwargs["_stack_offset"] == 3 |
| 171 | + |
| 172 | + |
| 173 | +@with_ansi_style(ru.AllowStyle.ALWAYS) |
| 174 | +def test_cmd2_base_console_init_always_interactive_true() -> None: |
| 175 | + """Test Cmd2BaseConsole initialization when ALLOW_STYLE is ALWAYS and is_interactive is True.""" |
| 176 | + with ( |
| 177 | + mock.patch('rich.console.Console.__init__', return_value=None) as mock_base_init, |
| 178 | + mock.patch('cmd2.rich_utils.Console', autospec=True) as mock_detect_console_class, |
| 179 | + ): |
| 180 | + mock_detect_console = mock_detect_console_class.return_value |
| 181 | + mock_detect_console.is_interactive = True |
| 182 | + |
| 183 | + ru.Cmd2BaseConsole() |
| 184 | + |
| 185 | + # Verify arguments passed to super().__init__ |
| 186 | + _, kwargs = mock_base_init.call_args |
| 187 | + assert kwargs['color_system'] == "truecolor" |
| 188 | + assert kwargs['force_terminal'] is True |
| 189 | + assert kwargs['force_interactive'] is True |
| 190 | + |
| 191 | + |
| 192 | +@with_ansi_style(ru.AllowStyle.ALWAYS) |
| 193 | +def test_cmd2_base_console_init_always_interactive_false() -> None: |
| 194 | + """Test Cmd2BaseConsole initialization when ALLOW_STYLE is ALWAYS and is_interactive is False.""" |
| 195 | + with ( |
| 196 | + mock.patch('rich.console.Console.__init__', return_value=None) as mock_base_init, |
| 197 | + mock.patch('cmd2.rich_utils.Console', autospec=True) as mock_detect_console_class, |
| 198 | + ): |
| 199 | + mock_detect_console = mock_detect_console_class.return_value |
| 200 | + mock_detect_console.is_interactive = False |
| 201 | + |
| 202 | + ru.Cmd2BaseConsole() |
| 203 | + |
| 204 | + _, kwargs = mock_base_init.call_args |
| 205 | + assert kwargs['color_system'] == "truecolor" |
| 206 | + assert kwargs['force_terminal'] is True |
| 207 | + assert kwargs['force_interactive'] is False |
| 208 | + |
| 209 | + |
| 210 | +@with_ansi_style(ru.AllowStyle.TERMINAL) |
| 211 | +def test_cmd2_base_console_init_terminal_true() -> None: |
| 212 | + """Test Cmd2BaseConsole initialization when ALLOW_STYLE is TERMINAL and it is a terminal.""" |
| 213 | + with ( |
| 214 | + mock.patch('rich.console.Console.__init__', return_value=None) as mock_base_init, |
| 215 | + mock.patch('cmd2.rich_utils.Console', autospec=True) as mock_detect_console_class, |
| 216 | + ): |
| 217 | + mock_detect_console = mock_detect_console_class.return_value |
| 218 | + mock_detect_console.is_terminal = True |
| 219 | + |
| 220 | + ru.Cmd2BaseConsole() |
| 221 | + |
| 222 | + _, kwargs = mock_base_init.call_args |
| 223 | + assert kwargs['color_system'] == "truecolor" |
| 224 | + assert kwargs['force_terminal'] is None |
| 225 | + assert kwargs['force_interactive'] is None |
| 226 | + |
| 227 | + |
| 228 | +@with_ansi_style(ru.AllowStyle.TERMINAL) |
| 229 | +def test_cmd2_base_console_init_terminal_false() -> None: |
| 230 | + """Test Cmd2BaseConsole initialization when ALLOW_STYLE is TERMINAL and it is not a terminal.""" |
| 231 | + with ( |
| 232 | + mock.patch('rich.console.Console.__init__', return_value=None) as mock_base_init, |
| 233 | + mock.patch('cmd2.rich_utils.Console', autospec=True) as mock_detect_console_class, |
| 234 | + ): |
| 235 | + mock_detect_console = mock_detect_console_class.return_value |
| 236 | + mock_detect_console.is_terminal = False |
| 237 | + |
| 238 | + ru.Cmd2BaseConsole() |
| 239 | + |
| 240 | + _, kwargs = mock_base_init.call_args |
| 241 | + assert kwargs['color_system'] is None |
| 242 | + assert kwargs['force_terminal'] is None |
| 243 | + assert kwargs['force_interactive'] is None |
| 244 | + |
| 245 | + |
| 246 | +@with_ansi_style(ru.AllowStyle.NEVER) |
| 247 | +def test_cmd2_base_console_init_never() -> None: |
| 248 | + """Test Cmd2BaseConsole initialization when ALLOW_STYLE is NEVER.""" |
| 249 | + with mock.patch('rich.console.Console.__init__', return_value=None) as mock_base_init: |
| 250 | + ru.Cmd2BaseConsole() |
| 251 | + |
| 252 | + _, kwargs = mock_base_init.call_args |
| 253 | + assert kwargs['color_system'] is None |
| 254 | + assert kwargs['force_terminal'] is False |
| 255 | + assert kwargs['force_interactive'] is None |
0 commit comments