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 3, 2023
1 parent 6189ea5 commit 9f98f8b
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 83 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ no-cov = "cov --no-cov"
python = ["310", "311"]

[tool.coverage.run]
branch = true
branch = false
parallel = true
omit = [
"trainfinity2/__about__.py",
Expand Down
14 changes: 2 additions & 12 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
import tests.run_debug
import trainfinity2.camera
import trainfinity2.graphics.drawer
import trainfinity2.graphics.rail_shapes
Expand Down Expand Up @@ -31,16 +32,10 @@ class SpriteList(list):
def draw(self):
pass

def move(self, dx, dy):
pass

class ShapeElementList(list):
def draw(self):
pass

def move(self, dx, dy):
pass

class FadeParticle:
pass

Expand Down Expand Up @@ -79,18 +74,12 @@ def create_ellipse_filled(self, *args, **kwargs):
def create_line(self, *args, **kwargs):
pass

def draw_circle_filled(self, *args, **kwargs):
pass

def draw_rectangle_filled(self, *args, **kwargs):
pass

def draw_rectangle_outline(self, *args, **kwargs):
pass

def draw_circle_outline(self, *args, **kwargs):
pass


@pytest.fixture
def game(monkeypatch: pytest.MonkeyPatch) -> Game:
Expand All @@ -99,6 +88,7 @@ def game(monkeypatch: pytest.MonkeyPatch) -> Game:
monkeypatch.setattr(trainfinity2.graphics.drawer, "arcade", MockArcade())
monkeypatch.setattr(trainfinity2.graphics.rail_shapes, "arcade", MockArcade())
monkeypatch.setattr(trainfinity2.graphics.train_drawer, "arcade", MockArcade())
monkeypatch.setattr(tests.run_debug, "arcade", MockArcade())
# Add a single water tile for code coverage
game = Game()
game.setup(terrain=Terrain(water=[Vec2(210, 210)]))
Expand Down
61 changes: 33 additions & 28 deletions tests/run_debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,39 @@
import arcade
from trainfinity2.__main__ import Game, Window
from pyglet.math import Vec2
from trainfinity2.model import Rail
from trainfinity2.model import Rail, Station
from trainfinity2.terrain import Terrain

game = Game()
window = Window(game)
game.setup(Terrain(water=[Vec2(0, 0)]))
game.grid._create_mine(Vec2(30, 30))
game.grid._create_factory(Vec2(180, 30))
game.grid.create_rail(
[
Rail(0, 60, 30, 60),
Rail(30, 60, 60, 60),
Rail(60, 60, 90, 60),
Rail(90, 60, 120, 60),
Rail(120, 60, 150, 60),
Rail(150, 60, 180, 60),
Rail(60, 60, 90, 90),
Rail(90, 90, 120, 90),
Rail(120, 90, 150, 60),
]
)
station1 = game.grid._create_station(Vec2(30, 60), True)
station2 = game.grid._create_station(Vec2(180, 60), True)
# game._create_signal(90, 60)
# game._create_signal(90, 90)
# game._create_signal(120, 60)
# game._create_signal(120, 90)
# game._create_train(station1, station2)
# game._create_train(station2, station1)

arcade.run()
def init(game: Game):
game.setup(Terrain(water=[Vec2(0, 0)]))
game.grid._create_mine(Vec2(30, 30))
game.grid._create_factory(Vec2(180, 30))
game.grid.create_rail(
{
Rail(0, 60, 30, 60),
Rail(30, 60, 60, 60),
Rail(60, 60, 90, 60),
Rail(90, 60, 120, 60),
Rail(120, 60, 150, 60),
Rail(150, 60, 180, 60),
Rail(60, 60, 90, 90),
Rail(90, 90, 120, 90),
Rail(120, 90, 150, 60),
}
)
game.grid._create_station(Station((Vec2(30, 60),)))
game.grid._create_station(Station((Vec2(180, 60),)))
# game._create_signal(90, 60)
# game._create_signal(90, 90)
# game._create_signal(120, 60)
# game._create_signal(120, 90)
# game._create_train(station1, station2)
# game._create_train(station2, station1)


if __name__ == "__main__":
game = Game()
window = Window(game)
init(game)
arcade.run()
26 changes: 23 additions & 3 deletions tests/test_game.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
import pytest
from pyglet.math import Vec2
from pytest import approx
from trainfinity2.constants import SECONDS_BETWEEN_IRON_CREATION
from trainfinity2.constants import (
GRID_BOX_SIZE,
GRID_WIDTH,
SECONDS_BETWEEN_IRON_CREATION,
)
from trainfinity2.game import Mode, Game
from trainfinity2.model import Rail, SignalColor, Station, Water
from tests.util import create_objects
Expand Down Expand Up @@ -731,6 +735,13 @@ def test_clicking_grid_in_signal_mode_creates_signal(self, game: Game):
game.on_left_click(61, 1)
assert len(game.grid.signals) == 2

def test_clicking_grid_in_signal_mode_when_no_rail_does_nothing(self, game: Game):
"""For code coverage."""
game.gui.disable()
game.gui.mode = Mode.SIGNAL
game.on_left_click(61, 1)
assert len(game.grid.signals) == 0

def test_green_signal_colors_are_shown_for_adjacent_positions(self, game: Game):
create_objects(
game.grid,
Expand Down Expand Up @@ -935,7 +946,7 @@ def test_a_train_without_wagons_reserves_both_blocks_when_leaving(self, game: Ga
"""
. M . F .
.-Sh.-S-.
.-S-.hS-.
""",
)
train = game._create_train(
Expand Down Expand Up @@ -987,7 +998,7 @@ def test_a_train_with_one_wagon_can_reserve_two_signal_blocks(self, game: Game):
"""
. M . F .
.-Sh.-S-.
.-S-.hS-.
""",
)
train = game._create_train(
Expand All @@ -1007,3 +1018,12 @@ def test_a_train_with_one_wagon_can_reserve_two_signal_blocks(self, game: Game):
== game.signal_controller._signal_blocks[0].reserved_by
== id(train)
)


class TestPlayer:
def test_grid_is_enlarged_when_leveling_up(self, game: Game):
assert game.grid.left == 0
assert game.grid.right == GRID_WIDTH
game.player.score = 10
assert game.grid.left == -GRID_BOX_SIZE
assert game.grid.right == GRID_WIDTH + GRID_BOX_SIZE
16 changes: 16 additions & 0 deletions tests/test_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@ def test_positions_between(self):


class TestBuildStation:
def test_cannot_build_station_away_from_mine_or_factory(self, grid: Grid):
create_objects(
grid,
"""
M . .
. . .
. . .
""",
)
assert grid._illegal_station_positions(Station((Vec2(30, 0), Vec2(60, 0)))) == {
Vec2(30, 0),
Vec2(60, 0),
}

def test_cannot_build_station_if_rail_in_wrong_direction(self, grid: Grid):
create_objects(
grid,
Expand Down
6 changes: 6 additions & 0 deletions tests/test_run_debug.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from tests.run_debug import init


def test_main(game):
"""For code coverage"""
init(game)
15 changes: 1 addition & 14 deletions trainfinity2/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,7 @@ def _is_click(self, mouse_down_x, mouse_down_y, mouse_up_x, mouse_up_y):
)

def on_mouse_release(self, x: int, y: int, button: int, modifiers: int):
if button == arcade.MOUSE_BUTTON_RIGHT:
self.is_mouse2_pressed = False
if self._is_click(self.mouse2_pressed_x, self.mouse2_pressed_y, x, y):
self.on_right_click(x, y)
elif button == arcade.MOUSE_BUTTON_LEFT:
if button == arcade.MOUSE_BUTTON_LEFT:
self.is_mouse1_pressed = False
if self._is_click(
self.mouse1_pressed_x,
Expand Down Expand Up @@ -207,12 +203,6 @@ def create_signals_at_click_position(self, x, y) -> list[Signal]:
signal.add_observer(self.drawer, ChangeEvent)
return signals

def create_signals_at_grid_position(self, x, y) -> list[Signal]:
signals = self.grid.create_signals_at_grid_position(x, y)
for signal in signals:
signal.add_observer(self.drawer, ChangeEvent)
return signals

def _create_train(
self, station1: Station, station2: Station, *, wagon_count: int = 3
):
Expand All @@ -236,9 +226,6 @@ def on_notify(self, object: Any, event: Event):
case Train(), DestroyEvent():
self.trains.remove(object)

def on_right_click(self, x, y):
pass

def on_mouse_motion(self, x: int, y: int, dx: int, dy: int):
if self.is_mouse2_pressed:
self._on_mouse_move_when_mouse_2_pressed(x, y)
Expand Down
25 changes: 0 additions & 25 deletions trainfinity2/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
Mine,
Rail,
Signal,
SignalColor,
Station,
Water,
)
Expand Down Expand Up @@ -179,22 +178,6 @@ def possible_next_rails_ignore_red_lights(
):
return self.rails_at_position(position) - {previous_rail}

def possible_next_rails(self, position: Vec2, previous_rail: Rail | None):
"""Given a position and where the train came from, return a list of possible rails
it can continue on.
Current rules:
1. The train cannot reverse, i.e. the output cannot contain `previous_rail`.
(if previous_rail = None, the train is at a standstill, e.g. at a station)
2. The train cannot go into a red light
Possible future rules:
3. The train cannot turn more than X degrees"""
next_rails = set(self.rails_at_position(position)) - {previous_rail}
next_rails_with_signals = next_rails.intersection(self.signals)
for rail in next_rails_with_signals:
if self.signals[(position, rail)].signal_color == SignalColor.RED:
next_rails.remove(rail)
return next_rails

def _rail_is_inside_grid(self, rail: Rail):
return all(
self._is_inside(x, y) for x, y in ((rail.x1, rail.y1), (rail.x2, rail.y2))
Expand Down Expand Up @@ -392,14 +375,6 @@ def enlarge_grid(self):
self._create_in_random_unoccupied_location(Factory)
self._create_in_random_unoccupied_location(Mine)

def _two_rails_at_position(self, position: Vec2) -> tuple[Rail, Rail] | None:
rails = self.rails_at_position(position)
match rails:
case (rail1, rail2):
return (rail1, rail2)
case _:
return None

def _closest_rail(self, x, y) -> Rail | None:
"""Return None if
1. manhattan distance larger than a grid box size
Expand Down

0 comments on commit 9f98f8b

Please sign in to comment.