Esempio n. 1
0
def test_bad_game_definitions():
    with pytest.raises(InvalidNumber, match='Not a number'):
        GuessGame('not a number')

    with pytest.raises(InvalidNumber, match='Negative number'):
        GuessGame(-10)

    with pytest.raises(InvalidNumber, match='Number too high'):
        GuessGame(16)

    # Check that 0 is OK for a number
    assert GuessGame(0)
    # Check that 15 is OK for a number
    assert GuessGame(15)
Esempio n. 2
0
def test_game_initialization():
    g = GuessGame(10)
    assert g.secret_number == 10
    assert g.max_guesses == 5
    assert g.attempt == 0

    g = GuessGame(15, 20)
    assert g.secret_number == 15
    assert g.max_guesses == 20
    assert g.attempt == 0

    g = GuessGame(0, 20)
    assert g.secret_number == 0
    assert g.max_guesses == 20
    assert g.attempt == 0
Esempio n. 3
0
def test_guess_correct(patched_input, capsys):
    g = GuessGame(5)
    g()
    actual = capsys.readouterr()[0].strip()
    expected = "\n".join(["Guess a number: ", "You guessed it!"])
    assert actual == expected
    assert g.attempt == 1
Esempio n. 4
0
def test_correct_guess(mock_input, capfd):
    g = GuessGame(5, max_guesses=1)
    mock_input.return_value = 5
    g()
    assert g.attempt == 1
    captured = capfd.readouterr()
    assert captured.out == "Guess a number: \nYou guessed it!\n"
Esempio n. 5
0
def test_invalid_input(mock_input, capfd):
    g = GuessGame(5, max_guesses=10)
    mock_input.side_effect = ['invalid', 5]
    g()
    captured = capfd.readouterr()
    assert captured.out == ("Guess a number: \n"
                            "Enter a number, try again\n"
                            "Guess a number: \n"
                            "You guessed it!\n")
Esempio n. 6
0
def test_prompt(mock, capfd):
    game = GuessGame(12)
    game()
    output = capfd.readouterr()[0]
    assert "Guess a number: " in output
    assert "Enter a number, try again" in output
    assert "Too low" in output
    assert "Too high" in output
    assert "You guessed it!" in output
    assert game.attempt == 3
Esempio n. 7
0
def test_basic_good_game(capsys):
    game = GuessGame(5)
    with patch.object(builtins, 'input', side_effect=['1', '9', 'a', '5']):
        game()
    lines = [line for line in capsys.readouterr().out.split('\n') if line]
    assert lines == [
        'Guess a number: ', 'Too low', 'Guess a number: ', 'Too high',
        'Guess a number: ', 'Enter a number, try again', 'Guess a number: ',
        'You guessed it!'
    ]
Esempio n. 8
0
def test_basic_bad_game(capsys):
    game = GuessGame(5)
    with patch.object(builtins, 'input', side_effect=['1', '2', '6', '7',
                                                      '9']):
        game()
    lines = [line for line in capsys.readouterr().out.split('\n') if line]
    assert lines == [
        'Guess a number: ', 'Too low', 'Guess a number: ', 'Too low',
        'Guess a number: ', 'Too high', 'Guess a number: ', 'Too high',
        'Guess a number: ', 'Too high', 'Sorry, the number was 5'
    ]
Esempio n. 9
0
def test_guess_correct(input_mock, capsys):
    the_game = GuessGame(5, max_guesses=3)
    the_game()
    actual = capsys.readouterr()[0].strip()
    expected = '\n'.join([
        'Guess a number: ',
        'Too high',
        'Guess a number: ',
        'Too low',
        'Guess a number: ',
        'You guessed it!'
    ])
    assert actual == expected
    assert the_game.attempt == 3
Esempio n. 10
0
def test_guess_out_of_turns(input_mock, capsys):
    the_game = GuessGame(5, max_guesses=3)
    the_game()
    actual = capsys.readouterr()[0].strip()
    expected = '\n'.join([
        'Guess a number: ',
        'Too high',
        'Guess a number: ',
        'Too low',
        'Guess a number: ',
        'Too high',
        'Sorry, the number was 5'
    ])
    assert actual == expected
    assert the_game.attempt == 3
Esempio n. 11
0
def test_guess_correct_full(input_mock, capsys):
    the_game = GuessGame(5)
    the_game()
    actual = capsys.readouterr()[0].strip()
    expected = '\n'.join([
        'Guess a number: ',
        'Too high',
        'Guess a number: ',
        'Too high',
        'Guess a number: ',
        'Too high',
        'Guess a number: ',
        'Too low',
        'Guess a number: ',
        'Too high',
        'Sorry, the number was 5'
    ])
    assert actual == expected
Esempio n. 12
0
def test_high_low_too_many_guesses(patched_input, capsys):
    g = GuessGame(5)
    g()
    actual = capsys.readouterr()[0].strip()
    expected = "\n".join([
        "Guess a number: ",
        "Too high",
        "Guess a number: ",
        "Too low",
        "Guess a number: ",
        "Enter a number, try again",
        "Guess a number: ",
        "Too high",
        "Guess a number: ",
        "Too low",
        "Guess a number: ",
        "Too high",
        "Sorry, the number was 5",
    ])
    assert actual == expected
Esempio n. 13
0
def game():
    game = GuessGame(10)
    return game
Esempio n. 14
0
def test_game():
    g = GuessGame(5)
    assert g.secret_number == 5
    assert g.max_guesses == 5
    assert g.attempt == 0
    assert MAX_NUMBER == 15
Esempio n. 15
0
def test_validate_guess(guess, responce):
    g = GuessGame(5)
    assert g._validate(guess) == responce
Esempio n. 16
0
def test_non_integer_secret_number(secret_number, exception_value):
    with pytest.raises(InvalidNumber, match=exception_value):
        GuessGame(secret_number)
Esempio n. 17
0
def test_incorrectly_invoked(error_number_to_guess, error):
    with pytest.raises(InvalidNumber) as e:
        GuessGame(error_number_to_guess)
    assert str(e.value) == error
Esempio n. 18
0
def test_default_value():
    game = GuessGame(1)
    assert game.secret_number == 1
    assert game.max_guesses == 5
Esempio n. 19
0
def test_too_many_attemts(mock, capfd):
    game = GuessGame(12)
    game()
    output = capfd.readouterr()[0]
    assert "Sorry, the number was 12" in output
    assert game.attempt == game.max_guesses
Esempio n. 20
0
def test_raises_too_high():
    with pytest.raises(InvalidNumber) as excinfo:
        GuessGame(16)
    assert 'Number too high' in str(excinfo.value)
Esempio n. 21
0
def test_raises_negative():
    with pytest.raises(InvalidNumber) as excinfo:
        GuessGame(-2)
    assert 'Negative number' in str(excinfo.value)
Esempio n. 22
0
def test_raises_not_int():
    with pytest.raises(InvalidNumber) as excinfo:
        GuessGame('test')
    assert 'Not a number' in str(excinfo.value)
Esempio n. 23
0
def the_game():
    return GuessGame(SECRET_NUMBER)
Esempio n. 24
0
def test_invalid_guesses(error_guess, error):
    g = GuessGame(5)
    with pytest.raises(InvalidNumber) as e:
        g._validate(error_guess)
    assert str(e.value) == error
Esempio n. 25
0
def test_guess_too_high(mock_input, capfd):
    g = GuessGame(5, max_guesses=1)
    mock_input.return_value = 10
    g()
    captured = capfd.readouterr()
    assert captured.out == "Guess a number: \nToo high\nSorry, the number was 5\n"