roll/tests/roll_test.py

43 lines
1.1 KiB
Python
Raw Normal View History

2023-08-13 18:53:51 +00:00
import pytest
2023-08-11 20:08:48 +00:00
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(
2023-08-13 18:53:51 +00:00
("roll", "expected"),
[(Roll(1, 20), "1d20"), (Roll(2, 20, 3), "2d20+3"), (Roll(4, 6, -3), "4d6-3")],
2023-08-11 20:08:48 +00:00
)
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)
2023-08-13 18:53:51 +00:00
assert modified_roll is not roll and modified_roll != roll
2023-08-11 20:08:48 +00:00
@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)