Debian Codegraph @

tests/types/test_types_number.py

raw symbols SCIP
1 from textwrap import dedent
2
3 import pytest
4
5 from cyclopts.exceptions import ValidationError
6 from cyclopts.types import (
7 HexUInt,
8 HexUInt8,
9 HexUInt16,
10 HexUInt32,
11 HexUInt64,
12 NormFloat,
13 PercentInt,
14 SignedNormFloat,
15 UInt8,
16 )
17
18
19 def test_nested_annotated_validator(app, assert_parse_args):
20 @app.default
21 def default(color: tuple[UInt8, UInt8, UInt8] = (0x00, 0x00, 0x00)):
22 pass
23
24 assert_parse_args(default, "0x12 0x34 0x56", (0x12, 0x34, 0x56))
25
26 with pytest.raises(ValidationError) as e:
27 app.parse_args("100 200 300", exit_on_error=False)
28 assert str(e.value) == 'Invalid value "300" for COLOR. Must be <= 255.'
29
30 with pytest.raises(ValidationError) as e:
31 app.parse_args("--color 100 200 300", exit_on_error=False)
32 assert str(e.value) == 'Invalid value "300" for --color. Must be <= 255.'
33
34
35 def test_nested_list_annotated_validator(app, assert_parse_args):
36 @app.default
37 def default(color: list[tuple[UInt8, UInt8, UInt8]] | None = None):
38 pass
39
40 assert_parse_args(
41 default,
42 "0x12 0x34 0x56 0x78 0x90 0xAB",
43 [(0x12, 0x34, 0x56), (0x78, 0x90, 0xAB)],
44 )
45
46 with pytest.raises(ValidationError) as e:
47 app.parse_args("100 200 300", exit_on_error=False)
48 assert str(e.value) == 'Invalid value "300" for COLOR. Must be <= 255.'
49
50 with pytest.raises(ValidationError) as e:
51 app.parse_args("--color 100 200 300", exit_on_error=False)
52 assert str(e.value) == 'Invalid value "300" for --color. Must be <= 255.'
53
54
55 @pytest.mark.parametrize(
56 "type_hint, expected",
57 [
58 (HexUInt, "0xAF"),
59 (HexUInt8, "0xAF"),
60 (HexUInt16, "0x00AF"),
61 (HexUInt32, "0x000000AF"),
62 (HexUInt64, "0x00000000000000AF"),
63 ],
64 )
65 def test_hexuint_help(app, console, type_hint, expected):
66 @app.command
67 def foo(a: type_hint = 0xAF): # pyright: ignore[reportInvalidTypeForm]
68 pass
69
70 with console.capture() as capture:
71 app.help_print("foo", console=console)
72
73 actual = capture.get()
74
75 assert expected in actual
76
77
78 def test_hexuint_help_no_default(app, console):
79 """Do not show the default, do not error out.
80
81 Checks for bug identified in:
82 https://github.com/BrianPugh/cyclopts/issues/437
83 """
84
85 @app.command
86 def foo(a: HexUInt32):
87 pass
88
89 with console.capture() as capture:
90 app.help_print("foo", console=console)
91
92 actual = capture.get()
93
94 expected = dedent(
95 """\
96 Usage: test_types_number foo A
97
98 ╭─ Parameters ───────────────────────────────────────────────────────╮
99 │ * A --a [required] │
100 ╰────────────────────────────────────────────────────────────────────╯
101 """
102 )
103
104 assert expected == actual
105
106
107 def test_norm_float(app, assert_parse_args):
108 @app.default
109 def default(val: NormFloat):
110 pass
111
112 assert_parse_args(default, "0.0", 0.0)
113 assert_parse_args(default, "0.5", 0.5)
114 assert_parse_args(default, "1.0", 1.0)
115
116 with pytest.raises(ValidationError):
117 app.parse_args("-0.1", exit_on_error=False)
118 with pytest.raises(ValidationError):
119 app.parse_args("1.1", exit_on_error=False)
120
121
122 def test_signed_norm_float(app, assert_parse_args):
123 @app.default
124 def default(val: SignedNormFloat):
125 pass
126
127 assert_parse_args(default, "-- -1.0", -1.0)
128 assert_parse_args(default, "0.0", 0.0)
129 assert_parse_args(default, "1.0", 1.0)
130
131 with pytest.raises(ValidationError):
132 app.parse_args("-- -1.1", exit_on_error=False)
133 with pytest.raises(ValidationError):
134 app.parse_args("1.1", exit_on_error=False)
135
136
137 def test_percent_int(app, assert_parse_args):
138 @app.default
139 def default(val: PercentInt):
140 pass
141
142 assert_parse_args(default, "0", 0)
143 assert_parse_args(default, "50", 50)
144 assert_parse_args(default, "100", 100)
145
146 with pytest.raises(ValidationError):
147 app.parse_args("-- -1", exit_on_error=False)
148 with pytest.raises(ValidationError):
149 app.parse_args("101", exit_on_error=False)