Skip to content

Commit

Permalink
fix issue where parenthetical annotations would not bubble up
Browse files Browse the repository at this point in the history
  • Loading branch information
zhudotexe committed Feb 28, 2020
1 parent 72856e5 commit 59cb08b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
5 changes: 3 additions & 2 deletions d20/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,10 +283,11 @@ def set(self):

@property
def children(self):
return self.value.children
return [self.value]

def set_child(self, index, value):
self.value.set_child(index, value)
self._child_set_check(index)
self.value = value

def __repr__(self):
return f"<Parenthetical value={self.value} operations={self.operations}>"
Expand Down
2 changes: 1 addition & 1 deletion d20/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def simplify_expr_annotations(expr, ambig_inherit=None):
:type expr: d20.Number
:param ambig_inherit: When encountering a child node with no annotation and the parent has ambiguous types, which \
to inherit. Can be ``None`` for no inherit, ``'left'`` for leftmost, or ``'right'`` for rightmost.
:type ambig_inherit: str
:type ambig_inherit: Optional[str]
"""
if ambig_inherit not in ('left', 'right', None):
raise ValueError("ambig_inherit must be 'left', 'right', or None.")
Expand Down
34 changes: 22 additions & 12 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,28 @@ def test_copy(self):
assert str(parse(expr)) == str(tree)


def test_annotation_simplify():
expr = roll("1 [a] + 2 + 3 [b] + 4").expr
utils.simplify_expr_annotations(expr, None)
assert SimpleStringifier().stringify(expr) == "1 + 2 [a] + 3 [b] + 4 = 10"

expr = roll("1 [a] + 2 + 3 [b] + 4").expr
utils.simplify_expr_annotations(expr, 'left')
assert SimpleStringifier().stringify(expr) == "1 + 2 [a] + 3 [b] + 4 [a] = 10"

expr = roll("1 [a] + 2 + 3 [b] + 4").expr
utils.simplify_expr_annotations(expr, 'right')
assert SimpleStringifier().stringify(expr) == "1 + 2 [a] + 3 [b] + 4 [b] = 10"
class TestAnnotationSimplify:
def test_annotation_simplify(self):
expr = roll("1 [a] + 2 + 3 [b] + 4").expr
utils.simplify_expr_annotations(expr, None)
assert SimpleStringifier().stringify(expr) == "1 + 2 [a] + 3 [b] + 4 = 10"

expr = roll("1 [a] + 2 + 3 [b] + 4").expr
utils.simplify_expr_annotations(expr, 'left')
assert SimpleStringifier().stringify(expr) == "1 + 2 [a] + 3 [b] + 4 [a] = 10"

expr = roll("1 [a] + 2 + 3 [b] + 4").expr
utils.simplify_expr_annotations(expr, 'right')
assert SimpleStringifier().stringify(expr) == "1 + 2 [a] + 3 [b] + 4 [b] = 10"

expr = roll("1 [a] + 2 + 3 [a] + 4").expr
utils.simplify_expr_annotations(expr.roll)
assert SimpleStringifier().stringify(expr) == "1 + 2 + 3 + 4 [a] = 10"

def test_parenthetical_annotation(self):
expr = roll("(1 [a])").expr
utils.simplify_expr_annotations(expr.roll)
assert SimpleStringifier().stringify(expr) == "(1) [a] = 1"


def test_simplify():
Expand Down

0 comments on commit 59cb08b

Please sign in to comment.