| 1 |
import pytest |
| 2 |
|
| 3 |
from rich.padding import Padding |
| 4 |
from rich.console import Console, ConsoleDimensions, ConsoleOptions |
| 5 |
from rich.style import Style |
| 6 |
from rich.segment import Segment |
| 7 |
|
| 8 |
|
| 9 |
def test_repr(): |
| 10 |
padding = Padding("foo", (1, 2)) |
| 11 |
assert isinstance(repr(padding), str) |
| 12 |
|
| 13 |
|
| 14 |
def test_indent(): |
| 15 |
indent_result = Padding.indent("test", 4) |
| 16 |
assert indent_result.top == 0 |
| 17 |
assert indent_result.right == 0 |
| 18 |
assert indent_result.bottom == 0 |
| 19 |
assert indent_result.left == 4 |
| 20 |
|
| 21 |
|
| 22 |
def test_unpack(): |
| 23 |
assert Padding.unpack(3) == (3, 3, 3, 3) |
| 24 |
assert Padding.unpack((3,)) == (3, 3, 3, 3) |
| 25 |
assert Padding.unpack((3, 4)) == (3, 4, 3, 4) |
| 26 |
assert Padding.unpack((3, 4, 5, 6)) == (3, 4, 5, 6) |
| 27 |
with pytest.raises(ValueError): |
| 28 |
Padding.unpack((1, 2, 3)) |
| 29 |
|
| 30 |
|
| 31 |
def test_expand_false(): |
| 32 |
console = Console(width=100, color_system=None) |
| 33 |
console.begin_capture() |
| 34 |
console.print(Padding("foo", 1, expand=False)) |
| 35 |
assert console.end_capture() == " \n foo \n \n" |
| 36 |
|
| 37 |
|
| 38 |
def test_rich_console(): |
| 39 |
renderable = "test renderable" |
| 40 |
style = Style(color="red") |
| 41 |
options = ConsoleOptions( |
| 42 |
ConsoleDimensions(80, 25), |
| 43 |
max_height=25, |
| 44 |
legacy_windows=False, |
| 45 |
min_width=10, |
| 46 |
max_width=20, |
| 47 |
is_terminal=False, |
| 48 |
encoding="utf-8", |
| 49 |
) |
| 50 |
|
| 51 |
expected_outputs = [ |
| 52 |
Segment(renderable, style=style), |
| 53 |
Segment(" " * (20 - len(renderable)), style=style), |
| 54 |
Segment("\n", style=None), |
| 55 |
] |
| 56 |
padding_generator = Padding(renderable, style=style).__rich_console__( |
| 57 |
Console(), options |
| 58 |
) |
| 59 |
for output, expected in zip(padding_generator, expected_outputs): |
| 60 |
assert output == expected |