Skip to content

Commit

Permalink
test the custom prompt class
Browse files Browse the repository at this point in the history
Signed-off-by: Grant Ramsay <seapagan@gmail.com>
  • Loading branch information
seapagan committed Feb 15, 2024
1 parent c7a6afc commit ee348e5
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
2 changes: 1 addition & 1 deletion py_maker/prompt/prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def process_response(self, value: str) -> PromptType: # type: ignore
value = value.strip()
try:
return_value: PromptType = self.response_type(value) # type: ignore
except ValueError as exc:
except ValueError as exc: # pragma: no cover
raise InvalidResponse(self.validate_error_message) from exc

if self.choices is not None:
Expand Down
56 changes: 56 additions & 0 deletions tests/test_prompt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"""Test suite for custom prompt classes in py_maker.prompt."""
import io

import pytest
from rich.console import Console

from py_maker.prompt import Confirm, InvalidResponse, Prompt


def test_confirm_yes() -> None:
"""Test Confirm.ask for a 'yes' response."""
user_input = "y\n"
console = Console(file=io.StringIO())
answer = Confirm.ask(
"Continue?", console=console, stream=io.StringIO(user_input)
)
assert answer is True


def test_confirm_no() -> None:
"""Test Confirm.ask for a 'no' response."""
user_input = "n\n"
console = Console(file=io.StringIO())
answer = Confirm.ask(
"Continue?", console=console, stream=io.StringIO(user_input)
)
assert answer is False


def test_prompt_check_choice() -> None:
"""Test that check_choice is case-insensitive."""
prompt = Prompt(
"What is your favorite color?", choices=["Red", "Green", "Blue"]
)
assert prompt.check_choice("red")
assert prompt.check_choice("GREEN")
assert not prompt.check_choice("yellow")


def test_prompt_process_response() -> None:
"""Test that process_response returns the original choice.
Even if the users types it in a different case, it should always return
the original choice.
"""
prompt = Prompt(
"What is your favorite color?", choices=["Red", "Green", "Blue"]
)
assert prompt.process_response("red") == "Red"
assert prompt.process_response("GREEN") == "Green"
with pytest.raises(InvalidResponse):
prompt.process_response("yellow")


def test_prompt_process_response_value_error() -> None:
"""Test process_response raises ValueError for invalid data."""

0 comments on commit ee348e5

Please sign in to comment.