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
.vscode
.coverage
.hypothesis

View File

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

View File

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

View File

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

View File

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

View File

@ -7,12 +7,12 @@ D20_MAX = 20
class Throw:
results: list[int]
sides: int
modifier: int = 0
modifier: int | None = None
@property
def total(self) -> int:
"""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
def is_critical_hit(self) -> bool:

View File

@ -1,33 +1,13 @@
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):
def test_roll_validation():
with pytest.raises(ValueError):
Roll(n, 20)
@given(st.integers(max_value=1))
def test_bad_sides(sides: int):
Roll(0, 20)
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()
Roll(1, 1)
@pytest.mark.parametrize(
@ -45,21 +25,16 @@ def test_from_bad_str(roll: str):
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)
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 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):
roll = Roll(n, sides)
throw = roll.throw()

View File

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