File: test_message_handling.py

package info (click to toggle)
textual 2.1.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 55,084 kB
  • sloc: python: 85,423; lisp: 1,669; makefile: 101
file content (50 lines) | stat: -rw-r--r-- 1,315 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
from textual.app import App, ComposeResult
from textual.message import Message
from textual.widget import Widget


async def test_message_inheritance_namespace():
    """Inherited messages get their correct namespaces.

    Regression test for https://github.com/Textualize/textual/issues/1814.
    """

    class BaseWidget(Widget):
        class Fired(Message):
            pass

        def trigger(self) -> None:
            self.post_message(self.Fired())

    class Left(BaseWidget):
        class Fired(BaseWidget.Fired):
            pass

    class Right(BaseWidget):
        class Fired(BaseWidget.Fired):
            pass

    class DummyWidget(Widget):
        # ensure that referencing a message type in other class scopes
        # doesn't break the namespace
        _event = Left.Fired

    handlers_called = []

    class MessageInheritanceApp(App[None]):
        def compose(self) -> ComposeResult:
            yield Left()
            yield Right()

        def on_left_fired(self):
            handlers_called.append("left")

        def on_right_fired(self):
            handlers_called.append("right")

    app = MessageInheritanceApp()
    async with app.run_test():
        app.query_one(Left).trigger()
        app.query_one(Right).trigger()

    assert handlers_called == ["left", "right"]