File: test_radiobutton.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 (63 lines) | stat: -rw-r--r-- 1,955 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
from __future__ import annotations

from textual.app import App, ComposeResult
from textual.widgets import RadioButton


class RadioButtonApp(App[None]):
    def __init__(self):
        super().__init__()
        self.events_received = []

    def compose(self) -> ComposeResult:
        yield RadioButton("Test", id="rb1")
        yield RadioButton(id="rb2")
        yield RadioButton(value=True, id="rb3")

    def on_radio_button_changed(self, event: RadioButton.Changed) -> None:
        self.events_received.append(
            (
                event.radio_button.id,
                event.radio_button.value,
                event.radio_button == event.control,
            )
        )


async def test_radio_button_initial_state() -> None:
    """The initial states of the radio buttons should be as we specified."""
    async with RadioButtonApp().run_test() as pilot:
        assert [button.value for button in pilot.app.query(RadioButton)] == [
            False,
            False,
            True,
        ]
        assert [button.has_class("-on") for button in pilot.app.query(RadioButton)] == [
            False,
            False,
            True,
        ]
        assert pilot.app.events_received == []


async def test_radio_button_toggle() -> None:
    """Test the status of the radio buttons after they've been toggled."""
    async with RadioButtonApp().run_test() as pilot:
        for box in pilot.app.query(RadioButton):
            box.toggle()
        assert [button.value for button in pilot.app.query(RadioButton)] == [
            True,
            True,
            False,
        ]
        assert [button.has_class("-on") for button in pilot.app.query(RadioButton)] == [
            True,
            True,
            False,
        ]
        await pilot.pause()
        assert pilot.app.events_received == [
            ("rb1", True, True),
            ("rb2", True, True),
            ("rb3", False, True),
        ]