def test_grid_renders(): # Given the means to start the program # When the user initiates the start # Then a 4x3 grid is rendered on the screen grid = Grid(size=[4, 3]) display = Display(grid) row = [".", ".", ".", "."] expected_df = pd.DataFrame([row, row, row]) assert_frame_equal(display.render(), expected_df)
def test_human_does_not_move_if_wall(mock_random): # Given a program in progress # When a human moves into a wall # Then the human will not move on that go mock_random.randint.return_value = 0 human = Human() grid = Grid(size=[4, 3]) grid.add_player(human, [0, 0]) grid.players_move() display = Display(grid) rendered_display = display.render() assert rendered_display[0].loc[0] == "H"
def test_display_human_and_zombie_in_grid(): """ Set up display with 1 human and 1 zombie Assert the expected display """ top_row = ["H", ".", ".", "."] second_row = [".", "Z", ".", "."] row = [".", ".", ".", "."] expected_df = pd.DataFrame([top_row, second_row, row, row]) grid = set_up_grid() display = Display(grid) assert_frame_equal(display.render(), expected_df)
def test_grid_renders_with_human(): # Given the means to start the program # When the user initiates the start # Then a human is occupying a single square human = Human() grid = Grid(size=[4, 3]) grid.add_player(human, [0, 0]) display = Display(grid) top_row = ["H", ".", ".", "."] row = [".", ".", ".", "."] expected_df = pd.DataFrame([top_row, row, row]) assert_frame_equal(display.render(), expected_df)
def test_display_game_display(mock_input, mock_print): """ Create display object and assert during game display is as expected """ mock_input.return_value = None grid = set_up_grid() display = Display(grid) df = display.render() number_of_humans = 1 number_of_zombies = 1 display.game_display(number_of_humans, number_of_zombies) mock_print.assert_called_with(f"Human count: {number_of_humans} \nZombie count: {number_of_zombies} \n {df}")
def test_human_moves_one_space(): #TODO: to ask andy. There were times when this test passed and failed randomly # Given a program in progress # When it is time for a new go or turn # Then the human will move 1 pace in a random direction (N, NE, E, SE, S, SW, W, NW) human = Human() grid = Grid(size=[4, 3]) grid.add_player(human, [2, 1]) grid.players_move() display = Display(grid) rendered_display = display.render() possible_human_coordinates = [[2, 0], [3, 0], [3, 1], [3, 2], [2, 2], [1, 2], [1, 1], [1, 0]] result = [] for coordinates in possible_human_coordinates: result.append(rendered_display[coordinates[0]].loc[coordinates[1]]) assert "H" in result
def test_end_game_display(mock_input, mock_print): """ Create display object and assert end game display is as expected """ mock_input.return_value = None grid = set_up_grid() display = Display(grid) df = display.render() number_of_humans = 1 number_of_zombies = 1 number_of_turns = 3 display.end_game_display(number_of_humans, number_of_zombies, number_of_turns) mock_print.assert_called_with( f"Human count: {number_of_humans}\nZombie count: {number_of_zombies}\n{df}\nNumber of turns: {number_of_turns}" f"\nHumans extinct!")
def test_grid_renders_with_zombie_and_human(): # Given the means to start the program # When the user initiates the start # Then the human and zombie are on different squares zombie = Zombie() human = Human() grid = Grid(size=[4, 3]) grid.add_player(zombie, [0, 0]) grid.add_player(human, [0, 2]) display = Display(grid) rendered_display = display.render() top_row = ["Z", ".", ".", "."] bottom_row = ["H", ".", ".", "."] row = [".", ".", ".", "."] expected_df = pd.DataFrame([top_row, row, bottom_row]) assert_frame_equal(rendered_display, expected_df)
def test_zombie_moves_towards_human(mock_move): # Given a program in progress # When it is time for a new go or turn # Then the zombie will move 1 pace towards the human """ Set up the game with Human on 0, 0 and zombie on 0, 2 Ensure that human will not move on their go Call players-move() Assert that zombie is at 0, 1 """ mock_move.return_value = [0, 0] human = Human() zombie = Zombie() grid = Grid(size=[4, 3]) grid.add_player(human, [0, 0]) grid.add_player(zombie, [0, 2]) grid.players_move() display = Display(grid) rendered_display = display.render() assert rendered_display[0].loc[1] == "Z"
def play(self, display_class=Display): # Dependency injection to ease testing """ While there are humans Display the grid Move all the players """ while self.number_of_humans > 0: Display(self.grid).game_display(self.number_of_humans, self.number_of_zombies) self.grid.players_move() self.grid.convert_if_needed() self._update_number_of_players() self.number_of_turns += 1 sleep(0.4) display_class(self.grid).end_game_display(self.number_of_humans, self.number_of_zombies, self.number_of_turns)
def test_display_inital_display(mock_input, mock_print): """ Create display object and assert intial display is as expected """ mock_input.return_value = None width = 3 length = 3 grid = Mock(width=width, length=length) display = Display(grid) display.initial_display() empty_row = ["." for i in range(width + 1)] df = pd.DataFrame(data=[empty_row for i in range(length + 1)]) display.initial_display() mock_print.assert_called_with(f"Please adjust screen to the size of the below grid:\n{df}\nplease hit enter")
def initial_display(self): Display(self.grid).initial_display()