141 lines
5.6 KiB
Python
141 lines
5.6 KiB
Python
import os
|
|
import unittest
|
|
|
|
from dungeonbattle.bootstrap import Bootstrap
|
|
from dungeonbattle.display.display import Display
|
|
from dungeonbattle.display.display_manager import DisplayManager
|
|
from dungeonbattle.entities.player import Player
|
|
from dungeonbattle.game import Game, KeyValues, GameMode
|
|
from dungeonbattle.menus import MainMenuValues
|
|
|
|
|
|
class TestGame(unittest.TestCase):
|
|
def setUp(self) -> None:
|
|
"""
|
|
Setup game.
|
|
"""
|
|
self.game = Game()
|
|
self.game.new_game()
|
|
display = DisplayManager(None, self.game)
|
|
self.game.display_refresh = display.refresh
|
|
|
|
def test_load_game(self) -> None:
|
|
self.assertRaises(NotImplementedError, Game.load_game, "game.save")
|
|
self.assertRaises(NotImplementedError, Display(None).display)
|
|
|
|
def test_bootstrap_fail(self) -> None:
|
|
"""
|
|
Ensure that the test can't play the game,
|
|
because there is no associated shell.
|
|
Yeah, that's only for coverage.
|
|
"""
|
|
self.assertRaises(Exception, Bootstrap.run_game)
|
|
self.assertEqual(os.getenv("TERM", "unknown"), "unknown")
|
|
|
|
def test_key_translation(self) -> None:
|
|
"""
|
|
Test key bindings.
|
|
"""
|
|
self.assertEqual(self.game.translate_key(
|
|
self.game.settings.KEY_UP_PRIMARY), KeyValues.UP)
|
|
self.assertEqual(self.game.translate_key(
|
|
self.game.settings.KEY_UP_SECONDARY), KeyValues.UP)
|
|
self.assertEqual(self.game.translate_key(
|
|
self.game.settings.KEY_DOWN_PRIMARY), KeyValues.DOWN)
|
|
self.assertEqual(self.game.translate_key(
|
|
self.game.settings.KEY_DOWN_SECONDARY), KeyValues.DOWN)
|
|
self.assertEqual(self.game.translate_key(
|
|
self.game.settings.KEY_LEFT_PRIMARY), KeyValues.LEFT)
|
|
self.assertEqual(self.game.translate_key(
|
|
self.game.settings.KEY_LEFT_SECONDARY), KeyValues.LEFT)
|
|
self.assertEqual(self.game.translate_key(
|
|
self.game.settings.KEY_RIGHT_PRIMARY), KeyValues.RIGHT)
|
|
self.assertEqual(self.game.translate_key(
|
|
self.game.settings.KEY_RIGHT_SECONDARY), KeyValues.RIGHT)
|
|
self.assertEqual(self.game.translate_key(
|
|
self.game.settings.KEY_ENTER), KeyValues.ENTER)
|
|
self.assertEqual(self.game.translate_key(' '), KeyValues.SPACE)
|
|
|
|
def test_key_press(self) -> None:
|
|
"""
|
|
Press a key and see what is done.
|
|
"""
|
|
self.assertEqual(self.game.state, GameMode.MAINMENU)
|
|
self.assertEqual(self.game.main_menu.validate(),
|
|
MainMenuValues.START)
|
|
self.game.handle_key_pressed(KeyValues.UP)
|
|
self.assertEqual(self.game.main_menu.validate(),
|
|
MainMenuValues.START)
|
|
self.game.handle_key_pressed(KeyValues.DOWN)
|
|
self.assertEqual(self.game.main_menu.validate(),
|
|
MainMenuValues.SETTINGS)
|
|
self.game.handle_key_pressed(KeyValues.ENTER)
|
|
self.assertEqual(self.game.state, GameMode.SETTINGS)
|
|
|
|
self.game.handle_key_pressed(KeyValues.SPACE)
|
|
self.assertEqual(self.game.state, GameMode.MAINMENU)
|
|
|
|
self.game.handle_key_pressed(KeyValues.DOWN)
|
|
self.assertEqual(self.game.main_menu.validate(),
|
|
MainMenuValues.EXIT)
|
|
self.assertRaises(SystemExit, self.game.handle_key_pressed,
|
|
KeyValues.ENTER)
|
|
|
|
self.game.handle_key_pressed(KeyValues.UP)
|
|
self.assertEqual(self.game.main_menu.validate(),
|
|
MainMenuValues.SETTINGS)
|
|
self.game.handle_key_pressed(KeyValues.UP)
|
|
self.assertEqual(self.game.main_menu.validate(),
|
|
MainMenuValues.START)
|
|
|
|
self.game.handle_key_pressed(KeyValues.ENTER)
|
|
self.assertEqual(self.game.state, GameMode.PLAY)
|
|
|
|
# Kill entities
|
|
for entity in self.game.map.entities.copy():
|
|
if not isinstance(entity, Player):
|
|
self.game.map.remove_entity(entity)
|
|
|
|
y, x = self.game.player.y, self.game.player.x
|
|
self.game.handle_key_pressed(KeyValues.DOWN)
|
|
new_y, new_x = self.game.player.y, self.game.player.x
|
|
self.assertEqual(new_y, y + 1)
|
|
self.assertEqual(new_x, x)
|
|
|
|
y, x = new_y, new_x
|
|
self.game.handle_key_pressed(KeyValues.RIGHT)
|
|
new_y, new_x = self.game.player.y, self.game.player.x
|
|
self.assertEqual(new_y, y)
|
|
self.assertEqual(new_x, x + 1)
|
|
|
|
y, x = self.game.player.y, self.game.player.x
|
|
self.game.handle_key_pressed(KeyValues.UP)
|
|
new_y, new_x = self.game.player.y, self.game.player.x
|
|
self.assertEqual(new_y, y - 1)
|
|
self.assertEqual(new_x, x)
|
|
|
|
y, x = self.game.player.y, self.game.player.x
|
|
self.game.handle_key_pressed(KeyValues.LEFT)
|
|
new_y, new_x = self.game.player.y, self.game.player.x
|
|
self.assertEqual(new_y, y)
|
|
self.assertEqual(new_x, x - 1)
|
|
|
|
self.game.handle_key_pressed(KeyValues.SPACE)
|
|
self.assertEqual(self.game.state, GameMode.MAINMENU)
|
|
|
|
def test_dead_screen(self) -> None:
|
|
"""
|
|
Kill player and render dead screen.
|
|
"""
|
|
self.game.state = GameMode.PLAY
|
|
# Kill player
|
|
self.game.player.take_damage(self.game.player,
|
|
self.game.player.health + 2)
|
|
y, x = self.game.player.y, self.game.player.x
|
|
for key in [KeyValues.UP, KeyValues.DOWN,
|
|
KeyValues.LEFT, KeyValues.RIGHT]:
|
|
self.game.handle_key_pressed(key)
|
|
new_y, new_x = self.game.player.y, self.game.player.x
|
|
self.assertEqual(new_y, y)
|
|
self.assertEqual(new_x, x)
|