def test_str_with_complete_board_returns_correct_string(): """Verifies that the string representation of a solved grid is correct.""" grid = Grid(((3, -1, -1), (-1, 2, -1), (0, -1, -1))) # Set path from "3" draw_path(grid, ((0, 0), (0, 1), (0, 2), (1, 2), (2, 2), (2, 1), (2, 0))) # Set path from "2" draw_path(grid, ((1, 1), (1, 0), (2, 0))) assert str(grid) == GRID_STRING_2
def test_is_head_complete_puzzle_returns_no_heads(): """Verifies that a completely solved puzzle has no remaining heads.""" grid = Grid(((3, -1, -1), (-1, 2, -1), (0, -1, -1))) # Set path from "3" draw_path(grid, ((0, 0), (0, 1), (0, 2), (1, 2), (2, 2), (2, 1), (2, 0))) # Set path from "2" draw_path(grid, ((1, 1), (1, 0), (2, 0))) # Test that there are no heads anymore assert not [point for row in grid for point in row if point.is_head()]
def test_get_next_with_no_child_and_some_open_neighbors_returns_first_available( ): """ Verifies that `get_next` returns the first available neighbor if some are open. """ grid = Grid(((3, -1, -1), (-1, 2, -1), (0, -1, -1))) # Set partial path from "3" draw_path(grid, ((0, 0), (0, 1), (0, 2), (1, 2))) assert get_next(grid[1][1]).location == (2, 1)
def test_get_head_with_complete_puzzle_returns_none(): """Verifies that a completely solved puzzle reports having no heads.""" grid = Grid(((3, -1, -1), (-1, 2, -1), (0, -1, -1))) # Set path from "3" draw_path(grid, ((0, 0), (0, 1), (0, 2), (1, 2), (2, 2), (2, 1), (2, 0))) # Set path from "2" draw_path(grid, ((1, 1), (1, 0), (2, 0))) assert not has_head(grid) assert get_head(grid) is None
def test_reveal_with_complete_puzzle_prints_but_does_not_backtrack(capsys): """Verifies that `reveal` on a complete puzzle prints as expected.""" grid = Grid(((3, -1, -1), (-1, 2, -1), (0, -1, -1))) # Set path from "3" draw_path(grid, ((0, 0), (0, 1), (0, 2), (1, 2), (2, 2), (2, 1), (2, 0))) # Set path from "2" draw_path(grid, ((1, 1), (1, 0), (2, 0))) # Test reveal with no delay reveal(grid, delay=0) assert capsys.readouterr().out == f"\x1b[J{SOLVED_GRID_STRING}\n"
def mock_strategy(grid): """ Mock algorithm to be applied only to the following grid: 2 · * * """ draw_path(grid, ((0, 0), (0, 1), (1, 1))) return True
def test_is_option_with_unavailable_neighbor_returns_false(): """ Verifies that `is_option` correctly identifies when a point is not an option. """ grid = Grid(((3, -1, -1), (-1, 2, -1), (0, -1, -1))) # Set partial path from "3" draw_path(grid, ((0, 0), (0, 1), (0, 2), (1, 2))) # Test neighbors on partial path from "3" current = grid[1][1] assert not is_option(current, grid[0][1]) assert not is_option(current, grid[1][2])
def test_get_head_with_incomplete_puzzle_returns_head(): """Verifies that `get_head` on a partially solved puzzle returns a head.""" grid = Grid(((3, -1, -1), (-1, 2, -1), (0, -1, -1))) # Set partial path from "3" draw_path(grid, ((0, 0), (0, 1), (0, 2))) # Test get_head returns head on path from "3" assert has_head(grid) assert get_head(grid).location == (0, 2) grid[0][2].child = grid[1][2] # Test get_head returns head on path from "2" assert has_head(grid) assert get_head(grid).location == (1, 1)
def test_is_head_with_sink_returns_false(): """Verifies that a sink never reports being a head.""" grid = Grid(((3, -1, -1), (-1, 2, -1), (0, -1, -1))) # Test sink with no parents assert not grid[2][0].is_head() # Set path from "3" draw_path(grid, ((0, 0), (0, 1), (0, 2), (1, 2), (2, 2), (2, 1), (2, 0))) # Test sink with one parent assert not grid[2][0].is_head() # Set path from "2" draw_path(grid, ((1, 1), (1, 0), (2, 0))) # Test sink with all parents assert not grid[2][0].is_head()
def test_remaining_segments_with_new_segment_returns_change(): """ Verifies that moving to a new segment decrements the number of remaining segments. """ grid = Grid(((3, -1, -1), (-1, 2, -1), (0, -1, -1))) # Test first point on second segment draw_path(grid, ((0, 0), (0, 1), (0, 2), (1, 2))) assert grid[1][2].remaining_segments == 2 # Test second point on second segment has no change grid[1][2].child = grid[2][2] assert grid[2][2].remaining_segments == 2 # Test first point on third segment grid[2][2].child = grid[2][1] assert grid[2][1].remaining_segments == 1
def test_has_relationship_with_relationship_returns_true(): """Verifies that `has_relationship` returns true for related points.""" grid = Grid(((3, -1, -1), (-1, 2, -1), (0, -1, -1))) # Set path from "3" draw_path(grid, ((0, 0), (0, 1), (0, 2), (1, 2), (2, 2), (2, 1), (2, 0))) # Set path from "2" draw_path(grid, ((1, 1), (1, 0), (2, 0))) # Test north center pipe point assert grid[0][0].has_relationship(Direction.EAST) assert grid[0][1].has_relationship(Direction.WEST) assert grid[0][1].has_relationship(Direction.EAST) assert grid[0][2].has_relationship(Direction.WEST) # Test center center source point assert grid[1][1].has_relationship(Direction.WEST) assert grid[1][0].has_relationship(Direction.EAST) # Test south west sink point assert grid[1][0].has_relationship(Direction.SOUTH) assert grid[2][0].has_relationship(Direction.NORTH) assert grid[2][0].has_relationship(Direction.EAST) assert grid[2][1].has_relationship(Direction.WEST)
def test_get_next_with_no_child_and_no_open_neighbors_returns_none(): """Verifies that `get_next` returns None if no neighbors are open.""" grid = Grid(((4, -1, 1), (0, -1, -1), (-1, -1, 0))) # Set (incorrect) path from "4" draw_path(grid, ((0, 0), (0, 1), (1, 1), (1, 2), (2, 2))) assert get_next(grid[1][2]) is None