-
-
Notifications
You must be signed in to change notification settings - Fork 703
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix tutorial docs, source, and tests to match current style
- Loading branch information
Showing
4 changed files
with
107 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
docs_src/multiple_values/multiple_options_with_multiple_values/tutorial001_an.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from typing import List, Tuple | ||
|
||
import typer | ||
from typing_extensions import Annotated | ||
|
||
|
||
def main(borrow: Annotated[List[Tuple[float, str]], typer.Option()] = []): | ||
if not borrow: | ||
print("Congratulations, you're debt-free!") | ||
raise typer.Exit(0) | ||
total = 0.0 | ||
for amount, person in borrow: | ||
print(f"Borrowed {amount:.2f} from {person}") | ||
total += amount | ||
print() | ||
print(f"Total borrowed: {total:.2f}") | ||
|
||
|
||
if __name__ == "__main__": | ||
typer.run(main) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
...al/test_multiple_values/test_multiple_options_with_multiple_values/test_tutorial001_an.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import subprocess | ||
|
||
import typer | ||
from typer.testing import CliRunner | ||
|
||
from docs_src.multiple_values.multiple_options_with_multiple_values import ( | ||
tutorial001_an as mod, | ||
) | ||
|
||
runner = CliRunner() | ||
app = typer.Typer() | ||
app.command()(mod.main) | ||
|
||
|
||
def test_main(): | ||
result = runner.invoke(app) | ||
assert result.exit_code == 0 | ||
assert "Congratulations, you're debt-free!" in result.output | ||
|
||
|
||
def test_borrow_1(): | ||
result = runner.invoke(app, ["--borrow", "2.5", "Mark"]) | ||
assert result.exit_code == 0 | ||
assert "Borrowed 2.50 from Mark" in result.output | ||
assert "Total borrowed: 2.50" in result.output | ||
|
||
|
||
def test_borrow_many(): | ||
result = runner.invoke( | ||
app, | ||
[ | ||
"--borrow", | ||
"2.5", | ||
"Mark", | ||
"--borrow", | ||
"5.25", | ||
"Sean", | ||
"--borrow", | ||
"1.75", | ||
"Wade", | ||
], | ||
) | ||
assert result.exit_code == 0 | ||
assert "Borrowed 2.50 from Mark" in result.output | ||
assert "Borrowed 5.25 from Sean" in result.output | ||
assert "Borrowed 1.75 from Wade" in result.output | ||
assert "Total borrowed: 9.50" in result.output | ||
|
||
|
||
def test_invalid_borrow(): | ||
result = runner.invoke(app, ["--borrow", "2.5"]) | ||
assert result.exit_code != 0 | ||
# TODO: when deprecating Click 7, remove second option | ||
|
||
assert ( | ||
"Option '--borrow' requires 2 arguments" in result.output | ||
or "--borrow option requires 2 arguments" in result.output | ||
) | ||
|
||
|
||
def test_script(): | ||
result = subprocess.run( | ||
["coverage", "run", mod.__file__, "--help"], | ||
capture_output=True, | ||
encoding="utf-8", | ||
) | ||
assert "Usage" in result.stdout |