roll/tests/roll_test.py

68 lines
1.9 KiB
Python

import pytest
from hypothesis import assume, given
from hypothesis import strategies as st
from roll.roll import Roll
@given(st.integers(max_value=0))
def test_bad_dice_count(n: int):
with pytest.raises(ValueError):
Roll(n, 20)
@given(st.integers(max_value=1))
def test_bad_sides(sides: int):
with pytest.raises(ValueError):
Roll(1, sides)
@given(
st.integers(min_value=1),
st.integers(min_value=2),
st.integers(),
)
def test_valid_dice(n: int, sides: int, modifier: int):
string = f"{n}d{sides}{modifier:+}" if modifier else f"{n}d{sides}"
roll = Roll(n, sides, modifier)
roll_from_str = Roll.from_str(string)
assert roll == roll_from_str
assert string == roll.to_str() and string == roll_from_str.to_str()
@pytest.mark.parametrize(
("roll", "expected"),
[(Roll(1, 20), "1d20"), (Roll(2, 20, 3), "2d20+3"), (Roll(4, 6, -3), "4d6-3")],
)
def test_str_roundtrip(roll: Roll, expected: str):
assert roll.to_str() == expected
assert Roll.from_str(expected) == roll
@pytest.mark.parametrize("roll", ["d90", "0d0", "-1d-1", "aba", "000", "1d1d1d"])
def test_from_bad_str(roll: str):
with pytest.raises(ValueError):
Roll.from_str(roll)
@given(
st.integers(min_value=1),
st.integers(min_value=2),
st.integers(),
st.integers(),
)
def test_modify(n: int, sides: int, modifier: int, new_modifier: int):
assume(modifier != new_modifier)
roll = Roll(n, sides, modifier)
modified_roll = roll.modify(new_modifier)
assert modified_roll == Roll(n, sides, new_modifier)
assert modified_roll is not roll and modified_roll != roll
@given(st.integers(min_value=1, max_value=50_000), st.integers(min_value=2))
def test_throw(n: int, sides: int):
roll = Roll(n, sides)
throw = roll.throw()
assert len(throw.results) == n
assert all(1 <= result <= sides for result in throw.results)