File: test_bind_tuple.py

package info (click to toggle)
python-cyclopts 3.12.0-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 3,288 kB
  • sloc: python: 11,445; makefile: 24
file content (106 lines) | stat: -rw-r--r-- 2,813 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
96
97
98
99
100
101
102
103
104
105
106
from typing import List, Tuple

import pytest

from cyclopts.exceptions import MissingArgumentError


@pytest.mark.parametrize(
    "cmd_str",
    [
        "1 2 80 160 255",
        "--coordinates 1 2 --color 80 160 255",
        "--color 80 160 255 --coordinates 1 2",
        "--color 80 160 255 --coordinates=1 2",
    ],
)
def test_bind_tuple_basic(app, cmd_str, assert_parse_args):
    @app.default
    def foo(coordinates: Tuple[int, int], color: Tuple[int, int, int]):
        pass

    assert_parse_args(foo, cmd_str, (1, 2), (80, 160, 255))


@pytest.mark.parametrize(
    "cmd_str",
    [
        "1 2 alice 100 200",
        "--coordinates 1 2 --data alice 100 200",
        "--data alice 100 200 --coordinates 1 2",
    ],
)
def test_bind_tuple_nested(app, cmd_str, assert_parse_args):
    @app.default
    def foo(coordinates: Tuple[int, int], data: Tuple[Tuple[str, int], int]):
        pass

    assert_parse_args(foo, cmd_str, (1, 2), (("alice", 100), 200))


@pytest.mark.parametrize(
    "cmd_str",
    [
        "1 2 alice 100 bob 200",
        "--coordinates 1 2 --data alice 100 --data bob 200",
        "--data alice 100 --coordinates 1 2 --data bob 200",
    ],
)
def test_bind_tuple_ellipsis(app, cmd_str, assert_parse_args):
    @app.default
    def foo(coordinates: Tuple[int, int], data: Tuple[Tuple[str, int], ...]):
        pass

    assert_parse_args(foo, cmd_str, (1, 2), (("alice", 100), ("bob", 200)))


@pytest.mark.parametrize(
    "cmd_str",
    [
        "1 2 3",
        "--values 1 --values 2 --values 3",
    ],
)
def test_bind_tuple_no_inner_types(app, cmd_str, assert_parse_args):
    @app.default
    def foo(values: Tuple):
        pass

    # Interpreted as a string because:
    #     1. Tuple -> Tuple[Any, ...]
    #     2. Any is treated the same as no annotation.
    #     3. Even if a default value was supplied, we couldn't unambiguously infer a type.
    #     4. This falls back to string.
    assert_parse_args(foo, cmd_str, ("1", "2", "3"))


@pytest.mark.parametrize(
    "cmd_str",
    [
        "1",
        "--coordinates 1",
    ],
)
def test_bind_tuple_insufficient_tokens(app, cmd_str):
    @app.default
    def foo(coordinates: Tuple[int, int]):
        pass

    with pytest.raises(MissingArgumentError):
        app.parse_args(cmd_str, print_error=False, exit_on_error=False)


@pytest.mark.parametrize(
    "cmd_str",
    [
        "--coordinates 1 2 --color 80 160 255 --coordinates 3 4",
        "--coordinates 1 2 --coordinates 3 4 --color 80 160 255",
        "1 2 3 4 --color 80 160 255",
    ],
)
def test_bind_list_of_tuple(app, cmd_str, assert_parse_args):
    @app.default
    def foo(coordinates: List[Tuple[int, int]], color: Tuple[int, int, int]):
        pass

    assert_parse_args(foo, cmd_str, [(1, 2), (3, 4)], (80, 160, 255))