Exemplo n.º 1
0
 def test_prompt_filter(self):
     self.inp.send_text("hello\n")
     input_prompt = InputPrompt(
         message="yes",
         style=None,
         default="world",
         qmark="!",
         vi_mode=False,
         input=self.inp,
         output=DummyOutput(),
         filter=lambda x: x * 2,
         transformer=lambda _: "what",
     )
     result = input_prompt.execute()
     self.assertEqual(result, "worldhelloworldhello")
     self.assertEqual(input_prompt.status["answered"], True)
     self.assertEqual(input_prompt.status["result"], "worldhello")
     self.assertEqual(
         input_prompt._get_prompt_message(),
         [
             ("class:answermark", "?"),
             ("class:answered_question", " yes"),
             ("class:answer", " what"),
         ],
     )
Exemplo n.º 2
0
 def test_vi_kb(self):
     prompt = InputPrompt(message="")
     self.assertEqual(prompt._editing_mode, EditingMode.EMACS)
     os.environ["INQUIRERPY_VI_MODE"] = "true"
     prompt = InputPrompt(message="")
     self.assertEqual(prompt._editing_mode, EditingMode.VI)
     prompt = InputPrompt(message="", vi_mode=False)
     self.assertEqual(prompt._editing_mode, EditingMode.VI)
     del os.environ["INQUIRERPY_VI_MODE"]
Exemplo n.º 3
0
 def test_handle_interrupt(self):
     prompt = InputPrompt(message="")
     with patch("prompt_toolkit.utils.Event") as mock:
         event = mock.return_value
         prompt._handle_interrupt(event)
     self.assertEqual(prompt.status["answered"], True)
     self.assertEqual(prompt.status["result"],
                      INQUIRERPY_KEYBOARD_INTERRUPT)
     self.assertEqual(prompt.status["skipped"], True)
Exemplo n.º 4
0
    def test_execute_kbi(self, mocked_run):
        prompt = InputPrompt(message="")
        mocked_run.return_value = INQUIRERPY_KEYBOARD_INTERRUPT
        self.assertTrue(prompt._raise_kbi)
        self.assertRaises(KeyboardInterrupt, prompt.execute, True)

        os.environ["INQUIRERPY_NO_RAISE_KBI"] = "True"
        prompt = InputPrompt(message="")
        self.assertFalse(prompt._raise_kbi)
        del os.environ["INQUIRERPY_NO_RAISE_KBI"]
Exemplo n.º 5
0
 def test_prompt_message_initial(self):
     input_prompt = InputPrompt(message="Enter your name",
                                style=None,
                                qmark="[?]")
     message = input_prompt._get_prompt_message()
     message = input_prompt._get_prompt_message()
     self.assertEqual(
         message,
         [
             ("class:questionmark", "[?]"),
             ("class:question", " Enter your name"),
             ("class:instruction", " "),
         ],
     )
Exemplo n.º 6
0
 def test_prompt_message_answered(self):
     input_prompt = InputPrompt(message="Enter your name",
                                style=None,
                                qmark="[?]")
     input_prompt.status["answered"] = True
     input_prompt.status["result"] = "haha"
     message = input_prompt._get_prompt_message()
     self.assertEqual(
         message,
         [
             ("class:answermark", "?"),
             ("class:answered_question", " Enter your name"),
             ("class:answer", " haha"),
         ],
     )
Exemplo n.º 7
0
 def test_prompt_result_async(self):
     self.inp.send_text("hello\n")
     input_prompt = InputPrompt(
         message="yes",
         style=None,
         default="world",
         qmark="!",
         vi_mode=False,
         input=self.inp,
         output=DummyOutput(),
     )
     result = asyncio.run(input_prompt.execute_async())
     self.assertEqual(result, "worldhello")
     self.assertEqual(input_prompt.status["answered"], True)
     self.assertEqual(input_prompt.status["result"], "worldhello")
     self.assertEqual(input_prompt.status["skipped"], False)
Exemplo n.º 8
0
    def test_prompt_completion(self):
        input_prompt = InputPrompt(
            message="yes",
            style=None,
            default="",
            qmark="!",
            vi_mode=False,
            input=self.inp,
            output=DummyOutput(),
            multiline=True,
            completer={
                "hello": None,
                "hey": None,
                "what": None
            },
        )

        completer = input_prompt._completer
        doc_text = "he"
        doc = Document(doc_text, len(doc_text))
        event = CompleteEvent()
        completions = [
            completion.text for completion in list(
                completer.get_completions(doc, event))  # type: ignore
        ]
        self.assertEqual(sorted(completions), ["hello", "hey"])
Exemplo n.º 9
0
 def test_prompt_result_multiline(self):
     self.inp.send_text("hello\nworld\nfoo\nboo\x1b\r")
     input_prompt = InputPrompt(
         message="yes",
         style=None,
         default="",
         qmark="!",
         vi_mode=False,
         input=self.inp,
         output=DummyOutput(),
         multiline=True,
     )
     result = input_prompt.execute()
     self.assertEqual(result, "hello\nworld\nfoo\nboo")
     self.assertEqual(input_prompt.status["answered"], True)
     self.assertEqual(input_prompt.status["result"],
                      "hello\nworld\nfoo\nboo")
     self.assertEqual(input_prompt.status["skipped"], False)
Exemplo n.º 10
0
    def test_register_kb(self, mocked_kb):
        prompt = InputPrompt(message="")
        hello = prompt.register_kb("alt-a", "alt-b", filter=True)

        @hello
        def _(_):  # type:ignore
            pass

        mocked_kb.assert_has_calls(
            [call("escape", "a", "escape", "b", filter=True)])

        condition = Condition(lambda: True)
        hello = prompt.register_kb("c-i", filter=condition)

        @hello
        def _(_):
            pass

        mocked_kb.assert_has_calls([call("c-i", filter=condition)])
Exemplo n.º 11
0
 def test_prompt_message_multiline(self):
     input_prompt = InputPrompt(
         message="Enter your name",
         style=None,
         default="",
         qmark="[?]",
         vi_mode=False,
         multiline=True,
         completer={
             "hello": None,
             "hey": None,
             "what": None
         },
     )
     message = input_prompt._get_prompt_message()
     self.assertEqual(
         message,
         [
             ("class:questionmark", "[?]"),
             ("class:question", " Enter your name"),
             ("class:instruction", " ESC + Enter to finish input"),
             ("class:questionmark", "\n%s " % INQUIRERPY_POINTER_SEQUENCE),
         ],
     )
     input_prompt.status["answered"] = True
     input_prompt.status["result"] = "haha\n123"
     message = input_prompt._get_prompt_message()
     self.assertEqual(
         message,
         [
             ("class:answermark", "?"),
             ("class:answered_question", " Enter your name"),
             ("class:answer", " haha...[3 chars]"),
         ],
     )
Exemplo n.º 12
0
    def test_resolver_transformer(self):
        prompt = ListPrompt(
            message="Select one", choices=["1", "2", "3"], transformer=lambda x: x * 2
        )
        self.assertEqual(
            prompt._get_prompt_message(),
            [
                ("class:questionmark", "?"),
                ("class:question", " Select one"),
                ("class:instruction", " "),
            ],
        )
        prompt.status["result"] = "1"
        prompt.status["answered"] = True
        self.assertEqual(
            prompt._get_prompt_message(),
            [
                ("class:answermark", "?"),
                ("class:answered_question", " Select one"),
                ("class:answer", " 11"),
            ],
        )

        prompt = InputPrompt(
            message="Select one",
            transformer=lambda x: x * 2,
        )
        prompt.status["result"] = "2"
        prompt.status["answered"] = True
        self.assertEqual(
            prompt._get_prompt_message(),
            [
                ("class:answermark", "?"),
                ("class:answered_question", " Select one"),
                ("class:answer", " 22"),
            ],
        )

        prompt = SecretPrompt(
            message="Select one",
            transformer=lambda x: x * 2,
        )
        prompt.status["result"] = "2"
        prompt.status["answered"] = True
        self.assertEqual(
            prompt._get_prompt_message(),
            [
                ("class:answermark", "?"),
                ("class:answered_question", " Select one"),
                ("class:answer", " 22"),
            ],
        )
Exemplo n.º 13
0
    def test_handle_completion(self):
        prompt = InputPrompt(message="")
        with patch("prompt_toolkit.utils.Event") as mock:
            event = mock.return_value
            prompt._handle_completion(event)
            mock.assert_not_called()

        prompt = InputPrompt(message="", completer={})
        with patch("prompt_toolkit.utils.Event") as mock:
            event = mock.return_value
            prompt._handle_completion(event)
            mock.assert_has_calls([
                call().app.current_buffer.complete_state.__bool__(),
                call().app.current_buffer.complete_next(),
            ])
Exemplo n.º 14
0
 def test_callable_called(
     self,
     MockedSession,
     MockedKeyBindings,
     MockedStyle,
     mocked_message,
     MockedLexer,
     mocked_completer,
 ):
     completer = mocked_completer()
     kb = MockedKeyBindings()
     style = MockedStyle()
     lexer = MockedLexer()
     InputPrompt(
         message="Enter your name",
         style=None,
         default="",
         qmark="[?]",
         vi_mode=False,
         multiline=True,
         completer={
             "hello": None,
             "hey": None,
             "what": None
         },
     )
     MockedSession.assert_called_once_with(
         message=mocked_message,
         key_bindings=kb,
         style=style,
         completer=completer,
         validator=ANY,
         validate_while_typing=False,
         input=None,
         output=None,
         editing_mode=EditingMode.EMACS,
         lexer=lexer,
         is_password=False,
         multiline=True,
         complete_style=CompleteStyle.COLUMN,
         wrap_lines=True,
         bottom_toolbar=None,
     )
     mocked_completer.assert_has_calls(
         [call({
             "hello": None,
             "hey": None,
             "what": None
         })])
     MockedLexer.assert_has_calls([call("class:input")])
Exemplo n.º 15
0
 def test_constructor_default(self, mocked_style, mocked_validator,
                              mocked_kb):
     input_prompt = InputPrompt(message="Enter your name",
                                style=None,
                                default="1")
     self.assertEqual(input_prompt._message, "Enter your name")
     mocked_style.assert_has_calls([call(get_sample_style())])
     self.assertEqual(input_prompt._default, "1")
     self.assertEqual(input_prompt._qmark, "?")
     self.assertEqual(input_prompt._amark, "?")
     self.assertEqual(input_prompt._editing_mode, EditingMode.EMACS)
     mocked_validator.assert_has_calls(
         [call(ANY, "Invalid input", move_cursor_to_end=True)])
     mocked_kb.assert_has_calls([call("c-c")])
Exemplo n.º 16
0
    def test_execute_filter(self, mocked_run):
        mocked_run.return_value = "1"
        prompt = InputPrompt(message="")
        result = prompt.execute()
        self.assertEqual(result, "1")

        prompt = InputPrompt(message="", filter=lambda result: int(result) * 2)
        result = prompt.execute()
        self.assertEqual(result, 2)
Exemplo n.º 17
0
 def test_prompt_message_kbi(self):
     input_prompt = InputPrompt(message="Enter your name",
                                style=None,
                                qmark="[?]")
     input_prompt.status["answered"] = True
     input_prompt.status["result"] = INQUIRERPY_KEYBOARD_INTERRUPT
     input_prompt.status["skipped"] = True
     message = input_prompt._get_prompt_message()
     self.assertEqual(message, [("class:skipped", "[?]"),
                                ("class:skipped", " Enter your name ")])
Exemplo n.º 18
0
 def test_keybinding_factory(self, mocked_kb):
     prompt = InputPrompt(message="")
     mocked_kb.assert_has_calls([call(Keys.Enter, filter=ANY)])
     mocked_kb.assert_has_calls([call(Keys.Escape, Keys.Enter, filter=ANY)])
     mocked_kb.assert_has_calls([call("c-c", filter=prompt._is_rasing_kbi)])
     mocked_kb.assert_has_calls(
         [call("c-d", filter=~prompt._is_rasing_kbi)])
     mocked_kb.assert_has_calls(
         [call("c-c", filter=~prompt._is_rasing_kbi)])
     mocked_kb.reset_mock()
     prompt = partial(InputPrompt,
                      message="",
                      keybindings={"hello": [{
                          "key": "c-d"
                      }]})
     self.assertRaises(RequiredKeyNotFound, prompt)
Exemplo n.º 19
0
 def test_constructor_custom(self, mocked_style, mocked_validator):
     input_prompt = InputPrompt(
         message=lambda _: "Enter your name",
         style=get_style({"questionmark": "#111111"}, style_override=False),
         qmark="[?]",
         amark="*",
         default=lambda _: "1",
         vi_mode=True,
         validate=NumberValidator(),
     )
     style = get_sample_style()
     style["questionmark"] = "#111111"
     self.assertEqual(input_prompt._message, "Enter your name")
     mocked_style.assert_has_calls([call(style)])
     self.assertEqual(input_prompt._default, "1")
     self.assertEqual(input_prompt._qmark, "[?]")
     self.assertEqual(input_prompt._amark, "*")
     self.assertEqual(input_prompt._editing_mode, EditingMode.VI)
     mocked_validator.assert_not_called()
Exemplo n.º 20
0
    def test_long_instruction(self, MockedSession):
        InputPrompt(message="", long_instruction="asfasdf")

        MockedSession.assert_called_once_with(
            message=ANY,
            key_bindings=ANY,
            style=ANY,
            completer=ANY,
            validator=ANY,
            validate_while_typing=False,
            input=None,
            output=None,
            editing_mode=EditingMode.EMACS,
            lexer=ANY,
            is_password=False,
            multiline=False,
            complete_style=CompleteStyle.COLUMN,
            wrap_lines=True,
            bottom_toolbar=[("class:long_instruction", "asfasdf")],
        )
Exemplo n.º 21
0
    def test_handle_skip(self) -> None:
        prompt = InputPrompt(message="",
                             mandatory=True,
                             mandatory_message="hello")
        with patch("prompt_toolkit.utils.Event") as mock:
            event = mock.return_value
            prompt._handle_skip(event)

        self.assertEqual(prompt._session.default_buffer.validation_state,
                         ValidationState.INVALID)
        self.assertEqual(str(prompt._session.default_buffer.validation_error),
                         "hello")
        self.assertEqual(prompt.status["answered"], False)
        self.assertEqual(prompt.status["skipped"], False)
        self.assertEqual(prompt.status["result"], None)

        prompt = InputPrompt(message="", mandatory=False)
        with patch("prompt_toolkit.utils.Event") as mock:
            event = mock.return_value
            prompt._handle_skip(event)
        self.assertEqual(prompt.status["answered"], True)
        self.assertEqual(prompt.status["skipped"], True)
        self.assertEqual(prompt.status["result"], None)
Exemplo n.º 22
0
    def test_prompt_message_instruction(self):
        input_prompt = InputPrompt(
            message="Enter your name:",
            style=None,
            default="",
            qmark="[?]",
            vi_mode=False,
            instruction="(abc)",
        )
        message = input_prompt._get_prompt_message()
        self.assertEqual(
            message,
            [
                ("class:questionmark", "[?]"),
                ("class:question", " Enter your name:"),
                ("class:instruction", " (abc) "),
            ],
        )

        input_prompt = InputPrompt(
            message="Enter your name:",
            style=None,
            default="",
            qmark="[?]",
            vi_mode=False,
            instruction="(abc)",
            multiline=True,
        )
        message = input_prompt._get_prompt_message()
        self.assertEqual(
            message,
            [
                ("class:questionmark", "[?]"),
                ("class:question", " Enter your name:"),
                ("class:instruction", " (abc) "),
                ("class:questionmark", "\n%s " % INQUIRERPY_POINTER_SEQUENCE),
            ],
        )
Exemplo n.º 23
0
    def test_optional_keyboard_interrupt(self, mocked_execute):
        mocked_execute.return_value = INQUIRERPY_KEYBOARD_INTERRUPT
        questions = [{"type": "input", "message": "hello"}]
        try:
            prompt(questions, raise_keyboard_interrupt=False)
        except KeyboardInterrupt:
            pass
        else:
            self.fail("should raise kbi")

        mocked_execute.return_value = INQUIRERPY_KEYBOARD_INTERRUPT
        self.assertRaises(
            KeyboardInterrupt, prompt, questions, raise_keyboard_interrupt=True
        )

        input_prompt = InputPrompt(message="hello")
        input_prompt.status["answered"] = True
        input_prompt.status["result"] = INQUIRERPY_KEYBOARD_INTERRUPT
        input_prompt.status["skipped"] = True
        self.assertEqual(
            input_prompt._get_prompt_message(),
            [("class:skipped", "?"), ("class:skipped", " hello ")],
        )