File: test_actions.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 (90 lines) | stat: -rw-r--r-- 2,694 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
from __future__ import annotations

from typing import Any

import pytest

from textual.actions import ActionError, parse


@pytest.mark.parametrize(
    ("action_string", "expected_namespace", "expected_name", "expected_arguments"),
    [
        ("spam", "", "spam", ()),
        ("hypothetical_action()", "", "hypothetical_action", ()),
        ("another_action(1)", "", "another_action", (1,)),
        ("foo(True, False)", "", "foo", (True, False)),
        ("foo.bar.baz(3, 3.15, 'Python')", "foo.bar", "baz", (3, 3.15, "Python")),
        ("m1234.n5678(None, [1, 2])", "m1234", "n5678", (None, [1, 2])),
    ],
)
def test_parse_action(
    action_string: str,
    expected_namespace: str,
    expected_name: str,
    expected_arguments: tuple[Any],
) -> None:
    namespace, action_name, action_arguments = parse(action_string)
    assert namespace == expected_namespace
    assert action_name == expected_name
    assert action_arguments == expected_arguments


@pytest.mark.parametrize(
    ("action_string", "expected_arguments"),
    [
        ("f()", ()),
        ("f(())", ((),)),
        ("f((1, 2, 3))", ((1, 2, 3),)),
        ("f((1, 2, 3), (1, 2, 3))", ((1, 2, 3), (1, 2, 3))),
        ("f(((1, 2), (), None), 0)", (((1, 2), (), None), 0)),
        ("f((((((1))))))", (1,)),
        ("f(((((((((1, 2)))))))))", ((1, 2),)),
        ("f((1, 2), (3, 4))", ((1, 2), (3, 4))),
        ("f((((((1, 2), (3, 4))))))", (((1, 2), (3, 4)),)),
    ],
)
def test_nested_and_convoluted_tuple_arguments(
    action_string: str, expected_arguments: tuple[Any]
) -> None:
    """Test that tuple arguments are parsed correctly."""
    _, _, args = parse(action_string)
    assert args == expected_arguments


@pytest.mark.parametrize(
    ["action_string", "expected_arguments"],
    [
        ("f('')", ("",)),
        ('f("")', ("",)),
        ("f('''''')", ("",)),
        ('f("""""")', ("",)),
        ("f('(')", ("(",)),
        ("f(')')", (")",)),  # Regression test for #2088
        ("f('f()')", ("f()",)),
    ],
)
def test_parse_action_nested_special_character_arguments(
    action_string: str, expected_arguments: tuple[Any]
) -> None:
    """Test that special characters nested in strings are handled correctly.

    See also: https://github.com/Textualize/textual/issues/2088
    """
    _, _, args = parse(action_string)
    assert args == expected_arguments


@pytest.mark.parametrize(
    "action_string",
    [
        "foo(,,,,,)",
        "bar(1 2 3 4 5)",
        "baz.spam(Tru, Fals, in)",
        "ham(not)",
        "cheese((((()",
    ],
)
def test_parse_action_raises_error(action_string: str) -> None:
    with pytest.raises(ActionError):
        parse(action_string)