|
20 | 20 | COMMAND_NAME,
|
21 | 21 | Cmd2Style,
|
22 | 22 | Color,
|
| 23 | + RichPrintKwargs, |
23 | 24 | clipboard,
|
24 | 25 | constants,
|
25 | 26 | exceptions,
|
@@ -2133,6 +2134,106 @@ def test_poutput_ansi_terminal(outsim_app) -> None:
|
2133 | 2134 | assert out == expected
|
2134 | 2135 |
|
2135 | 2136 |
|
| 2137 | +@with_ansi_style(ru.AllowStyle.ALWAYS) |
| 2138 | +def test_poutput_highlight(outsim_app): |
| 2139 | + rich_print_kwargs = RichPrintKwargs(highlight=True) |
| 2140 | + outsim_app.poutput( |
| 2141 | + "My IP Address is 192.168.1.100.", |
| 2142 | + rich_print_kwargs=rich_print_kwargs, |
| 2143 | + ) |
| 2144 | + out = outsim_app.stdout.getvalue() |
| 2145 | + assert out == "My IP Address is \x1b[1;92m192.168.1.100\x1b[0m.\n" |
| 2146 | + |
| 2147 | + |
| 2148 | +@with_ansi_style(ru.AllowStyle.ALWAYS) |
| 2149 | +def test_poutput_markup(outsim_app): |
| 2150 | + rich_print_kwargs = RichPrintKwargs(markup=True) |
| 2151 | + outsim_app.poutput( |
| 2152 | + "The leaves are [green]green[/green].", |
| 2153 | + rich_print_kwargs=rich_print_kwargs, |
| 2154 | + ) |
| 2155 | + out = outsim_app.stdout.getvalue() |
| 2156 | + assert out == "The leaves are \x1b[32mgreen\x1b[0m.\n" |
| 2157 | + |
| 2158 | + |
| 2159 | +@with_ansi_style(ru.AllowStyle.ALWAYS) |
| 2160 | +def test_poutput_emoji(outsim_app): |
| 2161 | + rich_print_kwargs = RichPrintKwargs(emoji=True) |
| 2162 | + outsim_app.poutput( |
| 2163 | + "Look at the emoji :1234:.", |
| 2164 | + rich_print_kwargs=rich_print_kwargs, |
| 2165 | + ) |
| 2166 | + out = outsim_app.stdout.getvalue() |
| 2167 | + assert out == "Look at the emoji 🔢.\n" |
| 2168 | + |
| 2169 | + |
| 2170 | +@with_ansi_style(ru.AllowStyle.ALWAYS) |
| 2171 | +def test_poutput_justify_and_width(outsim_app): |
| 2172 | + rich_print_kwargs = RichPrintKwargs(justify="right", width=10) |
| 2173 | + |
| 2174 | + # Use a styled-string when justifying to check if its display width is correct. |
| 2175 | + outsim_app.poutput( |
| 2176 | + su.stylize("Hello", style="blue"), |
| 2177 | + rich_print_kwargs=rich_print_kwargs, |
| 2178 | + ) |
| 2179 | + out = outsim_app.stdout.getvalue() |
| 2180 | + assert out == " \x1b[34mHello\x1b[0m\n" |
| 2181 | + |
| 2182 | + |
| 2183 | +@with_ansi_style(ru.AllowStyle.ALWAYS) |
| 2184 | +def test_poutput_no_wrap_and_overflow(outsim_app): |
| 2185 | + rich_print_kwargs = RichPrintKwargs(no_wrap=True, overflow="ellipsis", width=10) |
| 2186 | + |
| 2187 | + outsim_app.poutput( |
| 2188 | + "This is longer than width.", |
| 2189 | + soft_wrap=False, |
| 2190 | + rich_print_kwargs=rich_print_kwargs, |
| 2191 | + ) |
| 2192 | + out = outsim_app.stdout.getvalue() |
| 2193 | + assert out.startswith("This is l…\n") |
| 2194 | + |
| 2195 | + |
| 2196 | +@with_ansi_style(ru.AllowStyle.ALWAYS) |
| 2197 | +def test_poutput_pretty_print(outsim_app): |
| 2198 | + """Test that cmd2 passes objects through so they can be pretty-printed when highlighting is enabled.""" |
| 2199 | + rich_print_kwargs = RichPrintKwargs(highlight=True) |
| 2200 | + dictionary = {1: 'hello', 2: 'person', 3: 'who', 4: 'codes'} |
| 2201 | + |
| 2202 | + outsim_app.poutput( |
| 2203 | + dictionary, |
| 2204 | + rich_print_kwargs=rich_print_kwargs, |
| 2205 | + ) |
| 2206 | + out = outsim_app.stdout.getvalue() |
| 2207 | + assert out.startswith("\x1b[1m{\x1b[0m\x1b[1;36m1\x1b[0m: \x1b[32m'hello'\x1b[0m") |
| 2208 | + |
| 2209 | + |
| 2210 | +@with_ansi_style(ru.AllowStyle.ALWAYS) |
| 2211 | +def test_poutput_all_keyword_args(outsim_app): |
| 2212 | + """Test that all fields in RichPrintKwargs are recognized by Rich's Console.print().""" |
| 2213 | + rich_print_kwargs = RichPrintKwargs( |
| 2214 | + justify="center", |
| 2215 | + overflow="ellipsis", |
| 2216 | + no_wrap=True, |
| 2217 | + markup=True, |
| 2218 | + emoji=True, |
| 2219 | + highlight=True, |
| 2220 | + width=40, |
| 2221 | + height=50, |
| 2222 | + crop=False, |
| 2223 | + new_line_start=True, |
| 2224 | + ) |
| 2225 | + |
| 2226 | + outsim_app.poutput( |
| 2227 | + "My string", |
| 2228 | + rich_print_kwargs=rich_print_kwargs, |
| 2229 | + ) |
| 2230 | + |
| 2231 | + # Verify that something printed which means Console.print() didn't |
| 2232 | + # raise a TypeError for an unexpected keyword argument. |
| 2233 | + out = outsim_app.stdout.getvalue() |
| 2234 | + assert "My string" in out |
| 2235 | + |
| 2236 | + |
2136 | 2237 | def test_broken_pipe_error(outsim_app, monkeypatch, capsys):
|
2137 | 2238 | write_mock = mock.MagicMock()
|
2138 | 2239 | write_mock.side_effect = BrokenPipeError
|
|
0 commit comments