| 1 |
import io |
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
from rich.console import Console |
| 6 |
from rich.align import Align, VerticalCenter |
| 7 |
from rich.measure import Measurement |
| 8 |
|
| 9 |
|
| 10 |
def test_bad_align_legal(): |
| 11 |
# Legal |
| 12 |
Align("foo", "left") |
| 13 |
Align("foo", "center") |
| 14 |
Align("foo", "right") |
| 15 |
|
| 16 |
# illegal |
| 17 |
with pytest.raises(ValueError): |
| 18 |
Align("foo", None) |
| 19 |
with pytest.raises(ValueError): |
| 20 |
Align("foo", "middle") |
| 21 |
with pytest.raises(ValueError): |
| 22 |
Align("foo", "") |
| 23 |
with pytest.raises(ValueError): |
| 24 |
Align("foo", "LEFT") |
| 25 |
with pytest.raises(ValueError): |
| 26 |
Align("foo", vertical="somewhere") |
| 27 |
|
| 28 |
|
| 29 |
def test_repr(): |
| 30 |
repr(Align("foo", "left")) |
| 31 |
repr(Align("foo", "center")) |
| 32 |
repr(Align("foo", "right")) |
| 33 |
|
| 34 |
|
| 35 |
def test_align_left(): |
| 36 |
console = Console(file=io.StringIO(), width=10) |
| 37 |
console.print(Align("foo", "left")) |
| 38 |
assert console.file.getvalue() == "foo \n" |
| 39 |
|
| 40 |
|
| 41 |
def test_align_center(): |
| 42 |
console = Console(file=io.StringIO(), width=10) |
| 43 |
console.print(Align("foo", "center")) |
| 44 |
assert console.file.getvalue() == " foo \n" |
| 45 |
|
| 46 |
|
| 47 |
def test_align_right(): |
| 48 |
console = Console(file=io.StringIO(), width=10) |
| 49 |
console.print(Align("foo", "right")) |
| 50 |
assert console.file.getvalue() == " foo\n" |
| 51 |
|
| 52 |
|
| 53 |
def test_align_top(): |
| 54 |
console = Console(file=io.StringIO(), width=10) |
| 55 |
console.print(Align("foo", vertical="top"), height=5) |
| 56 |
expected = "foo \n \n \n \n \n" |
| 57 |
result = console.file.getvalue() |
| 58 |
print(repr(result)) |
| 59 |
assert result == expected |
| 60 |
|
| 61 |
|
| 62 |
def test_align_middle(): |
| 63 |
console = Console(file=io.StringIO(), width=10) |
| 64 |
console.print(Align("foo", vertical="middle"), height=5) |
| 65 |
expected = " \n \nfoo \n \n \n" |
| 66 |
result = console.file.getvalue() |
| 67 |
print(repr(result)) |
| 68 |
assert result == expected |
| 69 |
|
| 70 |
|
| 71 |
def test_align_bottom(): |
| 72 |
console = Console(file=io.StringIO(), width=10) |
| 73 |
console.print(Align("foo", vertical="bottom"), height=5) |
| 74 |
expected = " \n \n \n \nfoo \n" |
| 75 |
result = console.file.getvalue() |
| 76 |
print(repr(result)) |
| 77 |
assert result == expected |
| 78 |
|
| 79 |
|
| 80 |
def test_align_center_middle(): |
| 81 |
console = Console(file=io.StringIO(), width=10) |
| 82 |
console.print(Align("foo\nbar", "center", vertical="middle"), height=5) |
| 83 |
expected = " \n foo \n bar \n \n \n" |
| 84 |
result = console.file.getvalue() |
| 85 |
print(repr(result)) |
| 86 |
assert result == expected |
| 87 |
|
| 88 |
|
| 89 |
def test_align_fit(): |
| 90 |
console = Console(file=io.StringIO(), width=10) |
| 91 |
console.print(Align("foobarbaze", "center")) |
| 92 |
assert console.file.getvalue() == "foobarbaze\n" |
| 93 |
|
| 94 |
|
| 95 |
def test_align_right_style(): |
| 96 |
console = Console( |
| 97 |
file=io.StringIO(), |
| 98 |
width=10, |
| 99 |
color_system="truecolor", |
| 100 |
force_terminal=True, |
| 101 |
_environ={}, |
| 102 |
) |
| 103 |
console.print(Align("foo", "right", style="on blue")) |
| 104 |
assert console.file.getvalue() == "\x1b[44m \x1b[0m\x1b[44mfoo\x1b[0m\n" |
| 105 |
|
| 106 |
|
| 107 |
def test_measure(): |
| 108 |
console = Console(file=io.StringIO(), width=20) |
| 109 |
_min, _max = Measurement.get(console, console.options, Align("foo bar", "left")) |
| 110 |
assert _min == 3 |
| 111 |
assert _max == 7 |
| 112 |
|
| 113 |
|
| 114 |
def test_align_no_pad(): |
| 115 |
console = Console(file=io.StringIO(), width=10) |
| 116 |
console.print(Align("foo", "center", pad=False)) |
| 117 |
console.print(Align("foo", "left", pad=False)) |
| 118 |
assert console.file.getvalue() == " foo\nfoo\n" |
| 119 |
|
| 120 |
|
| 121 |
def test_align_width(): |
| 122 |
console = Console(file=io.StringIO(), width=40) |
| 123 |
words = "Deep in the human unconscious is a pervasive need for a logical universe that makes sense. But the real universe is always one step beyond logic" |
| 124 |
console.print(Align(words, "center", width=30)) |
| 125 |
result = console.file.getvalue() |
| 126 |
expected = " Deep in the human unconscious \n is a pervasive need for a \n logical universe that makes \n sense. But the real universe \n is always one step beyond \n logic \n" |
| 127 |
assert result == expected |
| 128 |
|
| 129 |
|
| 130 |
def test_shortcuts(): |
| 131 |
assert Align.left("foo").align == "left" |
| 132 |
assert Align.left("foo").renderable == "foo" |
| 133 |
assert Align.right("foo").align == "right" |
| 134 |
assert Align.right("foo").renderable == "foo" |
| 135 |
assert Align.center("foo").align == "center" |
| 136 |
assert Align.center("foo").renderable == "foo" |
| 137 |
|
| 138 |
|
| 139 |
def test_vertical_center(): |
| 140 |
console = Console(color_system=None, height=6) |
| 141 |
console.begin_capture() |
| 142 |
vertical_center = VerticalCenter("foo") |
| 143 |
repr(vertical_center) |
| 144 |
console.print(vertical_center) |
| 145 |
result = console.end_capture() |
| 146 |
print(repr(result)) |
| 147 |
expected = " \n \nfoo\n \n \n \n" |
| 148 |
assert result == expected |
| 149 |
assert Measurement.get(console, console.options, vertical_center) == Measurement( |
| 150 |
3, 3 |
| 151 |
) |