def test_style(self): style = get_style() self.assertEqual( style, InquirerPyStyle(get_sample_style()), ) os.environ["INQUIRERPY_STYLE_QUESTIONMARK"] = "#000000" os.environ["INQUIRERPY_STYLE_ANSWERMARK"] = "#000000" os.environ["INQUIRERPY_STYLE_ANSWER"] = "#111111" os.environ["INQUIRERPY_STYLE_QUESTION"] = "#222222" os.environ["INQUIRERPY_STYLE_ANSWERED_QUESTION"] = "#222222" os.environ["INQUIRERPY_STYLE_INSTRUCTION"] = "#333333" os.environ["INQUIRERPY_STYLE_INPUT"] = "#444444" os.environ["INQUIRERPY_STYLE_POINTER"] = "#555555" os.environ["INQUIRERPY_STYLE_CHECKBOX"] = "#66666" os.environ["INQUIRERPY_STYLE_SEPARATOR"] = "#777777" os.environ["INQUIRERPY_STYLE_SKIPPED"] = "#888888" os.environ["INQUIRERPY_STYLE_FUZZY_PROMPT"] = "#999999" os.environ["INQUIRERPY_STYLE_FUZZY_INFO"] = "#aaaaaa" os.environ["INQUIRERPY_STYLE_MARKER"] = "#bbbbbb" os.environ["INQUIRERPY_STYLE_FUZZY_BORDER"] = "#cccccc" os.environ["INQUIRERPY_STYLE_FUZZY_MATCH"] = "#dddddd" os.environ["INQUIRERPY_STYLE_VALIDATOR"] = "#dddddd" os.environ["INQUIRERPY_STYLE_SPINNER_PATTERN"] = "#ssssss" os.environ["INQUIRERPY_STYLE_SPINNER_TEXT"] = "#llllll" os.environ["INQUIRERPY_STYLE_LONG_INSTRUCTION"] = "#kkkkkk" style = get_style() self.assertEqual( style, InquirerPyStyle( { "questionmark": "#000000", "answermark": "#000000", "answer": "#111111", "input": "#444444", "question": "#222222", "answered_question": "#222222", "instruction": "#333333", "long_instruction": "#kkkkkk", "pointer": "#555555", "checkbox": "#66666", "separator": "#777777", "skipped": "#888888", "fuzzy_prompt": "#999999", "fuzzy_info": "#aaaaaa", "marker": "#bbbbbb", "validation-toolbar": "#dddddd", "fuzzy_match": "#dddddd", "frame.border": "#cccccc", "spinner_pattern": "#ssssss", "spinner_text": "#llllll", "bottom-toolbar": "noreverse", }, ), )
def test_list_bindings(self): prompt = ListPrompt( message="Select a fruit", choices=self.choices, default="watermelon", style=InquirerPyStyle({"pointer": "#61afef"}), vi_mode=True, qmark="[?]", pointer=">", instruction="(j/k)", ) self.assertEqual(prompt.content_control.selected_choice_index, 2) prompt._handle_down(None) self.assertEqual(prompt.content_control.selected_choice_index, 0) prompt._handle_up(None) self.assertEqual(prompt.content_control.selected_choice_index, 2) prompt._handle_up(None) self.assertEqual(prompt.content_control.selected_choice_index, 1) prompt._handle_down(None) self.assertEqual(prompt.content_control.selected_choice_index, 2) self.assertEqual(prompt.status, { "result": None, "answered": False, "skipped": False }) with patch("prompt_toolkit.utils.Event") as mock: event = mock.return_value prompt._handle_enter(event) self.assertEqual(prompt.status, { "result": "melon", "answered": True, "skipped": False })
def test_handle_enter_multi(self) -> None: prompt = ListPrompt( message="Select a fruit", choices=self.choices, default="watermelon", style=InquirerPyStyle({"pointer": "#61afef"}), vi_mode=True, qmark="[?]", pointer=">", instruction="(j/k)", multiselect=True, ) self.assertEqual(prompt.status, { "result": None, "answered": False, "skipped": False }) with patch("prompt_toolkit.utils.Event") as mock: event = mock.return_value prompt._handle_enter(event) self.assertEqual(prompt.status, { "result": ["melon"], "answered": True, "skipped": False })
def test_get_prompt_message(self): filepath_prompt = FilePathPrompt(message="brah", style=InquirerPyStyle({"foo": ""}), qmark="!", amark="x") message = filepath_prompt._get_prompt_message() self.assertEqual( message, [ ("class:questionmark", "!"), ("class:question", " brah"), ("class:instruction", " "), ], ) filepath_prompt.status["answered"] = True filepath_prompt.status["result"] = "hello" message = filepath_prompt._get_prompt_message() self.assertEqual( message, [ ("class:answermark", "x"), ("class:answered_question", " brah"), ("class:answer", " hello"), ], )
def test_format_style(self): raw = { "questionmark": "#000000", "answermark": "#mmmmmm", "answer": "#111111", "input": "#444444", "question": "#222222", "answered_question": "#222222", "instruction": "#333333", "long_instruction": "#kkkkkk", "pointer": "#555555", "checkbox": "#66666", "separator": "#777777", "skipped": "#888888", "fuzzy_prompt": "#999999", "fuzzy_info": "#aaaaaa", "marker": "#bbbbbb", "validator": "#dddddd", "fuzzy_match": "#dddddd", "fuzzy_border": "#cccccc", "spinner_pattern": "#ssssss", "spinner_text": "#llllll", "bottom-toolbar": "noreverse", } style = get_style(raw) raw["frame.border"] = raw.pop("fuzzy_border") raw["validation-toolbar"] = raw.pop("validator") self.assertEqual( style, InquirerPyStyle(raw), )
def test_list_prompt(self, mocked_term): mocked_term.return_value = (24, 80) message = 15 * "i" qmark = "[?]" instruction = 2 * "i" prompt = ListPrompt( message=message, choices=self.choices, default="watermelon", style=InquirerPyStyle({"pointer": "#61afef"}), vi_mode=True, qmark=qmark, pointer=">", instruction=instruction, show_cursor=True, wrap_lines=True, border=True, ) self.assertEqual(prompt._editing_mode, EditingMode.VI) self.assertIsInstance(prompt.content_control, InquirerPyListControl) self.assertIsInstance(prompt._kb, KeyBindings) self.assertIsInstance(prompt._style, Style) self.assertEqual(prompt._message, message) self.assertEqual(prompt._qmark, qmark) self.assertEqual(prompt.instruction, instruction) self.assertTrue(prompt._border, True)
def test_prompt_message(self): secret_prompt = SecretPrompt(message="fooboo", style=InquirerPyStyle({}), qmark="[?]", vi_mode=True) message = secret_prompt._get_prompt_message() self.assertEqual( message, [ ("class:questionmark", "[?]"), ("class:question", " fooboo"), ("class:instruction", " "), ], ) secret_prompt.status["answered"] = True secret_prompt.status["result"] = "hello" message = secret_prompt._get_prompt_message() self.assertEqual( message, [ ("class:answermark", "?"), ("class:answered_question", " fooboo"), ("class:answer", " *****"), ], ) # instruction secret_prompt = SecretPrompt( message="fooboo", style=InquirerPyStyle({}), qmark="[?]", vi_mode=True, instruction="(abc)", ) message = secret_prompt._get_prompt_message() self.assertEqual( message, [ ("class:questionmark", "[?]"), ("class:question", " fooboo"), ("class:instruction", " (abc) "), ], )
def test_wrap_lines_offset(self, mocked_term): mocked_term.return_value = (24, 80) message = 15 * "i" qmark = "[?]" instruction = 3 * "i" prompt = ListPrompt( message=message, choices=self.choices, default="watermelon", style=InquirerPyStyle({"pointer": "#61afef"}), vi_mode=True, qmark=qmark, pointer=">", instruction=instruction, show_cursor=True, wrap_lines=True, ) self.assertEqual( prompt.extra_line_count, (len(qmark) + 1 + len(message) + 1 + len(instruction) + 1) // 24, ) prompt = ListPrompt( message=message, choices=self.choices, default="watermelon", style=InquirerPyStyle({"pointer": "#61afef"}), vi_mode=True, qmark=qmark, pointer=">", instruction=instruction, show_cursor=False, wrap_lines=True, ) self.assertEqual( prompt.extra_line_count, (len(qmark) + 1 + len(message) + 1 + len(instruction) + 1 - 1) // 24, ) prompt._wrap_lines = False self.assertEqual(prompt.extra_line_count, 0)
def test_input(self): self.inp.send_text("./file1\n") filepath_prompt = FilePathPrompt( message="hello", style=InquirerPyStyle({"qmark": "bold"}), input=self.inp, output=DummyOutput(), ) result = filepath_prompt.execute() self.assertEqual(result, "./file1") self.assertEqual(filepath_prompt.status["answered"], True) self.assertEqual(filepath_prompt.status["result"], "./file1")
def test_default_true(self): self.inp.send_text("\n") confirm_prompt = ConfirmPrompt( message="hello", style=InquirerPyStyle({"qmark": "bold", "answer": "#000000"}), default=True, qmark="x", output=DummyOutput(), input=self.inp, ) result = confirm_prompt.execute() self.assertEqual(result, True) self.assertEqual(confirm_prompt.status["answered"], True) self.assertEqual(confirm_prompt.status["result"], True)
def test_prompt_validation(self, mocked_validate): def _hello(): secret_prompt._session.app.exit(result="yes") mocked_validate.side_effect = _hello self.inp.send_text("afas\n") secret_prompt = SecretPrompt( message="what", style=InquirerPyStyle({}), validate=PasswordValidator(length=8), input=self.inp, output=DummyOutput(), ) result = secret_prompt.execute() mocked_validate.assert_called_once() self.assertEqual(result, "yes") self.assertEqual(secret_prompt.status["answered"], False) self.assertEqual(secret_prompt.status["result"], None)
def test_list_prompt_message(self): prompt = ListPrompt( message="Select a fruit", choices=self.choices, default="watermelon", style=InquirerPyStyle({"pointer": "#61afef"}), vi_mode=True, qmark="[?]", pointer=">", instruction="(j/k)", ) self.assertEqual( prompt._get_prompt_message(), [ ("class:questionmark", "[?]"), ("class:question", " Select a fruit"), ("class:instruction", " (j/k) "), ], )
def test_validation(self, mocked_validate): def _hello(): filepath_prompt._session.app.exit(result="hello") mocked_validate.side_effect = _hello self.inp.send_text("hello\n") filepath_prompt = FilePathPrompt( message="fooboo", style=InquirerPyStyle({"qmark": ""}), default=".vim", validate=PathValidator(), input=self.inp, output=DummyOutput(), ) result = filepath_prompt.execute() mocked_validate.assert_called_once() self.assertEqual(result, "hello") self.assertEqual(filepath_prompt.status["answered"], False) self.assertEqual(filepath_prompt.status["result"], None)
def test_callable_called( self, MockedSession, MockedKeyBindings, MockedStyle, mocked_message, MockedLexer, ): def _validation(_): return True FilePathPrompt( message="yes", style=InquirerPyStyle({"yes": ""}), default="", qmark="XD", multicolumn_complete=True, validate=_validation, vi_mode=True, only_directories=True, ) kb = MockedKeyBindings() style = MockedStyle() lexer = MockedLexer() MockedSession.assert_called_once_with( message=mocked_message, key_bindings=kb, style=style, completer=ANY, validator=ANY, validate_while_typing=False, input=None, output=None, editing_mode=EditingMode.VI, lexer=lexer, is_password=False, multiline=False, complete_style=CompleteStyle.MULTI_COLUMN, wrap_lines=True, bottom_toolbar=None, ) MockedStyle.assert_has_calls([call({"yes": ""})])
def test_prompt_result(self): self.inp.send_text("what\n") secret_prompt = SecretPrompt( message="hello", style=InquirerPyStyle({"answer": ""}), default="yes", qmark="~", vi_mode=False, input=self.inp, output=DummyOutput(), ) result = secret_prompt.execute() self.assertEqual(result, "yeswhat") self.assertEqual( secret_prompt.status, { "answered": True, "result": "yeswhat", "skipped": False }, )
def test_handle_enter_validator(self): prompt = ListPrompt( message="Select a fruit", choices=self.choices, default="watermelon", style=InquirerPyStyle({"pointer": "#61afef"}), vi_mode=True, qmark="[?]", pointer=">", instruction="(j/k)", validate=lambda result: result != "watermelon", ) self.assertFalse(prompt._invalid) self.assertEqual(prompt.status, { "result": None, "answered": False, "skipped": False }) with patch("prompt_toolkit.utils.Event") as mock: event = mock.return_value prompt._handle_enter(event) self.assertEqual(prompt.status, { "result": None, "answered": False, "skipped": False }) self.assertTrue(prompt._invalid) prompt.content_control.selected_choice_index = 0 with patch("prompt_toolkit.utils.Event") as mock: event = mock.return_value prompt._handle_enter(event) self.assertEqual(prompt.status, { "result": "apple", "answered": True, "skipped": False })
def test_resolver_condition(self, mocked_execute, mocked_init): mocked_init.return_value = None mocked_execute.return_value = True questions = [ { "type": "confirm", "name": "first", "message": "Confirm first?", "default": True, }, { "type": "confirm", "name": "second", "message": "Confirm second?", "when": lambda result: result["first"] == True, }, { "type": "confirm", "name": "third", "message": "Confirm?", "when": lambda result: result["second"] == False, }, ] result = prompt(questions) mocked_execute.assert_has_calls([call(), call()]) mocked_init.assert_has_calls( [ call( message="Confirm first?", style=InquirerPyStyle(get_sample_style()), vi_mode=False, default=True, raise_keyboard_interrupt=True, session_result=ANY, keybindings={}, ), call( message="Confirm second?", style=InquirerPyStyle(get_sample_style()), vi_mode=False, raise_keyboard_interrupt=True, session_result=ANY, keybindings={}, ), ] ) self.assertEqual(result, {"first": True, "second": True, "third": None}) mocked_init.reset_mock() mocked_execute.reset_mock() questions = [ { "type": "confirm", "name": "first", "message": "Confirm first?", "default": True, }, { "type": "confirm", "name": "second", "message": "Confirm second?", "when": lambda result: result["first"] == False, }, { "type": "confirm", "name": "third", "message": "Confirm?", "when": lambda result: result["second"] == None, }, ] result = prompt(questions) mocked_execute.assert_has_calls([call(), call()]) mocked_init.assert_has_calls( [ call( message="Confirm first?", style=InquirerPyStyle(get_sample_style()), vi_mode=False, default=True, raise_keyboard_interrupt=True, session_result=ANY, keybindings={}, ), call( message="Confirm?", style=InquirerPyStyle(get_sample_style()), vi_mode=False, raise_keyboard_interrupt=True, session_result=ANY, keybindings={}, ), ] ) self.assertEqual(result, {"first": True, "second": None, "third": True})
def test_resolver_normal( self, mocked_confirm_execute, mocked_confirm_init, mocked_filepath_execute, mocked_filepath_init, ): mocked_confirm_init.return_value = None mocked_confirm_execute.return_value = False questions = [ {"type": "confirm", "message": "hello"}, ] result = prompt(questions) mocked_confirm_init.assert_called_once_with( message="hello", style=InquirerPyStyle(get_sample_style()), vi_mode=False, raise_keyboard_interrupt=True, session_result=ANY, keybindings={}, ) mocked_confirm_execute.assert_called_once() self.assertEqual(result, {0: False}) mocked_filepath_init.return_value = None mocked_filepath_execute.return_value = "hello.py" mocked_confirm_init.reset_mock() mocked_confirm_execute.reset_mock() questions = [ {"type": "confirm", "message": "hello"}, {"type": "confirm", "message": "world", "name": "foo"}, {"type": "filepath", "message": "whaat", "name": "boo", "default": "./"}, ] result = prompt(questions) mocked_confirm_init.assert_has_calls( [ call( message="hello", style=InquirerPyStyle(get_sample_style()), vi_mode=False, raise_keyboard_interrupt=True, session_result=ANY, keybindings={}, ), call( message="world", style=InquirerPyStyle(get_sample_style()), vi_mode=False, raise_keyboard_interrupt=True, session_result=ANY, keybindings={}, ), ] ) mocked_filepath_init.assert_has_calls( [ call( message="whaat", style=InquirerPyStyle(get_sample_style()), default="./", vi_mode=False, raise_keyboard_interrupt=True, session_result=ANY, keybindings={}, ) ] ) mocked_confirm_execute.assert_has_calls([call(), call()]) self.assertEqual(result, {0: False, "foo": False, "boo": "hello.py"})
def test_resolver_style_keys( self, mocked_confirm_execute, mocked_confirm_init, mocked_secret_execute, mocked_secret_init, ): mocked_confirm_execute.return_value = False mocked_confirm_init.return_value = None os.environ["INQUIRERPY_VI_MODE"] = "true" questions = [{"type": "confirm", "message": "Confirm?", "name": "question1"}] result = prompt(questions) mocked_confirm_execute.assert_called_once() mocked_confirm_init.assert_called_once_with( message="Confirm?", style=InquirerPyStyle(get_sample_style()), vi_mode=False, raise_keyboard_interrupt=True, session_result={"question1": False}, keybindings={}, ) self.assertEqual(result, {"question1": False}) del os.environ["INQUIRERPY_VI_MODE"] mocked_secret_init.return_value = None mocked_secret_execute.return_value = "111111" mocked_confirm_execute.reset_mock() mocked_confirm_init.reset_mock() mocked_confirm_execute.return_value = True questions = [ {"type": "confirm", "message": "Confirm?", "name": "10"}, {"type": "confirm", "message": "What?"}, {"type": "password", "message": "haha"}, ] result = prompt( questions, style={"qmark": "#ffffff"}, vi_mode=True, style_override=False ) mocked_confirm_execute.assert_has_calls([call(), call()]) mocked_confirm_init.assert_has_calls( [ call( message="Confirm?", style=InquirerPyStyle(get_sample_style({"qmark": "#ffffff"})), vi_mode=True, raise_keyboard_interrupt=True, session_result=ANY, keybindings={}, ), call( message="What?", style=InquirerPyStyle(get_sample_style({"qmark": "#ffffff"})), vi_mode=True, raise_keyboard_interrupt=True, session_result=ANY, keybindings={}, ), ] ) mocked_secret_init.reset_mock() questions = [ {"type": "confirm", "message": "Confirm?", "name": "10"}, {"type": "confirm", "message": "What?"}, {"type": "password", "message": "haha"}, ] result = prompt( questions, style={"qmark": "#ffffff"}, vi_mode=True, style_override=True, ) style = get_sample_style() for key in style.keys(): style[key] = "" style["qmark"] = "#ffffff" style["fuzzy_border"] = style.pop("frame.border") style["bottom-toolbar"] = "noreverse" mocked_secret_init.assert_has_calls( [ call( message="haha", style=InquirerPyStyle(style), vi_mode=True, raise_keyboard_interrupt=True, session_result={"10": True, 1: True, 2: "111111"}, keybindings={}, ) ] ) self.assertEqual(result, {"10": True, 1: True, 2: "111111"})