More tests and more coverage
This commit is contained in:
parent
8ccb74ea54
commit
ff435dc328
@ -8,9 +8,9 @@ class Item(Entity):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.held = False
|
||||
|
||||
def drop(self, x: int, y: int) -> None:
|
||||
def drop(self, y: int, x: int) -> None:
|
||||
self.held = False
|
||||
self.move(x, y)
|
||||
self.move(y, x)
|
||||
|
||||
def hold(self) -> None:
|
||||
self.held = True
|
||||
|
@ -2,7 +2,7 @@ from ..interfaces import FightingEntity, Map
|
||||
|
||||
|
||||
class Monster(FightingEntity):
|
||||
def act(self, map: Map) -> None:
|
||||
def act(self, m: Map) -> None:
|
||||
pass
|
||||
|
||||
|
||||
|
@ -66,12 +66,16 @@ class Tile(Enum):
|
||||
|
||||
|
||||
class Entity:
|
||||
x: int
|
||||
y: int
|
||||
x: int
|
||||
|
||||
def move(self, x: int, y: int) -> None:
|
||||
self.x = x
|
||||
def __init__(self):
|
||||
self.y = 0
|
||||
self.x = 0
|
||||
|
||||
def move(self, y: int, x: int) -> None:
|
||||
self.y = y
|
||||
self.x = x
|
||||
|
||||
def act(self, m: Map) -> None:
|
||||
"""
|
||||
@ -85,9 +89,12 @@ class FightingEntity(Entity):
|
||||
maxhealth: int
|
||||
health: int
|
||||
strength: int
|
||||
dead: bool
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.health = self.maxhealth
|
||||
self.dead = False
|
||||
|
||||
def hit(self, opponent: "FightingEntity") -> None:
|
||||
opponent.take_damage(self, self.strength)
|
||||
@ -98,4 +105,4 @@ class FightingEntity(Entity):
|
||||
self.die()
|
||||
|
||||
def die(self) -> None:
|
||||
pass
|
||||
self.dead = True
|
||||
|
81
dungeonbattle/tests/entities_test.py
Normal file
81
dungeonbattle/tests/entities_test.py
Normal file
@ -0,0 +1,81 @@
|
||||
import unittest
|
||||
|
||||
from dungeonbattle.entities.items import Bomb, Item
|
||||
from dungeonbattle.entities.monsters import Squirrel
|
||||
from dungeonbattle.entities.player import Player
|
||||
from dungeonbattle.interfaces import Entity, Map
|
||||
|
||||
|
||||
class TestEntities(unittest.TestCase):
|
||||
def setUp(self) -> None:
|
||||
"""
|
||||
Load example map that can be used in tests.
|
||||
"""
|
||||
self.map = Map.load("example_map.txt")
|
||||
|
||||
def test_basic_entities(self) -> None:
|
||||
"""
|
||||
Test some random stuff with basic entities.
|
||||
"""
|
||||
entity = Entity()
|
||||
entity.move(42, 64)
|
||||
self.assertEqual(entity.y, 42)
|
||||
self.assertEqual(entity.x, 64)
|
||||
self.assertIsNone(entity.act(self.map))
|
||||
|
||||
def test_fighting_entities(self) -> None:
|
||||
"""
|
||||
Test some random stuff with fighting entities.
|
||||
"""
|
||||
entity = Squirrel()
|
||||
self.assertIsNone(entity.act(self.map))
|
||||
self.assertEqual(entity.maxhealth, 10)
|
||||
self.assertEqual(entity.maxhealth, entity.health)
|
||||
self.assertEqual(entity.strength, 3)
|
||||
self.assertIsNone(entity.hit(entity))
|
||||
self.assertFalse(entity.dead)
|
||||
self.assertIsNone(entity.hit(entity))
|
||||
self.assertFalse(entity.dead)
|
||||
self.assertIsNone(entity.hit(entity))
|
||||
self.assertFalse(entity.dead)
|
||||
self.assertIsNone(entity.hit(entity))
|
||||
self.assertTrue(entity.dead)
|
||||
|
||||
def test_items(self) -> None:
|
||||
"""
|
||||
Test some random stuff with items.
|
||||
"""
|
||||
item = Item()
|
||||
self.assertFalse(item.held)
|
||||
item.hold()
|
||||
self.assertTrue(item.held)
|
||||
item.drop(42, 42)
|
||||
self.assertEqual(item.y, 42)
|
||||
self.assertEqual(item.x, 42)
|
||||
|
||||
def test_bombs(self) -> None:
|
||||
"""
|
||||
Test some random stuff with bombs.
|
||||
"""
|
||||
item = Bomb()
|
||||
squirrel = Squirrel()
|
||||
self.map.entities.append(item)
|
||||
self.map.entities.append(squirrel)
|
||||
squirrel.health = 2
|
||||
squirrel.move(41, 42)
|
||||
item.act(self.map)
|
||||
self.assertFalse(squirrel.dead)
|
||||
item.drop(42, 42)
|
||||
self.assertEqual(item.y, 42)
|
||||
self.assertEqual(item.x, 42)
|
||||
item.act(self.map)
|
||||
self.assertTrue(squirrel.dead)
|
||||
|
||||
def test_players(self) -> None:
|
||||
"""
|
||||
Test some random stuff with players.
|
||||
"""
|
||||
player = Player()
|
||||
self.assertEqual(player.strength, 5)
|
||||
self.assertEqual(player.health, player.maxhealth)
|
||||
self.assertEqual(player.maxhealth, 20)
|
@ -1,6 +1,6 @@
|
||||
import unittest
|
||||
|
||||
from dungeonbattle.interfaces import Map
|
||||
from dungeonbattle.interfaces import Map, Tile
|
||||
|
||||
|
||||
class TestInterfaces(unittest.TestCase):
|
||||
@ -12,3 +12,22 @@ class TestInterfaces(unittest.TestCase):
|
||||
self.assertEqual(m.width, 2)
|
||||
self.assertEqual(m.height, 2)
|
||||
self.assertEqual(m.draw_string(), ".█\n█.")
|
||||
|
||||
def test_load_map(self) -> None:
|
||||
"""
|
||||
Try to load a map from a file.
|
||||
"""
|
||||
m = Map.load("example_map.txt")
|
||||
self.assertEqual(m.width, 52)
|
||||
self.assertEqual(m.height, 17)
|
||||
|
||||
def test_tiles(self) -> None:
|
||||
"""
|
||||
Test some things about tiles.
|
||||
"""
|
||||
self.assertFalse(Tile.FLOOR.is_wall())
|
||||
self.assertTrue(Tile.WALL.is_wall())
|
||||
self.assertFalse(Tile.EMPTY.is_wall())
|
||||
self.assertTrue(Tile.FLOOR.can_walk())
|
||||
self.assertFalse(Tile.WALL.can_walk())
|
||||
self.assertTrue(Tile.EMPTY.can_walk())
|
||||
|
Loading…
Reference in New Issue
Block a user