Skip to content

Commit 7861af9

Browse files
committed
make dice result total and result properties
1 parent 90790f2 commit 7861af9

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

d20/dice.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,17 @@ def __init__(self, the_ast, the_roll, stringifier):
7070
"""
7171
self.ast = the_ast
7272
self.expr = the_roll
73-
self.total = int(the_roll.total)
74-
self.result = stringifier.stringify(the_roll)
73+
self.stringifier = stringifier
7574
self.comment = the_roll.comment
7675

76+
@property
77+
def total(self):
78+
return int(self.expr.total)
79+
80+
@property
81+
def result(self):
82+
return self.stringifier.stringify(self.expr)
83+
7784
@property
7885
def crit(self):
7986
"""

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
setuptools.setup(
1010
name="d20",
11-
version="1.0.1",
11+
version="1.0.2",
1212
author="Andrew Zhu",
1313
author_email="[email protected]",
1414
description="A formal grammar-based dice parser and roller for D&D and other dice systems.",

tests/test_dice.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,3 +197,13 @@ def test_ma_op():
197197
assert r("10d6ma1") == 10
198198
assert r("10d6ma0") == 0
199199
assert 10 <= r("10d6ma5") <= 50
200+
201+
202+
# modifying the tree directly
203+
def test_correct_results():
204+
result = roll("1+2+3")
205+
assert result.total == 6
206+
assert result.result == "1 + 2 + 3 = `6`"
207+
result.expr.roll = BinOp(result.expr.roll, '+', Literal(4))
208+
assert result.total == 10
209+
assert result.result == "1 + 2 + 3 + 4 = `10`"

0 commit comments

Comments
 (0)