Exemplo n.º 1
0
def test_prompt_validate_string_not_empty():
    with fake_input(""):
        with pytest.raises(EOFError):
            prompt("", empty=False)

    with fake_input(""):
        assert prompt("", empty=True) == ""
Exemplo n.º 2
0
def test_prompt_for_bool():
    test = lambda: prompt("bool test", type=bool)
    with fake_input("yes", "y", "no", "n"):
        assert test() is True
        assert test() is True
        assert test() is False
        assert test() is False
Exemplo n.º 3
0
def main():
    with Task("Checking python version") as task:
        task.result = "%d.%d.%d" % sys.version_info[:3]

    with Task("A quick task"):
        pass

    with Task("A task with a custom success message") as task:
        task.result = "that went well!"

    with Task("A task that fails") as task:
        raise Error

    with Task("A task that fails with a custom error"):
        raise Error("crash and burn...")

    try:
        with Task("A task that fails with some other exception"):
            x = 1 / 0
    except ZeroDivisionError:
        print("   ... the exception was reraised and caught as expected ...")

    with TaskGroup("This marks the start of a set of tasks"):
        with Task("Here's one"):
            pass
        with Task("Another one that fails"):
            raise Error
        with Task("Finally a third one") as task:
            task.result = "all done!"

    x = prompt("What is your name?", type=str, stripped=True, default="Foo")
    print("Hello, {} ({})".format(x, type(x)))

    y = prompt("What is your age?", type=int, default=42, retries=3)
    print("Got it: {} years ({})".format(y, type(y)))

    z = prompt("What is your favourite color?",
               choices=[
                   "red", "green", "blue",
                   Separator("--- Other options:"), "i hate colors"
               ],
               default="green")
    print("Color: {} ({})".format(z, type(z)))

    w = prompt("Are we done?", type=bool, default=True)
    print("Done? {} ({})".format(w, type(w)))
Exemplo n.º 4
0
def test_choice_prompt_with_separator():
    choices = ["foo", "bar", Separator(), "quit"]
    test = lambda: prompt("", choices=choices, retries=False)
    with fake_input("1", "2", "3", "4"):
        assert test() == "foo"
        assert test() == "bar"
        assert test() == "quit"
        with pytest.raises(ValidationError):
            test()
Exemplo n.º 5
0
def test_prompt_validate_integer_range():
    test = lambda: prompt(
        "validate integer range", type=int, retries=False, min=4, max=6)

    with fake_input("3", "4", "5", "6", "7"):
        with pytest.raises(ValidationError):
            test()

        assert test() == 4
        assert test() == 5
        assert test() == 6

        with pytest.raises(ValidationError):
            test()
Exemplo n.º 6
0
def test_prompt_for_choice():
    choices = ["red", "green", "blue"]
    with fake_input("1", "3", "2"):
        assert prompt("", choices=choices) == "red"
        assert prompt("", choices=choices) == "blue"
        assert prompt("", choices=choices) == "green"
Exemplo n.º 7
0
def test_prompt_for_integer():
    with fake_input("0", "0x0000", "123", "0xBEEF"):
        assert prompt("", type=int) == 0
        assert prompt("", type=int) == 0
        assert prompt("", type=int) == 123
        assert prompt("", type=int) == 0xBEEF
Exemplo n.º 8
0
def test_prompt_for_string():
    with fake_input("foo"):
        assert prompt("enter name", type=str) == "foo"
Exemplo n.º 9
0
def test_integer_prompt_with_empty_input():
    with fake_input("", ""):
        with pytest.raises(ValidationError):
            prompt("empty int", type=int, empty=False, retries=False)

        assert prompt("empty int", type=int, empty=True, retries=False) is None
Exemplo n.º 10
0
def test_prompt_validate_string_max_length():
    with fake_input("123", "12345"):
        prompt("", maxlen=3, retries=False)
        with pytest.raises(ValidationError):
            prompt("", maxlen=3, retries=False)
Exemplo n.º 11
0
def test_prompt_with_default_value():
    with fake_input("", "", "") as f:
        assert prompt("", type=str, default="foo") == "foo"
        assert prompt("", type=int, default=19) == 19
        assert prompt("", type=bool, default=True) is True
        assert f.eoi()