Ran flake8 and black against tests.

This commit is contained in:
Mark Wolfman
2021-04-16 11:28:05 -05:00
parent 0c3dbc73fc
commit e5ec51b0e7
11 changed files with 278 additions and 207 deletions
+64 -41
View File
@@ -1,11 +1,15 @@
#!/usr/bin/env python #!/usr/bin/env python
from unittest import TestCase from unittest import TestCase
from pathlib import Path
import warnings import warnings
from dungeonsheets import race, monsters, exceptions, spells, infusions from dungeonsheets import race, monsters, exceptions, spells, infusions
from dungeonsheets.character import Character, Wizard, Druid, read_character_file, _resolve_mechanic from dungeonsheets.character import (
Character,
Wizard,
Druid,
_resolve_mechanic,
)
from dungeonsheets.weapons import Weapon, Shortsword from dungeonsheets.weapons import Weapon, Shortsword
from dungeonsheets.armor import Armor, LeatherArmor, Shield from dungeonsheets.armor import Armor, LeatherArmor, Shield
@@ -15,13 +19,14 @@ class TestCharacter(TestCase):
def test_constructor(self): def test_constructor(self):
char = Character(name="Inara") char = Character(name="Inara")
self.assertIsInstance(char, Character)
def test_hit_dice(self): def test_hit_dice(self):
# Test the getter # Test the getter
char = Character() char = Character()
char.level = 2 char.level = 2
char.hit_dice_faces = 10 char.hit_dice_faces = 10
self.assertEqual(char.hit_dice, '2d10') self.assertEqual(char.hit_dice, "2d10")
def test_max_hp(self): def test_max_hp(self):
char = Wizard(level=3, constitution=12) char = Wizard(level=3, constitution=12)
@@ -29,18 +34,18 @@ class TestCharacter(TestCase):
def test_set_attrs(self): def test_set_attrs(self):
char = Character() char = Character()
char.set_attrs(name='Inara') char.set_attrs(name="Inara")
self.assertEqual(char.name, 'Inara') self.assertEqual(char.name, "Inara")
# Check that the weapons get loaded as objects not string # Check that the weapons get loaded as objects not string
char.set_attrs(weapons=['shortsword']) char.set_attrs(weapons=["shortsword"])
self.assertEqual(len(char.weapons), 1) self.assertEqual(len(char.weapons), 1)
self.assertTrue(isinstance(char.weapons[0], Shortsword)) self.assertTrue(isinstance(char.weapons[0], Shortsword))
# Check that armor and shield gets set_attrs # Check that armor and shield gets set_attrs
char.set_attrs(armor='leather armor', shield='shield') char.set_attrs(armor="leather armor", shield="shield")
self.assertFalse(isinstance(char.armor, str)) self.assertFalse(isinstance(char.armor, str))
self.assertFalse(isinstance(char.shield, str)) self.assertFalse(isinstance(char.shield, str))
# Check that race gets set to an object # Check that race gets set to an object
char.set_attrs(race='high elf') char.set_attrs(race="high elf")
self.assertIsInstance(char.race, race.HighElf) self.assertIsInstance(char.race, race.HighElf)
# Check inspiration works # Check inspiration works
char.set_attrs(inspiration=True) char.set_attrs(inspiration=True)
@@ -50,16 +55,20 @@ class TestCharacter(TestCase):
def test_homebrew_spells(self): def test_homebrew_spells(self):
char = Character() char = Character()
class MySpell(spells.Spell): class MySpell(spells.Spell):
name="my spell!" name = "my spell!"
char.set_attrs(spells=(MySpell,)) char.set_attrs(spells=(MySpell,))
self.assertIsInstance(char.spells[0], spells.Spell) self.assertIsInstance(char.spells[0], spells.Spell)
self.assertEqual(char.spells[0].name, "my spell!") self.assertEqual(char.spells[0].name, "my spell!")
def test_homebrew_infusions(self): def test_homebrew_infusions(self):
char = Character(classes="artificer") char = Character(classes="artificer")
class MyInfusion(infusions.Infusion): class MyInfusion(infusions.Infusion):
name="my infusion!" name = "my infusion!"
# Pass an already created infusion class # Pass an already created infusion class
char.set_attrs(infusions=(MyInfusion,)) char.set_attrs(infusions=(MyInfusion,))
self.assertIsInstance(char.infusions[0], infusions.Infusion) self.assertIsInstance(char.infusions[0], infusions.Infusion)
@@ -74,14 +83,18 @@ class TestCharacter(TestCase):
# Test a well defined mechanic # Test a well defined mechanic
NewSpell = _resolve_mechanic("mage_hand", spells, None) NewSpell = _resolve_mechanic("mage_hand", spells, None)
self.assertTrue(issubclass(NewSpell, spells.Spell)) self.assertTrue(issubclass(NewSpell, spells.Spell))
# Test an unknown mechanic # Test an unknown mechanic
def new_spell(**params): def new_spell(**params):
return spells.Spell return spells.Spell
NewSpell = _resolve_mechanic("hocus_pocus", spells, spells.Spell) NewSpell = _resolve_mechanic("hocus_pocus", spells, spells.Spell)
self.assertTrue(issubclass(NewSpell, spells.Spell)) self.assertTrue(issubclass(NewSpell, spells.Spell))
# Test direct resolution of a proper subclass # Test direct resolution of a proper subclass
class MySpell(spells.Spell): class MySpell(spells.Spell):
pass pass
NewSpell = _resolve_mechanic(MySpell, spells, spells.Spell) NewSpell = _resolve_mechanic(MySpell, spells, spells.Spell)
def test_wield_weapon(self): def test_wield_weapon(self):
@@ -89,34 +102,34 @@ class TestCharacter(TestCase):
char.strength = 14 char.strength = 14
char.weapon_proficiencies = [Shortsword] char.weapon_proficiencies = [Shortsword]
# Add a weapon # Add a weapon
char.wield_weapon('shortsword') char.wield_weapon("shortsword")
self.assertEqual(len(char.weapons), 1) self.assertEqual(len(char.weapons), 1)
sword = char.weapons[0] sword = char.weapons[0]
self.assertTrue(isinstance(sword, Weapon)) self.assertTrue(isinstance(sword, Weapon))
self.assertTrue(isinstance(sword, Shortsword)) self.assertTrue(isinstance(sword, Shortsword))
self.assertEqual(sword.attack_modifier, 4) # str + prof self.assertEqual(sword.attack_modifier, 4) # str + prof
self.assertEqual(sword.damage, '1d6+2') # str self.assertEqual(sword.damage, "1d6+2") # str
# Check if dexterity is used if it's higher (Finesse weapon) # Check if dexterity is used if it's higher (Finesse weapon)
char.weapons = [] char.weapons = []
char.dexterity = 16 char.dexterity = 16
char.wield_weapon('shortsword') char.wield_weapon("shortsword")
sword = char.weapons[0] sword = char.weapons[0]
self.assertEqual(sword.attack_modifier, 5) # dex + prof self.assertEqual(sword.attack_modifier, 5) # dex + prof
# Check if race weapon proficiencies are considered # Check if race weapon proficiencies are considered
char.weapons = [] char.weapons = []
char.weapon_proficiencies = [] char.weapon_proficiencies = []
char.race = race.HighElf() char.race = race.HighElf()
char.wield_weapon('shortsword') char.wield_weapon("shortsword")
sword = char.weapons[0] sword = char.weapons[0]
self.assertEqual(sword.attack_modifier, 5) self.assertEqual(sword.attack_modifier, 5)
def test_str(self): def test_str(self):
char = Wizard(name="Inara") char = Wizard(name="Inara")
self.assertEqual(str(char), 'Inara') self.assertEqual(str(char), "Inara")
self.assertEqual(repr(char), '<Wizard: Inara>') self.assertEqual(repr(char), "<Wizard: Inara>")
def test_is_proficient(self): def test_is_proficient(self):
char = Character(classes=['Wizard']) char = Character(classes=["Wizard"])
char.weapon_proficiencies char.weapon_proficiencies
sword = Shortsword() sword = Shortsword()
# Check for not-proficient weapon # Check for not-proficient weapon
@@ -131,18 +144,26 @@ class TestCharacter(TestCase):
def test_proficiencies_text(self): def test_proficiencies_text(self):
char = Character() char = Character()
char._proficiencies_text = ('hello', 'world') char._proficiencies_text = ("hello", "world")
self.assertIn('hello', char.proficiencies_text.lower()) self.assertIn("hello", char.proficiencies_text.lower())
self.assertIn('world', char.proficiencies_text.lower()) self.assertIn("world", char.proficiencies_text.lower())
# Check for extra proficiencies # Check for extra proficiencies
char._proficiencies_text += ("it's", "me") char._proficiencies_text += ("it's", "me")
self.assertIn("it's", char.proficiencies_text.lower()) self.assertIn("it's", char.proficiencies_text.lower())
self.assertIn('me', char.proficiencies_text.lower()) self.assertIn("me", char.proficiencies_text.lower())
# Check that race proficienceis are included # Check that race proficienceis are included
elf = race.HighElf() elf = race.HighElf()
char.race = elf char.race = elf
expected = ("hello", "world", "longswords", "shortswords", "shortbows", expected = (
"longbows", "it's", "me") "hello",
"world",
"longswords",
"shortswords",
"shortbows",
"longbows",
"it's",
"me",
)
for e in expected: for e in expected:
self.assertIn(e, char.proficiencies_text.lower()) self.assertIn(e, char.proficiencies_text.lower())
@@ -184,7 +205,7 @@ class TestCharacter(TestCase):
def test_equip_armor(self): def test_equip_armor(self):
char = Character(dexterity=16) char = Character(dexterity=16)
char.wear_armor('leather armor') char.wear_armor("leather armor")
self.assertTrue(isinstance(char.armor, Armor)) self.assertTrue(isinstance(char.armor, Armor))
# Now make sure the armor class is correct # Now make sure the armor class is correct
self.assertEqual(char.armor_class, 14) self.assertEqual(char.armor_class, 14)
@@ -197,7 +218,7 @@ class TestCharacter(TestCase):
def test_wield_shield(self): def test_wield_shield(self):
char = Character(dexterity=16) char = Character(dexterity=16)
char.wield_shield('shield') char.wield_shield("shield")
self.assertTrue(isinstance(char.shield, Shield), msg=char.shield) self.assertTrue(isinstance(char.shield, Shield), msg=char.shield)
# Now make sure the armor class is correct # Now make sure the armor class is correct
self.assertEqual(char.armor_class, 15) self.assertEqual(char.armor_class, 15)
@@ -207,12 +228,12 @@ class TestCharacter(TestCase):
def test_speed(self): def test_speed(self):
# Check that the speed pulls from the character's race # Check that the speed pulls from the character's race
char = Character(race='lightfoot halfling') char = Character(race="lightfoot halfling")
self.assertEqual(char.speed, '25') self.assertEqual(char.speed, "25")
# Check that a character with no race defaults to 30 feet # Check that a character with no race defaults to 30 feet
char = Character() char = Character()
char.race = None char.race = None
self.assertEqual(char.speed, '30') self.assertEqual(char.speed, "30")
class DruidTestCase(TestCase): class DruidTestCase(TestCase):
@@ -221,9 +242,8 @@ class DruidTestCase(TestCase):
be ignored.""" be ignored."""
char = Druid() char = Druid()
with warnings.catch_warnings(): with warnings.catch_warnings():
warnings.filterwarnings('ignore', message="Druids cannot learn spells") warnings.filterwarnings("ignore", message="Druids cannot learn spells")
char.set_attrs(spells=['invisibility'], char.set_attrs(spells=["invisibility"], spells_prepared=["druidcraft"])
spells_prepared=['druidcraft'])
# self.assertEqual(len(char.spells), 1) # self.assertEqual(len(char.spells), 1)
self.assertEqual(len(char.spells), 2) self.assertEqual(len(char.spells), 2)
self.assertIn(spells.Druidcraft(), char.spells) self.assertIn(spells.Druidcraft(), char.spells)
@@ -233,32 +253,35 @@ class DruidTestCase(TestCase):
# Druid level 2 # Druid level 2
char.level = 2 char.level = 2
# Set reasonable wild shapes # Set reasonable wild shapes
char.wild_shapes = ['Wolf'] char.wild_shapes = ["Wolf"]
self.assertIsInstance(char.wild_shapes[0], monsters.Wolf) self.assertIsInstance(char.wild_shapes[0], monsters.Wolf)
# Check what happens if a non-existent wild_shape is added # Check what happens if a non-existent wild_shape is added
with self.assertRaises(exceptions.MonsterError): with self.assertRaises(exceptions.MonsterError):
char.wild_shapes = ['Wolf', 'Hyperion Loader'] char.wild_shapes = ["Wolf", "Hyperion Loader"]
# Check what happens if a valid monster class is directly added # Check what happens if a valid monster class is directly added
char.wild_shapes = [monsters.Wolf(), ] char.wild_shapes = [
monsters.Wolf(),
]
self.assertIsInstance(char.wild_shapes[0], monsters.Wolf) self.assertIsInstance(char.wild_shapes[0], monsters.Wolf)
# Check that invalid monsters aren't accepted # Check that invalid monsters aren't accepted
char.wild_shapes = ['Wolf', 'giant eagle'] char.wild_shapes = ["Wolf", "giant eagle"]
self.assertEqual(len(char.wild_shapes), 1) self.assertEqual(len(char.wild_shapes), 1)
self.assertIsInstance(char.wild_shapes[0], monsters.Wolf) self.assertIsInstance(char.wild_shapes[0], monsters.Wolf)
def test_moon_druid_wild_shapes(self): def test_moon_druid_wild_shapes(self):
# Moon druid level 2 gets beasts up to CR 1 # Moon druid level 2 gets beasts up to CR 1
char = Druid(level=2, wild_shapes=['Ape'], circle='moon') char = Druid(level=2, wild_shapes=["Ape"], circle="moon")
self.assertEqual(len(char.wild_shapes), 1) self.assertEqual(len(char.wild_shapes), 1)
self.assertIsInstance(char.wild_shapes[0], monsters.Ape) self.assertIsInstance(char.wild_shapes[0], monsters.Ape)
# Moon druid above level 6 gets beasts up to CR level / 3 # Moon druid above level 6 gets beasts up to CR level / 3
char = Druid(level=9, wild_shapes=['ankylosaurus'], circle='moon') char = Druid(level=9, wild_shapes=["ankylosaurus"], circle="moon")
self.assertEqual(len(char.wild_shapes), 1) self.assertEqual(len(char.wild_shapes), 1)
self.assertIsInstance(char.wild_shapes[0], monsters.Ankylosaurus) self.assertIsInstance(char.wild_shapes[0], monsters.Ankylosaurus)
def test_can_assume_shape(self): def test_can_assume_shape(self):
class Beast(monsters.Monster): class Beast(monsters.Monster):
description = 'beast' description = "beast"
new_druid = Druid(level=1) new_druid = Druid(level=1)
low_druid = Druid(level=2) low_druid = Druid(level=2)
mid_druid = Druid(level=4) mid_druid = Druid(level=4)
@@ -270,7 +293,7 @@ class DruidTestCase(TestCase):
self.assertTrue(low_druid.can_assume_shape(beast)) self.assertTrue(low_druid.can_assume_shape(beast))
# Check that challenge rating is checked # Check that challenge rating is checked
hard_beast = Beast() hard_beast = Beast()
hard_beast.challenge_rating = 1/2 hard_beast.challenge_rating = 1 / 2
really_hard_beast = Beast() really_hard_beast = Beast()
really_hard_beast.challenge_rating = 1 really_hard_beast.challenge_rating = 1
self.assertFalse(low_druid.can_assume_shape(hard_beast)) self.assertFalse(low_druid.can_assume_shape(hard_beast))
+4 -4
View File
@@ -3,16 +3,16 @@ from unittest import TestCase
from dungeonsheets.exceptions import DiceError from dungeonsheets.exceptions import DiceError
from dungeonsheets import dice from dungeonsheets import dice
class TestDice(TestCase):
class TestDice(TestCase):
def test_read_dice_str(self): def test_read_dice_str(self):
out = dice.read_dice_str('1d6') out = dice.read_dice_str("1d6")
self.assertEqual(out.faces, 6) self.assertEqual(out.faces, 6)
self.assertEqual(out.num, 1) self.assertEqual(out.num, 1)
# Multiple digits # Multiple digits
out = dice.read_dice_str('15d10') out = dice.read_dice_str("15d10")
self.assertEqual(out.faces, 10) self.assertEqual(out.faces, 10)
self.assertEqual(out.num, 15) self.assertEqual(out.num, 15)
# Check a bad value # Check a bad value
with self.assertRaises(DiceError): with self.assertRaises(DiceError):
dice.read_dice_str('Ed15') dice.read_dice_str("Ed15")
+10 -5
View File
@@ -8,13 +8,18 @@ from dungeonsheets.features import create_feature, Feature, all_features
class TestFeatures(TestCase): class TestFeatures(TestCase):
"""Tests for features and feature-related activities.""" """Tests for features and feature-related activities."""
def test_all_features(self): def test_all_features(self):
# Make sure only features are returned # Make sure only features are returned
for ThisFeature in all_features(): for ThisFeature in all_features():
self.assertTrue(isinstance(ThisFeature, type), self.assertTrue(
f"``all_features`` returned {ThisFeature} (not a class)") isinstance(ThisFeature, type),
self.assertTrue(issubclass(ThisFeature, Feature), f"``all_features`` returned {ThisFeature} (not a class)",
f"``all_features`` returned {ThisFeature} (not a feature)") )
self.assertTrue(
issubclass(ThisFeature, Feature),
f"``all_features`` returned {ThisFeature} (not a feature)",
)
# Pick a couple of known features to spot-check for # Pick a couple of known features to spot-check for
all_the_features = list(all_features()) all_the_features = list(all_features())
self.assertIn(features.FalseIdentity, all_the_features) self.assertIn(features.FalseIdentity, all_the_features)
@@ -23,6 +28,6 @@ class TestFeatures(TestCase):
def test_create_feature(self): def test_create_feature(self):
NewFeature = create_feature(name="Hello world") NewFeature = create_feature(name="Hello world")
self.assertTrue(issubclass(NewFeature, Feature)) self.assertTrue(issubclass(NewFeature, Feature))
self.assertEqual(NewFeature.name, 'Hello world') self.assertEqual(NewFeature.name, "Hello world")
feature = NewFeature() feature = NewFeature()
print(feature, feature.__class__, type(feature)) print(feature, feature.__class__, type(feature))
+34 -30
View File
@@ -1,53 +1,56 @@
import unittest import unittest
from dungeonsheets import make_sheets, character, spells, features, latex from dungeonsheets import spells, features, latex
class MarkdownTestCase(unittest.TestCase): class MarkdownTestCase(unittest.TestCase):
"""Check that conversion of markdown formats to LaTeX code works """Check that conversion of markdown formats to LaTeX code works
correctly.""" correctly."""
def test_rst_bold(self): def test_rst_bold(self):
text = latex.rst_to_latex('**hello**') text = latex.rst_to_latex("**hello**")
self.assertEqual(text, '\n\\textbf{hello}\n') self.assertEqual(text, "\n\\textbf{hello}\n")
def test_hit_dice(self): def test_hit_dice(self):
text = latex.rst_to_latex('1d6+3') text = latex.rst_to_latex("1d6+3")
self.assertEqual(text.strip("\n"), '\\texttt{1d6+3}') self.assertEqual(text.strip("\n"), "\\texttt{1d6+3}")
def test_no_text(self): def test_no_text(self):
text = latex.rst_to_latex(None) text = latex.rst_to_latex(None)
self.assertEqual(text, '') self.assertEqual(text, "")
def test_verbatim(self): def test_verbatim(self):
text = latex.rst_to_latex('``hello, world``') text = latex.rst_to_latex("``hello, world``")
self.assertIn(r'\texttt{hello, world}', text) self.assertIn(r"\texttt{hello, world}", text)
def test_literal_backslash(self): def test_literal_backslash(self):
text = latex.rst_to_latex(r'\\') text = latex.rst_to_latex(r"\\")
self.assertEqual(r'\textbackslash{}', text.strip("\n")) self.assertEqual(r"\textbackslash{}", text.strip("\n"))
@unittest.skip("Headings are all screwed up because it treats them as the document title") @unittest.skip(
"Headings are all screwed up because it treats them as the document title"
)
def test_headings(self): def test_headings(self):
# Simple heading by itself # Simple heading by itself
text = latex.rst_to_latex('Hello, world\n------------\n\nGoodbye, world') text = latex.rst_to_latex("Hello, world\n------------\n\nGoodbye, world")
self.assertEqual('\\section*{Hello, world}\n', text) self.assertEqual("\\section*{Hello, world}\n", text)
# Simple heading with leading whitespace # Simple heading with leading whitespace
text = latex.rst_to_latex(' Hello, world\n ============\n') text = latex.rst_to_latex(" Hello, world\n ============\n")
self.assertEqual('\\section*{Hello, world}\n', text) self.assertEqual("\\section*{Hello, world}\n", text)
# Heading with text after it # Heading with text after it
text = latex.rst_to_latex('Hello, world\n============\n\nThis is some text') text = latex.rst_to_latex("Hello, world\n============\n\nThis is some text")
self.assertEqual('\\section*{Hello, world}\n\nThis is some text', text) self.assertEqual("\\section*{Hello, world}\n\nThis is some text", text)
# Heading with text before it # Heading with text before it
text = latex.rst_to_latex('This is a paragraph\n\nHello, world\n============\n') text = latex.rst_to_latex("This is a paragraph\n\nHello, world\n============\n")
self.assertEqual('This is a paragraph\n\n\\section*{Hello, world}\n', text) self.assertEqual("This is a paragraph\n\n\\section*{Hello, world}\n", text)
# Check that levels of headings are parsed appropriately # Check that levels of headings are parsed appropriately
text = latex.rst_to_latex('Hello, world\n^^^^^^^^^^^^\n') text = latex.rst_to_latex("Hello, world\n^^^^^^^^^^^^\n")
self.assertEqual('\\subsubsection*{Hello, world}\n', text) self.assertEqual("\\subsubsection*{Hello, world}\n", text)
text = latex.rst_to_latex('Hello, world\n^^^^^^^^^^^^\n', top_heading_level=3) text = latex.rst_to_latex("Hello, world\n^^^^^^^^^^^^\n", top_heading_level=3)
self.assertEqual('\\subparagraph*{Hello, world}\n', text) self.assertEqual("\\subparagraph*{Hello, world}\n", text)
# This is a bad heading missing with all the underline on one line # This is a bad heading missing with all the underline on one line
text = latex.rst_to_latex('Hello, world^^^^^^^^^^^^\n') text = latex.rst_to_latex("Hello, world^^^^^^^^^^^^\n")
self.assertEqual('Hello, world\\^\\^\\^\\^\\^\\^\\^\\^\\^\\^\\^\\^\n', text) self.assertEqual("Hello, world\\^\\^\\^\\^\\^\\^\\^\\^\\^\\^\\^\\^\n", text)
def test_bullet_list(self): def test_bullet_list(self):
tex = latex.rst_to_latex("\n- Hello\n- World\n\n") tex = latex.rst_to_latex("\n- Hello\n- World\n\n")
@@ -104,12 +107,13 @@ class MarkdownTestCase(unittest.TestCase):
def test_rst_all_spells(self): def test_rst_all_spells(self):
for spell in spells.all_spells(): for spell in spells.all_spells():
tex = latex.rst_to_latex(spell.__doc__) tex = latex.rst_to_latex(spell.__doc__)
self.assertNotIn("DUadmonition", tex, self.assertNotIn(
f"spell {spell} is not valid reStructured text") "DUadmonition", tex, f"spell {spell} is not valid reStructured text"
)
def test_rst_all_features(self): def test_rst_all_features(self):
for feature in features.all_features(): for feature in features.all_features():
tex = latex.rst_to_latex(feature.__doc__) tex = latex.rst_to_latex(feature.__doc__)
self.assertNotIn("DUadmonition", tex, self.assertNotIn(
f"feature {feature} is not valid reStructured text") "DUadmonition", tex, f"feature {feature} is not valid reStructured text"
)
+11 -13
View File
@@ -1,38 +1,36 @@
import unittest import unittest
import os import os
from pathlib import Path from pathlib import Path
import subprocess
from dungeonsheets import make_sheets, character from dungeonsheets import make_sheets, character
EG_DIR = os.path.abspath(os.path.join(os.path.split(__file__)[0], '../examples/')) EG_DIR = os.path.abspath(os.path.join(os.path.split(__file__)[0], "../examples/"))
CHARFILE = os.path.join(EG_DIR, 'rogue1.py') CHARFILE = os.path.join(EG_DIR, "rogue1.py")
class CharacterFileTestCase(unittest.TestCase): class CharacterFileTestCase(unittest.TestCase):
example_dir = Path(__file__).parent.parent / "examples" example_dir = Path(__file__).parent.parent / "examples"
# def test_(self):
# print(self.example_dir)
# subprocess.run(['makesheets', self.example_dir])
def test_load_character_file(self): def test_load_character_file(self):
charfile = CHARFILE charfile = CHARFILE
result = make_sheets.load_character_file(charfile) result = make_sheets.load_character_file(charfile)
self.assertEqual(result['strength'], 10) self.assertEqual(result["strength"], 10)
class PdfOutputTeestCase(unittest.TestCase): class PdfOutputTeestCase(unittest.TestCase):
basename = 'clara' basename = "clara"
def tearDown(self): def tearDown(self):
temp_files = [f'{self.basename}.pdf'] temp_files = [f"{self.basename}.pdf"]
for f in temp_files: for f in temp_files:
if os.path.exists(f): if os.path.exists(f):
os.remove(f) os.remove(f)
def test_file_created(self): def test_file_created(self):
# Check that a file is created once the function is run # Check that a file is created once the function is run
pdf_name = f'{self.basename}.pdf' pdf_name = f"{self.basename}.pdf"
# self.assertFalse(os.path.exists(pdf_name), f'{pdf_name} already exists.') # self.assertFalse(os.path.exists(pdf_name), f'{pdf_name} already exists.')
char = character.Character(name='Clara') char = character.Character(name="Clara")
char.saving_throw_proficiencies = ['strength'] char.saving_throw_proficiencies = ["strength"]
make_sheets.create_character_pdf(character=char, basename=self.basename) make_sheets.create_character_pdf(character=char, basename=self.basename)
self.assertTrue(os.path.exists(pdf_name), f'{pdf_name} not created.') self.assertTrue(os.path.exists(pdf_name), f"{pdf_name} not created.")
+1 -1
View File
@@ -2,7 +2,7 @@
from unittest import TestCase from unittest import TestCase
from dungeonsheets import monsters, exceptions from dungeonsheets import monsters
class MonsterTestCase(TestCase): class MonsterTestCase(TestCase):
+24 -28
View File
@@ -1,38 +1,39 @@
#!/usr/bin/env python #!/usr/bin/env python
from unittest import TestCase from unittest import TestCase
import warnings
from dungeonsheets import race, monsters, exceptions, spells
from dungeonsheets.character import Character from dungeonsheets.character import Character
from dungeonsheets.weapons import Weapon, Shortsword from dungeonsheets.weapons import Shortsword
from dungeonsheets.armor import Armor, LeatherArmor, Shield
class TestMulticlass(TestCase): class TestMulticlass(TestCase):
""" """
Tests for Multiclass character. Tests for Multiclass character.
""" """
def test_constructor(self): def test_constructor(self):
char = Character(name='Multiclass', char = Character(
classes=['wizard', 'fighter'], name="Multiclass", classes=["wizard", "fighter"], levels=[5, 4]
levels=[5, 4]) )
self.assertIsInstance(char, Character)
def test_level(self): def test_level(self):
char = Character(name='Multiclass', char = Character(
classes=['wizard', 'fighter'], name="Multiclass", classes=["wizard", "fighter"], levels=[5, 4]
levels=[5, 4]) )
self.assertEqual(char.level, 9) self.assertEqual(char.level, 9)
def test_spellcasting(self): def test_spellcasting(self):
char = Character(name='Multiclass', char = Character(
classes=['wizard', 'fighter'], name="Multiclass", classes=["wizard", "fighter"], levels=[5, 4]
levels=[5, 4]) )
self.assertEqual(len(char.spellcasting_classes), 1) self.assertEqual(len(char.spellcasting_classes), 1)
char = Character(name='Multiclass', char = Character(
classes=['wizard', 'fighter'], name="Multiclass",
subclasses=[None, 'Eldritch Knight'], classes=["wizard", "fighter"],
levels=[5, 4]) subclasses=[None, "Eldritch Knight"],
levels=[5, 4],
)
self.assertEqual(len(char.spellcasting_classes), 2) self.assertEqual(len(char.spellcasting_classes), 2)
# equivalent spellcasting level: 6 # equivalent spellcasting level: 6
self.assertEqual(char.spell_slots(spell_level=1), 4) self.assertEqual(char.spell_slots(spell_level=1), 4)
@@ -41,20 +42,15 @@ class TestMulticlass(TestCase):
self.assertEqual(char.spell_slots(spell_level=4), 0) self.assertEqual(char.spell_slots(spell_level=4), 0)
def test_proficiencies(self): def test_proficiencies(self):
char1 = Character(name='Multiclass', char1 = Character(
classes=['wizard', 'fighter'], name="Multiclass", classes=["wizard", "fighter"], levels=[5, 4]
levels=[5, 4]) )
for svt in ('intelligence', 'wisdom'): for svt in ("intelligence", "wisdom"):
self.assertIn(svt, char1.saving_throw_proficiencies) self.assertIn(svt, char1.saving_throw_proficiencies)
char2 = Character(name='Multiclass', char2 = Character(name="Multiclass", classes=["wizard", "rogue"], levels=[5, 4])
classes=['wizard', 'rogue'], char3 = Character(name="Multiclass", classes=["rogue", "wizard"], levels=[4, 5])
levels=[5, 4])
char3 = Character(name='Multiclass',
classes=['rogue', 'wizard'],
levels=[4, 5])
sword = Shortsword() sword = Shortsword()
self.assertTrue(char1.is_proficient(sword)) self.assertTrue(char1.is_proficient(sword))
# multiclassing into Rogue doesn't give simple weapon proficiency # multiclassing into Rogue doesn't give simple weapon proficiency
self.assertFalse(char2.is_proficient(sword)) self.assertFalse(char2.is_proficient(sword))
self.assertTrue(char3.is_proficient(sword)) self.assertTrue(char3.is_proficient(sword))
+55 -20
View File
@@ -6,21 +6,22 @@ import types
from dungeonsheets.readers import read_character_file from dungeonsheets.readers import read_character_file
EG_DIR = (Path(__file__).parent.parent / "examples").resolve() EG_DIR = (Path(__file__).parent.parent / "examples").resolve()
CHAR_PYTHON_FILE = EG_DIR / 'rogue1.py' CHAR_PYTHON_FILE = EG_DIR / "rogue1.py"
CHAR_JSON_FILE = EG_DIR / 'barbarian3.json' CHAR_JSON_FILE = EG_DIR / "barbarian3.json"
SPELLCASTER_JSON_FILE = EG_DIR / 'artificer2.json' SPELLCASTER_JSON_FILE = EG_DIR / "artificer2.json"
class PythonReaderTests(unittest.TestCase): class PythonReaderTests(unittest.TestCase):
def test_load_python_file(self): def test_load_python_file(self):
charfile = CHAR_PYTHON_FILE charfile = CHAR_PYTHON_FILE
result = read_character_file(charfile) result = read_character_file(charfile)
self.assertEqual(result['strength'], 10) self.assertEqual(result["strength"], 10)
class JSONReaderTests(unittest.TestCase): class JSONReaderTests(unittest.TestCase):
def test_load_json_file(self): def test_load_json_file(self):
charfile = CHAR_JSON_FILE charfile = CHAR_JSON_FILE
with warnings.catch_warnings(record=True) as w: with warnings.catch_warnings(record=True):
result = read_character_file(charfile) result = read_character_file(charfile)
expected_data = dict( expected_data = dict(
name="Ulthar Jenkins", name="Ulthar Jenkins",
@@ -35,9 +36,22 @@ class JSONReaderTests(unittest.TestCase):
constitution=19, constitution=19,
intelligence=8, intelligence=8,
hp_max=32, hp_max=32,
skill_proficiencies=["athletics", "survival",], skill_proficiencies=[
weapon_proficiencies=["simple weapons", "martial weapons", "battleaxe", "handaxe", "light hammer", "warhammer", "unarmed strike",], "athletics",
_proficiencies_text=["Brewer's Supplies",], "survival",
],
weapon_proficiencies=[
"simple weapons",
"martial weapons",
"battleaxe",
"handaxe",
"light hammer",
"warhammer",
"unarmed strike",
],
_proficiencies_text=[
"Brewer's Supplies",
],
languages="common, dwarvish", languages="common, dwarvish",
cp=26, cp=26,
sp=55, sp=55,
@@ -48,13 +62,17 @@ class JSONReaderTests(unittest.TestCase):
magic_items=(), magic_items=(),
armor="", armor="",
shield="", shield="",
personality_traits="Can easily dismember a body\n\nKnow fight battle tactics", personality_traits=(
"Can easily dismember a body\n\nKnow fight battle tactics"
),
ideals="Vengence", ideals="Vengence",
bonds="friends and adventurers.", bonds="friends and adventurers.",
flaws="Bloodthirsty and wants to solve every problem by murder", flaws="Bloodthirsty and wants to solve every problem by murder",
equipment=("warhammer, handaxe, explorer's pack, javelin (4), backpack, " equipment=(
"bedroll, mess kit, tinderbox, torch (10), rations (10), " "warhammer, handaxe, explorer's pack, javelin (4), backpack, "
"waterskin, hempen rope"), "bedroll, mess kit, tinderbox, torch (10), rations (10), "
"waterskin, hempen rope"
),
attacks_and_spellcasting="", attacks_and_spellcasting="",
spells_prepared=[], spells_prepared=[],
spells=[], spells=[],
@@ -68,16 +86,33 @@ class JSONReaderTests(unittest.TestCase):
def test_load_json_spells(self): def test_load_json_spells(self):
charfile = SPELLCASTER_JSON_FILE charfile = SPELLCASTER_JSON_FILE
with warnings.catch_warnings(record=True) as w: with warnings.catch_warnings(record=True):
result = read_character_file(charfile) result = read_character_file(charfile)
expected_data = dict( expected_data = dict(
spells_prepared=["cure wounds",], spells_prepared=[
spells=["spare the dying", "fire bolt", "absorb elements", "cure wounds",
"alarm", "catapult", "cure wounds", "detect magic", ],
"disguise self", "expeditious retreat", "faerie fire", spells=[
"false life", "feather fall", "grease", "identify", "spare the dying",
"jump", "longstrider", "purify food and drink", "fire bolt",
"sanctuary", "snare",], "absorb elements",
"alarm",
"catapult",
"cure wounds",
"detect magic",
"disguise self",
"expeditious retreat",
"faerie fire",
"false life",
"feather fall",
"grease",
"identify",
"jump",
"longstrider",
"purify food and drink",
"sanctuary",
"snare",
],
) )
for key, val in expected_data.items(): for key, val in expected_data.items():
this_result = result[key] this_result = result[key]
+13 -8
View File
@@ -8,13 +8,18 @@ from dungeonsheets.spells import create_spell, Spell, all_spells
class TestSpells(TestCase): class TestSpells(TestCase):
"""Tests for spells and spell-related activities.""" """Tests for spells and spell-related activities."""
def test_all_spells(self): def test_all_spells(self):
# Make sure only spells are returned # Make sure only spells are returned
for ThisSpell in all_spells(): for ThisSpell in all_spells():
self.assertTrue(isinstance(ThisSpell, type), self.assertTrue(
f"``all_spells`` returned {ThisSpell} (not a class)") isinstance(ThisSpell, type),
self.assertTrue(issubclass(ThisSpell, Spell), f"``all_spells`` returned {ThisSpell} (not a class)",
f"``all_spells`` returned {ThisSpell} (not a spell)") )
self.assertTrue(
issubclass(ThisSpell, Spell),
f"``all_spells`` returned {ThisSpell} (not a spell)",
)
# Pick a couple of known spells to spot-check for # Pick a couple of known spells to spot-check for
all_the_spells = list(all_spells()) all_the_spells = list(all_spells())
self.assertIn(spells.MagicMissile, all_the_spells) self.assertIn(spells.MagicMissile, all_the_spells)
@@ -23,17 +28,17 @@ class TestSpells(TestCase):
def test_create_spell(self): def test_create_spell(self):
NewSpell = create_spell(name="Hello world") NewSpell = create_spell(name="Hello world")
self.assertTrue(issubclass(NewSpell, Spell)) self.assertTrue(issubclass(NewSpell, Spell))
self.assertEqual(NewSpell.name, 'Hello world') self.assertEqual(NewSpell.name, "Hello world")
spell = NewSpell() spell = NewSpell()
print(spell, spell.__class__, type(spell)) print(spell, spell.__class__, type(spell))
def test_spell_str(self): def test_spell_str(self):
spell = Spell() spell = Spell()
spell.name = "My spell" spell.name = "My spell"
self.assertEqual(str(spell), 'My spell') self.assertEqual(str(spell), "My spell")
# Try with a ritual # Try with a ritual
spell.ritual = True spell.ritual = True
self.assertEqual(str(spell), 'My spell (R)') self.assertEqual(str(spell), "My spell (R)")
# Try with a ritual and a concentration # Try with a ritual and a concentration
spell.concentration = True spell.concentration = True
self.assertEqual(str(spell), 'My spell (R, C)') self.assertEqual(str(spell), "My spell (R, C)")
+23 -18
View File
@@ -2,27 +2,29 @@ from unittest import TestCase
from dungeonsheets import stats, character from dungeonsheets import stats, character
class TestStats(TestCase):
class TestStats(TestCase):
def test_mod_str(self): def test_mod_str(self):
self.assertEqual(stats.mod_str(-3), '-3') self.assertEqual(stats.mod_str(-3), "-3")
self.assertEqual(stats.mod_str(0), '+0') self.assertEqual(stats.mod_str(0), "+0")
self.assertEqual(stats.mod_str(2), '+2') self.assertEqual(stats.mod_str(2), "+2")
def test_saving_throw(self): def test_saving_throw(self):
# Try it with an ST proficiency # Try it with an ST proficiency
class MyClass(character.Character): class MyClass(character.Character):
saving_throw_proficiencies = ['strength'] saving_throw_proficiencies = ["strength"]
proficiency_bonus = 2 proficiency_bonus = 2
strength = stats.Ability(14) strength = stats.Ability(14)
my_class = MyClass() my_class = MyClass()
self.assertEqual(my_class.strength.saving_throw, 4) self.assertEqual(my_class.strength.saving_throw, 4)
def test_modifier(self): def test_modifier(self):
class MyCharacter(character.Character): class MyCharacter(character.Character):
saving_throw_proficiencies = ['strength'] saving_throw_proficiencies = ["strength"]
proficiency_bonus = 2 proficiency_bonus = 2
strength = stats.Ability(14) strength = stats.Ability(14)
my_char = MyCharacter() my_char = MyCharacter()
ranges = [ ranges = [
((1,), -5), ((1,), -5),
@@ -47,7 +49,9 @@ class TestStats(TestCase):
for value in range_: for value in range_:
my_char.strength = value my_char.strength = value
stat = my_char.strength stat = my_char.strength
msg = f"Stat {value} doesn't produce modifier {target} ({stat.modifier})" msg = (
f"Stat {value} doesn't produce modifier {target} ({stat.modifier})"
)
self.assertEqual(stat.modifier, target, msg) self.assertEqual(stat.modifier, target, msg)
def test_setter(self): def test_setter(self):
@@ -55,6 +59,7 @@ class TestStats(TestCase):
# Set up a dummy class # Set up a dummy class
class MyCharacter(character.Character): class MyCharacter(character.Character):
stat = stats.Ability() stat = stats.Ability()
char = MyCharacter() char = MyCharacter()
# Check that the stat works as expected once set # Check that the stat works as expected once set
char.stat = 15 char.stat = 15
@@ -63,30 +68,30 @@ class TestStats(TestCase):
def test_skill(self): def test_skill(self):
"""Test for a skill, that depends on another ability.""" """Test for a skill, that depends on another ability."""
class MyClass(character.Character): class MyClass(character.Character):
dexterity = stats.Ability(14) dexterity = stats.Ability(14)
acrobatics = stats.Skill(ability='dexterity') acrobatics = stats.Skill(ability="dexterity")
skill_proficiencies = [] skill_proficiencies = []
proficiency_bonus = 2 proficiency_bonus = 2
my_class = MyClass() my_class = MyClass()
self.assertEqual(my_class.acrobatics, 2) self.assertEqual(my_class.acrobatics, 2)
# Check for a proficiency # Check for a proficiency
my_class.skill_proficiencies = ['acrobatics'] my_class.skill_proficiencies = ["acrobatics"]
self.assertEqual(my_class.acrobatics, 4) self.assertEqual(my_class.acrobatics, 4)
def test_findattr(self): def test_findattr(self):
"""Check if the function can find attributes.""" """Check if the function can find attributes."""
class TestClass():
class TestClass:
my_attr = 47 my_attr = 47
YourAttr = 53 YourAttr = 53
test_class = TestClass() test_class = TestClass()
# Direct access # Direct access
self.assertEqual(stats.findattr(test_class, 'my_attr'), self.assertEqual(stats.findattr(test_class, "my_attr"), test_class.my_attr)
test_class.my_attr) self.assertEqual(stats.findattr(test_class, "YourAttr"), test_class.YourAttr)
self.assertEqual(stats.findattr(test_class, 'YourAttr'),
test_class.YourAttr)
# Swapping spaces for capitalization # Swapping spaces for capitalization
self.assertEqual(stats.findattr(test_class, 'my attr'), self.assertEqual(stats.findattr(test_class, "my attr"), test_class.my_attr)
test_class.my_attr) self.assertEqual(stats.findattr(test_class, "your attr"), test_class.YourAttr)
self.assertEqual(stats.findattr(test_class, 'your attr'),
test_class.YourAttr)
+3 -3
View File
@@ -6,8 +6,8 @@ from dungeonsheets.weapons import Weapon
class WeaponTestCase(unittest.TestCase): class WeaponTestCase(unittest.TestCase):
def test_weapon_damage(self): def test_weapon_damage(self):
weapon = Weapon() weapon = Weapon()
weapon.base_damage = '1d6' weapon.base_damage = "1d6"
self.assertEqual(weapon.damage, '1d6') self.assertEqual(weapon.damage, "1d6")
# Now add some bonus damage # Now add some bonus damage
weapon.damage_bonus = 2 weapon.damage_bonus = 2
self.assertEqual(weapon.damage, '1d6+2') self.assertEqual(weapon.damage, "1d6+2")