summaryrefslogtreecommitdiffstats
path: root/tests/test_theme.py
blob: 228cb189dfcc1426a848fa3a57dc20358411b5ec (plain)
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
import io
import os
import tempfile

import pytest

from rich.style import Style
from rich.theme import Theme, ThemeStack, ThemeStackError


def test_inherit():
    theme = Theme({"warning": "red"})
    assert theme.styles["warning"] == Style(color="red")
    assert theme.styles["dim"] == Style(dim=True)


def test_config():
    theme = Theme({"warning": "red"})
    config = theme.config
    assert "warning = red\n" in config


def test_from_file():
    theme = Theme({"warning": "red"})
    text_file = io.StringIO()
    text_file.write(theme.config)
    text_file.seek(0)

    load_theme = Theme.from_file(text_file)
    assert theme.styles == load_theme.styles


def test_read():
    theme = Theme({"warning": "red"})
    with tempfile.TemporaryDirectory("richtheme") as name:
        filename = os.path.join(name, "theme.cfg")
        with open(filename, "wt") as write_theme:
            write_theme.write(theme.config)
        load_theme = Theme.read(filename)
        assert theme.styles == load_theme.styles


def test_theme_stack():
    theme = Theme({"warning": "red"})
    stack = ThemeStack(theme)
    assert stack.get("warning") == Style.parse("red")
    new_theme = Theme({"warning": "bold yellow"})
    stack.push_theme(new_theme)
    assert stack.get("warning") == Style.parse("bold yellow")
    stack.pop_theme()
    assert stack.get("warning") == Style.parse("red")
    with pytest.raises(ThemeStackError):
        stack.pop_theme()