rich/tests/test_rule.py

131 lines
3.8 KiB
Python
Raw Normal View History

2020-04-19 14:23:53 +00:00
import io
import pytest
from rich.console import Console
from rich.rule import Rule
from rich.text import Text
def test_rule():
2020-06-20 17:29:01 +00:00
console = Console(
2021-03-01 19:42:00 +00:00
width=16,
file=io.StringIO(),
force_terminal=True,
legacy_windows=False,
_environ={},
2020-06-20 17:29:01 +00:00
)
2020-08-05 08:50:15 +00:00
console.print(Rule())
console.print(Rule("foo"))
2020-04-19 14:23:53 +00:00
console.rule(Text("foo", style="bold"))
console.rule("foobarbazeggfoobarbazegg")
2020-08-05 08:50:15 +00:00
expected = "\x1b[92m────────────────\x1b[0m\n"
expected += "\x1b[92m───── \x1b[0mfoo\x1b[92m ──────\x1b[0m\n"
2020-08-08 02:39:35 +00:00
expected += "\x1b[92m───── \x1b[0m\x1b[1mfoo\x1b[0m\x1b[92m ──────\x1b[0m\n"
2020-08-05 08:50:15 +00:00
expected += "\x1b[92m─ \x1b[0mfoobarbazeg…\x1b[92m ─\x1b[0m\n"
2020-07-27 16:21:03 +00:00
result = console.file.getvalue()
assert result == expected
2020-04-19 14:23:53 +00:00
2020-12-03 17:17:24 +00:00
def test_rule_error():
2021-03-01 19:42:00 +00:00
console = Console(width=16, file=io.StringIO(), legacy_windows=False, _environ={})
2020-12-03 17:17:24 +00:00
with pytest.raises(ValueError):
console.rule("foo", align="foo")
def test_rule_align():
2021-03-01 19:42:00 +00:00
console = Console(width=16, file=io.StringIO(), legacy_windows=False, _environ={})
2020-12-03 17:17:24 +00:00
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
2020-08-07 01:04:19 +00:00
def test_rule_cjk():
console = Console(
width=16,
file=io.StringIO(),
force_terminal=True,
color_system=None,
legacy_windows=False,
2021-03-01 19:42:00 +00:00
_environ={},
2020-08-07 01:04:19 +00:00
)
console.rule("欢迎!")
expected = "──── 欢迎! ────\n"
assert console.file.getvalue() == expected
2020-08-05 09:38:02 +00:00
@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
2020-08-05 09:38:02 +00:00
def test_characters():
2020-06-10 11:55:04 +00:00
console = Console(
2020-06-20 17:29:01 +00:00
width=16,
file=io.StringIO(),
force_terminal=True,
color_system=None,
2020-06-20 17:39:00 +00:00
legacy_windows=False,
2021-03-01 19:42:00 +00:00
_environ={},
2020-06-10 11:55:04 +00:00
)
2020-08-05 09:38:02 +00:00
console.rule(characters="+*")
console.rule("foo", characters="+*")
2020-08-05 12:31:57 +00:00
console.print(Rule(characters=".,"))
2020-08-05 09:38:02 +00:00
expected = "+*+*+*+*+*+*+*+*\n"
expected += "+*+*+ foo +*+*+*\n"
2020-08-05 12:31:57 +00:00
expected += ".,.,.,.,.,.,.,.,\n"
2020-06-10 11:55:04 +00:00
assert console.file.getvalue() == expected
2020-04-19 14:23:53 +00:00
def test_repr():
rule = Rule("foo")
assert isinstance(repr(rule), str)
def test_error():
with pytest.raises(ValueError):
2020-08-05 04:44:53 +00:00
Rule(characters="")