| 1 |
import io |
| 2 |
import os |
| 3 |
import tempfile |
| 4 |
|
| 5 |
import pytest |
| 6 |
|
| 7 |
from rich.style import Style |
| 8 |
from rich.theme import Theme, ThemeStack, ThemeStackError |
| 9 |
|
| 10 |
|
| 11 |
def test_inherit(): |
| 12 |
theme = Theme({"warning": "red"}) |
| 13 |
assert theme.styles["warning"] == Style(color="red") |
| 14 |
assert theme.styles["dim"] == Style(dim=True) |
| 15 |
|
| 16 |
|
| 17 |
def test_config(): |
| 18 |
theme = Theme({"warning": "red"}) |
| 19 |
config = theme.config |
| 20 |
assert "warning = red\n" in config |
| 21 |
|
| 22 |
|
| 23 |
def test_from_file(): |
| 24 |
theme = Theme({"warning": "red"}) |
| 25 |
text_file = io.StringIO() |
| 26 |
text_file.write(theme.config) |
| 27 |
text_file.seek(0) |
| 28 |
|
| 29 |
load_theme = Theme.from_file(text_file) |
| 30 |
assert theme.styles == load_theme.styles |
| 31 |
|
| 32 |
|
| 33 |
def test_read(): |
| 34 |
theme = Theme({"warning": "red"}) |
| 35 |
with tempfile.TemporaryDirectory("richtheme") as name: |
| 36 |
filename = os.path.join(name, "theme.cfg") |
| 37 |
with open(filename, "wt") as write_theme: |
| 38 |
write_theme.write(theme.config) |
| 39 |
load_theme = Theme.read(filename) |
| 40 |
assert theme.styles == load_theme.styles |
| 41 |
|
| 42 |
|
| 43 |
def test_theme_stack(): |
| 44 |
theme = Theme({"warning": "red"}) |
| 45 |
stack = ThemeStack(theme) |
| 46 |
assert stack.get("warning") == Style.parse("red") |
| 47 |
new_theme = Theme({"warning": "bold yellow"}) |
| 48 |
stack.push_theme(new_theme) |
| 49 |
assert stack.get("warning") == Style.parse("bold yellow") |
| 50 |
stack.pop_theme() |
| 51 |
assert stack.get("warning") == Style.parse("red") |
| 52 |
with pytest.raises(ThemeStackError): |
| 53 |
stack.pop_theme() |