def test_value_if_not_asked__BasicQuestion(should_ask, value_if_not_asked, expected_answer): interactions = [ Confirm( "a", SOME_STRING, True, should_ask=lambda answers: should_ask, value_if_not_asked=False, ), Choice( "b", SOME_STRING, SOME_OPTIONS, SOME_DEFAULT, should_ask=lambda answers: should_ask, value_if_not_asked=value_if_not_asked, ), BasicQuestion( "c", SOME_STRING, SOME_DEFAULT, should_ask=lambda answers: should_ask, value_if_not_asked=value_if_not_asked, ), ] results = get_answers(interactions, no_user_input=True) assert results == expected_answer
def test_to_answer__confirm_value_set__set_value(): questions = [Confirm(SOME_NAME, SOME_STRING, default=SOME_BOOL)] answers = to_answers(questions, Namespace(**{SOME_NAME: SOME_OTHER_BOOL})) assert SOME_NAME in answers assert answers[SOME_NAME] == SOME_OTHER_BOOL
def test_confirm__dynamic_message__confirm_dynamic_message(mocker): user_io = mocker.patch("columbo._interaction.user_io") Confirm(SOME_NAME, some_dynamic_string, some_dynamic_bool).ask(SOME_ANSWERS, no_user_input=True) user_io.confirm.assert_called_once_with(SOME_DYNAMIC_STRING_RESULT, default=SOME_OTHER_BOOL, no_user_input=True)
def test_confirm__static_message__confirm_called(mocker): user_io = mocker.patch("columbo._interaction.user_io") Confirm(SOME_NAME, SOME_STRING, SOME_BOOL).ask(SOME_ANSWERS, no_user_input=True) user_io.confirm.assert_called_once_with(SOME_STRING, default=SOME_BOOL, no_user_input=True)
def test_parse_args__parser_name__name_in_result(capsys): try: parse_args( [Confirm(SOME_NAME, SOME_STRING)], [SOME_INVALID_ARG_NAME, SOME_INVALID_OPTION], parser_name=SOME_OTHER_STRING, ) except SystemExit: assert SOME_OTHER_STRING in capsys.readouterr().err else: pytest.fail("System exit should have been thrown")
def test_confirm_copy__diff_message__confirm_diff_message(mocker): user_io = mocker.patch("columbo._interaction.user_io") Confirm(SOME_NAME, some_dynamic_string, some_dynamic_bool).copy(message=SOME_STRING, default=SOME_BOOL).ask(SOME_ANSWERS, no_user_input=True) user_io.confirm.assert_called_once_with(SOME_STRING, default=SOME_BOOL, no_user_input=True)
def test_confirm_copy__no_args__confirm_same_message(mocker): user_io = mocker.patch("columbo._interaction.user_io") question = Confirm(SOME_NAME, SOME_STRING, SOME_BOOL) question.ask(SOME_ANSWERS, no_user_input=True) question.copy().ask(SOME_ANSWERS, no_user_input=True) calls = user_io.confirm.mock_calls assert len(calls) == 2 assert calls[0] == calls[1]
def test_format_cli_help__parser_name__name_in_result(): result = format_cli_help([Confirm(SOME_NAME, SOME_STRING)], parser_name=SOME_OTHER_STRING) assert SOME_OTHER_STRING in result
def test_to_answer__confirm_dont_ask__value_not_stored(): questions = [Confirm(SOME_NAME, SOME_STRING, should_ask=lambda _: False)] result = to_answers(questions, Namespace(**{SOME_NAME: SOME_STRING})) assert SOME_NAME not in result
def test_create_parser__confirm_no_option__false(): parser = create_parser([Confirm(SOME_NAME, SOME_STRING)]) result = parser.parse_args([canonical_arg_name(f"no-{SOME_NAME}")]) assert vars(result)[SOME_NAME] is False
def test_create_parser__confirm_main_option__true(): parser = create_parser([Confirm(SOME_NAME, SOME_STRING)]) result = parser.parse_args([SOME_ARG_NAME]) assert vars(result)[SOME_NAME] is True
def test_create_parser__confirm_not_given__no_value(): parser = create_parser([Confirm(SOME_NAME, SOME_STRING)]) result = parser.parse_args([]) assert vars(result)[SOME_NAME] is None
( "choice static options, options listed", [Choice(SOME_NAME, SOME_STRING, SOME_OPTIONS, SOME_DEFAULT)], "--my-test-value {x,y,z}", ), ( "choice dynamic options, options not listed", [ Choice(SOME_NAME, SOME_STRING, some_dynamic_options, SOME_DEFAULT) ], "--my-test-value MY-TEST-VALUE", ), ( "confirm, enabled option", [Confirm(SOME_NAME, SOME_STRING)], "--my-test-value", ), ( "confirm, disabled option", [Confirm(SOME_NAME, SOME_STRING)], "--no-my-test-value", ), ], ) def test_format_cli_help__questions__contains_expected_result( questions, partial_expected_result, description): result = format_cli_help(questions) assert partial_expected_result in result, description
def test_confirm_copy__new_instance(): original = Confirm(SOME_NAME, SOME_STRING, SOME_BOOL) copy = original.copy() assert copy is not original
def test_confirm__no_input__default_value(): result = Confirm(SOME_NAME, SOME_STRING, SOME_BOOL).ask(SOME_ANSWERS, no_user_input=True) assert result == SOME_BOOL