Merge branch 'master' into map_generation
# Conflicts: # squirrelbattle/game.py # squirrelbattle/interfaces.py # squirrelbattle/tests/game_test.py
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
# Copyright (C) 2020 by ÿnérant, eichhornchen, nicomarg, charlse
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
import curses
|
||||
import os
|
||||
import unittest
|
||||
|
||||
@ -8,11 +9,13 @@ from ..bootstrap import Bootstrap
|
||||
from ..display.display import Display
|
||||
from ..display.display_manager import DisplayManager
|
||||
from ..entities.friendly import Merchant, Sunflower
|
||||
from ..entities.items import Bomb, Heart, Sword
|
||||
from ..entities.items import Bomb, Heart, Sword, Explosion, Shield, Helmet, \
|
||||
Chestplate, RingCritical, Monocle
|
||||
from ..entities.monsters import GiantSeaEagle
|
||||
from ..entities.player import Player
|
||||
from ..enums import DisplayActions
|
||||
from ..game import Game, KeyValues, GameMode
|
||||
from ..interfaces import Tile, Map
|
||||
from ..interfaces import Map, Tile
|
||||
from ..menus import MainMenuValues
|
||||
from ..resources import ResourceManager
|
||||
from ..settings import Settings
|
||||
@ -22,21 +25,21 @@ from ..translations import gettext as _, Translator
|
||||
class TestGame(unittest.TestCase):
|
||||
def setUp(self) -> None:
|
||||
"""
|
||||
Setup game.
|
||||
Sets the game up.
|
||||
"""
|
||||
self.game = Game()
|
||||
self.game.new_game()
|
||||
self.game.map = Map.load(ResourceManager.get_asset_path(
|
||||
"example_map.txt"))
|
||||
self.game.player.move(self.game.map.start_y, self.game.map.start_x)
|
||||
self.game.map = Map.load(
|
||||
ResourceManager.get_asset_path("example_map.txt"))
|
||||
self.game.map.add_entity(self.game.player)
|
||||
self.game.player.move(self.game.map.start_y, self.game.map.start_x)
|
||||
self.game.logs.add_message("Hello World !")
|
||||
display = DisplayManager(None, self.game)
|
||||
self.game.display_actions = display.handle_display_action
|
||||
|
||||
def test_load_game(self) -> None:
|
||||
"""
|
||||
Save a game and reload it.
|
||||
Saves a game and reloads it.
|
||||
"""
|
||||
bomb = Bomb()
|
||||
self.game.map.add_entity(bomb)
|
||||
@ -63,6 +66,7 @@ class TestGame(unittest.TestCase):
|
||||
|
||||
new_state = self.game.save_state()
|
||||
self.assertEqual(old_state, new_state)
|
||||
self.assertIsNone(self.game.message)
|
||||
|
||||
# Ensure that the bomb is loaded
|
||||
self.assertTrue(self.game.player.inventory)
|
||||
@ -90,7 +94,7 @@ class TestGame(unittest.TestCase):
|
||||
|
||||
def test_bootstrap_fail(self) -> None:
|
||||
"""
|
||||
Ensure that the test can't play the game,
|
||||
Ensures that the test can't play the game,
|
||||
because there is no associated shell.
|
||||
Yeah, that's only for coverage.
|
||||
"""
|
||||
@ -99,7 +103,7 @@ class TestGame(unittest.TestCase):
|
||||
|
||||
def test_key_translation(self) -> None:
|
||||
"""
|
||||
Test key bindings.
|
||||
Tests key bindings.
|
||||
"""
|
||||
self.game.settings = Settings()
|
||||
|
||||
@ -145,6 +149,12 @@ class TestGame(unittest.TestCase):
|
||||
self.assertEqual(KeyValues.translate_key(
|
||||
self.game.settings.KEY_DROP, self.game.settings),
|
||||
KeyValues.DROP)
|
||||
self.assertEqual(KeyValues.translate_key(
|
||||
self.game.settings.KEY_WAIT, self.game.settings),
|
||||
KeyValues.WAIT)
|
||||
self.assertEqual(KeyValues.translate_key(
|
||||
self.game.settings.KEY_LADDER, self.game.settings),
|
||||
KeyValues.LADDER)
|
||||
self.assertEqual(KeyValues.translate_key(' ', self.game.settings),
|
||||
KeyValues.SPACE)
|
||||
self.assertEqual(KeyValues.translate_key('plop', self.game.settings),
|
||||
@ -152,7 +162,7 @@ class TestGame(unittest.TestCase):
|
||||
|
||||
def test_key_press(self) -> None:
|
||||
"""
|
||||
Press a key and see what is done.
|
||||
Presses a key and asserts what is done is correct.
|
||||
"""
|
||||
self.assertEqual(self.game.state, GameMode.MAINMENU)
|
||||
self.assertEqual(self.game.main_menu.validate(),
|
||||
@ -238,17 +248,28 @@ class TestGame(unittest.TestCase):
|
||||
self.assertEqual(new_y, y)
|
||||
self.assertEqual(new_x, x - 1)
|
||||
|
||||
explosion = Explosion()
|
||||
self.game.map.add_entity(explosion)
|
||||
self.assertIn(explosion, self.game.map.entities)
|
||||
self.game.handle_key_pressed(KeyValues.WAIT)
|
||||
self.assertNotIn(explosion, self.game.map.entities)
|
||||
|
||||
self.game.handle_key_pressed(KeyValues.SPACE)
|
||||
self.assertEqual(self.game.state, GameMode.MAINMENU)
|
||||
|
||||
def test_mouse_click(self) -> None:
|
||||
"""
|
||||
Simulate mouse clicks.
|
||||
Simulates mouse clicks.
|
||||
"""
|
||||
self.game.state = GameMode.MAINMENU
|
||||
|
||||
# Change the color of the artwork
|
||||
self.game.display_actions(DisplayActions.MOUSE, 0, 10,
|
||||
curses.BUTTON1_CLICKED)
|
||||
|
||||
# Settings menu
|
||||
self.game.display_actions(DisplayActions.MOUSE, 25, 21)
|
||||
self.game.display_actions(DisplayActions.MOUSE, 25, 21,
|
||||
curses.BUTTON1_CLICKED)
|
||||
self.assertEqual(self.game.main_menu.position, 4)
|
||||
self.assertEqual(self.game.state, GameMode.SETTINGS)
|
||||
|
||||
@ -260,17 +281,19 @@ class TestGame(unittest.TestCase):
|
||||
self.game.state = GameMode.INVENTORY
|
||||
|
||||
# Click nowhere
|
||||
self.game.display_actions(DisplayActions.MOUSE, 0, 0)
|
||||
self.game.display_actions(DisplayActions.MOUSE, 0, 0,
|
||||
curses.BUTTON1_CLICKED)
|
||||
self.assertEqual(self.game.state, GameMode.INVENTORY)
|
||||
|
||||
# Click on the second item
|
||||
self.game.display_actions(DisplayActions.MOUSE, 8, 25)
|
||||
self.game.display_actions(DisplayActions.MOUSE, 8, 25,
|
||||
curses.BUTTON1_CLICKED)
|
||||
self.assertEqual(self.game.state, GameMode.INVENTORY)
|
||||
self.assertEqual(self.game.inventory_menu.position, 1)
|
||||
|
||||
def test_new_game(self) -> None:
|
||||
"""
|
||||
Ensure that the start button starts a new game.
|
||||
Ensures that the start button starts a new game.
|
||||
"""
|
||||
old_map = self.game.map
|
||||
old_player = self.game.player
|
||||
@ -293,7 +316,7 @@ class TestGame(unittest.TestCase):
|
||||
|
||||
def test_settings_menu(self) -> None:
|
||||
"""
|
||||
Ensure that the settings menu is working properly.
|
||||
Ensures that the settings menu is working properly.
|
||||
"""
|
||||
self.game.settings = Settings()
|
||||
|
||||
@ -305,13 +328,13 @@ class TestGame(unittest.TestCase):
|
||||
self.game.handle_key_pressed(KeyValues.ENTER)
|
||||
self.assertEqual(self.game.state, GameMode.SETTINGS)
|
||||
|
||||
# Define the "move up" key to 'w'
|
||||
# Define the "move up" key to 'h'
|
||||
self.assertFalse(self.game.settings_menu.waiting_for_key)
|
||||
self.game.handle_key_pressed(KeyValues.ENTER)
|
||||
self.assertTrue(self.game.settings_menu.waiting_for_key)
|
||||
self.game.handle_key_pressed(None, 'w')
|
||||
self.game.handle_key_pressed(None, 'h')
|
||||
self.assertFalse(self.game.settings_menu.waiting_for_key)
|
||||
self.assertEqual(self.game.settings.KEY_UP_PRIMARY, 'w')
|
||||
self.assertEqual(self.game.settings.KEY_UP_PRIMARY, 'h')
|
||||
|
||||
# Navigate to "move left"
|
||||
self.game.handle_key_pressed(KeyValues.DOWN)
|
||||
@ -332,7 +355,7 @@ class TestGame(unittest.TestCase):
|
||||
self.assertEqual(self.game.settings.KEY_LEFT_PRIMARY, 'a')
|
||||
|
||||
# Navigate to "texture pack"
|
||||
for ignored in range(10):
|
||||
for ignored in range(12):
|
||||
self.game.handle_key_pressed(KeyValues.DOWN)
|
||||
|
||||
# Change texture pack
|
||||
@ -379,7 +402,7 @@ class TestGame(unittest.TestCase):
|
||||
|
||||
def test_dead_screen(self) -> None:
|
||||
"""
|
||||
Kill player and render dead screen.
|
||||
Kills the player and renders the dead message on the fake screen.
|
||||
"""
|
||||
self.game.state = GameMode.PLAY
|
||||
# Kill player
|
||||
@ -395,13 +418,14 @@ class TestGame(unittest.TestCase):
|
||||
|
||||
def test_not_implemented(self) -> None:
|
||||
"""
|
||||
Check that some functions are not implemented, only for coverage.
|
||||
Checks that some functions are not implemented, only for coverage.
|
||||
"""
|
||||
self.assertRaises(NotImplementedError, Display.display, None)
|
||||
self.assertRaises(NotImplementedError, Display.update, None, self.game)
|
||||
|
||||
def test_messages(self) -> None:
|
||||
"""
|
||||
Display error messages.
|
||||
Displays error messages.
|
||||
"""
|
||||
self.game.message = "I am an error"
|
||||
self.game.display_actions(DisplayActions.UPDATE)
|
||||
@ -411,7 +435,7 @@ class TestGame(unittest.TestCase):
|
||||
|
||||
def test_inventory_menu(self) -> None:
|
||||
"""
|
||||
Open the inventory menu and interact with items.
|
||||
Opens the inventory menu and interacts with items.
|
||||
"""
|
||||
self.game.state = GameMode.PLAY
|
||||
# Open and close the inventory
|
||||
@ -472,7 +496,7 @@ class TestGame(unittest.TestCase):
|
||||
|
||||
def test_talk_to_sunflowers(self) -> None:
|
||||
"""
|
||||
Interact with sunflowers
|
||||
Interacts with sunflowers.
|
||||
"""
|
||||
self.game.state = GameMode.PLAY
|
||||
|
||||
@ -504,8 +528,8 @@ class TestGame(unittest.TestCase):
|
||||
self.assertEqual(self.game.state, GameMode.PLAY)
|
||||
self.assertTrue(self.game.logs.messages)
|
||||
# Ensure that the message is a good message
|
||||
self.assertIn(self.game.logs.messages[1][21:],
|
||||
Sunflower.dialogue_option)
|
||||
self.assertTrue(any(self.game.logs.messages[1].endswith(msg)
|
||||
for msg in Sunflower().dialogue_option))
|
||||
|
||||
# Test all directions to detect the friendly entity
|
||||
self.game.player.move(sunflower.y + 1, sunflower.x)
|
||||
@ -523,7 +547,7 @@ class TestGame(unittest.TestCase):
|
||||
|
||||
def test_talk_to_merchant(self) -> None:
|
||||
"""
|
||||
Interact with merchants
|
||||
Interacts with merchants.
|
||||
"""
|
||||
self.game.state = GameMode.PLAY
|
||||
|
||||
@ -545,21 +569,28 @@ class TestGame(unittest.TestCase):
|
||||
# Navigate in the menu
|
||||
self.game.handle_key_pressed(KeyValues.DOWN)
|
||||
self.game.handle_key_pressed(KeyValues.DOWN)
|
||||
self.game.handle_key_pressed(KeyValues.LEFT)
|
||||
self.assertFalse(self.game.is_in_store_menu)
|
||||
self.game.handle_key_pressed(KeyValues.RIGHT)
|
||||
self.assertTrue(self.game.is_in_store_menu)
|
||||
self.game.handle_key_pressed(KeyValues.UP)
|
||||
self.assertEqual(self.game.store_menu.position, 1)
|
||||
|
||||
self.game.player.hazel = 0x7ffff42ff
|
||||
|
||||
# The second item is not a heart
|
||||
merchant.inventory[1] = Sword()
|
||||
merchant.inventory[1] = sword = Sword()
|
||||
# Buy the second item by clicking on it
|
||||
item = self.game.store_menu.validate()
|
||||
self.assertIn(item, merchant.inventory)
|
||||
self.game.display_actions(DisplayActions.MOUSE, 8, 25)
|
||||
self.game.handle_key_pressed(KeyValues.ENTER)
|
||||
self.game.display_actions(DisplayActions.MOUSE, 7, 25,
|
||||
curses.BUTTON1_CLICKED)
|
||||
self.assertIn(item, self.game.player.inventory)
|
||||
self.assertNotIn(item, merchant.inventory)
|
||||
|
||||
# Buy a heart
|
||||
merchant.inventory[1] = Heart()
|
||||
self.game.display_actions(DisplayActions.REFRESH)
|
||||
item = self.game.store_menu.validate()
|
||||
self.assertIn(item, merchant.inventory)
|
||||
self.assertEqual(item, merchant.inventory[1])
|
||||
@ -576,9 +607,169 @@ class TestGame(unittest.TestCase):
|
||||
self.game.handle_key_pressed(KeyValues.ENTER)
|
||||
self.assertNotIn(item, self.game.player.inventory)
|
||||
self.assertIn(item, merchant.inventory)
|
||||
self.assertEqual(self.game.message, _("You do not have enough money"))
|
||||
self.assertEqual(self.game.message,
|
||||
_("The buyer does not have enough money"))
|
||||
self.game.handle_key_pressed(KeyValues.ENTER)
|
||||
|
||||
# Sell an item
|
||||
self.game.inventory_menu.position = len(self.game.player.inventory) - 1
|
||||
self.game.handle_key_pressed(KeyValues.LEFT)
|
||||
self.assertFalse(self.game.is_in_store_menu)
|
||||
self.assertIn(sword, self.game.player.inventory)
|
||||
self.assertEqual(self.game.inventory_menu.validate(), sword)
|
||||
old_player_money, old_merchant_money = self.game.player.hazel,\
|
||||
merchant.hazel
|
||||
self.game.handle_key_pressed(KeyValues.ENTER)
|
||||
self.assertNotIn(sword, self.game.player.inventory)
|
||||
self.assertIn(sword, merchant.inventory)
|
||||
self.assertEqual(self.game.player.hazel, old_player_money + sword.price)
|
||||
self.assertEqual(merchant.hazel, old_merchant_money - sword.price)
|
||||
|
||||
# Exit the menu
|
||||
self.game.handle_key_pressed(KeyValues.SPACE)
|
||||
self.assertEqual(self.game.state, GameMode.PLAY)
|
||||
|
||||
def test_equipment(self) -> None:
|
||||
"""
|
||||
Ensure that equipment is working.
|
||||
"""
|
||||
self.game.state = GameMode.INVENTORY
|
||||
|
||||
# sword goes into the main equipment slot
|
||||
sword = Sword()
|
||||
sword.hold(self.game.player)
|
||||
self.game.handle_key_pressed(KeyValues.EQUIP)
|
||||
self.assertEqual(self.game.player.equipped_main, sword)
|
||||
self.assertFalse(self.game.player.inventory)
|
||||
|
||||
# shield goes into the secondary equipment slot
|
||||
shield = Shield()
|
||||
shield.hold(self.game.player)
|
||||
self.game.handle_key_pressed(KeyValues.EQUIP)
|
||||
self.assertEqual(self.game.player.equipped_secondary, shield)
|
||||
self.assertFalse(self.game.player.inventory)
|
||||
|
||||
# helmet goes into the helmet slot
|
||||
helmet = Helmet()
|
||||
helmet.hold(self.game.player)
|
||||
self.game.handle_key_pressed(KeyValues.EQUIP)
|
||||
self.assertEqual(self.game.player.equipped_helmet, helmet)
|
||||
self.assertFalse(self.game.player.inventory)
|
||||
|
||||
# helmet goes into the armor slot
|
||||
chestplate = Chestplate()
|
||||
chestplate.hold(self.game.player)
|
||||
self.game.handle_key_pressed(KeyValues.EQUIP)
|
||||
self.assertEqual(self.game.player.equipped_armor, chestplate)
|
||||
self.assertFalse(self.game.player.inventory)
|
||||
|
||||
# Use bomb
|
||||
bomb = Bomb()
|
||||
bomb.hold(self.game.player)
|
||||
self.game.handle_key_pressed(KeyValues.EQUIP)
|
||||
self.assertEqual(self.game.player.equipped_secondary, bomb)
|
||||
self.assertIn(shield, self.game.player.inventory)
|
||||
self.game.state = GameMode.PLAY
|
||||
self.game.handle_key_pressed(KeyValues.USE)
|
||||
self.assertIsNone(self.game.player.equipped_secondary)
|
||||
self.game.state = GameMode.INVENTORY
|
||||
self.game.handle_key_pressed(KeyValues.EQUIP)
|
||||
self.assertEqual(self.game.player.equipped_secondary, shield)
|
||||
self.assertFalse(self.game.player.inventory)
|
||||
|
||||
# Reequip, which is useless but covers code
|
||||
sword.equip()
|
||||
shield.equip()
|
||||
helmet.equip()
|
||||
chestplate.equip()
|
||||
self.game.save_state()
|
||||
|
||||
# Unequip all
|
||||
sword.unequip()
|
||||
shield.unequip()
|
||||
helmet.unequip()
|
||||
chestplate.unequip()
|
||||
self.assertIsNone(self.game.player.equipped_main)
|
||||
self.assertIsNone(self.game.player.equipped_secondary)
|
||||
self.assertIsNone(self.game.player.equipped_helmet)
|
||||
self.assertIsNone(self.game.player.equipped_armor)
|
||||
self.assertIn(sword, self.game.player.inventory)
|
||||
self.assertIn(shield, self.game.player.inventory)
|
||||
self.assertIn(helmet, self.game.player.inventory)
|
||||
self.assertIn(chestplate, self.game.player.inventory)
|
||||
|
||||
# Test rings
|
||||
self.game.player.inventory.clear()
|
||||
ring = RingCritical()
|
||||
ring.hold(self.game.player)
|
||||
old_critical = self.game.player.critical
|
||||
self.game.handle_key_pressed(KeyValues.EQUIP)
|
||||
self.assertEqual(self.game.player.critical,
|
||||
old_critical + ring.critical)
|
||||
self.game.save_state()
|
||||
ring.unequip()
|
||||
|
||||
def test_monocle(self) -> None:
|
||||
"""
|
||||
The player is wearing a monocle, then the stats are displayed.
|
||||
"""
|
||||
self.game.state = GameMode.PLAY
|
||||
|
||||
monocle = Monocle()
|
||||
monocle.hold(self.game.player)
|
||||
monocle.equip()
|
||||
|
||||
sea_eagle = GiantSeaEagle()
|
||||
self.game.map.add_entity(sea_eagle)
|
||||
sea_eagle.move(2, 6)
|
||||
|
||||
self.game.display_actions(DisplayActions.REFRESH)
|
||||
|
||||
def test_ladders(self) -> None:
|
||||
"""
|
||||
Ensure that the player can climb on ladders.
|
||||
"""
|
||||
self.game.state = GameMode.PLAY
|
||||
|
||||
self.assertEqual(self.game.player.map.floor, 0)
|
||||
self.game.handle_key_pressed(KeyValues.LADDER)
|
||||
self.assertEqual(self.game.player.map.floor, 0)
|
||||
|
||||
# Move nowhere
|
||||
self.game.player.move(10, 10)
|
||||
self.game.handle_key_pressed(KeyValues.LADDER)
|
||||
self.assertEqual(self.game.player.map.floor, 0)
|
||||
|
||||
# Move down
|
||||
self.game.player.move(3, 40) # Move on a ladder
|
||||
self.game.handle_key_pressed(KeyValues.LADDER)
|
||||
self.assertEqual(self.game.map_index, 1)
|
||||
self.assertEqual(self.game.player.map.floor, 1)
|
||||
self.assertEqual(self.game.player.y, 1)
|
||||
self.assertEqual(self.game.player.x, 17)
|
||||
self.game.display_actions(DisplayActions.UPDATE)
|
||||
|
||||
# Move up
|
||||
self.game.handle_key_pressed(KeyValues.LADDER)
|
||||
self.assertEqual(self.game.player.map.floor, 0)
|
||||
self.assertEqual(self.game.player.y, 3)
|
||||
self.assertEqual(self.game.player.x, 40)
|
||||
self.game.display_actions(DisplayActions.UPDATE)
|
||||
|
||||
def test_credits(self) -> None:
|
||||
"""
|
||||
Load credits menu.
|
||||
"""
|
||||
self.game.state = GameMode.MAINMENU
|
||||
|
||||
self.game.display_actions(DisplayActions.MOUSE, 41, 41,
|
||||
curses.BUTTON1_CLICKED)
|
||||
self.assertEqual(self.game.state, GameMode.CREDITS)
|
||||
self.game.display_actions(DisplayActions.MOUSE, 21, 21,
|
||||
curses.BUTTON1_CLICKED)
|
||||
self.game.display_actions(DisplayActions.REFRESH)
|
||||
|
||||
self.game.state = GameMode.CREDITS
|
||||
self.game.handle_key_pressed(KeyValues.ENTER)
|
||||
|
||||
self.assertEqual(self.game.state, GameMode.MAINMENU)
|
||||
|
Reference in New Issue
Block a user