def test_multiple_choice__no_user_input__default_value(): result = user_io.multiple_choice( "Some question?", [SOME_STRING, SOME_OTHER_STRING], no_user_input=True, default=SOME_OTHER_STRING, ) assert result == SOME_OTHER_STRING
def test_multiple_choice__yes_user_input__prompt_result_mapped_to_value( mocker): # expected value is second option mocker.patch("prompt_toolkit.shortcuts.prompt", return_value="2") result = user_io.multiple_choice("Some question?", [SOME_STRING, SOME_OTHER_STRING], default=SOME_STRING) assert result == SOME_OTHER_STRING
def ask(self, answers: Answers, no_user_input: bool = False) -> str: """ Prompt the user with this question. :param answers: The answers that have been provided this far. :param no_user_input: If `True` the default value for the question will be used without waiting for the user to provide an answer. Default: False :return: The answer to the question. """ return user_io.multiple_choice( to_value(self._message, answers, str), to_value(self._options, answers, list), default=to_value(self._default, answers, str), no_user_input=no_user_input, )
def test_multiple_choice__no_options__value_error(): with pytest.raises(ValueError): user_io.multiple_choice("Some question?", [], default="100")
def test_multiple_choice__default_not_option__value_error(): with pytest.raises(ValueError): user_io.multiple_choice("Some question?", ["1", "2", "3"], default="100")