File: test_initial.py

package info (click to toggle)
textual 2.1.2-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 55,080 kB
  • sloc: python: 85,423; lisp: 1,669; makefile: 101
file content (95 lines) | stat: -rw-r--r-- 2,380 bytes parent folder | download | duplicates (2)
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
from textual.app import App, ComposeResult
from textual.color import Color
from textual.widget import Widget


class Base(Widget):
    DEFAULT_CSS = """
    Base {
        color: magenta;
    }
    """


class CustomWidget1(Base):
    DEFAULT_CSS = """
    CustomWidget1 {
        background: red
    }
    """


class CustomWidget2(CustomWidget1):
    DEFAULT_CSS = """
    CustomWidget2 {
        background: initial;
    }
    """


class CustomWidget3(CustomWidget2):
    pass


async def test_initial_default():
    class InitialApp(App):
        def compose(self) -> ComposeResult:
            yield Base(id="base")
            yield CustomWidget1(id="custom1")
            yield CustomWidget2(id="custom2")

    app = InitialApp()
    async with app.run_test():
        base = app.query_one("#base", Base)
        custom1 = app.query_one("#custom1", CustomWidget1)
        custom2 = app.query_one("#custom2", CustomWidget2)

        # No background set on base
        default_background = base.styles.background
        assert default_background == Color.parse("rgba(0,0,0,0)")
        # Customized background value, should be red
        assert custom1.styles.background == Color.parse("red")
        # Background has default value
        assert custom2.styles.background == default_background


async def test_initial():
    class InitialApp(App):
        CSS = """
        CustomWidget1 {
            color: red;
        }

        CustomWidget2 {
           color: initial;
        }

        CustomWidget3 {
            color: blue;
        }
        """

        def compose(self) -> ComposeResult:
            yield Base(id="base")
            yield CustomWidget1(id="custom1")
            yield CustomWidget2(id="custom2")
            yield CustomWidget3(id="custom3")

    app = InitialApp()
    async with app.run_test():
        base = app.query_one("#base")
        custom1 = app.query_one("#custom1")
        custom2 = app.query_one("#custom2")
        custom3 = app.query_one("#custom3")

        # Default color
        assert base.styles.color == Color.parse("magenta")

        # Explicitly set to red
        assert custom1.styles.color == Color.parse("red")

        # Set to initial, should be same as base
        assert custom2.styles.color == Color.parse("magenta")

        # Set to blue
        assert custom3.styles.color == Color.parse("blue")