107 lines
4.9 KiB
Python
107 lines
4.9 KiB
Python
"""Tests for dice operations."""
|
|
from unittest import mock
|
|
|
|
from django.test import TestCase
|
|
|
|
import dice.lib
|
|
|
|
|
|
class DiceLibTestCase(TestCase):
|
|
"""Test that a variety of dice rolls work as expected."""
|
|
|
|
def test_cypher_rolls(self):
|
|
"""Roll a variety of Cypher System rolls."""
|
|
# simple task, simple check
|
|
with mock.patch('random.SystemRandom.randint', return_value=5):
|
|
result = dice.lib.cypher_roll(difficulty=1)
|
|
self.assertEqual(result, (5, 1, True, None))
|
|
|
|
# simple failure
|
|
with mock.patch('random.SystemRandom.randint', return_value=2):
|
|
result = dice.lib.cypher_roll(difficulty=1)
|
|
self.assertEqual(result, (2, 0, False, None))
|
|
|
|
# rolled a 1
|
|
with mock.patch('random.SystemRandom.randint', return_value=1):
|
|
result = dice.lib.cypher_roll(difficulty=1)
|
|
self.assertEqual(result, (1, None, False, 'a GM intrusion'))
|
|
|
|
# rolled a 17 on an attack
|
|
with mock.patch('random.SystemRandom.randint', return_value=17):
|
|
result = dice.lib.cypher_roll(difficulty=1, is_attack=True)
|
|
self.assertEqual(result, (17, 5, True, '+1 damage'))
|
|
|
|
# rolled a 18 on an attack
|
|
with mock.patch('random.SystemRandom.randint', return_value=18):
|
|
result = dice.lib.cypher_roll(difficulty=1, is_attack=True)
|
|
self.assertEqual(result, (18, 6, True, '+2 damage'))
|
|
|
|
# rolled a 17
|
|
with mock.patch('random.SystemRandom.randint', return_value=17):
|
|
result = dice.lib.cypher_roll(difficulty=1)
|
|
self.assertEqual(result, (17, 5, True, None))
|
|
|
|
# rolled a 18
|
|
with mock.patch('random.SystemRandom.randint', return_value=18):
|
|
result = dice.lib.cypher_roll(difficulty=1)
|
|
self.assertEqual(result, (18, 6, True, None))
|
|
|
|
# rolled a 19
|
|
with mock.patch('random.SystemRandom.randint', return_value=19):
|
|
result = dice.lib.cypher_roll(difficulty=1)
|
|
self.assertEqual(result, (19, 6, True, 'a minor effect'))
|
|
|
|
# rolled a 20
|
|
with mock.patch('random.SystemRandom.randint', return_value=20):
|
|
result = dice.lib.cypher_roll(difficulty=1)
|
|
self.assertEqual(result, (20, 6, True, 'a MAJOR EFFECT'))
|
|
|
|
# mods affect the result of what the roll beats
|
|
with mock.patch('random.SystemRandom.randint', return_value=2):
|
|
result = dice.lib.cypher_roll(difficulty=1, mods='-5')
|
|
self.assertEqual(result, (2, 5, True, None))
|
|
|
|
# complex mods
|
|
with mock.patch('random.SystemRandom.randint', return_value=2):
|
|
result = dice.lib.cypher_roll(difficulty=3, mods='+1-4')
|
|
self.assertEqual(result, (2, 3, True, None))
|
|
|
|
# complex mods
|
|
with mock.patch('random.SystemRandom.randint', return_value=2):
|
|
result = dice.lib.cypher_roll(difficulty=3, mods='-4+1')
|
|
self.assertEqual(result, (2, 3, True, None))
|
|
|
|
# ...even without a difficulty known
|
|
with mock.patch('random.SystemRandom.randint', return_value=2):
|
|
result = dice.lib.cypher_roll(mods='-5')
|
|
self.assertEqual(result, (2, 5, None, None))
|
|
|
|
# general "don't know the difficulty" kind of check
|
|
with mock.patch('random.SystemRandom.randint', return_value=10):
|
|
result = dice.lib.cypher_roll(mods='-2')
|
|
self.assertEqual(result, (10, 5, None, None))
|
|
|
|
# general "don't know the difficulty" kind of check in the other direction
|
|
with mock.patch('random.SystemRandom.randint', return_value=10):
|
|
result = dice.lib.cypher_roll(mods='2')
|
|
self.assertEqual(result, (10, 1, None, None))
|
|
|
|
def test_reaction_roll(self):
|
|
"""Roll possible reactions."""
|
|
with mock.patch('random.SystemRandom.randint', return_value=1):
|
|
self.assertEqual(dice.lib.reaction_roll(), (1, 'VERY negative', '--'))
|
|
with mock.patch('random.SystemRandom.randint', return_value=2):
|
|
self.assertEqual(dice.lib.reaction_roll(), (2, 'negative', '-'))
|
|
with mock.patch('random.SystemRandom.randint', return_value=6):
|
|
self.assertEqual(dice.lib.reaction_roll(), (6, 'negative', '-'))
|
|
with mock.patch('random.SystemRandom.randint', return_value=7):
|
|
self.assertEqual(dice.lib.reaction_roll(), (7, 'positive, with complications', '~'))
|
|
with mock.patch('random.SystemRandom.randint', return_value=14):
|
|
self.assertEqual(dice.lib.reaction_roll(), (14, 'positive, with complications', '~'))
|
|
with mock.patch('random.SystemRandom.randint', return_value=15):
|
|
self.assertEqual(dice.lib.reaction_roll(), (15, 'positive', '+'))
|
|
with mock.patch('random.SystemRandom.randint', return_value=19):
|
|
self.assertEqual(dice.lib.reaction_roll(), (19, 'positive', '+'))
|
|
with mock.patch('random.SystemRandom.randint', return_value=20):
|
|
self.assertEqual(dice.lib.reaction_roll(), (20, 'VERY positive', '++'))
|