diff --git a/tests/test_tutorial/test_options/__init__.py b/tests/test_tutorial/test_options/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_tutorial/test_options/test_tutorial001.py b/tests/test_tutorial/test_options/test_tutorial001.py new file mode 100644 index 0000000..835ed58 --- /dev/null +++ b/tests/test_tutorial/test_options/test_tutorial001.py @@ -0,0 +1,48 @@ +import subprocess + +import typer +from typer.testing import CliRunner + +from options import tutorial001 as mod + +runner = CliRunner() + +app = typer.Typer() +app.command()(mod.main) + + +def test_help(): + result = runner.invoke(app, ["--help"]) + assert result.exit_code == 0 + assert "Say hi to NAME, optionally with a --lastname." in result.output + assert "If --formal is used, say hi very formally." in result.output + assert "Last name of person to greet." in result.output + assert "Say hi formally." in result.output + + +def test_1(): + result = runner.invoke(app, ["Camila"]) + assert result.exit_code == 0 + assert "Hello Camila" in result.output + + +def test_option_lastname(): + result = runner.invoke(app, ["Camila", "--lastname", "Gutiérrez"]) + assert result.exit_code == 0 + assert "Hello Camila Gutiérrez" in result.output + + +def test_formal(): + result = runner.invoke(app, ["Camila", "--lastname", "Gutiérrez", "--formal"]) + assert result.exit_code == 0 + assert "Good day Ms. Camila Gutiérrez." in result.output + + +def test_script(): + result = subprocess.run( + ["coverage", "run", mod.__file__, "--help"], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + encoding="utf-8", + ) + assert "Usage" in result.stdout diff --git a/tests/test_tutorial/test_options/test_tutorial002.py b/tests/test_tutorial/test_options/test_tutorial002.py new file mode 100644 index 0000000..77c880a --- /dev/null +++ b/tests/test_tutorial/test_options/test_tutorial002.py @@ -0,0 +1,40 @@ +import subprocess + +import typer +from typer.testing import CliRunner + +from options import tutorial002 as mod + +runner = CliRunner() + +app = typer.Typer() +app.command()(mod.main) + + +def test_1(): + result = runner.invoke(app, ["Camila"]) + assert result.exit_code != 0 + assert 'Error: Missing option "--lastname".' in result.output + + +def test_option_lastname(): + result = runner.invoke(app, ["Camila", "--lastname", "Gutiérrez"]) + assert result.exit_code == 0 + assert "Hello Camila Gutiérrez" in result.output + + +def test_help(): + result = runner.invoke(app, ["--help"]) + assert result.exit_code == 0 + assert "--lastname TEXT" in result.output + assert "[required]" in result.output + + +def test_script(): + result = subprocess.run( + ["coverage", "run", mod.__file__, "--help"], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + encoding="utf-8", + ) + assert "Usage" in result.stdout diff --git a/tests/test_tutorial/test_options/test_tutorial003.py b/tests/test_tutorial/test_options/test_tutorial003.py new file mode 100644 index 0000000..129e5d0 --- /dev/null +++ b/tests/test_tutorial/test_options/test_tutorial003.py @@ -0,0 +1,41 @@ +import subprocess + +import typer +from typer.testing import CliRunner + +from options import tutorial003 as mod + +runner = CliRunner() + +app = typer.Typer() +app.command()(mod.main) + + +def test_option_lastname(): + result = runner.invoke(app, ["Camila", "--lastname", "Gutiérrez"]) + assert result.exit_code == 0 + assert "Hello Camila Gutiérrez" in result.output + + +def test_option_lastname_prompt(): + result = runner.invoke(app, ["Camila"], input="Gutiérrez") + assert result.exit_code == 0 + assert "Lastname: " in result.output + assert "Hello Camila Gutiérrez" in result.output + + +def test_help(): + result = runner.invoke(app, ["--help"]) + assert result.exit_code == 0 + assert "--lastname TEXT" in result.output + assert "[required]" in result.output + + +def test_script(): + result = subprocess.run( + ["coverage", "run", mod.__file__, "--help"], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + encoding="utf-8", + ) + assert "Usage" in result.stdout diff --git a/tests/test_tutorial/test_options/test_tutorial004.py b/tests/test_tutorial/test_options/test_tutorial004.py new file mode 100644 index 0000000..7946248 --- /dev/null +++ b/tests/test_tutorial/test_options/test_tutorial004.py @@ -0,0 +1,41 @@ +import subprocess + +import typer +from typer.testing import CliRunner + +from options import tutorial004 as mod + +runner = CliRunner() + +app = typer.Typer() +app.command()(mod.main) + + +def test_option_lastname(): + result = runner.invoke(app, ["Camila", "--lastname", "Gutiérrez"]) + assert result.exit_code == 0 + assert "Hello Camila Gutiérrez" in result.output + + +def test_option_lastname_prompt(): + result = runner.invoke(app, ["Camila"], input="Gutiérrez") + assert result.exit_code == 0 + assert "Please tell me your last name: " in result.output + assert "Hello Camila Gutiérrez" in result.output + + +def test_help(): + result = runner.invoke(app, ["--help"]) + assert result.exit_code == 0 + assert "--lastname TEXT" in result.output + assert "[required]" in result.output + + +def test_script(): + result = subprocess.run( + ["coverage", "run", mod.__file__, "--help"], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + encoding="utf-8", + ) + assert "Usage" in result.stdout diff --git a/tests/test_tutorial/test_options/test_tutorial005.py b/tests/test_tutorial/test_options/test_tutorial005.py new file mode 100644 index 0000000..3b4557a --- /dev/null +++ b/tests/test_tutorial/test_options/test_tutorial005.py @@ -0,0 +1,50 @@ +import subprocess + +import typer +from typer.testing import CliRunner + +from options import tutorial005 as mod + +runner = CliRunner() + +app = typer.Typer() +app.command()(mod.main) + + +def test_prompt(): + result = runner.invoke(app, input="Old Project\nOld Project\n") + assert result.exit_code == 0 + assert "Deleting project Old Project" in result.output + + +def test_prompt_not_equal(): + result = runner.invoke( + app, input="Old Project\nNew Spice\nOld Project\nOld Project\n" + ) + assert result.exit_code == 0 + assert "Error: the two entered values do not match" in result.output + assert "Deleting project Old Project" in result.output + + +def test_option(): + result = runner.invoke(app, ["--project-name", "Old Project"]) + assert result.exit_code == 0 + assert "Deleting project Old Project" in result.output + assert "Project name: " not in result.output + + +def test_help(): + result = runner.invoke(app, ["--help"]) + assert result.exit_code == 0 + assert "--project-name TEXT" in result.output + assert "[required]" in result.output + + +def test_script(): + result = subprocess.run( + ["coverage", "run", mod.__file__, "--help"], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + encoding="utf-8", + ) + assert "Usage" in result.stdout diff --git a/tests/test_tutorial/test_options/test_tutorial006.py b/tests/test_tutorial/test_options/test_tutorial006.py new file mode 100644 index 0000000..df08073 --- /dev/null +++ b/tests/test_tutorial/test_options/test_tutorial006.py @@ -0,0 +1,34 @@ +import subprocess + +import typer +from typer.testing import CliRunner + +from options import tutorial006 as mod + +runner = CliRunner() + +app = typer.Typer() +app.command()(mod.main) + + +def test_call(): + result = runner.invoke(app) + assert result.exit_code == 0 + assert "Hello Wade Wilson" in result.output + + +def test_help(): + result = runner.invoke(app, ["--help"]) + assert result.exit_code == 0 + assert "--fullname TEXT" in result.output + assert "[default: Wade Wilson]" in result.output + + +def test_script(): + result = subprocess.run( + ["coverage", "run", mod.__file__, "--help"], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + encoding="utf-8", + ) + assert "Usage" in result.stdout