Compare commits

..

No commits in common. "1c54f860fa0a1624b3f55f6f76d9dc7679d5756a" and "18224b1ea75e7ea518acbb6317aad9a79177f360" have entirely different histories.

8 changed files with 29 additions and 59 deletions

1
.gitignore vendored
View File

@ -6,4 +6,3 @@ __pycache__
.pre-commit-config.yaml .pre-commit-config.yaml
.vscode .vscode
.coverage .coverage
.hypothesis

View File

@ -27,5 +27,6 @@ total | 1
## todo ## todo
- [x] roll with (dis)advantage - [x] roll with (dis)advantage
- [ ] interactive rolling mode
- [x] print criticals - [x] print criticals
- [x] use property testing - [ ] use property testing

View File

@ -23,8 +23,8 @@ env:
clean: clean:
hatch clean hatch clean
hatch env prune hatch env purge
-rm -r .{mypy,pytest,ruff}_cache rm -r .{mypy,pytest,ruff}_cache
-rm -r dist rm -r dist
-rm .coverage rm .coverage
fd __pycache__ --no-ignore --exec rm -r fd __pycache__ --no-ignore --exec rm -r

View File

@ -38,7 +38,6 @@ path = "roll/__about__.py"
[tool.hatch.envs.default] [tool.hatch.envs.default]
dependencies = [ dependencies = [
"coverage[toml]>=6.5", "coverage[toml]>=6.5",
"hypothesis",
"pytest", "pytest",
] ]
[tool.hatch.envs.default.scripts] [tool.hatch.envs.default.scripts]

View File

@ -16,7 +16,7 @@ class Roll:
dice_count: int = 1 dice_count: int = 1
sides: int = 20 sides: int = 20
modifier: int = 0 modifier: int | None = None
def __post_init__(self) -> None: def __post_init__(self) -> None:
if self.dice_count < 1: if self.dice_count < 1:
@ -34,13 +34,14 @@ class Roll:
msg = f"expected {value!r} to match pattern {ROLL_PATTERN.pattern!r}" msg = f"expected {value!r} to match pattern {ROLL_PATTERN.pattern!r}"
raise ValueError(msg) raise ValueError(msg)
dice_count, sides, modifier = match.groups() dice_count, sides, modifier = match.groups()
return cls(int(dice_count), int(sides), int(modifier) if modifier else 0) return cls(int(dice_count), int(sides), int(modifier) if modifier else None)
def modifier_str(self) -> str: def modifier_str(self) -> str:
"""return the modifier as a string""" """return the modifier as a string"""
if self.modifier == 0: if self.modifier is None:
return "" return ""
return f"{self.modifier:+}" sign = "+" if self.modifier > 0 else ""
return f"{sign}{self.modifier}"
def to_str(self) -> str: def to_str(self) -> str:
"""return the short representation of a roll, e.g. 3d4 or 2d20+3""" """return the short representation of a roll, e.g. 3d4 or 2d20+3"""

View File

@ -7,12 +7,12 @@ D20_MAX = 20
class Throw: class Throw:
results: list[int] results: list[int]
sides: int sides: int
modifier: int = 0 modifier: int | None = None
@property @property
def total(self) -> int: def total(self) -> int:
"""calculate the total of the throw, accounting for the modifier""" """calculate the total of the throw, accounting for the modifier"""
return sum(self.results) + self.modifier return sum(self.results) + (self.modifier or 0)
@property @property
def is_critical_hit(self) -> bool: def is_critical_hit(self) -> bool:

View File

@ -1,33 +1,13 @@
import pytest import pytest
from hypothesis import assume, given
from hypothesis import strategies as st
from roll.roll import Roll from roll.roll import Roll
@given(st.integers(max_value=0)) def test_roll_validation():
def test_bad_dice_count(n: int):
with pytest.raises(ValueError): with pytest.raises(ValueError):
Roll(n, 20) Roll(0, 20)
@given(st.integers(max_value=1))
def test_bad_sides(sides: int):
with pytest.raises(ValueError): with pytest.raises(ValueError):
Roll(1, sides) Roll(1, 1)
@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( @pytest.mark.parametrize(
@ -45,21 +25,16 @@ def test_from_bad_str(roll: str):
Roll.from_str(roll) Roll.from_str(roll)
@given( def test_modify():
st.integers(min_value=1), roll = Roll(2, 20)
st.integers(min_value=2), modified_roll = roll.modify(3)
st.integers(), assert modified_roll == Roll(2, 20, 3)
st.integers(), assert roll == Roll(2, 20)
)
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 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)) @pytest.mark.parametrize("n", list(range(1, 5)))
@pytest.mark.parametrize("sides", list(range(2, 100)))
def test_throw(n: int, sides: int): def test_throw(n: int, sides: int):
roll = Roll(n, sides) roll = Roll(n, sides)
throw = roll.throw() throw = roll.throw()

View File

@ -1,21 +1,16 @@
from hypothesis import given
from hypothesis import strategies as st
from roll.throw import Throw from roll.throw import Throw
@given(st.lists(st.integers(min_value=1), min_size=1), st.integers(min_value=2)) def test_throw():
def test_throw(results: list[int], sides: int): throw = Throw([1, 2, 3], sides=4)
throw = Throw(results, sides=sides) assert throw.total == 6
assert throw.total == sum(results)
assert not throw.is_critical_hit assert not throw.is_critical_hit
assert not throw.is_critical_miss assert not throw.is_critical_miss
@given(st.lists(st.integers(min_value=1), min_size=1), st.integers(min_value=2), st.integers()) def test_throw_with_modifier():
def test_throw_with_modifier(results: list[int], sides: int, modifier: int): throw = Throw([1, 2, 3], sides=4, modifier=5)
throw = Throw(results, sides=sides, modifier=modifier) assert throw.total == 11
assert throw.total == (sum(results) + modifier)
assert not throw.is_critical_hit assert not throw.is_critical_hit
assert not throw.is_critical_miss assert not throw.is_critical_miss