From 7f0d903b2611f45e280d5f82305cfb91bcd1590d Mon Sep 17 00:00:00 2001 From: tserg <8017125+tserg@users.noreply.github.com> Date: Mon, 23 Oct 2023 10:53:05 +0800 Subject: [PATCH] fix evaluate tests --- tests/ast/nodes/test_evaluate_binop_decimal.py | 13 +++++++++++-- tests/ast/nodes/test_evaluate_binop_int.py | 11 +++++++++-- tests/ast/nodes/test_evaluate_boolop.py | 12 ++++++++++-- tests/ast/nodes/test_evaluate_unaryop.py | 12 ++++++++++-- 4 files changed, 40 insertions(+), 8 deletions(-) diff --git a/tests/ast/nodes/test_evaluate_binop_decimal.py b/tests/ast/nodes/test_evaluate_binop_decimal.py index 5c9956caba..9e20d687bc 100644 --- a/tests/ast/nodes/test_evaluate_binop_decimal.py +++ b/tests/ast/nodes/test_evaluate_binop_decimal.py @@ -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 @@ -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 diff --git a/tests/ast/nodes/test_evaluate_binop_int.py b/tests/ast/nodes/test_evaluate_binop_int.py index 80c9381c0f..555a100ec4 100644 --- a/tests/ast/nodes/test_evaluate_binop_int.py +++ b/tests/ast/nodes/test_evaluate_binop_int.py @@ -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) @@ -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 diff --git a/tests/ast/nodes/test_evaluate_boolop.py b/tests/ast/nodes/test_evaluate_boolop.py index 8b70537c39..f57d6e9a60 100644 --- a/tests/ast/nodes/test_evaluate_boolop.py +++ b/tests/ast/nodes/test_evaluate_boolop.py @@ -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" @@ -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 diff --git a/tests/ast/nodes/test_evaluate_unaryop.py b/tests/ast/nodes/test_evaluate_unaryop.py index 63d7a0b7ff..0e88437aa9 100644 --- a/tests/ast/nodes/test_evaluate_unaryop.py +++ b/tests/ast/nodes/test_evaluate_unaryop.py @@ -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]) @@ -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