File: test_types_number.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 (42 lines) | stat: -rw-r--r-- 1,428 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
from typing import Optional

import pytest

from cyclopts.exceptions import ValidationError
from cyclopts.types import UInt8


def test_nested_annotated_validator(app, assert_parse_args):
    @app.default
    def default(color: tuple[UInt8, UInt8, UInt8] = (0x00, 0x00, 0x00)):
        pass

    assert_parse_args(default, "0x12 0x34 0x56", (0x12, 0x34, 0x56))

    with pytest.raises(ValidationError) as e:
        app.parse_args("100 200 300", exit_on_error=False)
    assert str(e.value) == 'Invalid value "300" for "COLOR". Must be <= 255.'

    with pytest.raises(ValidationError) as e:
        app.parse_args("--color 100 200 300", exit_on_error=False)
    assert str(e.value) == 'Invalid value "300" for "--color". Must be <= 255.'


def test_nested_list_annotated_validator(app, assert_parse_args):
    @app.default
    def default(color: Optional[list[tuple[UInt8, UInt8, UInt8]]] = None):
        pass

    assert_parse_args(
        default,
        "0x12 0x34 0x56 0x78 0x90 0xAB",
        [(0x12, 0x34, 0x56), (0x78, 0x90, 0xAB)],
    )

    with pytest.raises(ValidationError) as e:
        app.parse_args("100 200 300", exit_on_error=False)
    assert str(e.value) == 'Invalid value "300" for "COLOR". Must be <= 255.'

    with pytest.raises(ValidationError) as e:
        app.parse_args("--color 100 200 300", exit_on_error=False)
    assert str(e.value) == 'Invalid value "300" for "--color". Must be <= 255.'