1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
import io
import pytest
from rich.console import Console
from rich.rule import Rule
from rich.text import Text
def test_rule():
console = Console(
width=16,
file=io.StringIO(),
force_terminal=True,
legacy_windows=False,
_environ={},
)
console.print(Rule())
console.print(Rule("foo"))
console.rule(Text("foo", style="bold"))
console.rule("foobarbazeggfoobarbazegg")
expected = "\x1b[92m────────────────\x1b[0m\n"
expected += "\x1b[92m───── \x1b[0mfoo\x1b[92m ──────\x1b[0m\n"
expected += "\x1b[92m───── \x1b[0m\x1b[1mfoo\x1b[0m\x1b[92m ──────\x1b[0m\n"
expected += "\x1b[92m─ \x1b[0mfoobarbazeg…\x1b[92m ─\x1b[0m\n"
result = console.file.getvalue()
assert result == expected
def test_rule_error():
console = Console(width=16, file=io.StringIO(), legacy_windows=False, _environ={})
with pytest.raises(ValueError):
console.rule("foo", align="foo")
def test_rule_align():
console = Console(width=16, file=io.StringIO(), legacy_windows=False, _environ={})
console.rule("foo")
console.rule("foo", align="left")
console.rule("foo", align="center")
console.rule("foo", align="right")
console.rule()
result = console.file.getvalue()
print(repr(result))
expected = "───── foo ──────\nfoo ────────────\n───── foo ──────\n──────────── foo\n────────────────\n"
assert result == expected
def test_rule_cjk():
console = Console(
width=16,
file=io.StringIO(),
force_terminal=True,
color_system=None,
legacy_windows=False,
_environ={},
)
console.rule("欢迎!")
expected = "──── 欢迎! ────\n"
assert console.file.getvalue() == expected
@pytest.mark.parametrize(
"align,outcome",
[
("center", "───\n"),
("left", "… ─\n"),
("right", "─ …\n"),
],
)
def test_rule_not_enough_space_for_title_text(align, outcome):
console = Console(width=3, file=io.StringIO(), record=True)
console.rule("Hello!", align=align)
assert console.file.getvalue() == outcome
def test_rule_center_aligned_title_not_enough_space_for_rule():
console = Console(width=4, file=io.StringIO(), record=True)
console.rule("ABCD")
assert console.file.getvalue() == "────\n"
@pytest.mark.parametrize("align", ["left", "right"])
def test_rule_side_aligned_not_enough_space_for_rule(align):
console = Console(width=2, file=io.StringIO(), record=True)
console.rule("ABCD", align=align)
assert console.file.getvalue() == "──\n"
@pytest.mark.parametrize(
"align,outcome",
[
("center", "─ … ─\n"),
("left", "AB… ─\n"),
("right", "─ AB…\n"),
],
)
def test_rule_just_enough_width_available_for_title(align, outcome):
console = Console(width=5, file=io.StringIO(), record=True)
console.rule("ABCD", align=align)
assert console.file.getvalue() == outcome
def test_characters():
console = Console(
width=16,
file=io.StringIO(),
force_terminal=True,
color_system=None,
legacy_windows=False,
_environ={},
)
console.rule(characters="+*")
console.rule("foo", characters="+*")
console.print(Rule(characters=".,"))
expected = "+*+*+*+*+*+*+*+*\n"
expected += "+*+*+ foo +*+*+*\n"
expected += ".,.,.,.,.,.,.,.,\n"
assert console.file.getvalue() == expected
def test_repr():
rule = Rule("foo")
assert isinstance(repr(rule), str)
def test_error():
with pytest.raises(ValueError):
Rule(characters="")
|