Skip to content

Commit

Permalink
Add test for character
Browse files Browse the repository at this point in the history
  • Loading branch information
Liesegang committed Feb 27, 2024
1 parent 89269c3 commit 36d8553
Show file tree
Hide file tree
Showing 4 changed files with 272 additions and 36 deletions.
41 changes: 13 additions & 28 deletions coc7e_combat_simulator/attribute.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,14 @@
class Attribute:
def __init__(
self,
strength: int,
constitution: int,
size: int,
dexterity: int,
appearance: int,
intelligence: int,
power: int,
education: int,
luck: int,
):
self.strength = strength
self.constitution = constitution
self.size = size
self.dexterity = dexterity
self.appearance = appearance
self.intelligence = intelligence
self.power = power
self.education = education
self.luck = luck
from dataclasses import dataclass


def __repr__(self) -> str:
return (
f"Strength: {self.strength}, Constitution: {self.constitution}, Size: {self.size}, "
f"Dexterity: {self.dexterity}, Appearance: {self.appearance}, Intelligence: {self.intelligence}, "
f"Power: {self.power}, Education: {self.education}, Luck: {self.luck}"
)
@dataclass
class Attribute:
strength: int
constitution: int
size: int
dexterity: int
appearance: int
intelligence: int
power: int
education: int
luck: int
14 changes: 6 additions & 8 deletions coc7e_combat_simulator/character.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def __init__(self, name: str, attributes: Attribute, skills: List[Skill]):

@classmethod
def of(
cls, name: str, attribute_params: dict, skills: List[Skill] = []
cls, name: str, attribute_params: dict, skills: List[Skill]
) -> "Character":
attributes = Attribute(**attribute_params)
return cls(name, attributes, skills)
Expand Down Expand Up @@ -54,18 +54,16 @@ def roll_dice(n: int, sides: int) -> int:
def calculate_db(strength: int, size: int) -> str:
combined = strength + size

if 2 <= combined <= 64:
if combined <= 64:
return "-2"
elif 65 <= combined <= 84:
elif 65 <= combined < 85:
return "-1"
elif 85 <= combined <= 124:
elif 85 <= combined < 125:
return "0"
elif 125 <= combined <= 164:
elif 125 <= combined < 165:
return "1d4"
elif combined >= 165:
return "1d6"
else:
return "0"
return str((combined - 125) // 40) + "d6"

def add_skill(self, skill: Skill) -> None:
if not isinstance(skill, Skill):
Expand Down
132 changes: 132 additions & 0 deletions tests/strategies/test_target_selection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import unittest
from coc7e_combat_simulator.strategies.target_selection import (
MaximumHpTargetSelectionStrategy,
MinimumHpTargetSelectionStrategy,
)
from coc7e_combat_simulator.character import Character


class TestMinimumHpTargetSelectionStrategy(unittest.TestCase):
def setUp(self):
self.strategy = MinimumHpTargetSelectionStrategy()

def test_select_target(self):
# Create some test characters
main_character = Character.of_random("main", [])
main_character.side = "A"
opponent1 = Character.of(
"opponent_1",
{
"strength": 50,
"constitution": 50,
"size": 50,
"dexterity": 50,
"appearance": 50,
"intelligence": 50,
"power": 50,
"education": 50,
"luck": 50,
},
[],
)
opponent2 = Character.of(
"opponent_2",
{
"strength": 50,
"constitution": 50,
"size": 60,
"dexterity": 50,
"appearance": 50,
"intelligence": 50,
"power": 50,
"education": 50,
"luck": 50,
},
[],
)
opponent3 = Character.of(
"opponent_3",
{
"strength": 50,
"constitution": 50,
"size": 70,
"dexterity": 50,
"appearance": 50,
"intelligence": 50,
"power": 50,
"education": 50,
"luck": 50,
},
[],
)
opponents = [opponent1, opponent2, opponent3]
for opponent in opponents:
opponent.side = "B"

selected_target = self.strategy.select_target(main_character, opponents)
self.assertEqual(selected_target, opponent1)


class TestMaximumHpTargetSelectionStrategy(unittest.TestCase):
def setUp(self):
self.strategy = MaximumHpTargetSelectionStrategy()

def test_select_target(self):
# Create some test characters
main_character = Character.of_random("main", [])
main_character.side = "A"
opponent1 = Character.of(
"opponent_1",
{
"strength": 50,
"constitution": 50,
"size": 50,
"dexterity": 50,
"appearance": 50,
"intelligence": 50,
"power": 50,
"education": 50,
"luck": 50,
},
[],
)
opponent2 = Character.of(
"opponent_2",
{
"strength": 50,
"constitution": 50,
"size": 60,
"dexterity": 50,
"appearance": 50,
"intelligence": 50,
"power": 50,
"education": 50,
"luck": 50,
},
[],
)
opponent3 = Character.of(
"opponent_3",
{
"strength": 50,
"constitution": 50,
"size": 70,
"dexterity": 50,
"appearance": 50,
"intelligence": 50,
"power": 50,
"education": 50,
"luck": 50,
},
[],
)
opponents = [opponent1, opponent2, opponent3]
for opponent in opponents:
opponent.side = "B"

selected_target = self.strategy.select_target(main_character, opponents)
self.assertEqual(selected_target, opponent3)


if __name__ == "__main__":
unittest.main()
121 changes: 121 additions & 0 deletions tests/test_character.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import unittest
from coc7e_combat_simulator.character import Character, Attribute, Skill
from coc7e_combat_simulator.strategies.reaction import NothingReactionStrategy
from coc7e_combat_simulator.strategies.skill_selection import (
RandomSkillSelectionStrategy,
)
from coc7e_combat_simulator.strategies.target_selection import (
RandomTargetSelectionStrategy,
)


class TestCharacter(unittest.TestCase):
def setUp(self):
self.skills = [
Skill("Skill 1", 50, "3D6", True),
Skill("Skill 2", 70, "3D6", False),
Skill("Skill 3", 30, "3D6", True),
]
self.attributes = Attribute(
strength=10,
constitution=20,
size=30,
dexterity=40,
appearance=50,
intelligence=60,
power=70,
education=80,
luck=90,
)
self.character = Character("Test Character", self.attributes, self.skills)

def test_character_creation(self):
self.assertEqual(self.character.name, "Test Character")
self.assertEqual(self.character.attributes, self.attributes)
self.assertEqual(self.character.skills, self.skills)
self.assertEqual(self.character.hp, 5) # (20 + 30) // 10 = 5
self.assertEqual(self.character.mp, 14) # 70 // 5 = 10
self.assertEqual(self.character.db, "-2") # calculate_db(10, 30) = "-2"
self.assertIsNone(self.character.side)
self.assertIsInstance(
self.character.target_selection_strategy, RandomTargetSelectionStrategy
)
self.assertIsInstance(
self.character.skill_selection_strategy, RandomSkillSelectionStrategy
)
self.assertIsInstance(self.character.reply_strategy, NothingReactionStrategy)

def test_character_of_method(self):
attribute_params = {
"strength": 10,
"constitution": 20,
"size": 30,
"dexterity": 40,
"appearance": 50,
"intelligence": 60,
"power": 70,
"education": 80,
"luck": 90,
}
character = Character.of("Test Character", attribute_params, self.skills)
self.assertEqual(character.name, "Test Character")
self.assertEqual(character.attributes, self.attributes)
self.assertEqual(character.skills, self.skills)

def test_character_of_random_method(self):
character = Character.of_random("Test Character", self.skills)
self.assertEqual(character.name, "Test Character")
self.assertIsInstance(character.attributes, Attribute)
self.assertEqual(len(character.skills), 3)
self.assertGreaterEqual(character.attributes.strength, 15)
self.assertLessEqual(character.attributes.strength, 95)

def test_roll_dice_method(self):
result = Character.roll_dice(3, 6)
self.assertGreaterEqual(result, 3)
self.assertLessEqual(result, 18)

def test_calculate_db_method(self):
args = [
[1, 1, "-2"],
[32, 32, "-2"],
[32, 33, "-1"],
[42, 42, "-1"],
[42, 43, "0"],
[62, 62, "0"],
[62, 63, "1d4"],
[82, 82, "1d4"],
[82, 83, "1d6"],
[102, 102, "1d6"],
[102, 103, "2d6"],
[122, 122, "2d6"],
[122, 123, "3d6"],
[142, 142, "3d6"],
[642, 643, "29d6"],
[662, 662, "29d6"],
]
for arg in args:
result = Character.calculate_db(arg[0], arg[1])
self.assertEqual(result, arg[2])

def test_add_skill_method(self):
new_skill = Skill("New Skill", 80, "3D6", True)
self.character.add_skill(new_skill)
self.assertIn(new_skill, self.character.skills)

def test_add_skill_method_with_invalid_skill(self):
with self.assertRaises(ValueError):
self.character.add_skill("invalid")

def test_repr_method(self):
expected_repr = "Attributes: Attribute(strength=10, constitution=20, size=30, dexterity=40, " + \
"appearance=50, intelligence=60, power=70, education=80, luck=90)\n" + \
"HP: 5, MP: 14\n" + \
"Skills: Skill 1: Success Rate: 50%, Damage: 3D6, " + \
"Skill 2: Success Rate: 70%, Damage: 3D6, " + \
"Skill 3: Success Rate: 30%, Damage: 3D6"
self.assertEqual(repr(self.character), expected_repr)


if __name__ == "__main__":
unittest.main()

0 comments on commit 36d8553

Please sign in to comment.