Debian Codegraph @

tests/test_parameter_count.py

raw symbols SCIP
1 """Tests for Parameter(count=True) functionality."""
2
3 from typing import Annotated, Optional
4
5 import pytest
6
7 from cyclopts import Parameter
8 from cyclopts.exceptions import MissingArgumentError, UnknownOptionError
9
10
11 @pytest.mark.parametrize(
12 "tokens,expected",
13 [
14 ("", 0),
15 ("-v", 1),
16 ("-vv", 2),
17 ("-vvv", 3),
18 ("-v -v -v", 3),
19 ("-v --verbose -v", 3),
20 ("--verbose", 1),
21 ("--verbose --verbose", 2),
22 ("-v --verbose -vv --verbose", 5),
23 ],
24 )
25 def test_count_various_inputs(app, assert_parse_args, tokens, expected):
26 """Test count with various flag combinations."""
27
28 def cmd(verbose: Annotated[int, Parameter(name=("--verbose", "-v"), count=True)] = 0):
29 pass
30
31 app.default(cmd)
32 assert_parse_args(cmd, tokens, expected)
33
34
35 def test_count_negative_disabled(app):
36 """Ensure --no-verbose is NOT generated."""
37
38 def cmd(verbose: Annotated[int, Parameter(name="--verbose", count=True)] = 0):
39 pass
40
41 app.default(cmd)
42 with pytest.raises(UnknownOptionError):
43 app.parse_args("--no-verbose", exit_on_error=False)
44
45
46 @pytest.mark.parametrize(
47 "type_hint",
48 [str, bool, list, dict],
49 )
50 def test_count_wrong_type_error(app, type_hint):
51 """count=True with non-int type should error."""
52
53 def cmd(verbose: Annotated[type_hint, Parameter(count=True)]): # pyright: ignore[reportInvalidTypeForm]
54 pass
55
56 app.default(cmd)
57 with pytest.raises(ValueError, match="requires an int type hint"):
58 app.parse_args("", exit_on_error=False)
59
60
61 def test_count_optional_int(app, assert_parse_args):
62 """count=True with Optional[int] should work."""
63
64 def cmd(verbose: Annotated[Optional[int], Parameter(name="-v", count=True)] = 0):
65 pass
66
67 app.default(cmd)
68 assert_parse_args(cmd, "-vv", 2)
69
70
71 def test_count_multiple_parameters(app, assert_parse_args):
72 """Multiple count parameters in same command."""
73
74 def cmd(
75 verbose: Annotated[int, Parameter(name="-v", count=True)] = 0,
76 quiet: Annotated[int, Parameter(name="-q", count=True)] = 0,
77 ):
78 pass
79
80 app.default(cmd)
81 assert_parse_args(cmd, "", 0, 0)
82 assert_parse_args(cmd, "-vvv", 3, 0)
83 assert_parse_args(cmd, "-qq", 0, 2)
84 assert_parse_args(cmd, "-vv -qqq", 2, 3)
85
86
87 def test_count_with_other_parameters(app, assert_parse_args):
88 """Count flag mixed with regular parameters."""
89
90 def cmd(
91 verbose: Annotated[int, Parameter(name="-v", count=True)] = 0,
92 output: str = "default",
93 ):
94 pass
95
96 app.default(cmd)
97 assert_parse_args(cmd, "-vv --output test.txt", 2, "test.txt")
98 assert_parse_args(cmd, "--output foo -vvv", 3, "foo")
99
100
101 def test_count_no_default(app):
102 """Count without explicit default should be required."""
103
104 def cmd(verbose: Annotated[int, Parameter(name="-v", count=True)]):
105 pass
106
107 app.default(cmd)
108 with pytest.raises(MissingArgumentError):
109 app.parse_args("", exit_on_error=False)
110
111
112 def test_count_help_text(app, console):
113 """Verify help text includes user-provided description."""
114
115 def cmd(verbose: Annotated[int, Parameter(name=("-v", "--verbose"), count=True, help="Increase verbosity")] = 0):
116 """Command with count flag."""
117 pass
118
119 app.default(cmd)
120
121 with console.capture() as capture:
122 app.help_print([], console=console)
123 help_text = capture.get()
124
125 assert "increase verbosity" in help_text.lower()
126 assert "verbose" in help_text.lower()