Skip to content

Commit e5d1236

Browse files
committed
Add test for generate_list_converter
1 parent 62525f2 commit e5d1236

File tree

4 files changed

+169
-0
lines changed

4 files changed

+169
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from typing import List, Optional
2+
3+
import typer
4+
5+
6+
def names_callback(ctx: typer.Context, param: typer.CallbackParam, value: str):
7+
if ctx.resilient_parsing:
8+
return
9+
print(f"Validating param: {param.name}")
10+
if value is None:
11+
return value
12+
if "Camila" not in value:
13+
raise typer.BadParameter("Camila must be in the list")
14+
return value
15+
16+
17+
def main(
18+
names: Optional[List[str]] = typer.Option(None, "--name", callback=names_callback),
19+
):
20+
if names is None:
21+
print("No names provided")
22+
else:
23+
print("Hello {}".format(", ".join(names)))
24+
25+
26+
if __name__ == "__main__":
27+
typer.run(main)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from typing import List, Optional
2+
3+
import typer
4+
from typing_extensions import Annotated
5+
6+
7+
def names_callback(ctx: typer.Context, param: typer.CallbackParam, value: str):
8+
if ctx.resilient_parsing:
9+
return
10+
print(f"Validating param: {param.name}")
11+
if value is None:
12+
return value
13+
if "Camila" not in value:
14+
raise typer.BadParameter("Camila must be in the list")
15+
return value
16+
17+
18+
def main(
19+
names: Annotated[
20+
Optional[List[str]], typer.Option("--name", callback=names_callback)
21+
] = None,
22+
):
23+
if names is None:
24+
print("No names provided")
25+
else:
26+
print("Hello {}".format(", ".join(names)))
27+
28+
29+
if __name__ == "__main__":
30+
typer.run(main)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import os
2+
import subprocess
3+
import sys
4+
5+
import typer
6+
from typer.testing import CliRunner
7+
8+
from docs_src.options.callback import tutorial005 as mod
9+
10+
runner = CliRunner()
11+
12+
app = typer.Typer()
13+
app.command()(mod.main)
14+
15+
16+
def test_1():
17+
result = runner.invoke(app, ["--name", "Camila", "--name", "Victor"])
18+
assert result.exit_code == 0
19+
assert "Validating param: name" in result.output
20+
assert "Hello Camila, Victor" in result.output
21+
22+
23+
def test_2():
24+
result = runner.invoke(app, ["--name", "rick", "--name", "Victor"])
25+
assert result.exit_code != 0
26+
assert "Invalid value for '--name': Camila must be in the list" in result.output
27+
28+
29+
def test_3():
30+
result = runner.invoke(app, [])
31+
assert result.exit_code == 0
32+
assert "No names provided" in result.output
33+
34+
35+
def test_script():
36+
result = subprocess.run(
37+
[sys.executable, "-m", "coverage", "run", mod.__file__, "--help"],
38+
capture_output=True,
39+
encoding="utf-8",
40+
)
41+
assert "Usage" in result.stdout
42+
43+
44+
def test_completion():
45+
result = subprocess.run(
46+
[sys.executable, "-m", "coverage", "run", mod.__file__, " "],
47+
capture_output=True,
48+
encoding="utf-8",
49+
env={
50+
**os.environ,
51+
"_TUTORIAL005.PY_COMPLETE": "complete_bash",
52+
"COMP_WORDS": "tutorial005.py --",
53+
"COMP_CWORD": "1",
54+
},
55+
)
56+
assert "--name" in result.stdout
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import os
2+
import subprocess
3+
import sys
4+
5+
import typer
6+
from typer.testing import CliRunner
7+
8+
from docs_src.options.callback import tutorial005_an as mod
9+
10+
runner = CliRunner()
11+
12+
app = typer.Typer()
13+
app.command()(mod.main)
14+
15+
16+
def test_1():
17+
result = runner.invoke(app, ["--name", "Camila", "--name", "Victor"])
18+
assert result.exit_code == 0
19+
assert "Validating param: name" in result.output
20+
assert "Hello Camila, Victor" in result.output
21+
22+
23+
def test_2():
24+
result = runner.invoke(app, ["--name", "rick", "--name", "Victor"])
25+
assert result.exit_code != 0
26+
assert "Invalid value for '--name': Camila must be in the list" in result.output
27+
28+
29+
def test_3():
30+
result = runner.invoke(app, [])
31+
assert result.exit_code == 0
32+
assert "No names provided" in result.output
33+
34+
35+
def test_script():
36+
result = subprocess.run(
37+
[sys.executable, "-m", "coverage", "run", mod.__file__, "--help"],
38+
capture_output=True,
39+
encoding="utf-8",
40+
)
41+
assert "Usage" in result.stdout
42+
43+
44+
def test_completion():
45+
result = subprocess.run(
46+
[sys.executable, "-m", "coverage", "run", mod.__file__, " "],
47+
capture_output=True,
48+
encoding="utf-8",
49+
env={
50+
**os.environ,
51+
"_TUTORIAL005_AN.PY_COMPLETE": "complete_bash",
52+
"COMP_WORDS": "tutorial005_an.py --",
53+
"COMP_CWORD": "1",
54+
},
55+
)
56+
assert "--name" in result.stdout

0 commit comments

Comments
 (0)