Skip to content

Commit

Permalink
Merge pull request #14 from Pilifer/testing2
Browse files Browse the repository at this point in the history
Another step in gradual testing of game_window (issue #8)
  • Loading branch information
pvcraven authored Feb 29, 2020
2 parents 2670799 + cb330d0 commit b2793e5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 39 deletions.
37 changes: 9 additions & 28 deletions source/game_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,34 +56,15 @@ def setup(self):

self.game_engine.setup()

spacing = 37
y_value = SCREEN_HEIGHT - 75
sprite = arcade.Sprite("images/plus_button.png")
sprite.center_x = 200
sprite.center_y = y_value
sprite.name = "attack"
self.character_sheet_buttons.append(sprite)

y_value -= spacing
sprite = arcade.Sprite("images/plus_button.png")
sprite.center_x = 200
sprite.center_y = y_value
sprite.name = "defense"
self.character_sheet_buttons.append(sprite)

y_value -= spacing
sprite = arcade.Sprite("images/plus_button.png")
sprite.center_x = 200
sprite.center_y = y_value
sprite.name = "hp"
self.character_sheet_buttons.append(sprite)

y_value -= spacing
sprite = arcade.Sprite("images/plus_button.png")
sprite.center_x = 200
sprite.center_y = y_value
sprite.name = "capacity"
self.character_sheet_buttons.append(sprite)
for button_name, y_value in zip(
["attack", "defense", "hp", "capacity"],
range(SCREEN_HEIGHT - 75, 490, -37)
):
sprite = arcade.Sprite("images/plus_button.png")
sprite.center_x = 200
sprite.center_y = y_value
sprite.name = button_name
self.character_sheet_buttons.append(sprite)

def draw_hp_and_status_bar(self):
text = f"HP: {self.game_engine.player.fighter.hp}/{self.game_engine.player.fighter.max_hp}"
Expand Down
37 changes: 26 additions & 11 deletions tests/test_game_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
SPRITE_HEIGHT,
SPRITE_WIDTH,
STATUS_PANEL_HEIGHT,
)
EXPERIENCE_PER_LEVEL)
from game_window import main, MyGame
from themes.current_theme import colors

Expand Down Expand Up @@ -58,14 +58,26 @@ def test_init(self, mock_arcade, mock_engine):
assert window.time_since_last_move_check == 0
assert window.mouse_over_text is None
assert window.mouse_position is None
assert window.character_sheet_buttons == mock_arcade.SpriteList.return_value
mock_arcade.set_background_color.assert_called_once_with((255, 255, 255))

def test_setup(self, mock_arcade, mock_engine):
def test_setup(self, mocker, mock_arcade, mock_engine):
window = MyGame(100, 100, "foo")
mock_sprites = [Mock() for _ in range(4)]
mock_arcade.Sprite.side_effect = mock_sprites

window.setup()

mock_engine.return_value.setup.assert_called_once()
sprite_names = ["attack", "defense", "hp", "capacity"]
centre_ys = [602, 565, 528, 491]
for mock_sprite, name, centre_y in zip(mock_sprites, sprite_names, centre_ys):
assert mock_sprite.name == name
assert mock_sprite.center_x == 200
assert mock_sprite.center_y == centre_y
assert mock_arcade.SpriteList.return_value.append.call_args_list == [
call(mock_sprite) for mock_sprite in mock_sprites
]

def test_on_mouse_press_in_normal_state(
self, mock_arcade, mock_engine, mock_pixel_to_char
Expand Down Expand Up @@ -155,8 +167,10 @@ def test_on_draw_in_select_location_state(self, mocker, mock_arcade):

def test_draw_hp_and_status_bar_for_fifth_level(self, mocker, mock_arcade, mock_engine):
mock_draw_status_bar = mocker.patch("game_window.draw_status_bar")
mock_engine.return_value.player.fighter.level = 5
mock_engine.return_value.player.fighter.current_xp = 2001
lvl = len(EXPERIENCE_PER_LEVEL) + 1
mock_engine.return_value.player.fighter.level = lvl
xp = EXPERIENCE_PER_LEVEL[-1] + 1
mock_engine.return_value.player.fighter.current_xp = xp
mock_hp = mock_engine.return_value.player.fighter.hp
mock_max_hp = mock_engine.return_value.player.fighter.max_hp
window = MyGame(100, 100, "foo")
Expand All @@ -165,17 +179,19 @@ def test_draw_hp_and_status_bar_for_fifth_level(self, mocker, mock_arcade, mock_

assert mock_arcade.draw_text.call_args_list == [
call(f"HP: {mock_hp}/{mock_max_hp}", 0, 0, colors["status_panel_text"]),
call("XP: 2,001", 100, 0, (0, 0, 0, 255)),
call("Level: 5", 200, 0, (0, 0, 0, 255)),
call(f"XP: {xp:,}", 100, 0, (0, 0, 0, 255)),
call(f"Level: {lvl}", 200, 0, (0, 0, 0, 255)),
]
mock_draw_status_bar.assert_called_once_with(
65 / 2 + 2, 24, 65, 10, mock_hp, mock_max_hp
)

def test_draw_hp_and_status_bar_for_fourth_level(self, mocker, mock_arcade, mock_engine):
mock_draw_status_bar = mocker.patch("game_window.draw_status_bar")
mock_engine.return_value.player.fighter.level = 4
mock_engine.return_value.player.fighter.current_xp = 1999
lvl = len(EXPERIENCE_PER_LEVEL)
mock_engine.return_value.player.fighter.level = lvl
xp = EXPERIENCE_PER_LEVEL[-1] - 1
mock_engine.return_value.player.fighter.current_xp = xp
mock_hp = mock_engine.return_value.player.fighter.hp
mock_max_hp = mock_engine.return_value.player.fighter.max_hp
window = MyGame(100, 100, "foo")
Expand All @@ -184,14 +200,13 @@ def test_draw_hp_and_status_bar_for_fourth_level(self, mocker, mock_arcade, mock

assert mock_arcade.draw_text.call_args_list == [
call(f"HP: {mock_hp}/{mock_max_hp}", 0, 0, colors["status_panel_text"]),
call("XP: 1,999/2,000", 100, 0, (0, 0, 0, 255)),
call("Level: 4", 200, 0, (0, 0, 0, 255)),
call(f"XP: {xp:,}/{xp + 1:,}", 100, 0, (0, 0, 0, 255)),
call(f"Level: {lvl}", 200, 0, (0, 0, 0, 255)),
]
mock_draw_status_bar.assert_called_once_with(
65 / 2 + 2, 24, 65, 10, mock_hp, mock_max_hp
)


def test_draw_inventory_no_selected_item(self, mock_arcade, mock_engine):
mock_engine.return_value.player.inventory.capacity = 2
mock_engine.return_value.selected_item = None
Expand Down

0 comments on commit b2793e5

Please sign in to comment.