diff --git a/py_maker/prompt/prompt.py b/py_maker/prompt/prompt.py index d130b998..33ccb99b 100644 --- a/py_maker/prompt/prompt.py +++ b/py_maker/prompt/prompt.py @@ -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: diff --git a/tests/test_prompt.py b/tests/test_prompt.py new file mode 100644 index 00000000..36880fc7 --- /dev/null +++ b/tests/test_prompt.py @@ -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."""