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