From 2dda7f13205c508b8d20fc732b7db1b4070e39b7 Mon Sep 17 00:00:00 2001 From: tserg <8017125+tserg@users.noreply.github.com> Date: Tue, 21 Jan 2025 15:35:31 +0800 Subject: [PATCH] add warnings tests --- .../test_contract_size_limit_warning.py | 23 +++++++++ .../warnings/test_deprecation_warning.py | 50 +++++++++++++++++++ .../warnings/test_enum_usage_warning.py | 18 +++++++ tests/unit/compiler/test_compile_code.py | 22 -------- 4 files changed, 91 insertions(+), 22 deletions(-) create mode 100644 tests/functional/syntax/warnings/test_contract_size_limit_warning.py create mode 100644 tests/functional/syntax/warnings/test_deprecation_warning.py create mode 100644 tests/functional/syntax/warnings/test_enum_usage_warning.py diff --git a/tests/functional/syntax/warnings/test_contract_size_limit_warning.py b/tests/functional/syntax/warnings/test_contract_size_limit_warning.py new file mode 100644 index 0000000000..3e27304266 --- /dev/null +++ b/tests/functional/syntax/warnings/test_contract_size_limit_warning.py @@ -0,0 +1,23 @@ +import random + +import pytest + +import vyper + + +@pytest.fixture +def huge_bytestring(): + r = random.Random(b"vyper") + + return bytes([r.getrandbits(8) for _ in range(0x6001)]) + + +def test_contract_size_exceeded(huge_bytestring): + code = f""" +@external +def a() -> bool: + q: Bytes[24577] = {huge_bytestring} + return True +""" + with pytest.warns(vyper.warnings.ContractSizeLimit): + vyper.compile_code(code, output_formats=["bytecode_runtime"]) diff --git a/tests/functional/syntax/warnings/test_deprecation_warning.py b/tests/functional/syntax/warnings/test_deprecation_warning.py new file mode 100644 index 0000000000..6b30974e97 --- /dev/null +++ b/tests/functional/syntax/warnings/test_deprecation_warning.py @@ -0,0 +1,50 @@ +import json + +import pytest + +import vyper +from vyper.cli.vyper_json import compile_json + +deprecated = [ + """ +struct Foo: + a: uint256 + b: uint256 + +@external +def foo(): + f: Foo = Foo({a: 128, b: 256}) + """, + """ +event Foo: + a: uint256 + b: uint256 + +@external +def foo(): + log Foo({a: 128, b: 256}) + """, +] + + +@pytest.mark.parametrize("code", deprecated) +def test_deprecated_warning(code): + with pytest.warns(vyper.warnings.Deprecation): + vyper.compile_code(code) + + +def test_deprecated_optimize_boolean_flag(): + code = """ +@external +def foo(): + pass + """ + + input_json = { + "language": "Vyper", + "sources": {"contracts/foo.vy": {"content": code}}, + "settings": {"outputSelection": {"*": ["*"]}, "optimize": True}, + } + + with pytest.warns(vyper.warnings.Deprecation): + compile_json(json.dumps(input_json)) diff --git a/tests/functional/syntax/warnings/test_enum_usage_warning.py b/tests/functional/syntax/warnings/test_enum_usage_warning.py new file mode 100644 index 0000000000..887b1808af --- /dev/null +++ b/tests/functional/syntax/warnings/test_enum_usage_warning.py @@ -0,0 +1,18 @@ +import pytest + +import vyper + + +def test_enum_usage_warning(): + code = """ +enum Foo: + Fe + Fi + Fo + +@external +def foo() -> Foo: + return Foo.Fe + """ + with pytest.warns(vyper.warnings.EnumUsage): + vyper.compile_code(code) diff --git a/tests/unit/compiler/test_compile_code.py b/tests/unit/compiler/test_compile_code.py index 59ce8b83d3..8d3bd53433 100644 --- a/tests/unit/compiler/test_compile_code.py +++ b/tests/unit/compiler/test_compile_code.py @@ -1,28 +1,6 @@ -import random - -import pytest - import vyper -@pytest.fixture -def huge_bytestring(): - r = random.Random(b"vyper") - - return bytes([r.getrandbits(8) for _ in range(0x6001)]) - - -def test_contract_size_exceeded(huge_bytestring): - code = f""" -@external -def a() -> bool: - q: Bytes[24577] = {huge_bytestring} - return True -""" - with pytest.warns(vyper.warnings.ContractSizeLimit): - vyper.compile_code(code, output_formats=["bytecode_runtime"]) - - # test that each compilation run gets a fresh analysis and storage allocator def test_shared_modules_allocation(make_input_bundle): lib1 = """