roll/tests/roll_test.py

42 lines
1.2 KiB
Python

import pytest # type: ignore (TODO: figure out why pyright can't import pytest)
from roll.roll import Roll
def test_roll_validation():
with pytest.raises(ValueError):
Roll(0, 20)
with pytest.raises(ValueError):
Roll(1, 1)
@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)
def test_modify():
roll = Roll(2, 20)
modified_roll = roll.modify(3)
assert modified_roll == Roll(2, 20, 3)
assert roll == Roll(2, 20)
assert modified_roll is not roll
@pytest.mark.parametrize("n", list(range(1, 5)))
@pytest.mark.parametrize("sides", list(range(2, 100)))
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)