def test_call_read_choice(self, ptk_prompt: Mock, read_choice: Mock) -> None: prompt_spec = _prompt.create_prompt_spec(name="greet", choices=["Hi", "Hello"]) _prompt.read_user_variable(prompt_spec) read_choice.assert_called_once_with(prompt_spec) ptk_prompt.assert_not_called()
def test_select_choice(self, ptk_prompt: Mock) -> None: prompt_spec = _prompt.create_prompt_spec(name="greet", choices=["Hi", "Hello"]) ptk_prompt.return_value = "1" assert _prompt.read_user_choice(prompt_spec) == "Hi" ptk_prompt.return_value = "2" assert _prompt.read_user_choice(prompt_spec) == "Hello"
def test_integer_prompt(self, ptk_prompt: Mock, read_choice: Mock) -> None: prompt_spec = _prompt.create_prompt_spec(name="port", default=8000) _prompt.read_user_variable(prompt_spec) ptk_prompt.assert_called_once_with("port: ", default="8000", bottom_toolbar=None, completer=None, validator=ANY) read_choice.assert_not_called()
def test_call_ptk_prompt(self, ptk_prompt: Mock, read_choice: Mock) -> None: ptk_prompt.return_value = "hello" prompt_spec = _prompt.create_prompt_spec(name="test") assert _prompt.read_user_variable(prompt_spec) == "hello" ptk_prompt.assert_called_once_with("test: ", bottom_toolbar=None, completer=None, validator=ANY) read_choice.assert_not_called()
def test_prompt_with_help_text(self, ptk_prompt: Mock, read_choice: Mock) -> None: prompt_spec = _prompt.create_prompt_spec(name="test", help_text="Test info") _prompt.read_user_variable(prompt_spec) ptk_prompt.assert_called_once_with("test: ", bottom_toolbar="Test info", completer=None, validator=ANY) read_choice.assert_not_called()
def test_default_value(self, ptk_prompt: Mock) -> None: prompt_spec = _prompt.create_prompt_spec(name="greet", choices=["Hi", "Hello"], default="Hello") ptk_prompt.return_value = "1" _prompt.read_user_choice(prompt_spec) ptk_prompt.assert_called_once_with(ANY, default="2", bottom_toolbar=None, completer=None, validator=ANY)
def test_boolean_prompt(self, ptk_prompt: Mock, read_choice: Mock) -> None: prompt_spec = _prompt.create_prompt_spec(name="test", default=False) _prompt.read_user_variable(prompt_spec) ptk_prompt.assert_called_once_with( "test (y/n): ", default="n", bottom_toolbar=None, completer=None, validator=ANY, ) read_choice.assert_not_called()
def test_prompt_reraises_unknown_type_error(self, get_dataclass_keys: Mock, prompt_class: Mock) -> None: prompt_class.side_effect = TypeError("Fake error should be re-raised") with pytest.raises(TypeError, match="Fake error should be re-raised"): _prompt.create_prompt_spec(name="test")
def test_unknown_attribute_raises(self) -> None: msg = "PromptSpec definition for 'test' has unknown keys:" with pytest.raises(UserFacingError, match=msg): _prompt.create_prompt_spec(name="test", unknown="value")
def test_name_missing_raises(self) -> None: msg = "PromptSpec definition has no 'name'" with pytest.raises(UserFacingError, match=msg): _prompt.create_prompt_spec()
def test_deprecated_default_value_raises_error(self) -> None: with pytest.raises(ObsoleteError): _prompt.create_prompt_spec(name="test", default_value="hello")
def test_name_and_default(self) -> None: prompt_spec = _prompt.create_prompt_spec(name="test", default="hello") assert prompt_spec.name == "test" assert prompt_spec.default == "hello"
def test_empty_choices_raises_value_error(self, ptk_prompt: Mock) -> None: prompt_spec = _prompt.create_prompt_spec(name="greeting", choices=[]) with pytest.raises(UserFacingError, match="Choices for prompt cannot be empty"): _prompt.read_user_choice(prompt_spec)
def test_string_choices_raises_type_error(self, ptk_prompt: Mock) -> None: prompt_spec = _prompt.create_prompt_spec(name="greeting", choices="Hi, Hello") with pytest.raises(UserFacingError, match="Choices for prompt must be list"): _prompt.read_user_choice(prompt_spec)
def test_explicit_type(self, ptk_prompt: Mock, read_choice: Mock) -> None: prompt_spec = _prompt.create_prompt_spec(name="test", type="bool") prompt_spec.input_type is BoolInput
def test_name_only(self) -> None: prompt_spec = _prompt.create_prompt_spec(name="test") assert prompt_spec.name == "test" assert prompt_spec.default is None assert prompt_spec.input_type == StringInput