Пример #1
0
def test_ask(io):
    question = Question("What time is it?", "2PM")
    io.set_input("\n8AM\n")

    assert "2PM" == question.ask(io)

    io.clear_error()

    assert "8AM" == question.ask(io)
    assert "What time is it? " == io.fetch_error()
Пример #2
0
def test_ask_hidden_response(io):
    question = Question("What time is it?", "2PM")
    question.hide()
    io.set_input("8AM\n")

    assert "8AM" == question.ask(io)
    assert "What time is it? " == io.fetch_error()
Пример #3
0
def test_ask_and_validate(io):
    error = "This is not a color!"

    def validator(color):
        if color not in ["white", "black"]:
            raise Exception(error)

        return color

    question = Question("What color was the white horse of Henry IV?", "white")
    question.set_validator(validator)
    question.set_max_attempts(2)

    io.set_input("\nblack\n")
    assert "white" == question.ask(io)
    assert "black" == question.ask(io)

    io.set_input("green\nyellow\norange\n")

    with pytest.raises(Exception) as e:
        question.ask(io)

    assert error == str(e.value)
Пример #4
0
def test_no_interaction(io):
    io.set_interactive(False)

    question = Question("Do you have a job?", "not yet")
    assert "not yet" == question.ask(io)
Пример #5
0
def test_ask_question_with_special_characters(io):
    question = Question("What time is it, Sébastien?", "2PMë")
    io.set_input("\n")

    assert "2PMë" == question.ask(io)
    assert "What time is it, Sébastien? " == io.fetch_error()