|
3 | 3 | import pytest |
4 | 4 | import rich.box |
5 | 5 | from rich.console import Console |
6 | | -from rich.segment import Segment |
7 | 6 | from rich.style import Style |
8 | 7 | from rich.table import Table |
9 | 8 | from rich.text import Text |
@@ -143,189 +142,3 @@ def test_from_ansi_wrapper() -> None: |
143 | 142 | # Test empty string |
144 | 143 | input_string = "" |
145 | 144 | assert Text.from_ansi(input_string).plain == input_string |
146 | | - |
147 | | - |
148 | | -@pytest.mark.parametrize( |
149 | | - # Print with style and verify that everything but newline characters have style. |
150 | | - ('objects', 'sep', 'end', 'expected'), |
151 | | - [ |
152 | | - # Print nothing |
153 | | - ((), " ", "\n", "\n"), |
154 | | - # Empty string |
155 | | - (("",), " ", "\n", "\n"), |
156 | | - # Multple empty strings |
157 | | - (("", ""), " ", "\n", "\x1b[34;47m \x1b[0m\n"), |
158 | | - # Basic string |
159 | | - ( |
160 | | - ("str_1",), |
161 | | - " ", |
162 | | - "\n", |
163 | | - "\x1b[34;47mstr_1\x1b[0m\n", |
164 | | - ), |
165 | | - # String which ends with newline |
166 | | - ( |
167 | | - ("str_1\n",), |
168 | | - " ", |
169 | | - "\n", |
170 | | - "\x1b[34;47mstr_1\x1b[0m\n\n", |
171 | | - ), |
172 | | - # String which ends with multiple newlines |
173 | | - ( |
174 | | - ("str_1\n\n",), |
175 | | - " ", |
176 | | - "\n", |
177 | | - "\x1b[34;47mstr_1\x1b[0m\n\n\n", |
178 | | - ), |
179 | | - # Mutiple lines |
180 | | - ( |
181 | | - ("str_1\nstr_2",), |
182 | | - " ", |
183 | | - "\n", |
184 | | - "\x1b[34;47mstr_1\x1b[0m\n\x1b[34;47mstr_2\x1b[0m\n", |
185 | | - ), |
186 | | - # Multiple strings |
187 | | - ( |
188 | | - ("str_1", "str_2"), |
189 | | - " ", |
190 | | - "\n", |
191 | | - "\x1b[34;47mstr_1 str_2\x1b[0m\n", |
192 | | - ), |
193 | | - # Multiple strings with newline between them. |
194 | | - ( |
195 | | - ("str_1\n", "str_2"), |
196 | | - " ", |
197 | | - "\n", |
198 | | - "\x1b[34;47mstr_1\x1b[0m\n\x1b[34;47m str_2\x1b[0m\n", |
199 | | - ), |
200 | | - # Multiple strings and non-space value for sep |
201 | | - ( |
202 | | - ("str_1", "str_2"), |
203 | | - "(sep)", |
204 | | - "\n", |
205 | | - "\x1b[34;47mstr_1(sep)str_2\x1b[0m\n", |
206 | | - ), |
207 | | - # Multiple strings and sep is a newline |
208 | | - ( |
209 | | - ("str_1", "str_2"), |
210 | | - "\n", |
211 | | - "\n", |
212 | | - "\x1b[34;47mstr_1\x1b[0m\n\x1b[34;47mstr_2\x1b[0m\n", |
213 | | - ), |
214 | | - # Multiple strings and sep has newlines |
215 | | - ( |
216 | | - ("str_1", "str_2"), |
217 | | - "(sep1)\n(sep2)\n", |
218 | | - "\n", |
219 | | - ("\x1b[34;47mstr_1(sep1)\x1b[0m\n\x1b[34;47m(sep2)\x1b[0m\n\x1b[34;47mstr_2\x1b[0m\n"), |
220 | | - ), |
221 | | - # Non-newline value for end. |
222 | | - ( |
223 | | - ("str_1", "str_2"), |
224 | | - "(sep1)\n(sep2)", |
225 | | - "(end)", |
226 | | - "\x1b[34;47mstr_1(sep1)\x1b[0m\n\x1b[34;47m(sep2)str_2\x1b[0m\x1b[34;47m(end)\x1b[0m", |
227 | | - ), |
228 | | - # end has newlines. |
229 | | - ( |
230 | | - ("str_1", "str_2"), |
231 | | - "(sep1)\n(sep2)\n", |
232 | | - "(end1)\n(end2)\n", |
233 | | - ( |
234 | | - "\x1b[34;47mstr_1(sep1)\x1b[0m\n" |
235 | | - "\x1b[34;47m(sep2)\x1b[0m\n" |
236 | | - "\x1b[34;47mstr_2\x1b[0m\x1b[34;47m(end1)\x1b[0m\n" |
237 | | - "\x1b[34;47m(end2)\x1b[0m\n" |
238 | | - ), |
239 | | - ), |
240 | | - # Empty sep and end values |
241 | | - ( |
242 | | - ("str_1", "str_2"), |
243 | | - "", |
244 | | - "", |
245 | | - "\x1b[34;47mstr_1str_2\x1b[0m", |
246 | | - ), |
247 | | - ], |
248 | | -) |
249 | | -def test_apply_style_wrapper_soft_wrap(objects: tuple[str], sep: str, end: str, expected: str) -> None: |
250 | | - # Check if we are still patching Segment.apply_style(). If this check fails, then Rich |
251 | | - # has fixed the bug. Therefore, we can remove this test function and ru._apply_style_wrapper. |
252 | | - assert Segment.apply_style.__func__ is ru._apply_style_wrapper.__func__ # type: ignore[attr-defined] |
253 | | - |
254 | | - console = Console(force_terminal=True) |
255 | | - |
256 | | - try: |
257 | | - # Since our patch was meant to fix behavior seen when soft wrapping, |
258 | | - # we will first test in that condition. |
259 | | - with console.capture() as capture: |
260 | | - console.print(*objects, sep=sep, end=end, style="blue on white", soft_wrap=True) |
261 | | - result = capture.get() |
262 | | - assert result == expected |
263 | | - |
264 | | - # Now print with soft wrapping disabled. Since none of our input strings are long enough |
265 | | - # to auto wrap, the results should be the same as our soft-wrapping output. |
266 | | - with console.capture() as capture: |
267 | | - console.print(*objects, sep=sep, end=end, style="blue on white", soft_wrap=False) |
268 | | - result = capture.get() |
269 | | - assert result == expected |
270 | | - |
271 | | - # Now remove our patch and disable soft wrapping. This will prove that our patch produces |
272 | | - # the same result as unpatched Rich |
273 | | - Segment.apply_style = ru._orig_segment_apply_style # type: ignore[assignment] |
274 | | - |
275 | | - with console.capture() as capture: |
276 | | - console.print(*objects, sep=sep, end=end, style="blue on white", soft_wrap=False) |
277 | | - result = capture.get() |
278 | | - assert result == expected |
279 | | - |
280 | | - finally: |
281 | | - # Restore the patch |
282 | | - Segment.apply_style = ru._apply_style_wrapper # type: ignore[assignment] |
283 | | - |
284 | | - |
285 | | -def test_apply_style_wrapper_word_wrap() -> None: |
286 | | - """ |
287 | | - Test that our patch didn't mess up word wrapping. |
288 | | - Make sure it does not insert styled newlines or apply style to existing newlines. |
289 | | - """ |
290 | | - # Check if we are still patching Segment.apply_style(). If this check fails, then Rich |
291 | | - # has fixed the bug. Therefore, we can remove this test function and ru._apply_style_wrapper. |
292 | | - assert Segment.apply_style.__func__ is ru._apply_style_wrapper.__func__ # type: ignore[attr-defined] |
293 | | - |
294 | | - str1 = "this\nwill word wrap\n" |
295 | | - str2 = "and\nso will this\n" |
296 | | - sep = "(sep1)\n(sep2)\n" |
297 | | - end = "(end1)\n(end2)\n" |
298 | | - style = "blue on white" |
299 | | - |
300 | | - # All newlines should appear outside of ANSI style sequences. |
301 | | - expected = ( |
302 | | - "\x1b[34;47mthis\x1b[0m\n" |
303 | | - "\x1b[34;47mwill word \x1b[0m\n" |
304 | | - "\x1b[34;47mwrap\x1b[0m\n" |
305 | | - "\x1b[34;47m(sep1)\x1b[0m\n" |
306 | | - "\x1b[34;47m(sep2)\x1b[0m\n" |
307 | | - "\x1b[34;47mand\x1b[0m\n" |
308 | | - "\x1b[34;47mso will \x1b[0m\n" |
309 | | - "\x1b[34;47mthis\x1b[0m\n" |
310 | | - "\x1b[34;47m(end1)\x1b[0m\n" |
311 | | - "\x1b[34;47m(end2)\x1b[0m\n" |
312 | | - ) |
313 | | - |
314 | | - # Set a width which will cause word wrapping. |
315 | | - console = Console(force_terminal=True, width=10) |
316 | | - |
317 | | - try: |
318 | | - with console.capture() as capture: |
319 | | - console.print(str1, str2, sep=sep, end=end, style=style, soft_wrap=False) |
320 | | - assert capture.get() == expected |
321 | | - |
322 | | - # Now remove our patch and make sure it produced the same result as unpatched Rich. |
323 | | - Segment.apply_style = ru._orig_segment_apply_style # type: ignore[assignment] |
324 | | - |
325 | | - with console.capture() as capture: |
326 | | - console.print(str1, str2, sep=sep, end=end, style=style, soft_wrap=False) |
327 | | - assert capture.get() == expected |
328 | | - |
329 | | - finally: |
330 | | - # Restore the patch |
331 | | - Segment.apply_style = ru._apply_style_wrapper # type: ignore[assignment] |
0 commit comments