示例#1
0
    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()
示例#2
0
    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"
示例#3
0
    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()
示例#4
0
    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()
示例#5
0
    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()
示例#6
0
    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)
示例#7
0
    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()
示例#8
0
 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")
示例#9
0
 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")
示例#10
0
 def test_name_missing_raises(self) -> None:
     msg = "PromptSpec definition has no 'name'"
     with pytest.raises(UserFacingError, match=msg):
         _prompt.create_prompt_spec()
示例#11
0
 def test_deprecated_default_value_raises_error(self) -> None:
     with pytest.raises(ObsoleteError):
         _prompt.create_prompt_spec(name="test", default_value="hello")
示例#12
0
 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"
示例#13
0
 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)
示例#14
0
 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)
示例#15
0
 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
示例#16
0
 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