From ff435dc3280e19d3a0659e7128581323ddb0d3da Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Fri, 6 Nov 2020 16:13:28 +0100 Subject: [PATCH] More tests and more coverage --- dungeonbattle/entities/items.py | 4 +- dungeonbattle/entities/monsters.py | 2 +- dungeonbattle/interfaces.py | 15 +++-- dungeonbattle/tests/entities_test.py | 81 ++++++++++++++++++++++++++ dungeonbattle/tests/interfaces_test.py | 21 ++++++- 5 files changed, 115 insertions(+), 8 deletions(-) create mode 100644 dungeonbattle/tests/entities_test.py diff --git a/dungeonbattle/entities/items.py b/dungeonbattle/entities/items.py index 09750c7..3edec91 100644 --- a/dungeonbattle/entities/items.py +++ b/dungeonbattle/entities/items.py @@ -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 diff --git a/dungeonbattle/entities/monsters.py b/dungeonbattle/entities/monsters.py index f4a7a1f..59db0e7 100644 --- a/dungeonbattle/entities/monsters.py +++ b/dungeonbattle/entities/monsters.py @@ -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 diff --git a/dungeonbattle/interfaces.py b/dungeonbattle/interfaces.py index 5d57510..6546523 100644 --- a/dungeonbattle/interfaces.py +++ b/dungeonbattle/interfaces.py @@ -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 diff --git a/dungeonbattle/tests/entities_test.py b/dungeonbattle/tests/entities_test.py new file mode 100644 index 0000000..f7d75f1 --- /dev/null +++ b/dungeonbattle/tests/entities_test.py @@ -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) diff --git a/dungeonbattle/tests/interfaces_test.py b/dungeonbattle/tests/interfaces_test.py index 849e28b..d6ab078 100644 --- a/dungeonbattle/tests/interfaces_test.py +++ b/dungeonbattle/tests/interfaces_test.py @@ -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())