Skip to content

Commit

Permalink
Improve code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Godsmith committed Sep 10, 2023
1 parent dfcf04e commit 972e491
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 51 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ omit = [
exclude_lines = [
"no cov",
"if __name__ == .__main__.:",
"def __repr__(self):",
"if TYPE_CHECKING:",
]

Expand Down
85 changes: 53 additions & 32 deletions tests/test_game.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ def check(statement: Any, count=[0]):
count[0] += 1
if count[0] < 10000:
return statement
count[0] = 0
raise AssertionError("Max iterations reached")
count[0] = 0 # pragma: no cov
raise AssertionError("Max iterations reached") # pragma: no cov


def test_draw(game: Game):
Expand Down Expand Up @@ -568,45 +568,66 @@ def test_cannot_create_train_in_reserved_signal_block(self, game: Game):
assert len(game.trains) == 1


def test_clicking_and_dragging_in_destroy_mode_destroys_station_and_rail(
game: Game,
):
create_objects(
game.grid,
"""
. M . F .
class TestDestroyMode:
def test_hovering_in_destroy_mode_highlights_rails_to_be_destroyed(
self,
game: Game,
):
create_objects(
game.grid,
"""
.-.
""",
)
game.gui.mode = Mode.DESTROY
game.gui.disable()

.-S-.-S-.
""",
)
game.gui.mode = Mode.DESTROY
game.gui.disable()
assert len(game.drawer.rail_to_be_destroyed_shape_list) == 0

game.on_mouse_motion(x=15, y=15, dx=0, dy=0)

# 12 is the number of shapes a rail consists of when writing this test
assert len(game.drawer.rail_to_be_destroyed_shape_list) == 12

def test_clicking_and_dragging_in_destroy_mode_destroys_station_and_rail(
self,
game: Game,
):
create_objects(
game.grid,
"""
. M . F .
assert len(game.grid.station_from_position) == 2
assert len(game.grid.rails) == 4
.-S-.-S-.
""",
)
game.gui.mode = Mode.DESTROY
game.gui.disable()

game.on_mouse_press(x=45, y=15, button=arcade.MOUSE_BUTTON_LEFT, modifiers=0)
game.on_mouse_motion(x=46, y=15, dx=1, dy=30)
assert len(game.grid.station_from_position) == 2
assert len(game.grid.rails) == 4

assert len(game.grid.station_from_position) == 1
assert len(game.grid.rails) == 2
game.on_mouse_press(x=45, y=15, button=arcade.MOUSE_BUTTON_LEFT, modifiers=0)
game.on_mouse_motion(x=46, y=15, dx=1, dy=30)

assert len(game.grid.station_from_position) == 1
assert len(game.grid.rails) == 2

def test_clicking_in_destroy_mode_destroys_rail(game: Game):
create_objects(
game.grid,
"""
.-.-.
""",
)
game.gui.mode = Mode.DESTROY
game.gui.disable()
def test_clicking_in_destroy_mode_destroys_rail(self, game: Game):
create_objects(
game.grid,
"""
.-.-.
""",
)
game.gui.mode = Mode.DESTROY
game.gui.disable()

assert len(game.grid.rails) == 2
assert len(game.grid.rails) == 2

game.on_left_click(15, 15)
game.on_left_click(15, 15)

assert len(game.grid.rails) == 1
assert len(game.grid.rails) == 1


def test_iron_is_regularly_added_to_mines(game: Game):
Expand Down
8 changes: 4 additions & 4 deletions trainfinity2/graphics/drawer.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def __init__(self):
self._signal_shape_list = _ShapeElementList()

self._previous_rails_to_be_marked_as_to_be_destroyed: set[Rail] = set()
self._rail_to_be_destroyed_shape_list = _ShapeElementList()
self.rail_to_be_destroyed_shape_list = _ShapeElementList()

self._rails_being_built: set[Rail] = set()
self.rails_being_built_shape_element_list = _ShapeElementList()
Expand Down Expand Up @@ -429,18 +429,18 @@ def highlight(self, positions: Iterable[Vec2]):

def show_rails_to_be_destroyed(self, rails: set[Rail]):
if rails != self._previous_rails_to_be_marked_as_to_be_destroyed:
self._rail_to_be_destroyed_shape_list.clear()
self.rail_to_be_destroyed_shape_list.clear()
for rail in rails:
for shape in get_rail_shapes(rail, RAIL_TO_BE_DESTROYED_COLOR):
self._rail_to_be_destroyed_shape_list.append(shape)
self.rail_to_be_destroyed_shape_list.append(shape)
self._previous_rails_to_be_marked_as_to_be_destroyed = rails

def draw(self):
self._grid_shape_list.draw()
self._shape_list.draw()
self._sprite_list.draw()
self._rail_shape_list.draw()
self._rail_to_be_destroyed_shape_list.draw()
self.rail_to_be_destroyed_shape_list.draw()
self._signal_shape_list.draw()

self.rails_being_built_shape_element_list.draw()
Expand Down
5 changes: 0 additions & 5 deletions trainfinity2/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,6 @@ def draw(self):
self._sprite_list.draw()
self._text_sprite_list.draw()

def pan(self, dx, dy):
self._sprite_list.move(dx, dy)
self._text_sprite_list.move(dx, dy)
self._shape_element_list.move(dx, dy)

def refresh(self):
with self.camera:
"""Recreates the boxes. Necessary for example after zooming and after
Expand Down
6 changes: 0 additions & 6 deletions trainfinity2/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,6 @@ def __eq__(self, other: object):
def __hash__(self) -> int:
return hash(tuple(sorted(((self.x1, self.y1), (self.x2, self.y2)))))

def is_horizontal(self):
return self.y1 == self.y2

def is_vertical(self):
return self.x1 == self.x2

def to_illegal(self):
return replace(self, legal=False)

Expand Down
4 changes: 0 additions & 4 deletions trainfinity2/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ def approx_equal(a: float, b: float):
return abs(a - b) < 1.0


def _is_close(pos1: Vec2, pos2: Vec2):
return abs(pos1.x - pos2.x) < 0.5 and abs(pos1.y - pos2.y) < 0.5


class PointAndAngle(NamedTuple):
point: Vec2
angle: float
Expand Down

0 comments on commit 972e491

Please sign in to comment.