Skip to content

Instantly share code, notes, and snippets.

@linuxdaemon
Created March 19, 2019 00:32
Show Gist options
  • Save linuxdaemon/29cad9c3371f1e9182f3686d63a72170 to your computer and use it in GitHub Desktop.
Save linuxdaemon/29cad9c3371f1e9182f3686d63a72170 to your computer and use it in GitHub Desktop.
import re
import pytest
from mock import MagicMock
@pytest.fixture()
def seed():
import random
random.seed(654165151864151567878451235)
def test_n_rolls(seed):
from plugins.gaming import n_rolls
assert n_rolls(10, 20) == [7, 9, 20, 17, 11, 16, 16, 5, 5, 14]
@pytest.mark.parametrize('text,result', [
('2d5', 5),
('3d6', 10),
('12dF', -1),
('200d5', 586),
('300d6', 1030),
('1200dF', -19),
('2d20+4+5d10-2d8', 55),
])
def test_dice(seed, text, result):
from plugins.gaming import dice
notice = MagicMock()
out = dice(text, notice)
total = int(out.split()[0])
assert total == result
@pytest.mark.parametrize('roll_str', [
'a',
'foo',
'd-5',
'1d2 3d4 foo',
])
def test_invalid_roll(roll_str):
from plugins.gaming import dice, INVALID_ROLL
notice = MagicMock()
assert dice(roll_str, notice) is None
notice.assert_called_once_with(INVALID_ROLL.format(roll_str))
def test_num_only_roll():
from plugins.gaming import dice
notice = MagicMock()
assert dice('0', notice) is None
notice.assert_not_called()
def test_dice_overflow():
from plugins.gaming import dice
notice = MagicMock()
text = ('9' * 200) + 'd' + ('9' * 200)
assert dice(text, notice) == 'Thanks for overflowing a float, jerk >:['
notice.assert_not_called()
def test_roll_desc():
from plugins.gaming import dice, valid_diceroll
notice = MagicMock()
match = valid_diceroll.match('1d4+4d6 foo bar bazzed1')
assert dice(match, notice) == 'foo bar bazzed1: 16 (4, 1, 5, 3, 3)'
notice.assert_not_called()
def test_invalid_roll_desc():
from plugins.gaming import dice, INVALID_ROLL
notice = MagicMock()
match = re.match(r'^(\S+)(?:\s(.*))?$', '1d-4+4d6 foo bar bazzed1')
assert dice(match, notice) is None
notice.assert_called_once_with(INVALID_ROLL.format('1d-4+4d6'))
@pytest.mark.parametrize('input_text,choice', [
('1, b, foo', '1'),
('b or foo', 'b'),
])
def test_choose(seed, input_text, choice):
from plugins.gaming import choose
event = MagicMock()
assert choose(input_text, event) == choice
def test_one_choice():
from plugins.gaming import choose
event = MagicMock()
assert choose('a', event) is None
event.notice_doc.assert_called_once()
@pytest.mark.parametrize('coin_count,heads', [
(12, 7),
(200, 94),
])
def test_coin(seed, coin_count, heads):
from plugins.gaming import coin
notice = MagicMock()
action = MagicMock()
assert coin(str(coin_count), notice, action) is None
notice.assert_not_called()
action.assert_called_with(
'flips {} coins and gets {} heads and {} tails.'.format(
coin_count, heads, coin_count - heads
)
)
@pytest.mark.parametrize('text', [
'aa',
'foo bar',
'x01',
])
def test_coin_invalid_num(text):
from plugins.gaming import coin, INVALID_NUMBER
notice = MagicMock()
action = MagicMock()
assert coin(text, notice, action) is None
notice.assert_called_once_with(INVALID_NUMBER.format(text))
action.assert_not_called()
def test_no_coin(seed):
from plugins.gaming import coin
notice = MagicMock()
action = MagicMock()
assert coin('0', notice, action) is None
notice.assert_not_called()
action.assert_called_with('makes a coin flipping motion')
def test_one_coin(seed):
from plugins.gaming import coin
notice = MagicMock()
action = MagicMock()
assert coin('1', notice, action) is None
notice.assert_not_called()
action.assert_called_with('flips a coin and gets heads.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment