Skip to content

Commit

Permalink
106 Add test for print_dice() and calc_action(); Fix bugs in calc_act…
Browse files Browse the repository at this point in the history
…ion()
  • Loading branch information
CJ Holmes committed Mar 26, 2024
1 parent d97d482 commit 4645006
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
5 changes: 3 additions & 2 deletions mvkroller.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,10 @@ def adv_disadv(advantage, disadvantage, dicecounts, dicerolls):


def calc_action(fortunedicerolls, characterdicerolls):
"""Compute the action total, using up to one d20 and the highest character die roll."""
"""Compute the action total, using the highest two rolls. Up to one fortune die may be used."""
try:
action_dice = fortunedicerolls + characterdicerolls
action_dice = sorted(fortunedicerolls,reverse=True)[:1]
action_dice += characterdicerolls
action_dice.sort(reverse=True)
action_dice = action_dice[:2]
answer = f"**Action Total: {str(sum(action_dice))}** {str(action_dice)}\n"
Expand Down
49 changes: 49 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,54 @@ def test_roll_dice_exc(self):
with self.subTest(dice_count=dcount), self.assertRaises(roller.RollError):
roller.roll_dice(dcount)

def test_print_dice(self):
"""Print some valid die rolls."""
dataset = [
[{}, "Dice: \n"],
[{20:[5,12]}, "Dice: 2d20[5, 12] \n"],
[{20:[], 12:[1,12], 8:[1,2,3,4], 4:[3,2]},
"Dice: 2d12[1, 12] 4d8[1, 2, 3, 4] 2d4[3, 2] \n"
],
]
for (rolls, result) in dataset:
with self.subTest(rolls=rolls):
self.assertEqual(roller.print_dice(rolls), result)

def test_print_dice_exc(self):
"""print_dice can raise exceptions when the input is not a dictionary."""
dataset = [
[20, 1],
"nobody",
99,
]
for bad_roll in dataset:
with self.subTest(bad_roll=bad_roll), self.assertRaises(roller.RollError):
roller.print_dice(bad_roll)

def test_calc_action(self):
"""Ensure actions are tallied properly."""
dataset = [
[[1, 20], [2, 4, 6, 8], "**Action Total: 28** [20, 8]\n"],
[[20, 1], [8, 6, 4, 2], "**Action Total: 28** [20, 8]\n"],
[[11, 13, 5], [8, 6, 4, 2], "**Action Total: 21** [13, 8]\n"],
[[6, 7, 8], [9, 10, 11, 12], "**Action Total: 23** [12, 11]\n"],
]
for (fdice, cdice, answer) in dataset:
with self.subTest(dice=[fdice, cdice]):
self.assertEqual(roller.calc_action(fdice, cdice), answer)

def test_calc_actions_exc(self):
"""Give calc_actions() some bad data"""
dataset = [
[None, None],
["boopsie", "whoopsie"],
[12, 99],
[[1, 2], "grrrrr"],
["grrrr", [1, 2]],
]
for (fdice, cdice) in dataset:
with self.subTest(dice=[fdice, cdice]), self.assertRaises(roller.RollError):
roller.calc_action(fdice, cdice)

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

0 comments on commit 4645006

Please sign in to comment.