Python: Add test for 9 / 10 / 11 / 12

This commit is contained in:
Martin Thoma
2022-03-22 21:22:35 +01:00
parent 66b5297dae
commit f192a3db36
9 changed files with 130 additions and 52 deletions

View File

@@ -8,7 +8,9 @@ from acey_ducey import play_game
@mock.patch("random.shuffle") @mock.patch("random.shuffle")
def test_play_game_lose(mock_random_shuffle, monkeypatch: MonkeyPatch, capsys: CaptureFixture) -> None: def test_play_game_lose(
mock_random_shuffle, monkeypatch: MonkeyPatch, capsys: CaptureFixture
) -> None:
monkeypatch.setattr("sys.stdin", io.StringIO("100\n100")) monkeypatch.setattr("sys.stdin", io.StringIO("100\n100"))
T = TypeVar("T") T = TypeVar("T")

0
09_Battle/python/battle.py Normal file → Executable file
View File

0
09_Battle/python/battle_oo.py Normal file → Executable file
View File

View File

@@ -0,0 +1,27 @@
import io
from typing import Callable
import pytest
from _pytest.monkeypatch import MonkeyPatch
from battle import main as main_one
from battle_oo import main as main_oo
@pytest.mark.parametrize(
"main",
[main_one, main_oo],
)
def test_main(monkeypatch: MonkeyPatch, main: Callable[[], None]) -> None:
monkeypatch.setattr(
"sys.stdin",
io.StringIO(
"1,1\n1,2\n1,3\n1,4\n1,5\n1,6\n"
"2,1\n2,2\n2,3\n2,4\n2,5\n2,6\n"
"3,1\n3,2\n3,3\n3,4\n3,5\n3,6\n"
"4,1\n4,2\n4,3\n4,4\n4,5\n4,6\n"
"5,1\n5,2\n5,3\n5,4\n5,5\n5,6\n"
"6,1\n6,2\n6,3\n6,4\n6,5\n6,6\n"
),
)
main()

View File

@@ -0,0 +1,17 @@
import io
from _pytest.monkeypatch import MonkeyPatch
from _pytest.capture import CaptureFixture
from blackjack import main
def test_blackjack(monkeypatch: MonkeyPatch, capsys: CaptureFixture[str]) -> None:
nb_players = 1
instructions = "y"
bet = 100
monkeypatch.setattr(
"sys.stdin",
io.StringIO(f"{nb_players}\n{instructions}\n\n{bet}\ns\nn\n"),
)
main()

View File

@@ -43,12 +43,11 @@ def generate_enemy_positions() -> Set[int]:
return set(positions[:4]) return set(positions[:4])
def is_valid_position(pos) -> bool: def is_valid_position(pos: int) -> bool:
return pos in positions_list() return pos in positions_list()
def prompt_for_player_positions() -> Set[int]: def prompt_for_player_positions() -> Set[int]:
while True: while True:
raw_positions = input("WHAT ARE YOUR FOUR POSITIONS? ") raw_positions = input("WHAT ARE YOUR FOUR POSITIONS? ")
positions = {int(pos) for pos in raw_positions.split()} positions = {int(pos) for pos in raw_positions.split()}
@@ -75,7 +74,13 @@ def prompt_player_for_target() -> int:
return target return target
def attack(target, positions, hit_message, miss_message, progress_messages) -> bool: def attack(
target: int,
positions: Set[int],
hit_message: str,
miss_message: str,
progress_messages: str,
) -> bool:
"""Performs attack procedure returning True if we are to continue.""" """Performs attack procedure returning True if we are to continue."""
if target in positions: if target in positions:
@@ -89,9 +94,12 @@ def attack(target, positions, hit_message, miss_message, progress_messages) -> b
def init_enemy() -> Callable[[], int]: def init_enemy() -> Callable[[], int]:
"""Returns a closure analogous to prompt_player_for_target. Will """
choose from a unique sequence of positions to avoid picking the Return a closure analogous to prompt_player_for_target.
same position twice."""
Will choose from a unique sequence of positions to avoid picking the
same position twice.
"""
position_sequence = positions_list() position_sequence = positions_list()
random.shuffle(position_sequence) random.shuffle(position_sequence)

View File

@@ -0,0 +1,17 @@
import io
from _pytest.monkeypatch import MonkeyPatch
from bombardment import play
def test_bombardment(monkeypatch: MonkeyPatch) -> None:
monkeypatch.setattr(
"sys.stdin",
io.StringIO(
"\n1 2 3 4\n6\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10"
"\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20"
"\n21\n22\n23\n24\n25"
),
)
play()

View File

@@ -0,0 +1,16 @@
import io
from _pytest.monkeypatch import MonkeyPatch
from bombs_away import play_game
def test_bombs_away(monkeypatch: MonkeyPatch) -> None:
side = 1
target = 1
missions = 1
monkeypatch.setattr(
"sys.stdin",
io.StringIO(f"{side}\n{target}\n{missions}\n3\n50"),
)
play_game()

View File

@@ -40,19 +40,9 @@
"BOARD_WIDTH = 10\n", "BOARD_WIDTH = 10\n",
"BOARD_HEIGHT = 10\n", "BOARD_HEIGHT = 10\n",
"\n", "\n",
"SHIPS = [ (\"BATTLESHIP\", 5),\n", "SHIPS = [(\"BATTLESHIP\", 5), (\"CRUISER\", 3), (\"DESTROYER<A>\", 2), (\"DESTROYER<B>\", 2)]\n",
" (\"CRUISER\", 3),\n",
" (\"DESTROYER<A>\", 2),\n",
" (\"DESTROYER<B>\", 2) ]\n",
"\n", "\n",
"VALID_MOVES = [[-1, 0],\n", "VALID_MOVES = [[-1, 0], [-1, 1], [0, 1], [1, 1], [1, 0], [1, -1], [0, -1], [-1, -1]]"
" [-1, 1],\n",
" [ 0, 1],\n",
" [ 1, 1],\n",
" [ 1, 0],\n",
" [ 1,-1],\n",
" [ 0,-1],\n",
" [-1,-1]]"
] ]
}, },
{ {
@@ -149,8 +139,7 @@
" x_coord = x_coord + d_x\n", " x_coord = x_coord + d_x\n",
" y_coord = y_coord + d_y\n", " y_coord = y_coord + d_y\n",
" coords.append((x_coord, y_coord))\n", " coords.append((x_coord, y_coord))\n",
" return coords\n", " return coords"
"\n"
] ]
}, },
{ {
@@ -167,10 +156,9 @@
} }
], ],
"source": [ "source": [
"\n",
"for ship in range(len(SHIPS)):\n", "for ship in range(len(SHIPS)):\n",
" coords = generate_ship_coordinates(ship)\n", " coords = generate_ship_coordinates(ship)\n",
" print(f'{SHIPS[ship][0]:15}',f'{SHIPS[ship][1]:2}',coords)" " print(f\"{SHIPS[ship][0]:15}\", f\"{SHIPS[ship][1]:2}\", coords)"
] ]
}, },
{ {
@@ -180,8 +168,7 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"def create_blank_board():\n", "def create_blank_board():\n",
" return [ [ None for y in range(BOARD_WIDTH)] \n", " return [[None for y in range(BOARD_WIDTH)] for x in range(BOARD_HEIGHT)]"
" for x in range(BOARD_HEIGHT)]"
] ]
}, },
{ {
@@ -201,19 +188,20 @@
"def print_board(board):\n", "def print_board(board):\n",
"\n", "\n",
" # print board header (column numbers)\n", " # print board header (column numbers)\n",
" print(' ',end='')\n", " print(\" \", end=\"\")\n",
" for z in range(BOARD_WIDTH):\n", " for z in range(BOARD_WIDTH):\n",
" print(f'{z+1:3}',end='')\n", " print(f\"{z+1:3}\", end=\"\")\n",
" print('')\n", " print(\"\")\n",
"\n", "\n",
" for x in range(len(board)):\n", " for x in range(len(board)):\n",
" print(f'{x+1:2}',end='')\n", " print(f\"{x+1:2}\", end=\"\")\n",
" for y in range(len(board[x])):\n", " for y in range(len(board[x])):\n",
" if(board[x][y] is None):\n", " if board[x][y] is None:\n",
" print(f\"{' ':3}\",end='')\n", " print(f\"{' ':3}\", end=\"\")\n",
" else:\n", " else:\n",
" print(f\"{board[x][y]:3}\",end='')\n", " print(f\"{board[x][y]:3}\", end=\"\")\n",
" print('')\n", " print(\"\")\n",
"\n",
"\n", "\n",
"computer_board = create_blank_board()\n", "computer_board = create_blank_board()\n",
"print_board(computer_board)" "print_board(computer_board)"
@@ -247,7 +235,7 @@
"# test place_ship\n", "# test place_ship\n",
"board = create_blank_board()\n", "board = create_blank_board()\n",
"coords = generate_ship_coordinates(0)\n", "coords = generate_ship_coordinates(0)\n",
"print(f'{SHIPS[ship][0]:15}',f'{SHIPS[ship][1]:2}',coords)\n", "print(f\"{SHIPS[ship][0]:15}\", f\"{SHIPS[ship][1]:2}\", coords)\n",
"place_ship(board, coords, 0)\n", "place_ship(board, coords, 0)\n",
"print_board(board)" "print_board(board)"
] ]
@@ -281,6 +269,7 @@
"# For example: 2 destroyers, length 2, one at\n", "# For example: 2 destroyers, length 2, one at\n",
"# [(1,1),(2,2)] and other at [(2,1),(1,2)]\n", "# [(1,1),(2,2)] and other at [(2,1),(1,2)]\n",
"\n", "\n",
"\n",
"def generate_board():\n", "def generate_board():\n",
" board = create_blank_board()\n", " board = create_blank_board()\n",
"\n", "\n",
@@ -299,6 +288,7 @@
" place_ship(board, coords, ship)\n", " place_ship(board, coords, ship)\n",
" return board\n", " return board\n",
"\n", "\n",
"\n",
"print_board(generate_board())" "print_board(generate_board())"
] ]
}, },
@@ -322,6 +312,7 @@
" shots.append(random_x_y())\n", " shots.append(random_x_y())\n",
" return shots\n", " return shots\n",
"\n", "\n",
"\n",
"shots = generate_shots(5)\n", "shots = generate_shots(5)\n",
"print(shots)" "print(shots)"
] ]