Skip to content

Commit 290387b

Browse files
committed
Adds tests
1 parent a81fc77 commit 290387b

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

tests/test_type_conversion.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from enum import Enum
22
from pathlib import Path
3-
from typing import Any, List, Optional, Tuple
3+
from typing import Any, List, Optional, Tuple, Union
44

55
import click
66
import pytest
@@ -50,6 +50,31 @@ def opt(user: str | None = None):
5050
assert result.exit_code == 0
5151
assert "User: Camila" in result.output
5252

53+
@pytest.mark.parametrize(
54+
("value", "expected"),
55+
[
56+
("0", "ROOTED!"),
57+
("12", "ID: 12"),
58+
("name", "USER: name")
59+
],
60+
)
61+
def test_union(value, expected):
62+
app = typer.Typer()
63+
64+
@app.command()
65+
def opt(id_or_name: Union[int, str]):
66+
if isinstance(id_or_name, int):
67+
if id_or_name == 0:
68+
print("ROOTED!")
69+
else:
70+
print(f"ID: {id_or_name}")
71+
else:
72+
print(f"USER: {id_or_name}")
73+
74+
result = runner.invoke(app, [value])
75+
assert result.exit_code == 0
76+
assert expected in result.output
77+
5378

5479
def test_optional_tuple():
5580
app = typer.Typer()

0 commit comments

Comments
 (0)