Skip to content

Commit

Permalink
test: improve test_world
Browse files Browse the repository at this point in the history
  • Loading branch information
u8slvn committed Aug 31, 2024
1 parent 3d1198d commit 596c1eb
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<p align="center">
<a href="#readme" align="center">
<img alt="Until Zero - タイマー | logo" src="https://raw.githubusercontent.com/u8slvn/doggo/main/assets/splash.png">
<img alt="doggo logo" src="https://raw.githubusercontent.com/u8slvn/doggo/main/assets/splash.png">
</a>
</p>
<p align="center">
Expand Down
6 changes: 3 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import pytest


@pytest.fixture(scope="session", autouse=True)
def pygame():
"""Start and stop pygame instance for all tests."""
@pytest.fixture(scope="session")
def pygame_test():
"""Start and stop pygame instance for a test."""

pg.init()
pg.display.set_mode((1, 1))
Expand Down
8 changes: 4 additions & 4 deletions tests/dog/test_body.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def test_fur_random_returns_a_fur_color():
assert isinstance(fur, Fur)


def test_body_get_image_returns_surface_cyclically():
def test_body_get_image_returns_surface_cyclically(pygame_test):
body = Body(
fur=Fur.random(),
sprite_size=config.SPRITE_SIZE,
Expand All @@ -45,7 +45,7 @@ def test_body_get_image_returns_surface_cyclically():
assert next_cycle_image is frames[0]


def test_body_return_cycle_images_while_brain_state_is_the_same():
def test_body_return_cycle_images_while_brain_state_is_the_same(pygame_test):
body = Body(
fur=Fur.random(),
sprite_size=config.SPRITE_SIZE,
Expand Down Expand Up @@ -74,7 +74,7 @@ def test_body_return_cycle_images_while_brain_state_is_the_same():
assert fourth_image is body.images[StateID.IDLE][Direction.RIGHT][1]


def test_sprite_sheet_get_sprite_returns_a_sprite():
def test_sprite_sheet_get_sprite_returns_a_sprite(pygame_test):
sprite_sheet_path = ASSETS_PATH.joinpath("dogs/00.png")
sprite_sheet = SpriteSheet(path=sprite_sheet_path, columns=8, rows=9)

Expand All @@ -85,7 +85,7 @@ def test_sprite_sheet_get_sprite_returns_a_sprite():
assert sprite.get_height() == 48


def test_sprite_sheet_get_sprite_fails_if_loc_is_outside_of_the_sheet():
def test_sprite_sheet_get_sprite_fails_if_loc_is_outside_of_the_sheet(pygame_test):
sprite_sheet_path = ASSETS_PATH.joinpath("dogs/00.png")
sprite_sheet = SpriteSheet(path=sprite_sheet_path, columns=8, rows=9)

Expand Down
16 changes: 9 additions & 7 deletions tests/dog/test_dog.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from doggo.prepare import build_dog


def test_dog_can_move_right():
def test_dog_can_move_right(pygame_test):
dog = build_dog()
dog.brain.change_state(StateID.WALK) # We need a state that can move.
dog.brain.current_state.direction = Direction.RIGHT
Expand All @@ -18,7 +18,7 @@ def test_dog_can_move_right():
assert dog.rect.x > start_x_pos


def test_dog_can_move_left():
def test_dog_can_move_left(pygame_test):
dog = build_dog()
dog.brain.change_state(StateID.WALK) # We need a state that can move.
dog.brain.current_state.direction = Direction.LEFT
Expand All @@ -37,7 +37,7 @@ def test_dog_can_move_left():
],
)
def test_dog_clone_appeared_when_hitting_a_world_boundary(
direction, start_pos, x_signe
pygame_test, direction, start_pos, x_signe
):
dog = build_dog()
dog.brain.change_state(StateID.WALK) # We need a state that can move.
Expand All @@ -61,7 +61,9 @@ def test_dog_clone_appeared_when_hitting_a_world_boundary(
(Direction.LEFT, lambda dog: 0),
],
)
def test_dog_clone_disappeared_when_not_hitting_a_world_boundary(direction, start_pos):
def test_dog_clone_disappeared_when_not_hitting_a_world_boundary(
pygame_test, direction, start_pos
):
dog = build_dog()
dog.brain.change_state(StateID.WALK)
dog.brain.current_state.direction = direction
Expand All @@ -83,15 +85,15 @@ def test_dog_clone_disappeared_when_not_hitting_a_world_boundary(direction, star
assert dog.clone.visible is False


def test_dog_draw_on_screen(pg_screen_mock):
def test_dog_draw_on_screen(pygame_test, pg_screen_mock):
dog = build_dog()

dog.draw(screen=pg_screen_mock)

pg_screen_mock.blit.assert_called_once_with(dog.image, dest=dog.rect)


def test_dog_draw_on_screen_with_its_clone(mocker, pg_screen_mock):
def test_dog_draw_on_screen_with_its_clone(mocker, pygame_test, pg_screen_mock):
dog = build_dog()
dog.clone.visible = True

Expand All @@ -104,7 +106,7 @@ def test_dog_draw_on_screen_with_its_clone(mocker, pg_screen_mock):
]


def test_dog_brain_info_are_accessible_from_dog():
def test_dog_brain_info_are_accessible_from_dog(pygame_test):
dog = build_dog()

assert isinstance(dog.current_state, StateID)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_prepare.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from doggo.prepare import build_landscape


def test_build_dog():
def test_build_dog(pygame_test):
dog = build_dog()

assert isinstance(dog, Dog)
Expand All @@ -19,7 +19,7 @@ def test_build_dog():
assert isinstance(dog.body, Body)


def test_build_landscape():
def test_build_landscape(pygame_test):
landscape = build_landscape()

assert isinstance(landscape, Landscape)
Expand Down
3 changes: 2 additions & 1 deletion tests/test_world.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@


@pytest.fixture
def create_world():
def create_world(mocker):
mocker.patch("doggo.world.pg.init")
world = None

def _(render_size=None):
Expand Down

0 comments on commit 596c1eb

Please sign in to comment.