Skip to content

Commit

Permalink
fix evaluate tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tserg committed Oct 23, 2023
1 parent 3f496ca commit 7f0d903
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 8 deletions.
13 changes: 11 additions & 2 deletions tests/ast/nodes/test_evaluate_binop_decimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from vyper import ast as vy_ast
from vyper.exceptions import OverflowException, TypeMismatch, ZeroDivisionException
from vyper.semantics import validate_semantics

st_decimals = st.decimals(
min_value=-(2**32), max_value=2**32, allow_nan=False, allow_infinity=False, places=10
Expand Down Expand Up @@ -71,10 +72,18 @@ def foo({input_value}) -> decimal:

literal_op = " ".join(f"{a} {b}" for a, b in zip(values, ops))
literal_op = literal_op.rsplit(maxsplit=1)[0]
vyper_ast = vy_ast.parse_to_ast(literal_op)

expected = f"""
@external
def foo() -> decimal:
return {literal_op}
"""

vyper_ast = vy_ast.parse_to_ast(expected)
try:
validate_semantics(vyper_ast, {})
vy_ast.folding.replace_literal_ops(vyper_ast)
expected = vyper_ast.body[0].value.value
expected = vyper_ast.body[0].body[0].value.value
is_valid = -(2**127) <= expected < 2**127
except (OverflowException, ZeroDivisionException):
# for overflow or division/modulus by 0, expect the contract call to revert
Expand Down
11 changes: 9 additions & 2 deletions tests/ast/nodes/test_evaluate_binop_int.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from vyper import ast as vy_ast
from vyper.exceptions import ZeroDivisionException
from vyper.semantics import validate_semantics

st_int32 = st.integers(min_value=-(2**32), max_value=2**32)

Expand Down Expand Up @@ -110,11 +111,17 @@ def foo({input_value}) -> int128:
literal_op = " ".join(f"{a} {b}" for a, b in zip(values, ops))
literal_op = literal_op.rsplit(maxsplit=1)[0]

vyper_ast = vy_ast.parse_to_ast(literal_op)
expected = f"""
@external
def foo() -> int128:
return {literal_op}
"""
vyper_ast = vy_ast.parse_to_ast(expected)

try:
validate_semantics(vyper_ast, {})
vy_ast.folding.replace_literal_ops(vyper_ast)
expected = vyper_ast.body[0].value.value
expected = vyper_ast.body[0].body[0].value.value
is_valid = True
except ZeroDivisionException:
is_valid = False
Expand Down
12 changes: 10 additions & 2 deletions tests/ast/nodes/test_evaluate_boolop.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from hypothesis import strategies as st

from vyper import ast as vy_ast
from vyper.semantics import validate_semantics

variables = "abcdefghij"

Expand Down Expand Up @@ -52,8 +53,15 @@ def foo({input_value}) -> bool:
literal_op = " ".join(f"{a} {b}" for a, b in zip(values, comparators))
literal_op = literal_op.rsplit(maxsplit=1)[0]

vyper_ast = vy_ast.parse_to_ast(literal_op)
expected = f"""
@external
def foo() -> bool:
return {literal_op}
"""

vyper_ast = vy_ast.parse_to_ast(expected)
validate_semantics(vyper_ast, {})
vy_ast.folding.replace_literal_ops(vyper_ast)
expected = vyper_ast.body[0].value.value
expected = vyper_ast.body[0].body[0].value.value

assert contract.foo(*values) == expected
12 changes: 10 additions & 2 deletions tests/ast/nodes/test_evaluate_unaryop.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest

from vyper import ast as vy_ast
from vyper.semantics import validate_semantics


@pytest.mark.parametrize("bool_cond", [True, False])
Expand Down Expand Up @@ -30,8 +31,15 @@ def foo(a: bool) -> bool:
contract = get_contract(source)

literal_op = f"{'not ' * count}{bool_cond}"
vyper_ast = vy_ast.parse_to_ast(literal_op)
expected = f"""
@external
def foo() -> bool:
return {literal_op}
"""

vyper_ast = vy_ast.parse_to_ast(expected)
validate_semantics(vyper_ast, {})
vy_ast.folding.replace_literal_ops(vyper_ast)
expected = vyper_ast.body[0].value.value
expected = vyper_ast.body[0].body[0].value.value

assert contract.foo(bool_cond) == expected

0 comments on commit 7f0d903

Please sign in to comment.