1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
|
import pytest
from clikit.ui.components import ChoiceQuestion
def test_ask_choice(io):
io.set_input(
"\n"
"1\n"
" 1 \n"
"John\n"
"1\n"
"John\n"
"1\n"
"0,2\n"
" 0 , 2 \n"
"\n"
"\n"
"4\n"
"0\n"
"-2\n"
)
heroes = ["Superman", "Batman", "Spiderman"]
question = ChoiceQuestion("What is your favorite superhero?", heroes, "2")
question.set_max_attempts(1)
# First answer is an empty answer, we're supposed to receive the default value
assert "Spiderman" == question.ask(io)
question = ChoiceQuestion("What is your favorite superhero?", heroes)
question.set_max_attempts(1)
assert "Batman" == question.ask(io)
assert "Batman" == question.ask(io)
question = ChoiceQuestion("What is your favorite superhero?", heroes)
question.set_error_message('Input "{}" is not a superhero!')
question.set_max_attempts(2)
io.clear_error()
assert "Batman" == question.ask(io)
assert 'Input "John" is not a superhero!' in io.fetch_error()
question = ChoiceQuestion("What is your favorite superhero?", heroes, "1")
question.set_max_attempts(1)
with pytest.raises(Exception) as e:
question.ask(io)
assert 'Value "John" is invalid' == str(e.value)
question = ChoiceQuestion("What is your favorite superhero?", heroes)
question.set_max_attempts(1)
question.set_multi_select(True)
assert ["Batman"] == question.ask(io)
assert ["Superman", "Spiderman"] == question.ask(io)
assert ["Superman", "Spiderman"] == question.ask(io)
question = ChoiceQuestion("What is your favorite superhero?", heroes, "0,1")
question.set_max_attempts(1)
question.set_multi_select(True)
assert ["Superman", "Batman"] == question.ask(io)
question = ChoiceQuestion("What is your favorite superhero?", heroes, " 0 , 1 ")
question.set_max_attempts(1)
question.set_multi_select(True)
assert ["Superman", "Batman"] == question.ask(io)
question = ChoiceQuestion("What is your favourite superhero?", heroes)
question.set_max_attempts(1)
with pytest.raises(ValueError) as e:
question.ask(io)
assert 'Value "4" is invalid' == str(e.value)
assert "Superman" == question.ask(io)
with pytest.raises(ValueError) as e:
question.ask(io)
assert 'Value "-2" is invalid' == str(e.value)
|